Assignment 11

Due Tuesday, February 19 at 11:20am on Canvas


Assignment 11.1: What's Your Sign? (10 pts)


Venice ast sm.jpg

  • For this assignment, let's write an astrology program.
  • Welcome the user (see sample output below)
  • Prompt the user to enter a numerical value (1-12) for the month of his/her birth (see sample output)
  • Prompt the user to enter a numerical value (1-31) for the day of his/her birth (see sample output)
  • Using the chart below for the astrological signs, determine the user's sign based on his or her input.
  • Expect to need to use if, else if and else statements.
  • Expect to use logical operators (&&, ||).
  • Submit your source code in a file called Sign.java to Canvas when finished    

Aries             March 21-April 19
Taurus            April 20-May 20
Gemini            May 21 - June 21
Cancer            June 22 - July 22
Leo               July 23 - August 22
Virgo             August 23 - September 22
Libra             September 23 - October 22
Scorpio           October 23 - November 21
Sagittarius       November 22 - December 21
Capricorn         December 22 - January 19
Aquarius          January 20 - February 18
Pisces            February 19 - March 20


Your sample output must be identical to mine:

What's your sign?
Please enter the month of your birth (1-12): 3
Please enter the day of your birth (1-31): 3
Your Sign is Pisces.


Alternately,

What's your sign?
Please enter the month of your birth (1-12): 4
Please enter the day of your birth (1-31): 12
Your Sign is Aires.


Alternately,

What's your sign?
Please enter the month of your birth (1-12): 100
Please enter the day of your birth (1-31): -2
Invalid Entry. Please run the program to try again.


Assignment 11.2: Prices (10pts)

  • Copy the following program into a file named Price.java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    
    public static void main(String[] args) {
        String name;
        double price = 0;
      Scanner input = new Scanner(System.in);

    System.out.print("Enter the product name: "); name = input.nextLine();
     
    System.out.print("Price of the " + name + ": "); price = input.nextDouble(); // Insert new statements here System.out.println("Total price: $" + price); }
  • Alter the block comments to include your name and section information to the top of the program.
  • Compile and run the starter program to make sure you entered it correctly.

    When you run the program, the output should look like this:

    Enter the product name: iPod Nano
    Price of the iPod Nano: 149.50
    Total price: $149.5
    

    Note the format of the numbers output for the total price. We will address this formatting issue later in the exercise.

  • Run the program again for a product with a very high cost, like a Boeing 777:
    Enter the product name: Boeing 777
    Price of the Boeing 777: 212345678
    Total price: $2.12345678E8
    

    Note the format of the numbers output for the total price. This format is called exponential notation. You may have encountered it in some of your math classes.

  • Let us correct the formatting of the total price. Alter the System.out statement that prints the total price:
    System.out.printf("Total price: $%.2f\n", price);
    
    Compile and run your program again and verify the output looks like:
    Enter the product name: Boeing 777
    Price of the Boeing 777: 212345678
    Total price: $212345678.00
    
  • Let us add a final variable that we will use later in our program. Enter the following code after the printf statement and before the statement that prints the total price:
    final int percent = 100;
    

    We are declaring this variable rather than using a magic number.

  • Now we will add sales tax to the price of the product. Enter the following code after the percent variable and before the statement that prints the total price:
    double taxRate = 0;
    System.out.print("Enter sales tax rate (%): ");
    taxRate = input.nextDouble();
    double tax = price * taxRate / percent;
    price += tax;
    

    Notice the last statement: price += tax;. This is an alternate way to code the statement: price = price + tax;.

  • Compile and run your modified program and verify the output looks like:
    Enter the product name: iPod nano
    Price of the iPod nano: 89.50
    Enter sales tax rate (%): 9.5
    Total price: $98.00
    
  • Now we will find the whole dollars and cents of the amount to demonstrate casting. Enter the following code after the statement that prints the total price and before the closing curly brace of main:
    int dollars = (int) price;
    System.out.println("In whole dollars: $" + dollars);
    

    Notice the (int) in the first statement. This is known as a type cast or just cast

  • Compile and run your modified program and verify the output looks like. Then, submit Prices.java to Canvas.
    Enter the product name: iPod nano
    Price of the iPod nano: 89.50
    Enter sales tax rate (%): 9.5
    Total price: $98.00
    In whole dollars: $98
    




Assignment 11.3: Humpty Dumpty (10 pts)
Humpty Dumpty
Create a new Java file called HumptyDumpty.java.

Write a program that uses a while loop to print the numbers from 1 to 150, with each number printed on its own line.
For multiples of 4, print "Humpty Dumpty" instead of the number
For the multiples of 5, print "Sat on a wall" instead of the number.
For numbers which are multiples of both 4 and 5, print "Had a great fall"
You must use a while loop for full credit.
Hint: Test for numbers that are multiples of both 4 and 5 first.
When you are finished, and your output looks like mine, upload your assignment to Canvas.

Note: This assignment inspiration comes from the following English nursery rhyme:

Humpty Dumpty sat on a wall,
Humpty Dumpty had a great fall;
All the King's horses
And all the King's men,
Couldn't put Humpty together again.

Your Output Should Look Like the Following. Note that some values have been omitted for brevity.

1
2
3
Humpty Dumpty
Sat on a wall
6
7

Humpty Dumpty
9
Sat on a wall
11
Humpty Dumpty
13
14
Sat on a wall
Humpty Dumpty
17
18
19
Had a great fall
...

Had a great fall
141
142
143
Humpty Dumpty
Sat on a wall
146
147
Humpty Dumpty
149
Sat on a wall