Java simple program

Discussion in 'Mac Programming' started by impulse462, Jul 24, 2009.

  1. macrumors 65816

    impulse462

    Joined:
    Jun 3, 2009
    Location:
    SF Bay Area
    #1
    Hi all,

    I'm testing into the AP Computer Science class for my senior year and at my school they teach java.

    I've had some experience with C, so im familiar with coding (kinda), however I only started this whole computer programming thing about 2 weeks ago.

    One of the questions on this review sheet is to write a program in which the user inputs 3 integers and the program displays the sum,average,product, smallest and the largest of these 3 integers.

    The sum,average, and product I all have down, but for the love of me I have no idea what to do to make the program determine which is the largest and smallest integer

    http://pastie.org/558429

    this is my code so far. I know it has something to do with the if loop, but I just can't think of what to type. Any help would be greatly appreciated
     
  2. Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #2
    There's many ways to do it and I think recursion is probably too much for knowing there will only be 3 numbers... if I were you I would probably do something like the following...

    How can you tell if one number is larger than another? Can you store the larger one somewhere? Can you then check if that number is larger than your third number?

    I don't want to just give you the code especially since you're trying to learn, but hopefully that will steer you in the right direction.

    You could also use the ternary operator but that's probably a bit further into the class maybe.
     
  3. thread starter macrumors 65816

    impulse462

    Joined:
    Jun 3, 2009
    Location:
    SF Bay Area
    #3
    Well I would use

    if(a<b)

    If that statement were true, is it possible to give the "truthness" a variable and then have a second "if" loop comparing that variable of "truthness" to the third integer c?

    sounds kind of weird i know..
     
  4. macrumors 6502

    Cinder6

    Joined:
    Jul 9, 2009
    #4
    Also not wanting to give away the answer here. But, in general, what will you know if you compare each number with every other number? How are you going to keep track of which is largest and smallest? By comparing one number to another (if a<b), you'll have potential candidates for largest and smallest. You should then test out the third number on these two numbers, now that you know how they rate against each other...

    Oh yeah, convention says you should Capitalize the name of every class you create. I'd actually thought Java mandated this, but I'm apparently wrong.
     
  5. Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #5
    Nah it's not a mandate, it's just in the code conventions set up by Sun.

    As for the question asked by the OP... once you know if a < b or a > b you might store the larger in another variable. Then you can check the larger against c.
     
  6. thread starter macrumors 65816

    impulse462

    Joined:
    Jun 3, 2009
    Location:
    SF Bay Area
    #6
    Yes in my book it says to capitalize every class too, since these were the exercises i didnt bother.

    I'm still working on this.

    EDIT:

    OMG I think i got it: http://pastie.org/558453
     
  7. macrumors newbie

    Joined:
    Oct 12, 2007
    #7
    Code:
    
    if(a<b)
    		{
    			if(a>c)
    			{
    				System.out.printf("%d is the smallest\n",c,a);
    			}
    			if(a<c)
    			{
    				System.out.printf("%d is the smallest\n",a,c);
    			}
    		}
    		if(a>b)
    		{
    			if(b<c)
    			{
    				System.out.printf("%d is the smallest\n",b,c);
    			}
    			if(b>c)
    			{
    				System.out.printf("%d is the smallest\n",c,b);
    			}
    		}		
    
    
    What happens with the following input: 3, 5, 3?
     
  8. thread starter macrumors 65816

    impulse462

    Joined:
    Jun 3, 2009
    Location:
    SF Bay Area
    #8
    Ok, I've added more to it: http://pastie.org/558663

    For some reason if I put in 3,3,5 or 3,3,1 the smallest just doesn't show up. I'm pretty sure the code is right for the if(a==b) part.
     
  9. macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #9
    I don't know much java but when you use:

    Code:
    System.out.printf("%d is the smallest\n",c,a);
    aren't you actually trying to print two integers out with only one "%d"?

    Don't know if this will help but I just noticed and thought that could be wrong.

    Sorry if im talking rubbish.

    Stephen
     
  10. macrumors 65816

    parapup

    Joined:
    Oct 31, 2006
    #10
    Instead of the long if/else blocks for determining min and mix - Array sorting is the right thing to do -

    Code:
    	int[] array = { a ,b , c};
    		Arrays.sort(array);
    		System.out.printf("The minimum is %d", array[0]);
    
    		System.out.printf("The maximum is %d", array[array.length-1]);
    
     
  11. macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #11
    Another option is to use the java.lang.Math package, although the array method is much more flexible if you are dealing with an larger/unknown number of inputs.

    Code:
    int maxNum = Math.max( a, Math.max( b, c ));
    int minNum = Math.min( a, Math.min( b, c ));
     
  12. thread starter macrumors 65816

    impulse462

    Joined:
    Jun 3, 2009
    Location:
    SF Bay Area
    #12
    Ah,thanks for the tips.

    My test doesnt cover arrays, so I don't quite know what those are yet, but thanks again.
     
  13. macrumors 6502

    Cinder6

    Joined:
    Jul 9, 2009
    #13
    Yeah, 'a' will never print.

    As for everyone offering the (arguably better) more complex solutions--keep in mind that this is a test to get into an introductory class.
     
  14. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #14
    With 3 values, this is pretty trivial.
    Code:
    if(a>=b && a>= ???) {
      max = a;
    } else if(???){
      max = b;
    } else {
      max = c;
    }
    
    you'd have to fill in the ???s, but it should be pretty obvious.

    You could work min into the same block. Once you know which is the max, there're only 2 choices for the minimum.

    If you had a list of arbitrary length, then sorting, etc. makes more sense, but for such a small set of values, the if-then-else is pretty small.

    Also, printf isn't really needed. You can use + to join the String representation of an int with another String, then just display it with System.out.println.

    -Lee
     
  15. thread starter macrumors 65816

    impulse462

    Joined:
    Jun 3, 2009
    Location:
    SF Bay Area
    #15
    Thanks for all the replies.

    I have a habit of using printf because that is what i used in C.

    i know that println makes a new line, but what is the difference between print and printf?

    is printf only capable of something like printf("This is %s\n",variable");

    while print is only capable of putting text on the screen?
     
  16. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #16
    Take a look at the PrintStream docs to compare:
    http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html

    I most commonly use the String version of println, because it's so trivial to compose a String using + with all sorts of other types. If you are using complex format specifiers with width, precision, etc maybe printf makes sense, but Java is not C, so it might be good to acclimate yourself with some of the conveniences it provides.

    -Lee
     

Share This Page