5A
Write an application called IfPractice. It will have a main method. There is no starter coder.
In the main method: do the following:
System.out.print("Enter your first and last name: ");| First Name | Nickname |
|---|---|
| James or james | Jim |
| Robert or robert | Bobby |
| Ashwani or ashwani | Ash |
| Any other name | Buddy |
System.out.print("How old are you? ");| This age and over | Under | Message |
|---|---|---|
| 0 | 18 | You are a child |
| 18 | 21 | You can vote, but you can not drink |
| 21 | 65 | You are a full adult |
| 65 | You can get Social Security |
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:
if (age >= 18 && age < 21)age >= 18. It HAS to be true because you have just determined that age < 18 is false.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)
setDimensions, only change the width and the height if both are > 0. If either is 0, don't make any changes.GroceryBill (hw3)
add method: only add the amount if it is greater than 0remove method: only remove the amount if it is greater than 0 and also less than or equal to the subtotal 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
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.