• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
  • Frits Walraven
  • fred rosenberger
  • salvin francis

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

Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Posts: 1558
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Posts: 73243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic