Assignment 13
Due Tuesday, February 26 at 11:20am on Canvas


Assignment 13.1: Multiplication Tables (10 pts)

  • One of the principle applications of nested loops is to display tabular data (data organized into rows and columns).
  • For this assignment, let's practice the nested loop concept we discussed last class by creating a program to display the times tables.
  • Name your Java project Multiplication
  • First, the user should see a message like this one appear on the console:
***Time to Learn the Times Table!***

Enter the size of the times table: _
  • Next, use a nested for loop to display the times table for the number entered from 1 to 12 (see example below)
  • To help you get started, you can fill in the missing parts of the code here:

for (int row = ????; row <= 12; row++) { //printing times table 1 through 12
    for (int col = ????; col <= ????; col++) { //for the numbers up to size
        System.out.print( col + " * " + row + "\t"); //add more to this print statement
    }

    System.out.println();

}

  • Now, as an intermediate step, run your code and verify that you get the following output:

***Time to Learn The Times Table!***

Enter the size of the times table: 5
1 * 1     2 * 1     3 * 1     4 * 1     5 * 1   
1 * 2     2 * 2     3 * 2     4 * 2     5 * 2   
1 * 3     2 * 3     3 * 3     4 * 3     5 * 3   
1 * 4     2 * 4     3 * 4     4 * 4     5 * 4   
1 * 5     2 * 5     3 * 5     4 * 5     5 * 5   
1 * 6     2 * 6     3 * 6     4 * 6     5 * 6   
1 * 7     2 * 7     3 * 7     4 * 7     5 * 7   
1 * 8     2 * 8     3 * 8     4 * 8     5 * 8   
1 * 9     2 * 9     3 * 9     4 * 9     5 * 9   
1 * 10    2 * 10    3 * 10    4 * 10    5 * 10   
1 * 11    2 * 11    3 * 11    4 * 11    5 * 11   
1 * 12    2 * 12    3 * 12    4 * 12    5 * 12       

  • Finally, let's alter the print statement inside the for loop so that we print out the products, getting an output like the following:
***Time to Learn The Times Table!***

Enter the size of the times table: 5
1 * 1 = 1      2 * 1 = 2      3 * 1 = 3      4 * 1 = 4      5 * 1 = 5   
1 * 2 = 2      2 * 2 = 4      3 * 2 = 6      4 * 2 = 8      5 * 2 = 10   
1 * 3 = 3      2 * 3 = 6      3 * 3 = 9      4 * 3 = 12     5 * 3 = 15   
1 * 4 = 4      2 * 4 = 8      3 * 4 = 12     4 * 4 = 16     5 * 4 = 20   
1 * 5 = 5      2 * 5 = 10     3 * 5 = 15     4 * 5 = 20     5 * 5 = 25   
1 * 6 = 6      2 * 6 = 12     3 * 6 = 18     4 * 6 = 24     5 * 6 = 30   
1 * 7 = 7      2 * 7 = 14     3 * 7 = 21     4 * 7 = 28     5 * 7 = 35   
1 * 8 = 8      2 * 8 = 16     3 * 8 = 24     4 * 8 = 32     5 * 8 = 40   
1 * 9 = 9      2 * 9 = 18     3 * 9 = 27     4 * 9 = 36     5 * 9 = 45   
1 * 10 = 10    2 * 10 = 20    3 * 10 = 30    4 * 10 = 40    5 * 10 = 50   
1 * 11 = 11    2 * 11 = 22    3 * 11 = 33    4 * 11 = 44    5 * 11 = 55   
1 * 12 = 12    2 * 12 = 24    3 * 12 = 36    4 * 12 = 48    5 * 12 = 60  
  • Note: Part of the output may get cut off if the user enters too large of a number or the numbers might not line up perfectly. Both are okay.
  • When you are finished, submit your assignment to Canvas.


Assignment 13.2: Healthy Nuts (10 pts)

Health Nut

  • Encoding and decoding information can save memory space and speed transmission of data.
  • Businesses often encode orders to reduce writing and speed up customer orders. In this project we will look at decoding orders for a nut company.
  • Nuts are a healthy snack. The Global Burden of Disease Study [1] calculated that eating more nuts could save 2.5 million lives each year.
  • From another perspective, your life span may be increased by two years by eating nuts regularly [2].
  • Write a program (Nuts.java) that prompts the user for a single input where the first one or two characters is a nut in the following shorthand code and the last characters are the number of ounces for the nut type.
image source

Code Nut Variety
AAlmonds
BN    Brazil Nuts
C    
Cashews
HN
Hazelnuts
MMacadamias
PPeanuts
PE
Pecans
PN
Pine Nuts
PI
Pistachios
W
Walnuts

  • For example, the code for twelve ounces of Brazil Nuts is BN12 and seven ounces of Cashews is C7.
  • Notice there are no spaces between the code and quantity.
  • The quantity can be any amount greater than or equal to zero.Ask the user for the following inputs (and no other input) in this order, as shown in the example output below:
  • A single order code like: BN12 (no spaces allowed!!)
  • A 'y' or 'n' (without the quotes) for the repeat loop
  • Assume the user enters all the codes with UPPERCASE letters as shown.
  • Store the order code in a single String variable.
  • Then use substring() to analyze and process the input String to print a full description of the nut order.
  • Add a while statement that allows the user to repeat the program by entering a 'y' (without the quotes)
  • If the user enters an invalid code, display the message, "We don't have that nut." as shown in the example output.
  • Submit Nuts.java to Canvas when you are finished.

The output of your program should look identical to the sample output below except user input may vary.:

Welcome to the Health Nut Company!

Enter the nut order code: BN12
12 oz. Brazil Nuts

Another item? (y/n) y

Enter the nut order code: C7
7 oz. Cashews

Another item? (y/n) Y

Enter the nut order code: XY123
We don't have that nut.

Another item? (y/n) n

We will go nuts to get you this order!

Assignment 13.3: Vowels (10 pts)
aeiou

  • Write a program that takes in a sentence from the user and then counts the number of vowels in the sentence.
  • The user should be able to enter as many sentences as desired.
  • You will need to use String.charAt(), String.length(), and two loops - a while loop and a for loop to solve this problem correctly.
  • Open up a new Java class file called Vowels.java
  • First, welcome the user to the program and explain its purpose:
Welcome! Give me a sentence and I will count its vowels.
  • Then prompt the user to enter a sentence.
Please enter a sentence or q to quit: |
  • Store the user input as a string variable.
  • Now, using a for loop and the String.length() method, count how many vowels (a, e, i, o, u) there are in the sentence.
  • Note that the program should also count capital vowels (A, E, I, O, U).
  • Finally, output the number of vowels like so:
There are 5 vowels in your sentence.
  • Next, place the prompt and for loop inside of a while loop so that the user can enter multiple sentences.
  • If the user enters q, the loop should end and the program should print out a message "Goodbye!"
  • Once your program is working identically to the examples below, submit it to Canvas.

The output of your program should look identical to the sample output below except user input may vary.

Welcome! Give me a sentence and I will count its vowels.

Please enter a sentence or q to quit: Abracadabra, Brah!
There are 6 vowels in your sentence.

Please enter a sentence or q to quit: A dame who knows the ropes isn't likely to get tied up.
There are 16 vowels in your sentence.

Please enter a sentence or q to quit: Animals are friends not food.
There are 10 vowels in your sentence.

Please enter a sentence or q to quit: q

Goodbye!