Print Options

Card layout: ?

← Back to notecard set|Easy Notecards home page

Instructions for Side by Side Printing
  1. Print the notecards
  2. Fold each page in half along the solid vertical line
  3. Cut out the notecards by cutting along each horizontal dotted line
  4. Optional: Glue, tape or staple the ends of each notecard together
  1. Verify Front of pages is selected for Viewing and print the front of the notecards
  2. Select Back of pages for Viewing and print the back of the notecards
    NOTE: Since the back of the pages are printed in reverse order (last page is printed first), keep the pages in the same order as they were after Step 1. Also, be sure to feed the pages in the same direction as you did in Step 1.
  3. Cut out the notecards by cutting along each horizontal and vertical dotted line
To print: Ctrl+PPrint as a list

17 notecards = 5 pages (4 cards per page)

Viewing:

Java Problem Solving and Programming - CH 3 Exercises

front 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

back 1

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

front 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.

back 2

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

front 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?

back 3

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"))

front 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

back 4

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

front 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

back 5

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

front 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.

back 6

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);
}

front 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)

back 7

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

front 8

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

back 8

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

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

front 9

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

back 9

The expression will always returns true

front 10

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

back 10

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

front 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.

back 11

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");
}

front 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.

back 12

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");
}

front 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.

back 13

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");
}

front 14

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

back 14

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");
}

front 15

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

back 15

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");
}

front 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.

back 16

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

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

front 17

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

back 17

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");
}