0% found this document useful (0 votes)
39 views69 pages

Computer Project Class 12 (AutoRecovered)

Uploaded by

bhaveshyaa2904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views69 pages

Computer Project Class 12 (AutoRecovered)

Uploaded by

bhaveshyaa2904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Introduction to Java:

JAVA was developed by James Gosling at Sun


Microsystems Inc in the year 1991, later
acquired by Oracle Corporation. It is a simple
programming language. Java makes writing,
compiling, and debugging programming easy. It
helps to create reusable code and modular
programs.
Java is a class-based, object-oriented
programming language and is designed to have
as few implementation dependencies as
possible. A general-purpose programming
language made for developers to write once run
anywhere that is compiled Java code can run on
all platforms that support Java. Java applications
are compiled to byte code that can run on any
Java Virtual Machine.

OBJECTS:
Object − Objects have states and behaviours.
Example: A dog has states - colour, name, breed
as well as behaviours – wagging the tail, barking,
eating. An object is an instance of a class .
Let us now look deep into what are objects. If we
consider the real-world, we can find many
objects around us, cars, dogs, humans, etc. All
these objects have a state and a behaviour.
If we consider a dog, then its state is - name,
breed, colour, and the behaviour is - barking,
wagging the tail, running.
If you compare the software object with a real-
world object, they have very similar
characteristics.
Software objects also have a state and a
behaviour. A software object's state is stored in
fields and behaviour is shown via methods.
So, in software development, methods operate
on the internal state of an object and the object-
to-object communication is done via methods.

CLASS:
Class − A class can be defined as a
template/blueprint that describes the
behaviour/state that the object of its type
support.
A class is a blueprint from which individual
objects are created.
Following is the sample of a class:
//sample of a class
class Project
{void in()
{
}//method in
void check()
{
}//method check
void disp()
{
}//method disp
}//class
;A class can have any number of methods to
access the value of various kinds of methods. In
the above example, in(), check() and disp() are
methods.

HISTORY AND DEVELOPMENT OF JAVA:


Different types of Java Programming:
Stand Alone System (Java Application)
A standalone application is an application that
runs locally on the device and doesn’t require
anything else to be functional. All the logic is built
into the app, so it doesn’t need an internet
connection nor any other services installed.

Unlike web applications that run in a browser and


don’t need to be installed, standalone apps
require completely the opposite. They need no
browser to run, but often demand a device to be
installed on. However, there are
also portable standalone applications created
for some platforms (like Windows) that can run
just by double-clicking a single file, with no
installation.
INTERNET APPLETS (JAVA APPLETS):
Java applets are used to provide interactive
features to web applications and can be executed
by browsers for many platforms. They are small,
portable Java programs embedded in HTML
pages and can run automatically when the pages
are viewed. Malware authors have used Java
applets as a vehicle for attack. A Java applet is an
applet delivered to the users in the form of Java
Byte Code. Java applets can run in a Web
browser using a Java Virtual Machine (JVM).

COMPILER AND INTERPRETER:


COMPILER:
A Compiler will translate the whole programme at
once. It will compile the whole programme and
then show us the errors. A compiler is used to
convert a Java programme into byte code. It is
machine independent. This makes Java
programme easily transferrable.
INTERPRETER:
Java interpreter is a computer program
(system software) that implements the JVM. It is
responsible for reading and executing the
program. It is designed in such a way that it can
read the source program and translate the
source code instruction by instruction. It
converts the high-level program into
assembly language (machine language).

CONCEPT OF DATA TYPES:


Tokens:
Java tokens are smallest elements of a program
which are identified by the compiler. Tokens in
java include identifiers, keywords, literals,
operators and, separators.
Types of tokens:
 Literals:
Literals in Java are similar to normal variables but
their values cannot be changed once assigned. In
other words, literals are constant variables with
fixed values. These are defined by users and can
belong to any data type. Java supports five types
of literals which are as follows:
1.Integer
2.Float
3.Double
4.Character
5.String
6.Boolean
//Sample for types of literals

class literals

{void in()
{int a=50;//integer literal

float b=(float)31.3;//float literal

double c=56.23487654357876;//double literal

char d='B';//character literal

String e="Bhaveshyaa";//string literal

boolean f=true;//boolean literal

System.out.println("a= " +a);

System.out.println("b= " +b);

System.out.println("c= " +c);

System.out.println("d= " +d);

System.out.println("e= " +e);

System.out.println("f= " +f);

Sample Output:

a= 50

b= 31.3

c= 56.23487654357876

d= B

e= Bhaveshyaa
f= true

 Identifiers:
Identifiers in Java are symbolic names used
for identification. They can be a class name,
variable name, method name, package
name, constant name, and more. However,
In Java, there are some reserved words that
cannot be used as an identifier.

 Variable:

A variable is a container which holds the


value while the Java program is executed. A
variable is assigned with a data type.
Variable is a name of memory location. There
are three types of variables in java: local,
instance and static.

DATA TYPES IN JAVA:


Primitive data types:
The data types which are independent of any
other type, are known as Primitive Data
Types. These data types are also called Basic
Data Types. For example, byte, int, long,
float, double, etc.

Numeric Data types:


Numeric data types deal with number-
oriented values. It includes Integer and
Floating type data.

Integer Type:
A variable declared integer type contains a
whole number. The number may be a
positive or a
negative number but without decimal point.
There are four types of declarations under
this category, They are:
 Byte: Used for bit wise operations.
 Short: Used for a small range of integers.
 Int: Used for integers having size bigger than
short.
 long: Used for large integers.

OPERATORS:
Operators are basically the symbols or
tokens, which perform arithmetical or logical
operations to yield a meaningful result.
Basically, there are three types of operators
used in Java. They are:
 Arithmetical Operators: +, -, /, *, etc.
 Relational Operators: <, >, ==, !=, <=, etc.
 Logical Operators: &&, \\, !, etc.
ASSIGNMENTS:
The Java Assignment Operators are used
when you want to assign a value to the
expression. The assignment operator
denoted by the single equal sign =. In a Java
assignment statement, any expression can
be on the right side and the left side must be
a variable name.
For example, int m=15; :15 is stored in the
variable m which is integer type.
char chr =’k’; : The character ‘k’ is stored in
character type variable chr.

TYPES OF OPERATORS:
 Arithmetical Operators:
The operators, which are used to perform
arithmetical calculations in a program, are
known as Arithmetical Operators. Some basic
calculations like addition (+), subtraction (-),
multiplication (*), division (/), modulus (%)
are often needed while processing.
//to perform all the arithmetical operations.

import java.util.*;

class arithmetic

{ Scanner sc=new Scanner(System.in);

int a,b;//global variables

double add,sub,product,division,mod;//global variables

void in()

{System.out.println("Enter any two numbers");

a=sc.nextInt();

b=sc.nextInt();

add=a+b;//addition operator

sub=a-b;//subtraction operator

product=a*b;//multiplication operator

division=a/b;//division operator

mod=a%b;//modulus operator

void disp()
{ System.out.println("Sum of "+a+" and "+b+" ="+add);//printing
the values in the terminal window.

System.out.println("Difference of "+a+" and "+b+" ="+sub);

System.out.println("Product of "+a+" and "+b+" ="+product);

System.out.println("Quotient of "+a+" and "+b+"


="+division);

System.out.println("Remainder of "+a+" and "+b+"


="+mod);

public static void main()

{arithmetic a1=new arithmetic();

a1.in();

a1.disp();//function call

}//main

}//class

Input:

Enter any two numbers

10

Output:

Sum of 10 and 5 =15.0

Difference of 10 and 5 =5.0


Product of 10 and 5 =50.0

Quotient of 10 and 5 =2.0

Remainder of 10 and 5 =0.0

 Unary (+) Operator:


This operator is used before the operand. It is
used as a pointer to a variable, which results
in the same variable.
For example,
1. If a=8, then +a will result in 8.
2. If a=-10, then +a will result in -10.

 Unary (-) Operator:


This operator is used similar to Unary plus
(+). It is also added before the operand.
Unary minus (-) operator reverts the sigh of
an operand.
For example,
1. If a=4, then -a will result in -4.
2. If a=-3.6, then -a will result in 3.6

 Unary increment and decrement


operators:
Unary increment operator (++) increases the
value of an operand by one. Unary decrement
Operator (--) decreases the value of an
operand by one.
Prefix:
This operator works on the principle ”Change
before action”.
For example, p=5;
p=++p, then output will be 6.
d=11;
d=--d, then output will be 10.
Postfix:
This operator works on the principle ”Change
after action”.
For example,
p=5,
m=p++*4, then the output will be 5*4=20,
but the updated value of p will become 6
which is not shown on the terminal window.
p=5,
m=p--*4, then the output will be 5*4=20, but
the updated value of p will become 4 which is
not shown on the terminal window.

 Relational operators:
These operators are used to check the
relationship among the operands. Relational
operators compare the values of the variables
and results in terms of ‘True’ or ‘False’.

Operator Description Example

== Is Equal To 3 == 5 returns false

!= Not Equal To 3 != 5 returns true

> Greater Than 3 > 5 returns false

< Less Than 3 < 5 returns true

 Logical Operators:
These operators are used to perform logical
“AND”, “OR” and “NOT” operation, i.e. the
function similar to AND gate and OR gate in
digital electronics. They are used to combine
two or more conditions/constraints or to
complement the evaluation of the original
condition under particular consideration.
1.‘Logical AND’ Operator (&&):
This operator returns true when both the
conditions under consideration are satisfied
or are true.

Syntax:
condition1 && condition2
2.'Logical OR' Operator (||):
This operator returns true when one of the
two conditions under consideration are
satisfied or are true.

Syntax:
condition1 || condition2
3.'Logical NOT' Operator(!): Unlike the
previous two, this is a unary operator and
returns true when the condition under
consideration is not satisfied or is a false
condition.
Syntax:
! (condition)

MATH FUNCTIONS:
1. Math.sqrt():
This function is used to find the square root of a
number. It returns a double type value.

//Finding the square root of a number


import java.util.*;
class math
{ double num,sq;//global variables
Scanner sc=new Scanner(System.in);//scanner class
void in()
{System.out.println("Enter any number");
num=sc.nextDouble();
sq=Math.sqrt(num);//finding the square root.

System.out.println("Square root of " +num+ " is "+sq);//printing


the square root.
}
public static void main()
{math m1=new math();
m1.in();
}//main
}//class
Input:
Enter any number
6
Output:
Square root of 6.0 is 2.449489742783178

2. Math.min():
This function returns the minimum of two
numbers. The return value depends on the
values used as the arguments of the function.
//finding the minimum of two numbers.
import java.util.*;
class min
{double n1,n2,min;//global variables
Scanner sc=new Scanner(System.in);
void in()
{System.out.println("Enter any two numbers");
n1=sc.nextDouble();
n2=sc.nextDouble();
min=Math.min(n1,n2);

System.out.println("Minimum of " + n1 + " and " +n2+ " is "


+min);//printing min of 2 numbers
}
public static void main()//main method
{min m1=new min();
m1.in();
}//main
}//class
Input:
Enter any two numbers
4
9
Output:
Minimum of 4.0 and 9.0 is 4.0

3. Math.max():
This function is used to find the maximum of two
given arguments. It returns a value depending
upon the arguments.
//finding the minimum of two numbers.
import java.util.*;
class max
{double n1,n2,max;//global variables
Scanner sc=new Scanner(System.in);
void in()
{System.out.println("Enter any two numbers");
n1=sc.nextDouble();
n2=sc.nextDouble();
max=Math.max(n1,n2);

System.out.println("Maximum of " + n1 + " and " +n2+ " is "


+max);//printing min of 2 numbers
}
public static void main()//main method
{max m1=new max();
m1.in();
}//main
}//class
Input:
Enter any two numbers
16
27
Output:
Maximum of 16.0 and 27.0 is 27.0

4. Math.pow():
This function is used to find the power raised to
a specified base. It always returns a double type
value.
//finding the minimum of two numbers.
import java.util.*;
class power
{double n1,n2,power;//global variables
Scanner sc=new Scanner(System.in);
void in()
{System.out.println("Enter any two numbers");
n1=sc.nextDouble();
n2=sc.nextDouble();
power=Math.pow(n1,n2);
System.out.println("Power of " + n1 + " and " +n2+ " is "
+power);//printing the power
}
public static void main()//main method
{power p1=new power();
p1.in();
}//main
}//class
Input:
Enter any two numbers
2
3
Output:
Power of 2.0 and 3.0 is 8.0

5. Math.log():
This function returns natural logarithmic value of
a given argument. It always returns a double
type value.
//finding the log of a number.
import java.util.*;
class log
{double num,log;//global variables
Scanner sc=new Scanner(System.in);
void in()
{System.out.println("Enter any number");
num=sc.nextDouble();
log=Math.log(num);
System.out.println("Logarithmic value of " + num + " is "
+log);//printing log of a number
}
public static void main()//main method
{log l1=new log();
l1.in();
}//main
}//class
Input:
Enter any numbers
6.25
Output:
Logarithmic value of 6.25 is 1.8325814637483102

6. Math.abs():
This function is used to return an absolute value
(i.e, only magnitude of a number). It returns
int/long/double values depending upon the type
of the given argument.
//finding the absolute value of a number.
import java.util.*;
class abs
{double num,abs;//global variables
Scanner sc=new Scanner(System.in);
void in()
{System.out.println("Enter any numbers");
num=sc.nextDouble();
abs=Math.abs(num);

System.out.println("Absolute value of " + num + " is "


+abs);//printing absolute value of a number
}
public static void main()//main method
{abs a1=new abs();
a1.in();
}//main
}//class
Input:
Enter any numbers
-9.99
Output:
Absolute value of -9.99 is 9.99

7. Math.rint():
This function returns nearest integer value when
a real number is used as its argument. This
return value will be in double data type i.e,
adding () after decimal point. However, it
predicts the different outputs for positive and
negative numbers.
//finding the nearest value of a number.
import java.util.*;
class rint
{double num,rint;//global variables
Scanner sc=new Scanner(System.in);
void in()
{System.out.println("Enter any number");
num=sc.nextDouble();
rint=Math.rint(num);

System.out.println("Nearest value of " + num + " is "


+rint);//printing nearest value of a number
}
public static void main()//main method
{rint r1=new rint();
r1.in();
}//main
}//class
Input: Input:
Enter any number Enter any number
4.5 8.7
Output: Output:
Nearest value of 4.5 is 4.0 Nearest value of 8.7 is 9.0

8. Math.round():
This function returns the value in rounded form.
If the fractional part is less than 0.5 then it
returns the same value of the integer part of the
argument otherwise, it returns the next higher
integer value. This function always returns the
integer value in long or int data type.
//To round off a number.
import java.util.*;
class round
{double num,round;//global variables
Scanner sc=new Scanner(System.in);
void in()
{System.out.println("Enter any number");
num=sc.nextDouble();
round=Math.round(num);

System.out.println("Round off value of " + num + " is "


+round);//printing round off value of a number
}
public static void main()//main method
{round r1=new round();
r1.in();
}//main
}//class
Input: Input:
Enter any number Enter any number
5.6 4.3
Output: Output:
Round off value of 5.6 is 6.0 Round off value of 4.3 is 4.0

9. Math.ceil():
This function returns the higher integer out of
two integer numbers between which the given
argument falls. It always returns the value as a
double data type whether the argument is given
as an integer (int), float or a double data type.
//Functioning of Math ciel function
import java.util.*;
class ceil
{double num,ceil;//global variables
Scanner sc=new Scanner(System.in);
void in()
{System.out.println("Enter any number");
num=sc.nextDouble();
ceil=Math.ceil(num);

System.out.println("Answer is " + num + " is " +ceil);//printing


the answer
}
public static void main()//main method
{ceil c1=new ceil();
c1.in();
}//main
}//class
Input:
Enter any number
Output:
-8.9
Answer is -8.9 is -8.0

10. Math.exp():
This function provides exponential value, i.e, e x.
It returns a double type value.
//Finding the exponent of a number
import java.util.*;
class exp
{double num,exp;//global variables
Scanner sc=new Scanner(System.in);
void in()
{System.out.println("Enter any number");
num=sc.nextDouble();
exp=Math.exp(num);

System.out.println("Exponent of " + num + " is "


+exp);//printing the exponent
}
public static void main()//main method
{exp e1=new exp();
e1.in();
}//main
}//class
Input:
Enter any number
6.25
Output:
Exponent of 6.25 is 518.012824668342

11. Math.random():
This function is used to get a number randomly
between 0 and 1. It returns a double type value.
Normally, the return value is fractional number .
//Finding a random number
class ran
{double ran;//global variables
void in()
{ ran=Math.random();

System.out.println("Random number between 0 and 1 is "


+ran);//printing the exponent
}
public static void main()//main method
{ran r1=new ran();
r1.in();
}//main
}//class
Output:
Random number between 0 and 1 is 0.9211382929160767
DECISION MAKING IN JAVA:
Conditional Branching:
1.If Statement:
We can use if statement to check a specified
condition. It performs a course of action if the
condition is true otherwise, the action is ignored.
For example,
If(n>=10&&n<=99)
{System.out.println(“The number is a double
digit number”);
}
2. If and only if:
In a logical situation where a number of ‘if’
statements are used simultaneously such that
each if statement includes one or more
statements to execute when condition is true, ‘if
and only if’ construct is used.
Programmes using ‘if and only if’:
Question:
The telephone department charges from their
customers for the no.of calls made as per the
give tarrif:
Number of calls: Rate:
Up to 50 calls Free(no charge)
For next 100 calls ₹1 per call
For next 200 calls ₹1.10 per call
Beyond 350 calls ₹1.20 per call
However, every customer has to pay a fixed
monthly charge of ₹180. Write a program in java
to input the number of calls made and print the
monthly bill.

//to caluclate the amount to be paid of a telephone bill


import java.util.*;

class telephone

{Scanner sc=new Scanner(System.in);//scanner class

double ch,c;//global variable

void in()

{System.out.println("Enter the number of calls made");

c=sc.nextInt();

if(c<=50)//use of if and only if

{ch=0+180;//calculation of amount to paid

System.out.println("Amount to be paid ="+ ch);//printing


the amount

if(c>50&&c<=150)

{ch=1*c+180;

System.out.println("Amount to be paid ="+ch);

if(c>150&&c<=350)

{ch=1.10*c+180;

System.out.println("Amount to be paid ="+ch);

if(c>350)
{ch=1.20*c+180;

System.out.println("Amount to be paid ="+ch);

}//if and only if

}//in

public static void main()//main method

{telephone t1=new telephone();

t1.in();//function call

}//main

}//class

Input: Output:

Enter the number of calls made Amount to be paid =180.0

50

Input: Output:

Enter the number of calls made Amount to be paid =300.0

120

Input: Output:

Enter the number of calls made Amount to be paid =422.0

220

Enter the number of calls made Amount to be paid =624.0

370

Question:
Write a program in java to enter two numbers a
and b where (a>b) along with a code and display
1.Product of the numbers.
2.The quotient when first number is divided by
the second number.
3.The remainder when first number is divided by
the second number.
Perform the above calculations by checking the
code whether it is 1,2 or 3 respectively.

//to perform differnet tasks by using if statement

import java.util.*;
class code

{Scanner sc=new Scanner(System.in);

int a,b,code;

double pro,quo,rem;

void in()

{System.out.println("Enter two numbers where first no.is greater


than the second no.");

a=sc.nextInt();

b=sc.nextInt();

System.out.println("1.Product");

System.out.println("2.Quotient");

System.out.println("3.Remainder");

System.out.println("Enter the code");

code=sc.nextInt();

if(code==1)

{pro=a*b;

System.out.println("Product of numbers = "+pro);

if(code==2)

{quo=a/b;

System.out.println("Quotient of the numbers = "+quo);


}

if(code==3)

{rem=a%b;

System.out.println("Remainder of the numbers = "+rem);

public static void main()

{code c1=new code();

c1.in();

}//main

}//class

Input:

Enter two numbers where first no.is greater than the second no.
20
5
1.Product
2.Quotient
3.Remainder
Enter the code
1
Output:
Product of numbers = 100.0
Output (when code selected is 2 and a=20,b=5):
Quotient of the numbers = 4.0
Output (when code selected is 3 and a=20,b=5):
Remainder of the numbers = 0.0

3. If else if statement:
It is also a logical construct where more than two
actions are to be taken depending upon the
conditions by using a key word ‘elseif’.
Syntax:
If(condition 1)
Statement 1
else if(condition 2)
Statement 2
else
Statement 3
Programmes using if else if statement :
Question:
A company introduces an early retirement
scheme to their employees below 60 years of age
according to the following terms and conditions:
Age Monthly Salary
Up to 40 years 1.1% of basic+
DA(20% of basic.

>40 years and upto 1.25% of


basic+
50 years DA(30% of basic)
>50 years and upto 1.5% of
basic+
59 years DA(40% of
basic)

Write a program in java to calculate the total


montlhy salary to be drawn by an employee
taking age and basic as inputs.

//to claculate total monthly salary to be drawn


import java.util.*;
class retirement
{double age,salary,da1,da2,da3,b1,b2,b3;//global variables
void in()
{Scanner sc=new Scanner(System.in);
System.out.println("Enter your age");
age=sc.nextDouble();
System.out.println("Enter your salary");
salary=sc.nextDouble();
da1=0.2*salary;//calculation of DA
da2=0.3*salary;
da3=0.4*salary;
b1=0.011*salary+da1;//calculation of monthly salary
b2=0.0125*salary+da2;
b3=0.015*salary+da3;
}
void check()
{if (age<=40)//use of if else if
System.out.println("Total salary="+b1);
else if(age>40&&age<=50)
System.out.println("Total salary="+b2);
else if(age>50&&age<=59)
System.out.println("Total salary="+b3);
else
System.out.println("Wrong Input");
}
public static void main()
{retirement r1=new retirement();
r1.in();
r1.check();
}//main
}//class
Input: Output:
Enter your age Total salary=8440.0
30
Enter your salary
40000
Input: Output:
Enter your age Total salary=17187.5
45
Enter your salary
55000
Input: Output:
Enter your age Total salary=24900.0
59
Enter your salary
60000
Question:
Write a program in java to input marks in 3
subjects secured by a student. Find the grade
obtained by him in the term examination as per
the following norms.
Marks obtained: Grade:
>=90% A+
>=80% and <90% A
>=70% and <80% B+
>=60% and <70% B
>=50% and <60% C+
>=40% and <50% C
<40% No Grade

//to calculate grade of a student


import java.util.*;
class marks
{double marks1,marks2,marks3,tm,p;//global variables
void in()
{Scanner sc=new Scanner(System.in);
System.out.println("Enter marks in 3 subjects");
marks1=sc.nextDouble();
marks2=sc.nextDouble();
marks3=sc.nextDouble();
tm=marks1+marks2+marks3;//to claculate sum of marks
p=tm/3;//to claculate percentage of marks
System.out.println("Percentage="+p);
}
void check()//to check the grade
{if (p>=90)//use of if else if
System.out.println("The grade is A+");
else if (p>=80&&p<=90)
System.out.println("The grade is A");
else if(p>=70&&p<=80)
System.out.println("The grade is B+");
else if(p>=60&&p<=70)
System.out.println("The grade is B");
else if(p>=50&&p<=60)
System.out.println("The grade is C+");
else if(p>=40&&p<=50)
System.out.println("The grade is C");
else if(p<40)
System.out.println("No Grade");
else
System.out.println("wrong");
}
public static void main()
{ marks m1=new marks();
m1.in();
m1.check();
}//main
}//class
Input:
Enter marks in 3 subjects
75
80
90
Output:
Percentage=81.66666666666667
The grade is A

Input:
Enter marks in 3 subjects
50
60
55
Output:
Percentage=55.0
The grade is C+

Input:
Enter marks in 3 subjects
29
35
18
Output:
Percentage=27.333333333333332
No Grade

4. Nested if (if-else-if) statement:


When an if statement is placed within another if
statement, the construct is known as nested if
statement. It can be done by using if-else-if
structure.
Syntax:
if (condition 1)
{ if (condition 2)
Statement 1
Else
Statement 2
}
Else
{ if (condition 3)
Statement 3
Else
Statement 4
}
Programes using nested if:
Question:
Write a program in java to enter three unequal
numbers and display the second greatest number
among the three numbers.
Sample input: Sample
output:
Enter three numbers: The second
45,67,32 greatest
number
is 45
//to find out the second greatest number
import java.util.*;
class second_greatest
{Scanner sc= new Scanner(System.in);
int a,b,c;//global variables
void in()
{System.out.println("Enter three numbers");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a>b&&a>c)//use of nested if
{if(b>c)
System.out.println("The second greatest number is " +b);
else
System.out.println("The second greatest number is " +c);
}
if(b>a&&b>c)
{if(a>c)
System.out.println("The second greatest number is " +a);
else
System.out.println("The second greatest number is " +c);
}
if(c>a&&c>b)
{if(a>b)
System.out.println("The second greatest number is " +a);
else
System.out.println("The second greatest number is " +b);
}
}
public static void main()
{second_greatest s1=new second_greatest();
s1.in();
}//main
}//class
Input:
Enter three numbers
211
242
532
Output:
The second greatest number is 242

Switch Case/Menu Driven Program:


This is a branching control statement, where the
end of each case acts as an exit from the switch
statement. In switch case statement, a number of
statements are put together with different cases.
The switch statement works as a jumping
statement, where the control is transferred to a
specific case as per the given switch value. Each
case ends with a break statement, which acts as
a case terminator that guides the control to come
out of the switch block.
Syantax:
switch(n)
{ case 1:
Task to perform ;
break;
case 2:
Task to perform ;
break;
default:
Default statement;
}
Question:
Write a program in java for a school where the
basic slary and experience allowance of a
teacher is considered as per the qualification and
experience in the table shown below:
Category Qualification Experience allowance Total Basic
1. Primary ₹500/- per year
Basic+exp.allowance
2. TGT ₹700/- per year
Basic+exp.allowance
3. PGT ₹900/- per year
Basic+exp.allowance
//to calculate gross and net salary.
import java.util.*;
class exp_allowance
{int cat,y;//global variables of int type
double basic,da,hra,cta,pf,gross,net,ea,tb;//global variables of
double type
Scanner sc=new Scanner(System.in);
void in()
{System.out.println("Enter the catagory(1,2 or 3)");
cat=sc.nextInt();
System.out.println("Enter basic salary");
basic=sc.nextDouble();
System.out.println("Enter years of service");
y=sc.nextInt();
}
void check()
{switch(cat)//use of switch case
{case 1:ea=500*y;
calc();//calling calc() method
disp();//calling disp() method
break;
case 2:ea=700*y;
calc();
disp();
break;
case 3:ea=900*y;
calc();
disp();
break;
}
}
void calc()
{ tb=basic+ea;//claculation of total basic
da=40/100*tb;//claculation of DA
hra=20/100*tb;//claculation of HRA
cta=10/100*tb;//claculation of CTA
pf=8.33/100*tb;//claculation of PF
gross=tb+da+hra+cta;//claculation of gross salary
net=gross-pf;//claculation of net basic
}
void disp()//displaying all the calculations
{System.out.println("Entered Category is " +cat);
System.out.println("Entered Basic Salary is " +basic);
System.out.println("Entered Years of Service is " +y);
System.out.println("Total Basic= " +tb);
System.out.println("Gross Salary= " +gross);
System.out.println("Net Salary= " +net);
System.out.println("Experience allowance= " +ea);
}
public static void main()
{exp_allowance e1=new exp_allowance();
e1.in();
e1.check();
}//main
}//class
Input:
Enter the catagory(1,2 or 3)
1
Enter basic salary
40000
Enter years of service
5
Output:
Entered Category is 1
Entered Basic Salary is 40000.0
Entered Years of Service is 5
Total Basic= 42500.0
Gross Salary= 42500.0
Net Salary= 38959.75
Experience allowance= 2500.0
//to display the answers according to users choice
import java.util.*;
class menu
{int a,b,sum,diff,product,age,c;//global variables of int type
double division;//global variables of double type
char g,M,m,F,f;//global variables of character type
Scanner sc=new Scanner(System.in);
void in()
{System.out.println("*****MENU******");
System.out.println("1.Arithmetic Operations");
System.out.println("2.Major or Minor");
System.out.println("3.Gender of a person");
System.out.println("Enter your choice");
c=sc.nextInt();
{switch(c)//use of switch case
{ case 1: arithmetic();//calling a method
break;
case 2:major_minor();
break;
case 3:gender();
break;
}
}
}
void arithmetic()//performing arithmetic operations
{System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
sum=a+b;
System.out.println("Sum of two numbers="+sum);
diff=a-b;
System.out.println("Difference of two numbers="+diff);
product=a*b;
System.out.println("Product of two numbers="+product);
division=a/b;
System.out.println("Quotient of two numbers="+division);
}
void major_minor()//finding whether a person is a major or a minor
{ System.out.println("Enter your age");
age= sc.nextInt();
if(age>=18)
System.out.println("Major");
else
System.out.println("Minor");
}
void gender()//finding the gender of a person
{System.out.println("Enter your gender:(M/F)");
g=sc.next().charAt(0);
if(g=='M'||g=='m')
System.out.println("The gender is male");
else if(g=='F'||g=='f')
System.out.println("The gender is female");
else
System.out.println("No such gender");
}
public static void main()
{menu m1=new menu();
m1.in();
}//menu
}//class
Input: Output:
*****MENU****** Sum of two numbers=60
1.Arithmetic Operations Difference of two numbers=40
2.Major or Minor Product of two numbers=500
3.Gender of a person Quotient of two numbers=5.0
Enter your choice
1
Enter two numbers
50
10

Input: Output:
*****MENU****** Minor
1.Arithmetic Operations
2.Major or Minor
3.Gender of a person
Enter your choice
2
Enter your age
16
Input: Output:
*****MENU****** Major
1.Arithmetic Operations
2.Major or Minor
3.Gender of a person
Enter your choice
2
Enter your age
26

Input Output:
*****MENU****** The gender is male
1.Arithmetic Operations
2.Major or Minor
3.Gender of a person
Enter your choice
3
Enter your gender:(M/F)
M
Input: Output:
*****MENU****** The gender is female
1.Arithmetic Operations
2.Major or Minor
3.Gender of a person
Enter your choice
3
Enter your gender:(M/F)
f

ITERATION THROUGH LOOPS:


LOOP : Loop is a repetitive structure used in a
program, to execute a set of statements for a
given number of time, in order to carry out a
specific task.
A looping structure contains the following parts:
Control Variable: A variable which starts with
an initial value and determines the duration of
the repetition is known as Control variable.
Body of the loop: A set of statements, which
are executed within the loop simultaneously.
Test condition: Each loop contains a test
condition. The loop has to be repeated or
termiated, depends upon the test condition. The
control enters the body of the loop for execution,
if the test condition is true, otherwise it
terminates.
Step value: The step value in a looping
structure determines the increment or
decrement(i.e., updating) of the control variable
unless the test condition is false.
FIXED ITERATIONS:
For Loop:
In this type of looping structure, the statements
are repeated for fixed number of times. The
control terminates after repeating the execution
of the statements for a given number of times
Syntax:
Initial value Test condition Update
for(a=1; a<=10; a++)
The order of execution of the loop is as follows:
1. First of all the variable is initialized to 1.
2. Control checks the condition, if it is true, then
it executes the statements enclosed within the
{ }.
3. After executing the loop body, it returns back
to the for statement and increases the value of a
by 1.
4. The steps are repeated from (2) and finally the
control exits the loop, as soon as the test
condition is false.

You might also like