This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Having issues with homework assignment

Brandon Baldanza
Greenhorn
Posts: 13
I am trying my best to grasp these concepts in beginning Java. This is the code I have thus far and tried several things to do basic IF statements & algebra expressions with no success. I do not want someone to simply do the homework for me. Really want to understand what the next steps are. Any help would be great. I have attached the homework assignment and output of what the code should render. Thanks!

Here are the instructions with image of code executed

Week 4 Assignment

1. Create a class called IfstatementExample that has a main method.
2. Create the following variables and initialize them to 0.
a. Call the variables:
i. int number1 = 0;
ii. int number2 = 0;
iii. int number3 = 0;
iv. int intResult = 0;
v. double dblResult = 0;
vi. double dblResult2 = 0;
3. Create a scanner object called input.
4. Print the following string to the console:
a. "Enter a integer: "
5. Accept an integer from the keyboard and store it in the variable called number1.
6. Print the following string to the console:
a. "Enter an odd number greater than 10: "
7. Accept the odd number from the keyboard and store it in the variable called number2.
8. Multiply number1 by 100, divide the result by 10 and then add 5 to that result. Store the result in intResult.
9. Divide number2 by 2 and store the result in number3.
10. Print the result to the console using:
a. “dblResult contains " + dblResult
11. Divide number2 by 2.0 and store the result in dblResult2.
12. Print the result to the console using:
a. "dblResult2 contains " + dblResult2
13. Store the remainder of dividing 27 by 6 into number3.
14. Print number3 to the console using:
a. "The remainder is: " + number3
15. If intResult is less than 100 print the following message:
i. "intResult is less than 100"
b. Otherwise if intResult is not less than 100 print this message:
i. "intResult is greater than 100"
a. Enter a valid number for each input prompt.
b. Cut and paste your code into a Word document.
c. Take a snapshot of the console output (See Example Run below) and copy it to a Word document.
d. Your snapshot should look similar to Example Run shown below.
e. Upload the word document to canvas under the Week 4 Assignment.

Capture.PNG

Brandon Baldanza
Greenhorn
Posts: 13
. . . . . .

Remove much of text because it is the same as in the previous post. No need to write the same thing twice unchanged.

Campbell Ritchie
Marshal
Posts: 55677
161
Welcome to the Ranch

Those instructions are very strict, and therefore easy to implement. I suggest you go back to line 1 of the instructions, and check whether you have actually achieved that. You appear to have written int number1, but what about the = 0 bit? Oh, you appear to have changed that bit.

Start by deleting all code after that, or commenting it out by starting each line with //. Then compile the class. Then add a line like
System.out.printf("number1 = %d%n", number1);
Repeatedly reinstate code line by line, and move the print instruction and see what it happening. You will probably find that you can get halfway down your code before you find an error. And maybe you will be able to correct the errors without asking for more help. I think you will get as far as populating all your numbers from the keyboard before anything goes wrong. When things go wrong, show us what you have at that stage and we shall do our best to guide you to sort out the problem.

Never never never close a Scanner pointing to System.in or similar. Not even if Eclipse complains about resource leaks.

Carey Brown
Bartender
Posts: 2980
46
You're up to step 8. What have you tried? Where are you stuck?

Campbell Ritchie
Marshal
Posts: 55677
161
I would prefer not to assign a double to 0, which is an integer. Use 0.0 instead.

The difference between double literals and integer literals is leading me to anticipate a problem you might have with your arithmetic later.

Brandon Baldanza
Greenhorn
Posts: 13
Correct im stuck on line 8, not sure how I can write that line of code to "Multiply number1 by 100, divide the result by 10 and then add 5 to that result. Store the result in intResult." My teacher didn't really show us how to do that in class, the book im using isnt very clear either.

Ganesh Patekar
Bartender
Posts: 696
23
did you notice, you may loose precision here
Brandon Baldanza wrote:7. Accept the odd number from the keyboard and store it in the variable called number2.
9. Divide number2 by 2 and store the result in number3.
• because according to rule 7 if user enters odd number 23 stored in number2 then
• Rule no 9 says divide number2 by 2 means 23/2 results 11.5 where you are storing it in int number3 which looses precision 0.5 by implicit narrowing and stores 11 in int number3.
• It would be easier to trace what is happening if you create a separate method for this rather writing complete code in main method. Worth reading Main Is A Pain
•
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Ganesh Patekar wrote:did you notice, you may loose precision here

that may be the point of the assignment (well...one of them).

Ganesh Patekar
Bartender
Posts: 696
23
Brandon Baldanza wrote:Correct im stuck on line 8, not sure how I can write that line of code to "Multiply number1 by 100, divide the result by 10 and then add 5 to that result. Store the result in intResult." My teacher didn't really show us how to do that in class, the book im using isnt very clear either.
Please read this The Arithmetic Operators, If still get problems, you are welcome

Brandon Baldanza
Greenhorn
Posts: 13
Would this work for line 8? int intresult = number1 * 100 / number1 + 5;
Biggest problem im facing is how to store the result in the correct place.

Ganesh Patekar
Bartender
Posts: 696
23
Brandon Baldanza wrote:Would this work for line 8? int intresult = number1 * 100 / number1 + 5;
Biggest problem im facing is how to store the result in the correct place.
line 8 says
• Multiply number1 by 100, divide the result by 10 and then add 5 to that result. Store the result in intResult.
• I think mistakenly you wrote / number1 rather than /10.
• Yes this would work  int intresult = ( number1 * 100 ) / 10 + 5; but remember loss of precision. Added round brackets around  number1 * 100 just for our understanding.
•
Ganesh Patekar
Bartender
Posts: 696
23
I apologize for this
Ganesh Patekar wrote:but remember loss of precision.
there is no issue of loss of precision as result is in int.

Stefan Evans
Bartender
Posts: 1836
10
Just pointing out what appears to be an error in the instructions and my interpretation of what it should be:

9. Divide number2 by 2 and store the result in number3 dblResult.
10. Print the result to the console using:
a. “dblResult contains " + dblResult

@Brandon:  The code you have posted still fails to compile.  It shows me a problem at

Syntax error on token ":", ; expected

Brandon Baldanza
Greenhorn
Posts: 13
Here is what iv come up with.. Still having major issues. Thanks everyone for the pointers. Just dont seem to be grasping the concepts. Iv reached out to my teacher as well. Been going back through my book as well. Im getting errors on line 7, 23, 25, 27, 28.

Carey Brown
Bartender
Posts: 2980
46
One error can sometimes cause other errors, so it's best to start your debugging with the first error message. I suspect that line 7 is giving you a problem because you haven't imported Scanner.

Brandon Baldanza
Greenhorn
Posts: 13
Thanks! I got rid of a few errors. By making this change. Now I still have 23, 25, 27 & 28. Line 23 says "Duplicate local number variable number3"

Ganesh Patekar
Bartender
Posts: 696
23
• You forgot to import java.util.Scanner
• On line 24 and 28 , you declared int variables number3 and dblResult2 second time.
• On line 26 and 29, It supposed to be dblResult and dblResult2 but you wrote dbResult and dbResult2

•
Stefan Evans
Bartender
Posts: 1836
10
line 23 - Duplicate local variable number3
line 25 - dbResult cannot be resolved to a variable
line 27 - Duplicate local variable dblResult2
line 27 - Type mismatch: cannot convert from double to int
line 28 - dbResult2 cannot be resolved to a variable

All of these look valid.
All of these look (to me) to be simple to fix.

Duplicate local variable:  You have declared a variable with the same name more than once.  (line 12 and line 24).  Ensure that a variable is declared only once.
"... cannot be resolved to a variable" : compare the spelling of the declaration of the variable to its use.  (line 14 and line 26)

Ganesh Patekar
Bartender
Posts: 696
23
Campbell Ritchie wrote:Never never never close a Scanner pointing to System.in or similar. Not even if Eclipse complains about resource leaks.
Can you please tell me the reason why we should not close it, I really have no notion about it ?   Actually I also never closed that but OP made me ponder on that

Brandon Baldanza
Greenhorn
Posts: 13
Iv gotten pretty far through all the pointers you guys have gave me. All errors are gone but the program is not executing like the screenshot my instructor gave me. The "If, Then" statements seem to be the issue now.

Capture.PNG
1.PNG

Ganesh Patekar
Bartender
Posts: 696
23
• Your problem is dbResult is not having value you are expecting correct so
• First I would ask you to understand why you have dbResult variable.
• What value are you storing in dbResult? did you really assign any value to dbResult?

•
Carey Brown
Bartender
Posts: 2980
46
You want this instead. You'll have to look VERY closely to see the difference.

Carey Brown
Bartender
Posts: 2980
46
And BTW, it is generally recommended that you always use braces with IFs, WHILEs, and FORs. Example

Campbell Ritchie
Marshal
Posts: 55677
161
Ganesh Patekar wrote:. . . Actually I also never closed that but OP made me ponder on that
OP did; look at line 30.

What do you think will happen if you close that Scanner?

Ganesh Patekar
Bartender
Posts: 696
23
Campbell Ritchie wrote:OP did; look at line 30.
Yes I know OP closed that. I couldn't convey what I meant, please bear my poor English writing
Campbell Ritchie wrote:What do you think will happen if you close that Scanner?
Will be a nightmare for me, realised the reason of your fervent statement Never * 3
Java 8 SE Doc wrote:Closes this scanner.
If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked. If this scanner is already closed then invoking this method will have no effect. Attempting to perform search operations after a scanner has been closed will result in an IllegalStateException.

• So close method of InputStreamReader is invoked which closes this input stream and releases any system resources associated with the stream.
• If I close that input stream then I can't use input stream to take input from user anymore in my application.
• Example:
• Output:
Enter Name
Java
Name: Java
Enter Surname:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at timepass.ScannerCloseDemo.getInput(ScannerCloseDemo.java:30)
at timepass.ScannerCloseDemo.main(ScannerCloseDemo.java:36)

Please correct me, If I'm wrong somewhere.

Campbell Ritchie
Marshal
Posts: 55677
161
Well done trying it out There is nothing to correct. You have closed System.in and you can never open it again. Any attempt to use it will produce an Exception. I don't know whether Scanner uses an input stream reader or a buffered reader behind the scenes. Whichever method you use to access System.in in future, you will simply get a different Exception.

Ganesh Patekar
Bartender
Posts: 696
23