CS 149: Introduction to Programming
James Madison University, Spring 2019 Semester

Homework 5: Conditionals and Logic

Objectives

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.

For this homework, you might find (on CodingBat's site) the Boolean Logic video 1 and video 2 useful.

Exercise 5.1   CodingBat Logic-1

The Logic-1 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 Logic-2

The Logic-2 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.4. 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 existing program 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:

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:

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:

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 I-95".