CIS 160 - Methods and menus

Objectives

  • Validate user input
  • Create and use methods
  • Use selection to implement a menu
  • Use repetition to continue until the user chooses to exit

Program requirements

Your task is to create a program which presents a menu of various shapes. When a particular shape is selected, the program should ask the user for the appropriate input and calculate the area of the given shape. You are required to match the output of the sample program exactly. Once the user is given the results of their area calculation, then the menu should be presented again until the user chooses to exit.

Although not required, it would make the assignment easier if you created a method to get an integer that displayed a prompt and did user input validation. The method would ideally take a prompt, a minimum value, and a maximum value. It would display the prompt and then get user input. It would display an error message and repeat the process if the input was invalid. Once valid input was entered it would return that value.

Pseudocode

Here is a skeleton of the program in something similar to pseudocode:

displayMenu(): display the menu return calcRectangle(): ask the user for a length and width, both constrained to be between 0 and 300 return the area [length * width] calcTriangle(): ask the user for a height and base, both constrained to be between 0 and 200 return the area [0.5 * base * height] calcCircle(): ask the user for a radius, constrained to be between 0 and 250 return the area [PI * radius * radius] Note: Java provides PI as Math.PI double calcHexagon(); ask the user for the length of a side, constrained to be between 0 and 200 return the area (2.598075 * side * side) main: repeat display menu choice = get user's choice process choice using a switch statement to call the appropriate method and display the returned answer until user chooses 'X'

Sample run

Area Calculator Menu: (R)ectangle (C)ircle (H)exagon (T)riangle E(x)it Enter choice: s Error: Invalid choice Enter choice: r Enter the rectangle's height (0-300): 2 Enter the rectangle's width (0-300): 350 Error: value above maximum allowed Enter the rectangle's width (0-300): 20 The area is 40.00 Area Calculator Menu: (R)ectangle (C)ircle (H)exagon (T)riangle E(x)it Enter choice: C Enter the circle's radius (0-250): -4 Error: value below minimum allowed Enter the circle's radius (0-250): 10 The area is 314.16 Area Calculator Menu: (R)ectangle (C)ircle (H)exagon (T)riangle E(x)it Enter choice: f Error: Invalid choice Enter choice: h Enter the length of a hexagon side segment (0-200): 20 The area is 1039.23 Area Calculator Menu: (R)ectangle (C)ircle (H)exagon (T)riangle E(x)it Enter choice: 3 Error: Invalid choice Enter choice: T Enter the triangle's height (0-200): 20 Enter the triangle's base length (0-200): 10 The area is 100.00 Area Calculator Menu: (R)ectangle (C)ircle (H)exagon (T)riangle E(x)it Enter choice: x

Rubric

  • 5 points for following coding standards (indentation, naming, documentation, etc.)
  • 5 points for having the displayMenu function working properly
  • 5 points for having the calcRectangle function working properly
  • 5 points for having the calcTriangle function working properly
  • 5 points for having the calcCircle function working properly
  • 5 points for having the calcHexagon function working properly
  • 5 points for having the menu system working properly (loops until user quits, calls correct functions, displays results)
  • 5 points for having the user input validation working properly