5A

Write an application called IfPractice. It will have a main method. There is no starter coder.

In the main method: do the following:

Hint: To get just the first name, you need the index of the space. You can get that with the String method indexOf. indexOf returns the index of the first occurrence of the substring or -1 if the substring is not found.

Use this code to get the index of the space

int indexOfSpace = name.indexOf(" ");

If the indexOfSpace is > -1, use substring to get all characters in the first name. Otherwise use the whole name entered as the first name

Notes:

For the draft, do the first 2 steps.

5A draft:
5A final: (link updated Sept 19)

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

You should not print error messages from constructors or methods of 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

Window (hw4)

GroceryBill (hw3)

Hal9000 (hw3)

If you do not have a correct version of one of these classes, you can get it from the posted solutions.

For the draft, make the changes to Window

5B draft:
5B final:

5C

Write a class called TwoNumbers which will ask the user for two numbers 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 numbers (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 whitespaces and then read up to the next whitespace (the space), leaving that whitespace in the input stream. The second call to nextDouble will ignore leading whitespaces and then read up to the next whitespace (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
first is equal to second The first number is equal to the second
first is greater than second The first number is greater than second
first is less than second The first number is less than second
the numbers are within .0001 of each other The numbers are close together
the numbers are more that 10000 apart The numbers are far apart
they have the same sign/different sign The numbers have the same/different sign

If the numbers are equal only print the one message. Otherwise print all that apply. 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 structure and do not just use all ifs.

Note for advanced students: Do not use System.exit()

For the draft, get the numbers from the user and print the appropriate one of the first 3 comments.

5C draft:
5C final: