Oop Exp2
Oop Exp2
Oop Exp2
. Aim: WAP to print the grade for an input test score: using the if-else ladder
• Percentage • Grade
• 0-60 • F
• 61-70 • D
• 71-80 • C
• 81-90 • B
• 91-100 • A
Theory:
Java supports two types of castings – primitive data type casting and reference type casting.
Reference type casting is nothing but assigning one Java object to another object. It comes with
very strict rules.
Java data type casting comes with 3 flavors.
1. Implicit casting
2. Explicit casting
3. Boolean casting.
1. Implicit casting (widening conversion)
A data type of lower size (occupying less memory) is assigned to a data type of higher size. This
is done implicitly by the JVM. The lower size is widened to higher size. This is also named
as automatic type conversion.
Examples:
In the above code 4 bytes integer value is assigned to 8 bytes double value.
A data type of higher size (occupying more memory) cannot be assigned to a data type of
lower size. This is not done implicitly by the JVM and requires explicit casting; a casting
operation to be performed by the programmer. The higher size is narrowed to lower size.
n the above code, 8 bytes double value is narrowed to 4 bytes int value. It raises error. Let us
explicitly type cast it.
The double x is explicitly converted to int y. The thumb rule is, on both sides, the same data type
should exist.
Java has very flexible three looping mechanisms. You can use one of the following three loops:
while Loop
do...while Loop
for Loop
Java provides a rich set of operators to manipulate variables. We can divide all the Java
operators into the following groups:
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Conditional Operator
Special Operator
TheBitwiseOperators:
Java defines several bitwise operators, which can be applied to the integer types, long, int,
short, char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b =
13; now in binary format they will be as follows:
a = 0011 1100
b = 0000 1101
~a = 1100 0011
2 | (bitwise or)
Binary OR Operator copies a bit if it exists in either operand.
Example: (A | B) will give 61 which is 0011 1101
3 ^ (bitwise XOR)
Binary XOR Operator copies the bit if it is set in one operand but not both.
Example: (A ^ B) will give 49 which is 0011 0001
SIES GRADUATE SCHOOL OF TECHNOLOGY Page 20
4 ~ (bitwise compliment)
Binary Ones Complement Operator is unary and has the effect of 'flipping'
bits.
Example: (~A ) will give -61 which is 1100 0011 in 2's complement form
due to a signed binary number.
In addition to the nested if statement, Java provides a second method for choosing from many
alternative actions. Compare the following code, each of which accomplishes the same task.
The default case, at the end of the swtich statement, acts similarly to the else at the end of the
nested if. It will catch any value that doesn't have an exact match in the cases. Although it is not
necessary to include the default case (just as it is not necessary to include the else), it is good
programming practise to account for any unexpected values.
One of the significant limitations of the switch statement is the expression that can be used to
control it. With an if statement, anything can be compared to produce an true or false result
(e.g., primitive data such as int or float, more complex data such as Strings, or even objects
created by the user).
In a switch statement, however, the expression controlling the switch must have an integer
representation, which limits it to the following data types: byte, short, int, long, or char.
Example :
switch (score)
{
case 1:
grade = 'A';
break;
case 2:
grade =
'B';
SIES GRADUATE SCHOOL OF TECHNOLOGY Page 21
break;
case 3:
grade =
'C';
break;
default:
System.out.println("Invalid score – grade of ? assigned");
grade = '?';
break;
With this example we are going to demonstrate how to generate prime numbers with a simple
for loop. A prime number is a number that has no positive divisors other than 1 and itself. In
short, to generate a prime number using a for loop you should:
▪ Create a for statement with an int i variable from 1 to a max int number, and step equal
to 1.
▪ For each one of the numbers in the loop create a boolean isPrimeNumber equal to
true and create another loop where the number is divided to other numbers from 2
up to the number, and if the result is zero, then the boolean isPrimeNumber is set to
false.
Program logic :
Conclusion: Thus we have implemented basic java programs and studied basic
syntax of Java programming language.
a. WAP to print the grade for an input test score: using the if-else ladder.
Percentage Grade
1. 0-60 - E
2. 61-70 - D
3. 71-80 - C
4. 81-90 - B
5. 91-100 – A
import java.util.*;
class grade
int e=sc.nextInt();
int m=sc.nextInt();
int p=sc.nextInt();
int c=sc.nextInt();
int b=sc.nextInt();
int avg=(e+m+p+c+b)/5;
if(avg>90)
System.out.println("A Grade");
else if(avg<91&&avg>80)
System.out.println("B Grade");
else if(avg<81&&avg>70)
System.out.println("C Grade");
else if(avg<71&&avg>60)
System.out.println("D Grade");
else
System.out.println("E Grade");
}
OUTPUT:
b. Switch-case: menu driven calculator.
import java.util.*;
class calculator
int z=0;
double d=0.0;
double a=sc.nextDouble();
double b=sc.nextDouble();
do
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
int c=sc.nextInt();
switch(c)
case 1:
{
d=a+b;
System.out.println("Addition="+d);
break;
case 2:
d=a-b;
System.out.println("Subraction="+d);
break;
case 3:
d=a*b;
System.out.println("Multiplication="+d);
break;
case 4:
d=a/b;
System.out.println("Division="+d);
break;
default:
System.out.println("Invalid Choice");
}
System.out.println("Press 1 to continue");
z=sc.nextInt();
while(z==1);
OUTPUT:
c. WAP to find whether the entered number is Armstrong no or not.
import java.util.*;
class armstrong
double r=0.0;
double sum=0.0;
int count=0;
int n=sc.nextInt();
int a=n;
int b=n;
while(a!=0)
count++;
a=a/10;
}
while(b!=0)
r=b%10;
sum=sum+Math.pow(r,count);
b=b/10;
if(sum==n)
System.out.println("Armstrong number");
else
OUTPUT:
Conclusion: Through this program we have studied and implemented the use of control
statements