Java: Java Problem Solving and Programming - CH 3 Exercises Flashcards


Set Details Share
created 14 years ago by primet21
7,678 views
book cover
Java
Chapter 3
Chapter 3 - Flow of Control: Branching
updated 11 years ago by primet21
Grade levels:
9th grade, 10th grade, 11th grade, 12th grade, College: First year, College: Second year, College: Third year, College: Fourth year, Graduate school, Professional
Subjects:
programming, java, computers, beginner, programming languages
show moreless
Page to share:
Embed this setcancel
COPY
code changes based on your size selection
Size:
X
Show:

1

Write a fragment of code that will test whether an integer variable score contains a valid test score. Valid test scores are in the range 0 to 100

if(score >= 0 && score <= 100)
{
System.out.println("valid test score");
}
else
{
System.out.println("invalid test score");
}

2

Write a fragment of code that will change the integer value stored in x as follows. If x is even, divide x by 2. If x is odd, multiply x by 3 and subtract 1.

if(x % 2 == 0) //even
{
x = x / 2;
}
else //odd
{
x = x * 3 - 1;
}

3

Suppose you are writing a program that asks the user to give a yes-or-no response. Assume that the program reads the user's response into the String variable response.
a) If response is yes or y, set the boolean variable accept to true;otherwise, set it to false.
b) How would you change the code so that it will also accept Yes and Y?

a)
boolean accept = false;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your response");
String response = keyboard.next();
if(response.equals("yes") || response.equals("y"))
{
accept = true;
}
else
{
accept = false;
}

b)
Change if statement to:
if(response.equals("yes") || response.equals("y")
|| response.equals("Yes") || response.equals("Y"))

4

Consider the following fragment of code:
if(x > 5)
System.out.println("A");
else if(x < 10)
System.out.println("B");
else
System.out.println("C");

What is displayed if x is:
a)4 b)5 c)6 d)9 e)10 f)11

a)B
b)B
c)A
d)A
e)A
f)A

5

Consider the following fragment of code:
if(x > 5)
{
System.out.println("A");
if(x < 10)
System.out.println("B");
}
else
System.out.println("C");

What is displayed if x is:
a)4 b)5 c)6 d)9 e)10 f)11

a)C
b)C
c)
A
B
d)
A
B
e)A
f)A

6

We would like to assess a service charge for cashing a check. The service charge depends on the amount of the check. If the check amount is less than $10, we will charge $1. If the amount is greater than $10 but less than $100, we will charge 10 percent of the amount. If the amount is greater than $100, but less than $1,000, we will charge $5 plus 5 percent of the amount. If the value is over $1,000, we will charge $40 plus 1 percent of the amount. Use a multibranch if-else statement in a fragment of code to compute the service charge.

double serviceCharge = 0.0;
if(amount < 10)
{
serviceCharge = 1.0;
}
else if(amount < 100)
{
serviceCharge = 0.1 * amount;
}
else if(amount < 1000)
{
serviceCharge = 5 + (0.05 * amount);
}
else if(amount > 1000)
{
serviceCharge = 40 + (0.01 * amount);
}

7

What is the value of each of the following boolean expressions if x is 5, y is 10, and z is 15?
a)(x < 5 && y > x)
b)(x < 5 || y > x)
c)(x > 3 || y < 10 && z == 15)
d)(!(x > 3) && x != z || x + y == z)

a)false
b)true
c)true
d)true

8

The following code fragment will not compile. Why?
if !x > x + y
x = 2 * x;
else
x = x + 3;

2 reasons:
First: "!" cannot be applied to an int
Second: Parenthesis are needed around if statement

Should look like this:
if(!(x > x + y))

9

Consider the boolean expression ((x > 10) || (x < 100)). Why is this expression probably not what the programmer intended?

The expression will always returns true

10

Consider the boolean expression ((2 < 5) && (x < 100)). Why is this expression probably not what the programmer intended?

Since (2 < 5) is always true there is no reason to include it in the if statement.

11

Write a switch statement to convert a letter grade into an equivalent numeric value on a four-point scale. Set the value of the variable gradeValue to 4.0 for an A, 3.0 for a B, 2.0 for a C, 1.0 for a D and 0.0 for an F. For any other letter, set the value to 0.0 and display an error message.

switch(grade)
{
case 'A':
gradeValue = 4.0;
break;
case 'B':
gradeValue = 3.0;
break;
case 'C':
gradeValue = 2.0;
break;
case 'D':
gradeValue = 1.0;
break;
case 'F':
gradeValue = 0.0;
break;
default:
gradeValue = 0.0;
System.out.println("Invalid Grade");
}

12

Consider the previous question, but include + or - letter grades. A+ is 4.25, A- is 3.75, B+ is 3.25, B- is 2.75, and so on.
a)Why can't we use one switch statement with no other conditionals to convert these additional letter grades?
b)Write a fragment of code that will do the conversion using a multibranch if-else statement.
c)Write a fragment of code that will do the conversion using nested switch statements.

a)
Switch statements cannot be used with strings and A+, A-, etc are strings.

b)
if(grade.equals("A+"))
{
gradeValue = 4.25;
}
else if(grade.equals("A"))
{
gradeValue = 4.0;
}
else if(grade.equals("A-"))
{
gradeValue = 3.75
}
//process B, C and D the same as A
else if(grade.equals("F+"))
{
gradeValue = 0.25;
}
else if(grade.equals("F") || grade.equals("F-"))
{
gradeValue = 0.0;
}
else
{
gradeValue = 0.0;
System.out.println("Invalid Grade");
}

c)
char gradeLetter = grade.charAt(0);
char plusMinus = ' ';
if(grade.length() == 2)
{
plusMinus = grade.charAt(1);
}
switch(gradeLetter)
{
case 'A':
switch(plusMinus)
{
case '+':
gradeValue = 4.25;
break;
case '-':
gradeValue = 3.75;
break;
default:
gradeValue = 4.0;
}
//process B, C and D the same as A
case 'F':
switch(plusMinus)
{
case '+':
gradeValue = 0.25;
break;
case '-':
default:
gradeValue = 0.0;
}
default:
gradeValue = 0.0;
System.out.println("Invalid Grade");
}

13

Imagine a program that displays a menu of five possible choices, lettered a through e. Suppose the user's selection is read into the character variable choice. Write a switch statement that reacts to this choice by displaying a message that indicates the choice. Display an error message if the user makes an invalid choice.

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a letter a - e inclusively");
String response = keyboard.next();
char choice = ' ';
if(response.length() == 1)
{
choice = response.charAt(0);
}
switch(choice)
{
case 'a':
System.out.println("You entered a");
break;
case 'b':
System.out.println("You entered b");
break;
case 'c':
System.out.println("You entered c");
break;
case 'd':
System.out.println("You entered d");
break;
case 'e':
System.out.println("You entered e");
break;
default:
System.out.println("Invalid Character");
}

14

Repeat the previous exercise, but define an enumeration and use it within the switch statement.

enum Choice {a, b, c, d, e}
Choice choice = Choice.a;
switch(choice)
{
case a:
System.out.println("You entered a");
break;
case b:
System.out.println("You entered b");
break;
case c:
System.out.println("You entered c");
break;
case d:
System.out.println("You entered d");
break;
case e:
System.out.println("You entered e");
break;
default:
System.out.println("Invalid Character");
}

15

Repeat Exercise 13, but use a multibranch if-else statement instead of a switch statement.

if(choice == 'a')
{
System.out.println("You entered a");
}
else if(choice == 'b')
{
System.out.println("You entered b");
}
else if(choice == 'c')
{
System.out.println("You entered c");
}
else if(choice == 'd')
{
System.out.println("You entered d");
}
else if(choice == 'e')
{
System.out.println("You entered e");
}
else
{
System.out.println("Invalid Character");
}

16

Given that the int variable temp contains a temperature that is not negative, write a Java statement that uses the conditional operator to set the String variable label to either "degree" or "degrees". We wan to use label to produce grammatically correct output, such as 0 degrees, 1 degree, 2 degrees, and so on. If you have not studied the conditional operator, use an if-else statement instead.

//using conditional operator
String label = temp + " degree" + (temp != 1 ? "s" : "");

//using if statement
String label = temp + " degree";
if(temp != 1)
{
label += "s";
}

17

Write Java statements that create a yes-or-no dialog box to answer the question, "Are you in college?"

int answer = JOptionPane.showConfirmDialog(null, "Are you in college?", "Question", JOptionPane.YES_NO_OPTION);
if(answer == JOptionPane.YES_OPTION)
{
System.out.println("Hello College Student");
}
else if(answer == JOptionPane.NO_OPTION)
{
System.out.println("Hello Non-College Student");
}
else
{
System.out.println("You closed the dialog");
}