CS110 Programming Assignment 5 Working with Methods

Instructor: Trish Cornez






Brief Answers The Math Java library contains a set of useful methods, such as Math.sqrt(), and Math.random().
Define the following Math methods and provide an example statement of each. NOTE: Math.ceil() is useful for Program 2.
  1. abs()
  2. tan()
  3. ceil()
  4. floor()
  5. fabs()




PROGRAM 1
Write a program that simulates the Monty Hall Game.
Don't forget to validate user input and use methods, in addition to main() to perform major tasks.

The Monty Hall Game is named after a host of a television game show from the 70's in which a contestant could win a new car by guessing the right door that the car was behind. It became a popular puzzle because the best playing strategy in counter-intuitive.

The game is played in the following manner:


Guidelines

  1. In this game you will need to generate a random number representing the door that has the car behind it. Your program must be able to generate a random integer between 1 and 3.
  2. Allow your user to change their mind by asking them a yes or no style question. Respond to 'y' as a yes and all other answers as no.




PROGRAM 2
  Print the source code of this program plus the output of the following four executions:
  • Execution 1: Input the weight as a negative number of ounces.
  • Execution 2: Input the weight as 0ounces.
  • Execution 3: Input the weight as 5 ounces.
  • Execution 4: Input the weight as 42 ounces.




Write three methods for this program assignment as follows:

Speedy-Pants Delivery Inc. charges by weight for delivery of packages. The delivery charge for the first pound is $3.00 and $0.50 is added onto the charge for each additional four ounces.
For example,
A package weighing more than 16 but at most 20 ounces costs $3.50 to deliver.
A package weighing more than 20 but at most 24 ounces costs $4.00 to deliver. etc.
Write a program that inputs the weight of a package in ounces, computes the charge for delivery, and displays it to the screen. NOTE: One pound equals 16 ounces.

Note:
(1) Use a double-valued method, getWeightInput, to prompt the user for input.
(2) Use a double-valued method, computeDeliveryCharge, to compute the charge for delivery.
(3) Use a void method, displayDeliveryCharge, to display the delivery charge.