Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes The Art & Science of Java Chapter 4 Exercise 12 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The Art & Science of Java Chapter 4 Exercise 12" Watch "The Art & Science of Java Chapter 4 Exercise 12" New topic

The Art & Science of Java Chapter 4 Exercise 12

M Ford

Joined: Mar 06, 2012
Posts: 10
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

Joined: Dec 08, 2010
Posts: 1545

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.

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
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.
I agree. Here's the link:
subject: The Art & Science of Java Chapter 4 Exercise 12
It's not a secret anymore!