Homework 5
Objectives
 Write if statements to make decisions in a program.
 Use relational and logical operators in expressions.
Note: CodingBat is a free site of live problems to build skill in Java and/or Python. It was created by Nick Parlante, who is Computer Science lecturer at Stanford. If you create an account, CodingBat will automatically save your progress online. You should also link the account with my email for review / grading purposes chaoaj@jmu.edu.
For this homework, you might find (on CodingBat's site) the Boolean Logic video 1 and video 2 useful.
Exercise 5.1 CodingBat Logic1
The Logic1 problems require if
, else
, &&
(and), 
(or), and !
(not). For this exercise, solve the following problems:
Complete these problems online first, until you get them 100% correct. Then download the BatLogic1.java source file and paste your code into the corresponding method stubs. Resolve any Checkstyle errors, and submit your final code to Autolab.
Exercise 5.2 CodingBat Logic2
The Logic2 problems are more a bit more challenging, but not fundamentally different from what you have already seen. For this exercise, solve the following problems:
Complete these problems online first, until you get them 100% correct. Then download the BatLogic2.java source file and paste your code into the corresponding method stubs. Resolve any Checkstyle errors, and submit your final code to Autolab.
Exercise 5.3 Exact Change
This exercise is a continuation of Exercise 3.5. Copy your previous solution into a file named ExactChange.java, and modify the second half of the program to display the results in this format:
Change Due $1.21 1 dollar 2 dimes 1 penny
Specifically, do not print any coins with a zero count. And if the count is one, display the singular name (e.g., dollar, quarter, dime, nickel, penny) instead of the plural name. As before, there should be a blank line before and after the "Change Due" line.
Hint: You might find it easier to calculate the number of each coin first, before outputting any results. Doing so will make the arithmetic and logic patterns more clear in the source code.
Exercise 5.4 Credit Card
You have been hired by CS CARD International, a major credit card company, to work on an application for generating monthly account statements. The main method has already been written. Download CreditCard.java and read the main method to get a sense of what the program should do. Then implement the required helper methods:

public static double inputDouble(Scanner in, String prompt)
This method is similar to Exercise 4.2, but with one additional feature. If the next token of user input is not a double, inputDouble should discard it and return 0.0 instead. That way, the program won't crash if the user enters invalid input.

public static double calculateInterest(double priorBalance, double addtlCharges)
The interest is $0.00 if the previous balance was $0.00 or less. If the previous balance was greater than $0.00, the interest is 2% of the total amount owed (i.e., previous balance plus additional charges).

public static double calculateMinPayment(double balance)
The minimum payment depends on the new balance (i.e., including additional charges and interest):
 $0.00 : for new balance less than $0
 new balance : for new balance between $0 and $49.99 (inclusive)
 $50.00 : for new balance between $50 and $300 (inclusive)
 20% of the new balance : for new balance over $300
Your solution must NOT include any logical operators (
&&
,
, and!
). Write your if statements in a way that requires only one comparison at a time.
After completing the helper methods, be sure to test the program multiple times with different inputs. Here is an example of what it will look like:
Type the previous balance: 123.45 Type the additional charges: 678.90 CS CARD International Statement =============================== Previous balance: $ 123.45 Additional charges: $ 678.90 Interest: $ 16.05 New balance: $ 818.40 Minimum payment: $ 163.68
Exercise 5.5 Interstate Numbers
In the United States, interstate highways follow a specific numbering system based on their type and direction:

One and twodigit numbers are reserved for "primary" interstates (e.g., I95). Northsouth routes are assigned odd numbers, and eastwest routes are assigned even numbers.

Threedigit numbers are for "auxiliary" interstates (e.g., I495). If the first digit is even, the road is typically a "beltway" around a major city. If the first digit is odd, the road is typically a "spur" that deviates from a primary interstate.
Define a class named Interstate with a single method getInfo that takes an integer parameter number and returns a string. There are five possible return values, depending on the parameter value:
"primary eastwest"
– even number with one or two digits (e.g., I70)"primary northsouth"
– odd number with one or two digits (e.g., I25)"beltway for I??"
– threedigit number with even first digit (e.g., I280)"spur for I??"
– threedigit number with odd first digit (e.g., I395)"invalid"
– if the given interstate number is not between 1 and 999
Beltway and spur results must include the last two digits of the interstate number in the place of ??
. For example, getInfo(395) would return "spur for I95".
Submissions for all problems for HW5 should be made to https://autolab.cs.jmu.edu