205
if(age > 64 )
System.out.print("Senior voter");
if(age < 18 )
System.out.print("Under age");
if(age >= 18 && age < 65 )
System.out.print("Regular voter");
8.The following code fragment is supposed to print out the values 25, 60, and 8, in
that order. Instead, it prints out 50, 60, and 4. Why?
length = 25 ;
width = 60 ;
if(length = 50 )
height = 4 ;
else
height = 8 ;
System.out.print(""+ length + " "+ width + " "+ height);
9.The following Java code segment is almost unreadable because of the
inconsistent indentation and the random placement of left and right braces. Fix
the indentation and align the braces properly.
// This is a nonsense program segment
if(a > 0 )
if(a < 20 )
{
System.out.print("A is in range.");
b = 5 ;
}
else
{
System.out.print("A is too large.");
b = 3 ;
}
else
System.out.print("A is too small.");
System.out.print("All done.")
10.Given the floatvariables x1,x2,y1,y2, and m, write a code segment to find the
slope of a line through the two points (x1,y1) and (x2,y2). Use the formula
m= y2– y^1
x2– x 1