Assignment 10

Due Tuesday, May 10 at 9:00am on Catalyst


Assignment 9.3: SSN (10 pts)
  • Open a new Java class named SSN.java
  • Your program should prompt a user to enter a social security number as a series of numbers.
  • It will check whether the user entered a valid 9-digit social security number number and report an error message if the ID number entered is too short or too long (no other error checking is required).
  • It will then display the ID number in the format XX-XXX-XXXX
  • Your program should work identically to these examples (except user input will vary):

Please enter your 9 digit SSN#: 203456
Sorry! That SSN is invalid.
Please run the program again.
  • Alternately:

Please enter your 9 digit SSN#: 1034566330
Sorry! That SSN is invalid.
Please run the program again.

  • Alternately,
Please enter your 9 digit SSN#: 204568555
You entered: 204-56-8555
  • Hint: Use Strings, not ints. And, make use of the String methods we discussed in class under Lesson 6.
  • Important: You are required to use exactly one if and one else for full credit. Do not use else if for this assignment!
  • Submit SSN.java to Canvas when you are finished.



Assignment 9.4: Playing Cards (10 pts)
  • This is a challenging assignments, so get started early!
  • Open up a new Java project in Eclipse and name it Cards with a class named Cards.java
  • The goal of this program is to take in user input describing a playing card in the following shorthand notation:
A                    Ace
2....10            Card Values
J                    Jack
Q                   Queen
K                    King
D                    Diamonds
H                    Hearts
S                    Spades
C                    Clubs
  • Your program will take in as user input a shorthand notation for a single card.
  • Your program will then output the full description for that card.
  • See the sample output below:
Welcome!
Enter the Card Notation: QS
You Entered: Queen of Spades
  • Here is another sample output:
                Welcome!
Enter the Card Notation: 4H
You Entered: 4 of Hearts
  • To accomplish the goals of this program, you will need to use if statements.
  • There are two approaches to the program
    • Complete the program using 52 if statements (one for each of the possible value and suit combinations of the playing cards). For example, your 52 if statements will include the ones below:
if (input.equalsIgnoreCase("QS")) {
   System.out.println("You entered: Queen of Spades");
}
else if (input.equalsIgnoreCase("QH"))
    System.out.println("You entered: Queen of Hearts");
...
    • OR, Complete the program with fewer if statements, making use of the charAt(i) and length() functions
  • Important note: The tricky part will be to handle the 10 correctly.
  • When you are finished, upload your Cards.java program.




Assignment 10.1: Days in a Month (10 pts)
  • Develop a program that first asks the user to enter a month (January, February, and so on) and then prints the number of days in the month followed by the word "days". For February, print the phrase "28 or 29 days".
  • The user should be able to enter upper or lower case letters for the days of the month and the program should still recognize the entry month.
  • The name of the Java project for this program should be MonthDays.
  • Submit MonthDays.java to Catalyst when finished.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name.

  • Your program must operate like this, such that the same sequence of inputs produces the same output:

I will print the number of days in a month.
Enter the month (1-12): September
30 days

Alternately:

I will print the number of days in a month.
Enter the month (1-12): february
28 or 29 days


Alternately:

I will print the number of days in a month.
Enter the month: JaNuAry
31 days

Hint: Thirty days hath September

Assignment 10.2: Making a Receipt (10 pts)
  • Let's write a program to create a receipt.
  • Find your Cookies.java assignment by downloading it from Catalyst. We are going to alter it to include prices and a total.
  • Scroll down to the bottom to view the outcome of this program to give you a sense of what you need to accomplish.
  • Start by declaring 4 new double variables at the top of your program.
  • These variables will be used to store the total price of the user's order of each cookie. For example:
    • Note that you will need to declare two more double variables for the other two cookie flavors.
    • We are also going to create some new constant double variables to store the price of the cookies.
    • Create four more variables like this one - one for the price of each cookie:

        final double PRICE_CHOC_CHIP = 1.95;

    • Why are we using a const here? Will these prices ever change in the program?
    • Also, note that these variables need to be capitalized to use good programming style.
    • Finally, declare a double called total to store the total price for the entire customer purchase.
    • Below your variable declarations, you should have two System.out statements to welcome your user to the program. Your statements should produce output identical to the following:
    • Next, we need to add information about prices. Add the following System.out.println statement below the other two.
System.out.println("We sell the following flavors:");
    • Note the use of \n in the above statement. What does each one do?
    • Now, write four System.out.println statements to declare the price of each cookie. Each statement should look like the statement below:
System.out.println("Chocolate Chip Cookies -- $1.95 each");

    • When you are finished, you should have five System.out statements, which print out the following. Make sure your output is identical!
    • Next, you need to collect the user's order. 
    • If you don't already have it, add a System.out statement to prompt the user to enter how many chocolate chip cookies they would like, followed by an input statement to store the information given by the user as your chocolate chip variable. Your code should look similar to the following (your variable name may be different).
System.out.println("Please enter the number of chocolate chip cookies you would like to purchase: ");
num_choc_chip = input.nextInt();
    • If you don't already have them in your program, add three more pairs of prompt-input statements like the ones above for the other three flavors of cookie.
    • Your program's output should now look identical to the following (except for user input):


    • Next, it is time to calculate the amount the user will need to pay for each cookie. To find the subtotal for each cookie, we need to multiply the price of the cookie by the number of cookies the user wishes to order. Below your last input statement, add a statement like the following to your program (Note - your variable names may be different!).

            choc_total = num_choc_chip * PRICE_CHOC_CHIP;

    • Add three more statements, similar to the one above, one for each of the remaining cookie types.

    The rest of this program will be dedicated to printing out the receipt.
    • Add the following System.out statement:
    System.out.println("You ordered the following:\n");

    • What does \n do in the above statement?
    • Next, let's print a subtotal for each type of cookie ordered. Add a partial print statement like the one below:

    System.out.print("Chocolate Chip Cookies:\t" + num_choc_chip + " @ $1.95 each...\t");

    • Because we are printing out decimal values, we need to specify a particular level of precision for the output of the price.
    • Complete the above print out by adding a printf below it like the following.
    • statement to set the precision under which you want the output to display.

    System.out.printf("$%.2f\n", choc_total);

    • Add three similar sets of print statements for the remaining cookie flavors.
    • The output of your program should now be identical to the following:


    Welcome to Cookie Magic, where every bite of cookie is magical!
    Allow me to assist you with your order!

    We sell the following flavors:
    Chocolate Chip Cookies -- $1.95 each
    Chocolate Mint Cookies -- $2.15 each
    Peanut Butter Cookies -- $2.05 each
    Oatmeal Raisin Cookies -- $1.85 each

    Please enter the number of chocolate chip cookies you would like to purchase: 3
    Please enter the number of chocolate mint cookies you would like to purchase: 2
    Please enter the number of peanut butter cookies you would like to purchase: 5
    Please enter the number of oatmeal raisin cookies you would like to purchase: 1

    You Ordered the Following:
    Chocolate Chip Cookies: 3 @ $1.95 each... $5.85
    Chocolate Mint Cookies: 2 @ $2.15 each... $4.30
    Peanut Butter Cookies: 5 @ $2.05 each... $10.25
    Oatmeal Raisin Cookies: 1 @ $1.85 each... $1.85

    • We also need to calculate the total price to display to the user. Add the subtotal for each cookie together and assign the result to the total variable. How would you do this? Complete the statement below:
    total = choc_total + mint_total; \\You fill in the rest
    • Now, we need to print out the total price. Below your last printf statement, add the following one:


    System.out.printf("\nTotal: \t\t$%.2f\n\n", total);

    • Have we added enough \t to make the total line up with the subtotal for each cookie? The answer is no. Add more \t to the above statement until the total lines up with the subtotals.
    • Finally, let's thank the user for purchasing from us. Add a final print statement to your program.
    • This statement should print the message: Thank you for your order! Please come again!
    • The output of your program should look identical to the one below. If it is not identical, then you will need to fix it!
    • When you are finished, submit to Catalyst


    Welcome to Cookie Magic, where every bite of cookie is magical!
    Allow me to assist you with your order!

    We sell the following flavors:
    Chocolate Chip Cookies -- $1.95 each
    Chocolate Mint Cookies -- $2.15 each
    Peanut Butter Cookies -- $2.05 each
    Oatmeal Raisin Cookies -- $1.85 each

    Please enter the number of chocolate chip cookies you would like to purchase: 3
    Please enter the number of chocolate mint cookies you would like to purchase: 2
    Please enter the number of peanut butter cookies you would like to purchase: 5
    Please enter the number of oatmeal raisin cookies you would like to purchase: 1

    You Ordered the Following:
    Chocolate Chip Cookies: 3 @ $1.95 each...    $5.85
    Chocolate Mint Cookies: 2 @ $2.15 each...    $4.30
    Peanut Butter Cookies: 5 @ $2.05 each...     $10.25
    Oatmeal Raisin Cookies: 1 @ $1.85 each...    $1.85

    Total:                                       $22.25

    Thank you for your order! Please come again!