Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The Art & Science of Java Chapter 4 Exercise 12 FindLargest.java

 
M Ford
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, Im a little stuck with this one. Im doing this Java programming using the book The Art & Science of Java by Eric Roberts.

Basically what this program should achieve is that the user enters in a list of integers, one line at a time, and keep going until the SENTINEL(in this case 0) is called. When the SENTINEL is called, the program "SHOULD" takes a look back at all the integers entered and displays the LARGEST of them all.

I think if you see below, I shouldn't think I'm that far off but can anyone help me out and point out what exactly that I am missing from what you can see below:









At the moment, what mine displays is the last integer entered instead of the Largest integer in the list.

Any input is much appreciated.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi M Ford,

Welcome to CodeRanch!

Please UseCodeTags.

Secondly, please post complete code. What is inside acm.program package? What does ConsoleProgram class do? Why FindLargest has to extend ConsoleProgram?

Now, coming to the point, before you do anything, I would advice to take pencil and paper, and write the complete flow step by step. Please do not start coding before you understand what you want to do.

Currently, your program displays last number which is not equal to SENTINEL (by the way, why SENTINEL is not final?) entered because you are storing last number entered in findLargest variable (again, this name sounds like a method name, not a variable name)

Hint : to find largest number, at some point of time, you need to compare two numbers, right? Your code is not doing it at all.

I hope this helps.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

You have made the mistake of trying to write code before you worked out what it does. You should go through the specification and write down on paper what you intend to do. You will need a pencil and eraser; the latter is very important
Don’t write any code until you have completed that process.
Also check what it says about a list. If that means a java.util.List<Integer> that is what you will have to use. You will find it easier to use when you know about boxing conversions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic