Homework 5: Conditionals and Logic

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 e-mail 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 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.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 two-digit numbers are reserved for "primary" interstates (e.g., I-95). North-south routes are assigned odd numbers, and east-west routes are assigned even numbers.

  • Three-digit numbers are for "auxiliary" interstates (e.g., I-495). 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 east-west" – even number with one or two digits (e.g., I-70)
  • "primary north-south" – odd number with one or two digits (e.g., I-25)
  • "beltway for I-??" – three-digit number with even first digit (e.g., I-280)
  • "spur for I-??" – three-digit number with odd first digit (e.g., I-395)
  • "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 I-95".

 

Submissions for all problems for HW5 should be made to https://autolab.cs.jmu.edu 

 

Back to Top