5A

Here is a list of suggested tips for a dinner in a fine restaurant. The tip is a percentage of the bill for the dinner.

Service Service quality number Percent of bill to tip
great 3 25%
good 2 20%
fair 1 10%
poor 0 5%

 

Write an application called DinnerTip. It will have a main method. Ask the user for the cost of dinner and the quality of the service. Use these prompts

System.out.print("What was the bill for your dinner? ");
System.out.print("How was the service? 0 is poor and 3 is great: ");

Get both inputs and the do the error checking.

Make sure the bill is greater than 0. If not print the error message

"The cost must be a positive number"

and terminate (return - do not use System. exit).

Check to be sure the service quality is a number between 0 and 3 inclusive. If not print the error message: (The \n keeps the output looking good in Codecheck)

System.out.println("\nThe satisfaction rating must be 0, 1, 2, or 3");

and terminate (return - do not use System. exit).

The main method terminates when the Java Virtual Machine encounters the closing brace. So there should be no executable statements between the error message and the closing brace of the method.

Otherwise calculate tip as a percentage of the bill. Do not calculate the tip in each branch. Only do the arithmetic once. Only print the tip in one place.

Then print the tip Use the format below. (You are required to use printf . Print 2 decimal places)

"The tip should be $5.26"

I recommend you copy/paste the prompts and the messages so you will get exactly what Codecheck expects.

No Javadoc this time

For the draft, ask for the cost and the quality of service and print those out on one line with a space between them like this:

32.45 2

5A draft:
5A final:

5B

Now for a bit of silliness.

According to the web comic xkcd, it is "creepy" to date someone too young. The comic uses the formula below to determine the lowest acceptable age for a date.

age / 2 + 7

web comic

So if your age is 18, the youngest person you should date is 16. Anything below 16 is "creepy"

Your date also applies the formula to his/her age to see if dating you would be "creepy." If your date is 22, then they should not date anyone younger than 18. You are 18 so you two are good, and you are off to a movie. But if your date is 26, dating anyone below 20 is "creepy" for them. You may want to rethink this date.

Write an application called DatingPool to evaluate the creepiness of a date. Since it is an application, it will have a main method.

Create and use one Scanner. Use these prompt to ask the user their age, the age of their date (both ints) and their own name (the name may be more than one word).

System.out.print("What is your age? ");
System.out.print("What is your date's age? ");
System.out.print("What is your name? ");

If either the user's age or the date's age is <=0, print this error message and end the program. Do not do anything else. Do not use System. exit():

System.out.println("Ages must be positive integers");

On one line print, the user's name and then ", you are " and then the age. On the next line, print "Your date is " and their age.

Now print the appropriate message.

If your date's age is not creepy for your age and your age is not creepy for your date's age, then print "Have a good time"

otherwise, if your date's age is creepy for your age, print "Creepy alert! Your date is too young for you"

otherwise if your age is creepy for your date's age print, "Creepy alert! You are too young for your date"

For the draft, ask for user's age, date's age, and name. Test that ages are positive integers. If not, print the error message. If so, on one line print, the user's name and then ", you are " and then the age. On the next line, print "Your date is " and their age.

No Javadoc this time.

5B draft:
5B final:

5C

We do not want users of our classes to be able to set invalid values for instance variables. Now that you know how to use if statements, you are going to add error checking to a a few classes from earlier homeworks. You can copy correct solutions from Codecheck.

You should not print error messages from constructors or methods of object. Often you would throw an exception, but we have not covered exceptions yet. But we still need to be sure the data in our objects are valid. Follow the directions below to add error checking to each class.

Use proper if structure. Do not do unnecessary tests

Monogram:

GrainElevator:

Golfer:

Note that you will need to modify constructor and both set methods.

For the draft, add the error checking for Monogram

5C draft:
5C final: