PDA

View Full Version : Java simple program

impulse462
Jul 24, 2009, 10:24 PM
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
Jul 24, 2009, 10:32 PM
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
Jul 24, 2009, 10:38 PM
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
Jul 24, 2009, 10:44 PM
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
Jul 24, 2009, 10:47 PM
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.

impulse462
Jul 24, 2009, 10:55 PM
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

braves4life
Jul 25, 2009, 12:37 AM
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
Jul 25, 2009, 09:37 AM
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
Jul 25, 2009, 11:08 AM
I don't know much java but when you use:

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
Jul 25, 2009, 11:30 AM
Instead of the long if/else blocks for determining min and mix - Array sorting is the right thing to do -

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
Jul 25, 2009, 11:45 AM
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.

int maxNum = Math.max( a, Math.max( b, c ));
int minNum = Math.min( a, Math.min( b, c ));

impulse462
Jul 25, 2009, 11:51 AM
Ah,thanks for the tips.

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

Cinder6
Jul 25, 2009, 01:49 PM
I don't know much java but when you use:

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
Jul 25, 2009, 02:14 PM
With 3 values, this is pretty trivial.

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
Jul 25, 2009, 02:30 PM
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
Jul 25, 2009, 02:40 PM
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