CS46A Fall 18

5A

Write a class called AnalyzingNumbers which will ask the user for two doubles and print out comments based on their values. (no starter file. You will write all the code)

Use this exact prompt to get input:

System.out.print("Enter two doubles (like this: 41.7 -22.5): ");

The user will enter both numbers on the same line at the same time. This is possible because the first call to nextDouble will skip any leading white spaces and then read up to the next white space (the space), leaving that space in the input stream. The second call to nextDouble will ignore leading white spaces and then read up to the next white space (the newline) leaving the newline in the input stream. This is a handy way to request multiple inputs.

Here are the conditions and the comments to print.

Condition Comment to print
the second number is greater than 100 The second number is greater than 100 (if it is not, you do not print anything)
the first number is equal to an integer The first number is an integer / The first number is an not an integer (25.0 would be an integer 25.3 would not) You will need to cast
first is equal to second The numbers are equal
first is greater than second The first number is larger
second number is greater than the first The second number is larger
the numbers have the same sign The numbers have the same sign / The numbers have different signs
   

Copy and paste the prompt and the replies. That way you will have exactly what Codecheck expects. Print the comments in the order given above

Use the appropriate if structures. Do not use unnecessary tests or have do-nothing if or else statements.

Note for advanced students: Do not use System.exit().Codecheck will not be happy.

For the draft, do the first step. And then print System.out.println("All done"); (Only print All done in the draft)

5A draft:
5A final:

5B

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 few classes from earlier homeworks. You can copy correct solutions from my solutions if you need to.

You should not print error messages from constructors or methods of an object. In the real world, you might throw an exception when the parameter is invalid, but we have not covered exceptions yet. We will just 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 . Do not have empty if or else blocks

SnowGlobe (hw4)

Person (hw3)

ChocolateKissesStash (hw 3)

For the draft, just make the changes to SnowGlobe

For the final, you will submit two classes: Person.java and ChocolateKissesStash.java. Note that there are 2 testers this time.

5B draft:
5B final:

5C

Complete the class RestaurantHelper to help restaurant customers with the bill.

It has the following constructor:

public RestaurantHelper(double mealCost, int serviceQuality)where serviceQuality is an int 0 to 3 which represents the quality of service. 0 is poor service, 1 is fair, 2 is good, and 3 is excellent service. Define and use constants for the quality of service like this:

The constructor will initialize the instance variables.

 

Provide methods:

Below is a table of the tip for different quality of service levels. The tip is a percentage of the bill.

Quality of service Quality of service rating Percent of fare to tip
excellent 3 25%
good 2 20%
fair 1 10 %
poor 0 5%

Define and use constants for the percentages like this

Provide Javadoc.

For the draft, define the constants and the constructor

5C draft:
5C final: