Sekani Millette HW6: If (A 10) (B 0 C 1 )
Sekani Millette HW6: If (A 10) (B 0 C 1 )
HW 6
3.6 Write an if statement that assigns 0 to the variable b and assigns 1 to the variable c
if the variable a is less than 10.
if (a < 10)
{b = 0;
c = 1;
}
3.8 Write an if-else statement that assigns 20 to the variable y if the variable x is greater
than 100.
Otherwise, it should assign 0 to the variable y.
if (x < 100)
y = 20;
else
y = 0;
3.9 Write an if-else statement that assigns 1 to x when y is equal to 100. Otherwise, it
should assign 0 to x.
if (y == 100)
x = 1;
else
y = 0;
3.10 Write an if-else statement that assigns 0.10 to commission unless sales is greater
than or equal to 50000.0, in which case it assigns 0.2 to commission.
3.11 Write an if-else statement that assigns 0 to the variable b and assigns 1 to the
variable c if the variable a is less than 10. Otherwise, it should assign -99 to the variable
b and assign 0 to the variable c.
if (a < 10)
{b = 0;
c = 0;
}
else
{b = 99;
c = 0;
}
3.12 Write nested if statements that perform the following test: If amount1 is greater
than 10 and amount2 is less than 100, display the greater of the two.
3.13 Write code that tests the variable x to determine whether it is greater than 0. If x is
greater than 0, the code should test the variable y to determine whether it is less than
20. If y is less than 20, the code should assign 1 to the variable z. If y is not less than
20, the code should assign 0 to the variable z.
if (x > 0)
{ if (y < 20)
z = 1;
else
z = 0;
}
1 1
2 1
3 2
4 2
5 3
10 3
6. Write an if statement that prints the message "The number is valid" if the variable
grade is within the range 0 through 100.
7. Write an if statement that prints the message "The number is valid" if the variable
temperature is within the range -50 through 150.
8. Write an if statement that prints the message "The number is not valid" if the variable
hours is outside the range 0 through 80.
9. Write an if-else statement that displays the string objects titlel and title2 in
alphabetical order.
if (title1.compareTo(title2)<0)
System.out.println(title1 + “ comes before “ + title2 + “ alphabetically.”);
else
System.out.println(title2 + “ comes before “ + title1 + “ alphabetically.”);
6. What risk does a programmer take when not placing a trailing else at the end of an if-
else-if statement?
There is a chance that the program will not compile because Java is looking for
other factors that may be possible.