ion 3
Mention the output of the following :
class relativeOP [10]
{
public static void main()
{
int x,y,c=4;
x=20; y=10;
System.out.println(" x > y : "+(x>y));
System.out.println(" x < y : "+(x<y));
System.out.println(" x == y : "+(x==y));
System.out.println(" output 1 : "+(c=x*y));
System.out.println(" output 1 : "+(c==x*y));
}
}
Write the valid statements for the following in java : [2 x 5 =10]
i)Print the rounded off value of 234.49
ii)Print the absolute value of -9.99
iii)Print the largest of -45 and -50
iv) Print the smallest of -56 and -57.4
v) Print the random integer between 50 and 70
SECTI
Answers
Answer:
X > Y:True ; X<Y: False; X==Y: False; output 1: 200; Output 1:true
Explanation Override any value:
here so first you can declare or initialize a veriable globally and then inside any method you can change the values of that integer if the integer is not static.
valid statement: System.out.println("2 x 5 =" +(2*5))
i) there are 2 ways
A) Double number=234.49;
int value=(int)number;
System.out.println(value);
B) import java. lang. Math; //needed for round function
Double number=234.49;
System.out.println(Math.round(number))
/* I personally prefer 1st method cause it doesnt require you to import new libraries, if you are using math library you can use 2nd method then it is easy*/
ii)import java. lang. Math.*; //sometime it doesnt work when you dont add *
int x = -9.99;
// get and print their absolute values
System.out.println("the absolute value of " + x + "=" + Math.abs(x));
iii) int x= -45;
int y= -50;
if(x>y)
{
System.out.println("Largest is:" +x);
}
else {
System.out.println("Largest is:"+y);
}
iv) float x= -56; //you can use double here to save memory
float y= -57.4;
if(x>y)
{
System.out.println("smallest is:" +y);
}
else {
System.out.println("smallest is:"+x);
}
v) import java.util.Random;
for (int i = 0; i < 10; i++) {
int random = getRandomInRange(50, 70);
System.out.print(random + " ");
}
System.out.println();
/* you can also use
Random random = new Random();
int randomNumber = random.nextInt(upperBound - lowerBound) + lowerBound;
But this method is upper bount exclusive*/