|
Fundamental Programming Concepts
Summer 2000 |
|
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:
|
|||||||||||||||||||
Preliminaries | |||||||||||||||||||
Practice programs using ifYour 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.
New String OperationsYou 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 equalityYou 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 stringsSince 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 updateThe 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:
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:
InputThe input to your program will be the dealer's two cards. These will be entered as:
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. ProcessingYou 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. OutputYour 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
|
|||||||||||||||||||
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:
You will submit your lab using FTP.
|