Java Reference
In-Depth Information
case 2:
System.out.println("false");
break;
default:
System.out.println("unknown");
break;
}
19. Given the following code, state five appropriate test values for the variable,
dollars, that would cause the catch statement to be executed.
try
{
double dollars = getSales();
if (dollars < 0) throw new NumberFormatException;
}
catch (NumberFormatException e)
{
outputLabel.setText("You must enter a valid dollar amount greater than zero.");
}
20. Assuming correct variable declaration and assignment, write a while state-
ment for each of the following conditions.
answer is equal to yes
done is set to false
moreRecords is set to true
sex is male and age is greater than or equal to 21
21. Write a try statement and catch statement that will test a value entered by the
user into a TextField named myAge. If the value is not a valid age, the catch
statement should display a message in a Label named errorLabel. Hint: the
error message should display for errors in range, validity, and data type.
22. Given two positive integer variables, intA and intB, write a sequence of state-
ments to assign the variable with the larger value to intBig and the variable
with the smaller value to intSmall. If intA and intB are equal, assign either to
intSame. Be sure to show variable declarations.
23. The values of three variables, intU, intV, and intW, are positive and not equal
to each other. Using if and else statements, determine which has the smallest
value and assign this value to intLittle. Be sure to show variable declarations.
24. Write a partial program to display a message that reads, “Amanda Hefner,
you may be the winner of $10,000.00!” in a JOptionPane dialog box. The
message should use the proper concatenation and formatting. The title of
the dialog box should be, Amanda Hefner, Lucky Winner. Properly declare
any necessary variables. Use the corresponding variables listed below for the
first name, last name, and winnings amount, but do not code those values
directly into the statements.
String firstName = "Amanda"
String lastName = "Hefner"
double winnings = 10000.00
25. Write a partial program that creates a color called purple using RGB
values and then displays the color as the background of an applet.
Search WWH ::




Custom Search