Chapter 5: Conditionals and Loops
Lab Exercises
Topics Lab
Exercises
Boolean expressions PreLab Exercises (in-class)
The if statement Computing
a Raise (in-class)
The switch statement A
Charge Account Statement (submit)
Activities
at
Rock, Paper,
Scissors (extra credit)
Prelab Exercises
Sections 5.1-5.3
1. Rewrite each condition below in valid Java
syntax (give a boolean
expression):
a. x > y > z
b. x and y are both less
than 0
c. neither x nor y is less
than 0
d. x is equal to y but not
equal to z
2. Suppose gpa is a
variable containing the grade point average of a student. Suppose the goal of a
program is to let a student know if he/she made the Dean's list (the gpa must be 3.5 or above). Write
an if... else... statement that
prints out the appropriate message (either "Congratulations—you made the
Dean's List" or "Sorry you didn't make the Dean's List").
3. Complete the following program to determine
the raise and new salary for an employee by adding if ... else statements to
compute the raise. The input to the program includes the current annual salary
for the employee and a number indicating the performance rating (1=excellent,
2=good, and 3=poor). An employee with a rating of 1 will receive a 6% raise, an
employee with a rating of 2 will receive a 4% raise, and one with a rating of 3
will receive a 1.5% raise.
// ***************************************************************
// Salary.java
// Computes the raise and
new salary for an employee
// ***************************************************************
import java.util.Scanner;
public
class Salary
{
public
static void main (String[] args)
{
double
currentSalary;
// current annual salary
double rating; // performance rating
double
raise; // dollar amount of the
raise
Scanner scan = new Scanner(System.in);
// Get the current
salary and performance rating
System.out.print
("Enter the current salary: ");
currentSalary = scan.nextDouble();
System.out.print
("Enter the performance rating: ");
rating
= scan.nextDouble();
// Compute the raise
-- Use if ... else ...
// Print the results
System.out.println
("Amount of your raise: $" + raise);
System.out.println
("Your new salary: $" + currentSalary +
raise);
}
}
Computing A Raise
File Salary.java
contains most of a program that takes as input an employee's salary and a
rating of the employee's performance and computes the raise for the employee.
This is similar to question #3 in the pre-lab, except that the performance
rating here is being entered as a String—the three possible ratings are
"Excellent", "Good", and "Poor". As in the
pre-lab, an employee who is rated excellent will receive a 6% raise, one rated
good will receive a 4% raise, and one rated poor will receive a 1.5% raise.
Add the if... else... statements to program Salary
to make it run as described above. Note that you will have to use the equals method of the String class (not
the relational operator ==) to compare two strings (see Section 5.3, Comparing
Data).
//
***************************************************************
// Salary.java
//
// Computes the amount of a raise and the new
// salary for an employee. The current salary
// and a performance rating (a String:
"Excellent",
// "Good" or "Poor") are
input.
//
***************************************************************
import java.util.Scanner;
import java.text.NumberFormat;
public class Salary
{
public static void
main (String[] args)
{
double currentSalary; //
employee's current salary
double
raise; // amount of the raise
double newSalary; //
new salary for the employee
String rating; // performance rating
Scanner scan = new Scanner(System.in);
System.out.print
("Enter the current salary: ");
currentSalary
= scan.nextDouble();
System.out.print
("Enter the performance rating (Excellent, Good, or Poor): ");
rating = scan.next();
// Compute the raise using if ...
newSalary
= currentSalary + raise;
// Print the results
NumberFormat
money = NumberFormat.getCurrencyInstance();
System.out.println();
System.out.println("Current Salary:
" + money.format(currentSalary));
System.out.println("Amount of your raise: " + money.format(raise));
System.out.println("Your new salary:
" + money.format(newSalary));
System.out.println();
}
}
A Charge Account Statement
Write a program to prepare the monthly charge account statement
for a customer of CS CARD International, a credit card company. The program
should take as input the previous balance on the account and the total amount
of additional charges during the month. The program should then compute the
interest for the month, the total new balance (the previous balance plus
additional charges plus interest), and the minimum payment due. Assume the
interest is 0 if the previous balance was 0 but if the previous balance was
greater than 0 the interest is 2% of the total owed (previous balance plus
additional charges). Assume the minimum payment is as follows:
new
balance for a new balance less
than $50
$50.00 for a new balance between $50 and
$300 (inclusive)
20% of the new balance for a new balance over $300
So if the new balance is $38.00 then the person must pay the whole
$38.00; if the balance is $128 then the person must pay $50; if the balance is
$350 the minimum payment is $70 (20% of 350). The program should print the
charge account statement in the format below. Print the actual dollar amounts
in each place using currency format from the NumberFormat
class—see Listing 3.4 of the text for an example that uses this class.
CS CARD International Statement
===============================
Previous Balance: $
Additional Charges: $
Interest: $
New Balance: $
Minimum Payment: $
Activities at
As activity directory at Lake LazyDays
Resort, it is your job to suggest appropriate activities to guests based on the
weather:
temp >= 80: swimming
60 <= temp < 80: tennis
40 <= temp < 60: golf
temp < 40: skiing
1. Write a program that prompts the user for a
temperature, then prints out the activity appropriate for that temperature. Use
a cascading if, and be sure that your conditions are no more complex than
necessary.
2. Modify your program so that if the
temperature is greater than 95 or less than 20, it prints "Visit our
shops!". (Hint: Use a boolean operator in your condition.) For other
temperatures print the activity as before.
Rock, Paper, Scissors
Program Rock.java
contains a skeleton for the game Rock, Paper, Scissors. Open it and save it to
your directory. Add statements to the program as indicated by the comments so
that the program asks the user to enter a play, generates a random play for the
computer, compares them and announces the winner (and why). For example, one
run of your program might look like this:
$ java
Rock
Enter your play: R, P, or S
r
Computer play is S
Rock crushes scissors, you
win!
Note that the user should be able to enter either
upper or lower case r, p, and s. The user's play is stored as a string
to make it easy to convert whatever is entered to upper case. Use a switch
statement to convert the randomly generated integer for the computer's play to a
string.
//
****************************************************************
// Rock.java
//
// Play Rock, Paper, Scissors with the user
//
//
****************************************************************
import java.util.Scanner;
import java.util.Random;
public class Rock
{
public static void
main(String[] args)
{
String personPlay; //User's play -- "R",
"P", or "S"
String computerPlay; //Computer's play -- "R",
"P", or "S"
int computerInt;
//Randomly generated number used to determine
//computer's play
Scanner scan = new Scanner(System.in);
Random generator = new Random();
//Get player's play -- note that this is stored as a string
//Make player's play uppercase for
ease of comparison
//Generate computer's play (0,1,2)
//Translate computer's randomly
generated play to string
switch (computerInt)
{
}
//Print computer's play
//See who won. Use nested ifs instead of &&.
if (personPlay.equals(computerPlay))
System.out.println("It's
a tie!");
else if (personPlay.equals("R"))
if
(computerPlay.equals("S"))
System.out.println("Rock crushes scissors. You win!!");
else
//... Fill in rest
of code
}
}