Java Course 9: Test Yourself

Write programs to do the following

  1. Prompt the user to enter the length and width of a square. Output the area of the square.
  2. Prompt the user to enter a test score. Output their grade: Under 100 – Fail, Under 120 Pass, Under 130 Merit, 130+ Distinction
  3. Use a for loop to print out the following sequence: 12, 15, 18, 21, 24, 27
  4. Use a for loop to print out the first 8 square numbers: 1,4,9,16,25,36,49,64
  5. Make a counting program. Use a do while loop to keep asking “Another?” until the user enters “n”. Output the number of times the loop repeated
  6. Create a file called names.txt containing the names John, Paul, George and Ringo. Write a program the reads in the text file and prints out the data
  7. Create a string array containing the four seasons: spring, summer, autumn and winter. Print out a random season

Extension

Write a program that prints out the verses of the song the Twelve Days of Christmas. You will get bonus marks for elegant coding.

On the first day of Christmas
my true love sent to me:
A Partridge in a Pear Tree

On the second day of Christmas
my true love sent to me:
2 Turtle Doves
and a Partridge in a Pear Tree

On the third day of Christmas
my true love sent to me:
3 French Hens
2 Turtle Doves
and a Partridge in a Pear Tree

.
.
.

On the first day of Christmas
my true love sent to me:
12 Drummers Drumming
11 Pipers Piping
10 Lords a Leaping
9 Ladies Dancing
8 Maids a Milking
7 Swans a Swimming
6 Geese a Laying
5 Golden Rings
4 Calling Birds
3 French Hens
2 Turtle Doves
and a Partridge in a Pear Tree

Leave a Comment