You are on page 1of 4

Answer the following Checkpoint questions from your textbook:

3.7 Write an if statement that displays "Goodbye" if the String


variable myWord starts with the character 'D'.

Ans:

if (myCharacter == ‘D’)

System.out.println(“Goodbye”);

3.10 Write an if-else statement that assigns 0.10


to commissions unless sales is greater than or equal to 50000.0, in which
case it assigns 0.2 to commissions.

Ans:

if (sales >= 50000.0)

commissions = 0.2;

else

commissions = 0.01;

3.15 Write a program that displays "is ice" if the temperature is below 0,
displays "is water, if the temperature is above 0 and below 100, and
displays "is vapour" if the temperature is above 100.

Ans:

if (temperature < 0)

System.out.println(“is ice”);

else if (temperature >= 0 && temperature <= 100)

System.out.println(“is water”);

else

System.out.println(“is vapour”);

3.18 Write an if statement that displays the message “The number is


valid” if the variable speed is within the range 0 through 200.

Ans:
if (speed >= 0 && speed <= 200)

System.out.println(“The number is valid”);

3.21 Assume the variables name1 and name2 reference two different
String objects, containing different strings. Write code that displays the
strings referenced by these variables in alphabetical order.

Ans:

if ( name1.compareTo(name2) >0)

System.out.println(name2 + name1);

else

System.out.println(name1 + name2);

3.25 Rewrite the following if-else-if statement as a switch statement.

if (selection == 'A')

System.out.println("You selected A.");

else if (selection == 'B')

System.out.println("You selected B.");

else if (selection == 'C')

System.out.println("You selected C.");

else if (selection == 'D')

System.out.println("You selected D.");

else

System.out.println("Not good with letters, eh?");


Ans:
switch (selection)
{
case ‘A’:
System.out.println("You selected A.");
break;
case ‘B’:
System.out.println("You selected B.");
break;
case ‘C’:
System.out.println("You selected C.");
break;
case ‘D’:
System.out.println("You selected D.");
break;
default:
System.out.println("Not good with letters, eh?");
}

3.28 What will the following code display?


int funny = 7, serious = 15;

funny = serious * 2;

switch (funny)

case 0 :

System.out.println("That is funny.");

break;

case 30:

System.out.println("That is serious.");

break;

case 32:

System.out.println("That is seriously funny.");

break;

default:

System.out.println(funny);

Ans:

That is serious
3.29 Assume the following variable declaration exists in a program:

double number = 1234567.456;

Write a statement that uses System.out.printf to display the value of the


number variable formatted as:

1,234,567.46

Ans:

System.out.println(“%,.2f”,number);

You might also like