L06

Due by the end of Tuesday, October 17


SUBMIT / CHECK

Today's Activities

This week we are reviewing the material we've covered over the first part of the term.

Activity #1
Create a program call Silly. Have it:
  1. Print out the message This is just a silly program.
  2. Print out your name and A-number, identified as the author.
  3. Read a positive integer from the user (with a suitable prompt).
  4. Echo back that integer value.
This is just a silly program. By Mark Young (A00000000) Enter a positive integer: 10 10
Activity #2
Revise the program so that it keeps reading positive integers until the user enters a number less than or equal to zero. Use the sentinel loop idiom described in class.
This is just a silly program. By Mark Young (A00000000) Enter a positive integer: 10 10 Enter another positive integer: 15 15 Enter another positive integer: 3 3 Enter another positive integer: 0
Activity #3
Revise the program again so that instead of just printing out the number, it says whether the number is even or odd. (Hint: The number is even if its remainder when divided by two is zero; otherwise it is odd.)
This is just a silly program. By Mark Young (A00000000) Enter a positive integer: 10 10 is even Enter another positive integer: 15 15 is odd Enter another positive integer: 3 3 is odd Enter another positive integer: 0
Activity #4
Revise the program again. For even numbers, have the program print the numbers from 1 to half of the number entered.
This is just a silly program. By Mark Young (A00000000) Enter a positive integer: 10 10 is even 1 2 3 4 5 Enter another positive integer: 15 15 is odd Enter another positive integer: 3 3 is odd Enter another positive integer: 0
Activity #5
Revise the program one more time. For odd numbers, have the program call a method named printTriangle. The method says that it will print a triangle of the given height.
This is just a silly program. By Mark Young (A00000000) Enter a positive integer: 10 10 is even 1 2 3 4 5 Enter another positive integer: 15 15 is odd I will print a triangle of height 15 Enter another positive integer: 3 3 is odd I will print a triangle of height 3 Enter another positive integer: 0
Activity #6
Last revision! Have the method printTriangle actually print the triangle of the given height.
This is just a silly program. By Mark Young (A00000000) Enter a positive integer: 10 10 is even 1 2 3 4 5 Enter another positive integer: 15 15 is odd I will print a triangle of height 15 * ** *** **** ***** ****** ******* ******** ********* ********** *********** ************ ************* ************** *************** Enter another positive integer: 3 3 is odd I will print a triangle of height 3 * ** *** Enter another positive integer: 0
Hint: The way to print a triangle is very similar to the way to print a rectangle (see the program DrawRectangle). There is only one difference: the number of stars on each line is not equal to the width of the rectangle; it is equal to the number of the line (one star on line 1, two stars on line 2, and so on). So how does the code from DrawRectangle need to be changed?

Submit this/these files:

You will be graded on the following:

  1. Uses the read+clear idiom given in class for reading each number (that is, it "tidies the input stream")
  2. Reads numbers up to and not after the first non-positive one ...
  3. ... using the sentinel loop idiom ("read, test, process, read") for reading up to a non-positive number
  4. Uses the appropriate operator for checking the number's remainder
  5. Uses the correct branch control for choosing between even and odd numbers ...
  6. ... does not duplicate that control
  7. Prints out whether the number is even or odd
  8. For even numbers prints out the numbers from 1 to half that number ...
  9. ... using the correct loop idiom for printing the count
  10. For odd numbers calls a method
  11. ... which takes the required height as an argument
  12. The method prints out the required triangle ...
  13. ... using nested loops of the correct idiom
  14. All code meets the style guidelines for the course -- indentation, spacing, names and javadocs, to name but a few

SUBMIT   /   CHECK