Fundamental Programming Concepts
Summer 2000

Lab 4

Overview
As always, you should read through this lab and think about how you will solve the problems on it before you sit down at a computer. Doing so will actually decrease the amount of time it takes you to finish the lab. No kidding!

This lab requires the use of:

  • if statements
  • switch statements
  • New operations on strings
  • An updated version of the CS99 package

 

Preliminaries

Practice programs using if

Your textbook doesn't identify practice program that need the use of if statements alone -- most of the problems at the end of chapter 3 also require loops. Here are some problems that only require ifs. They're simpler than the problems in parts I-III of this lab. So if you're not comfortable yet with ifs, you're welcome to work on these problems. They are not part of the lab, you will not turn them in, and they will not be graded. You are, however, welcome to work on them during lab time.

  1. A three-minute telephone call to Jasper, IN costs $1.15. Each additional minute costs $0.26. Given the total length of a call in minutes, print the cost.
  2. When you first learned to divide, you expressed answers using a quotient and a remainder rather than a fraction or decimal quotient. For example, if you divided 7 by 2, your answer would have been 3 R 1. Given two integers, divide the larger by the smaller and print the answer in this form. Do not assume that the numbers are entered in any order.
  3. Revise your program from problem 2 so that, if there is no remainder, you print only the quotient without a remainder or the letter R.
  4. Given three integers, print only the largest.
  5. A substance floats in water if its density (mass/volume) is less than 1 g/cc. It sinks if it is 1 or more. Given the mass and volume of an object, print whether it will sink or float.

New String Operations

You may find it helpful to learn some methods from the String class. Below are two such methods. You can find more in LL p. 75. You don't necessarily need these for this lab's programs -- it depends on how you implement your solutions.

String equality

You can check whether two strings are equal using the equals method. Here's an example:

String firstName, lastName;
...
if (firstName.equals(lastName)) {
    System.out.println("Your first and last names are the same!");
}

Note that you call this a bit differently than other methods we've used before. You write the name of the first string variable you want to compare, followed by a dot, the name of the method, and then the name of the second string variable in parentheses.

You can also use a string literal instead of the second variable, e.g.:

String cupType;
...
if (cupType.equals("Gold and ornate")) {
    System.out.println("You choose... poorly.");
}

Getting characters from strings

Since a string is composed of characters, it's natural to want to retrieve the individual characters that are in a string. You can do so with the charAt method:

char firstInitial;
String firstName;
...
c = firstName.charAt(0);

Note that you call the charAt method in a manner identical to how you call the equals method. The parameter you pass it must be a number from 0 to one less than the number of characters in the string. 0 retrieves the first character, 1 retrieves the second, and so forth.

CS99 package update

The Console class has been updated to include a new method called readChar. A new class called TypeTransformer has been provided to allow you to transform strings to integers and doubles. Check the package's page to download the new version and read the documentation for the new methods. Here's an example of transforming a string to an integer:

String s;
int i;
...
i = TypeTransformer.stringToInt(s);

 

Part I: Calculator
Write a program that simulates a simple 16-key calculator. Prompt the user for a number (which may be a floating-point number), an operation (either '+', '-', '*', or '/'), and then another number. Output the result of the operation along with the operation, e.g., "3 + 2 = 5". The operations you must support are addition, subtraction, multiplication, and division.

Use if statements to make choices in your code (not switch or ?:). Make use of else clauses, nested ifs, and extended ifs as necessary -- your primary goals should be simplicity and readability.

Your program must be sufficiently robust to handle division by 0. That is, you should print a user-friendly error message. Your program should not produce the following error (which is what will happen if you allow the division operation to occur):

Exception in thread "main" java.lang.ArithmeticException: / by zero

Your program must also be able handle any case where the user enters an invalid operation.

 
Part II: PlanetaryWeight
The force of gravity is different for each of the nine planets in our solar system. For example, on Mercury it is only 0.38 times as strong as on Earth. Thus, if you weigh 100 pounds (on Earth), you would weigh only 38 pounds on Mercury. Write a program that allows you to enter a weight (on Earth) and a one-letter code indicating the choice of planet to which you would like the weight to be converted. (Hint: user-friendliness dictates that your program should tell the user what the one-letter codes are before prompting for the code.) Output should be the weight on the desired planet together with the planet name. 

Use a switch statement in the program for computation (not if or ?:). You may also use the same statement to do your output. Note that since characters can be treated as integers in Java, it is possible for the condition (selector) and cases in a switch statement to be of type char.

The relative forces of gravity are:

Earth 1.00
Jupiter 2.65
Mars 0.39
Mercury 0.38
Neptune 1.23
Pluto 0.05
Saturn 1.17
Uranus 1.05
Venus 0.78

Your program should be sufficiently robust to handle a one-letter code that is invalid.

 

Part III: Blackjack
Write a program that, given the dealer's first two cards in Blackjack, determines whether he should hit or stand. For the relevant rules of Blackjack, read on:

The card game of Blackjack is popular in casinos throughout the country. The game is based on the principle of coming as close to a total of 21 points in your hand as possible, but not going over 21, which is called busting

The game is played with at least a full deck of 52 cards (ace, 2-10, jack, queen, king; in all four suits). The number cards (2-10) are each worth the number of points on them; face cards (jack, queen, king) are each worth 10 points, and aces are worth either 1 or 11 at the player's choice. 

Each player is dealt two cards by the dealer, who also receives two cards. If a player has a score of 21 with those first two cards (which requires an ace and a face card or 10), she is said to have a blackjack and wins immediately. Otherwise, the player can then hit -- ask for another card. Hitting continues until the player either busts or stands -- stops asking for more cards. Once all players have busted or stood, the dealer can hit or stand. Once the dealer stands or busts, the game is over and the bets are settled.

One advantage a player has in the game is that, although she may choose to hit or stand according to whatever strategy she chooses, the dealer must hit or stand according to very specific and known rules. These rules vary slightly from casino to casino. We will use a common variation for this program.

Dealer Rules:

  1. The dealer must count an ace as 11 points, unless this causes the dealer to bust. Then the dealer must count the ace as 1 point.
  2. If the dealer has a total of 17 points or more, he must stand.
  3. If the dealer has a total of less than 17 points, he must hit.

Input

The input to your program will be the dealer's two cards. These will be entered as:

Card Entered as
Ace A
2 2
3 3
... ...
10 10
Jack J
Queen Q
King K

No suits will be entered. You should prompt for each card separately. We will not try to enter any other cards than those shown above.

Processing

You may use any type of conditional statement you choose. Make your statements as readable as possible, using consistent bracing, and avoiding deep nesting. You will probably find it helpful to write a couple methods to perform repeated tasks, e.g., calculating how many points a card is worth. The fewer conditionals that you have to use, the better.

Output

Your output should include the dealer's points, and whether the dealer has a blackjack, or should hit or stand according to the above rules. Note that busting is not possible with only 2 cards.

Sample Run

Blackjack Dealer
----------------

Enter my first card: J
Enter my second card: A

Dealer's hand is worth: 21
Dealer has a Blackjack!

 

Submitting
You should gather the folders for each of the three programs into a single folder called Lab4, then submit that entire folder.

Your folders should contain at least the following files:

  • Lab4
    • Calculator
      • Calculator.mcp
      • Calculator.java
      • CS99.jar
    • PlanetaryWeight
      • PlanetaryWeight.mcp
      • PlanetaryWeight.java
      • CS99.jar
    • Blackjack
      • Blackjack.mcp
      • Blackjack.java
      • CS99.jar

You will submit your lab using FTP.