Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

impulse462

macrumors 68020
Original poster
Jun 3, 2009
2,075
2,860
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
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
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.
 

impulse462

macrumors 68020
Original poster
Jun 3, 2009
2,075
2,860
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..
 

Cinder6

macrumors 6502a
Jul 9, 2009
509
50
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.
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
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.

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.
 

braves4life

macrumors newbie
Oct 12, 2007
11
0
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?
 

impulse462

macrumors 68020
Original poster
Jun 3, 2009
2,075
2,860
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.
 

SRossi

macrumors regular
May 27, 2009
202
0
Glasgow, Scotland
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
 

parapup

macrumors 65816
Oct 31, 2006
1,291
49
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]);
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
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 ));
 

impulse462

macrumors 68020
Original poster
Jun 3, 2009
2,075
2,860
Ah,thanks for the tips.

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

Cinder6

macrumors 6502a
Jul 9, 2009
509
50
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

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

impulse462

macrumors 68020
Original poster
Jun 3, 2009
2,075
2,860
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?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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?

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.