CIS 160 - Repetition assignment

Objectives

  • Use Java repetition statments to implement solutions to programmming problems
  • Format output to match requirements
  • Get user input from the command line

Overview

Note: Write a Java program for each of the programming projects listed below. The source code listings for these programs, and all others in this course, must include comments at the beginning of the source code which contain your name, the course, the date, and the assignment number. Submit your .java source files in Desire2Learn when you are done. It is considered cheating to copy work. Problems may be discussed, but not copied.

Program #1

The processes of finding the largest number, smallest number, and adding up a series of numbers are used frequently in computer applications. Write a program that inputs an arbitrary series of numbers from the user. After each number the user enters display the smallest number, largest number, sum of the numbers, and average of the numbers so far. Have the program stop when the user enters the string "Quit" (whether it is lowercase or uppercase). Also display how many numbers have been entered after each number input.

Your program should have the following variables:

  • a string to hold the user input
  • a double to hold the numeric equivalent of the string the user typed in
  • an integer to keep track of how many numbers have been entered so far
  • a double to keep track of the sum
  • a double to keep track of the largest number entered so far
  • a double to keep track of the smallest number entered so far
  • a double to hold the average value of all the numbers entered

Hint: One way you can find the largest number is to assume the first number is the largest and save it in a variable named max. Then compare each number entered after that to max. If any number is greater than max, then max should be set equal to the new largest number.

Sample run for program #1

Enter a number ('quit' to exit): -3 Count: 1, Minimum: -3.0, Maximum: -3.0, Sum: -3.0, Average: -3.0 Enter a number ('quit' to exit): 3 Count: 2, Minimum: -3.0, Maximum: 3.0, Sum: 0.0, Average: 0.0 Enter a number ('quit' to exit): 2 Count: 3, Minimum: -3.0, Maximum: 3.0, Sum: 2.0, Average: 0.7 Enter a number ('quit' to exit): 14 Count: 4, Minimum: -3.0, Maximum: 14.0, Sum: 16.0, Average: 4.0 Enter a number ('quit' to exit): -9 Count: 5, Minimum: -9.0, Maximum: 14.0, Sum: 7.0, Average: 1.4 Enter a number ('quit' to exit): 10 Count: 6, Minimum: -9.0, Maximum: 14.0, Sum: 17.0, Average: 2.8 Enter a number ('quit' to exit): quit

Program #2

Write a Java application that asks the user for an integer from 2 through 15. If the number is out of range, display an error message and exit. Otherwise use a loop to display a right triangle with a height and base equal to the number the user entered. Use the "*" character to display the triangle. You should use nested loops to display the triangle. Any output statements you use for the triangle should print only a single "*" or a newline.

Sample runs for program #2

Enter an integer from 2 through 15: 1 Number out of range Enter an integer from 2 through 15: 16 Number out of range Enter an integer from 2 through 15: 10 * ** *** **** ***** ****** ******* ******** ********* ********** Enter an integer from 2 through 15: 5 * ** *** **** *****

Rubric

  • Program #1 is worth 20 points:
    • 5 points for correct operation of the input loop
    • 8 points for correctly computing the count, minimum, maximum, sum, and average
    • 4 points for having the output formatted as shown in the example
    • 3 points for proper documentation comments and for correctly following coding conventions (indentation, naming rules, etc.)
  • Program #2 is worth 20 points:
    • 5 points for correct input validation
    • 12 points for correctly displaying a triangle using nested loops
    • 3 points for proper documentation comments and for correctly following coding conventions (indentation, naming rules, etc.)