• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to return the largest and smallest integers entered by a user?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm just learning Java, so try not to get too technical. I'm wondering how I can make my application, so that when a user enters a integer, the application will return the smallest and largest numbers in addition to sum, average, and product. Here is my code.

 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks very good to me. I'm not sure what help you're looking for.
 
Dustin Boor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After it displays "The sum is.., the average is.... the product is..." I want it to display "The largest number is..., the smallest number is..." Does that make sense?
 
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Add this block of program.

if(a>b) {
if(a>c) { large=a; small = c; }
else { large=c; small=a;}
}
else {
if(b>c) {large = b; small = c;}
else {large =c; small=b;}
}
System.out.println(large+ "   "+ small);
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use these methods to find out min and max among them Math.max and Math.min
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Durgarao
Welcome at the ranch!
If b >= a (and c), what can we say about the smaller of a and c?

@Dustin
Wecome too!
do you notice anything particular with the average?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Boor wrote:I'm just learning Java, so try not to get too technical. I'm wondering how I can make my application, so that when a user enters a integer, the application will return the smallest and largest numbers in addition to sum, average, and product.


OK, well first off, what you've described is not one problem; it's many.

Specifically, it involves:
1. Getting the user to enter one integer.
2. Repeating #1 until some "end-point" - in your case after you've received 3 numbers.
3. Storing the integers they've entered somewhere.
4. Calculating (or keeping track of) the number of integers they've entered.
5. Calculating (or keeping track of) the total of all integers they've entered.
6. Calculating (or keeping track of) the product of all integers they've entered.
7. Getting the smallest number from the ones you've stored.
8. Getting the largest number from the ones you've stored.
8. Calculating the average.
9. Displaying the results.

And you're trying to do all that in one lump of code - main().

Now what you've got is fine, and clearly follows your thought process, but another way of doing it is to treat each task separately and build them up gradually as separate methods. This is the way programmers work in the real world: They break big problems down into smaller ones, and tackle each one individually.

Here's another way of looking at your program. I've tried to keep to the same names as you used:Do you see how much neater your main() method looks now?
That's because it's not cluttered up with implementation ("how to") code.

Do you also see that I've been able to add display statements for "smallest" and "largest", even though I haven't actually implemented the methods yet?

That was on purpose, by the way - it's still up to you to work out how to do them.

HIH

Winston
 
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Winston wrote, there are many steps, but in my opinion problems 4 to 8 are one single problem (with just a bit different characteristic), my approach would be something like:
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Durgarao Adari wrote:
Add this block of program.

if(a>b) {
if(a>c) { large=a; small = c; }
else { large=c; small=a;}
}
else {
if(b>c) {large = b; small = c;}
else {large =c; small=b;}
}
System.out.println(large+ "   "+ small);



@Durgarao, Welcome to the Ranch!

Here at the Ranch, we discourage just handing solutions to people. For one, the Ranch is not a code mill.. We give hints and suggestions for improvement but for the most part, leave it to people to figure out their own solutions to their problems. There are other places you can go if you just want to quickly cut and paste someone else's code. Also, your proposed solution is incorrect, which could cause more confusion for the OP.
 
Durgarao Adari
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Junilu Lacar

Thanks for the guidelines. This was my first post (answer) and did not go through the sticky notes and forum rules. Agree that better response would be to provide clues rather direct solutions.

But your post itself is kind of rude being over 7500 posts experience.

Firstly it was too early to ask me to go to other forums me being just one post.

Secondly if you have gone through the all the replies someone (already) pointed that my solution has flaw. You dont have to redo it again. It is duplicating of a post.

Thirdly the solution has flaw but is good enough for the original poster to test the code and make required changes in this case for the smallest number. And solutions or proposed solutions from every member can not stand as perfect solutions. Some other member could have even better solution etc. That is what is forums are for!!!

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Boor wrote:Here is my code.


I have to agree with Carey that for a self-professed beginner, you have written some pretty good code. It actually makes me think that you either have the right instincts as a programmer or you already have previous programming experience in another language.

The one thing I can point out is that your comments are a bit too gratuitous and redundant. If you must try to explain your code, the best comments explain WHY something is being done, not what is happening or how it's done. The code be clear enough to show what and how. I have replaced your comments with ones that are a little bit more reasonable, if you must have comments in your code to get a grade.


When you have comments like that, it can naturally lead to refactoring along the lines of what Winston suggested:

The method names make seem strange but they make the high-level code that calls them read more naturally like plain old English statements rather than dry, robotic computerese, IMO.  You can extract that out even more by doing this:

And finally, you can remove the comments that are obviously and (should now be) painfully redundant:

I have left that first comment in there to let you decide whether or not you still want to comment that the main method is the entry point of the program given the code that's left in there.

As far as your original question, checking 3 numbers for largest and smallest can be done at least a couple of ways: using min() and max() as suggested before or using a series of if-else statements. Generalizing that logic to allow for more numbers is easier if you use a structure like an array to hold the numbers you are evaluating.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Durgarao Adari wrote:But your post itself is kind of rude being over 7500 posts experience.


I apologize if my response came off that way but I had no intention on being rude to you.

Firstly it was too early to ask me to go to other forums me being just one post.


I should have phrased it differently, I did not mean that you personally should go somewhere else. What I meant and should have written was "If someone wants to merely cut and paste code, there are other places they can go to do that."

Secondly if you have gone through the all the replies someone (already) pointed that my solution has flaw. You dont have to redo it again. It is duplicating of a post.


It's not uncommon for moderators to say the same things in slightly different ways. Sometimes we reiterate things for emphasis and sometimes we don't read all of the posts in a thread. It happens.

And solutions or proposed solutions from every member can not stand as perfect solutions.


True, and even we moderators can give incorrect solutions sometimes but we are also quick to point out flaws, even in our own code. Don't take those kinds of comments personally. As you said, we're all here to learn and accepting that we've made mistakes is part of learning.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tobias Bachert wrote:. . .

My, there's some good stuff there

Can I ask you please to give a bit of explanation of that line; many people here have never seen a method reference before.
 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method accepts as first argument an object of type IntSupplier, which is a functional interface (-> exactly one abstract method, in this case its a method that accepts no argument and returns an int).
Since Java8 it is possible to pass method references to methods accepting such interfaces (its comparable to anonymous classes).

In Java7, the code would have been
With Java8, it is possible to omit the redundant code parts (there is only one calculateStatisticsFor method with a matching signature -> compiler can infer the IntSupplier part, IntSupplier has only one method that has to be implemented -> compiler can infer method signature), which leads to

as we have only one line, the brackets are redundant too:
which can be finally shortened to the expression
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I love seeing examples of refactoring a problem to use the new features of Java 8.

This isn't Java 8, but here's my take on the problem. I tend to be leery of problems that take "only" three numbers. This leads you to implement code that is brittle and not easily expanded to take N numbers.


 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Boor wrote:I'm just learning Java, so try not to get too technical


Poor OP must be thinking, "Sheesh, if this isn't getting too technical, I can't imagine what is!"

You'll have to excuse our enthusiasm. Somewhere in this thread are the two suggestions you can try out first: a series of if-then-else statements or the Math.min and Math.max() methods.
 
Message for you sir! I think it is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic