home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2692
Discussions: 188,384, Posts: 2,243,498, Members: 232,614
Old February 3rd, 2003, 10:45 PM   Digg it!   #1 (permalink)
Best To Avoid Me
 
Martoch's Avatar
 
Join Date: Mar 2002
Location: Under Your Bed
Posts: 8,596
JAVA help please?

Alright, here's the situation...

I've got to come up with an applet that will read five integers and will determine and print the largest and smallest integers in the group.

Well so far I'm having problems coming up with algorithms to accomplish this...to be quite honest I'm having "programmer's block" if there is such a thing. I've only been taking JAVA for about 3 weeks now and this is our first program "from scratch".

So far I've gotten as far as declaring my variables...I just need a hint as to how to get the largest/smallest integers separated from the rest of the group. I'm not looking for the complete answer, just some help with getting started.

Here's what I have so far:

import java.awt.Graphics;
import javax.swing.*;

public class BigSmallApplet extends JApplet {

public void init()
{
String firstNumber;
String secondNumber;
String thirdNumber;
String fourthNumber;
String fifthNumber;
String result;

int n1;
int n2;
int n3;
int n4;
int n5;

firstNumber = JOptionPane.showInputDialog( "Enter first integer:" );
secondNumber = JOptionPane.showInputDialog( "Enter second integer:" );
thirdNumber = JOptionPane.showInputDialog( "Enter third integer:" );
fourthNumber = JOptionPane.showInputDialog( "Enter fourth integer:" );
fifthNumber = JOptionPane.showInputDialog( "Enter fifth integer:" );

result = "";

Next is where I would begin my "if" statements right?
Like...

if ( n1 > n2 )
result = result + ??? ARGH! It seems like I should have 10,000 "if" statements to sort out the numbers...frustration!!!

Thanks for any help/suggestions!
Mike
__________________
~ Camp Crystal Lake counselor positions opening daily ~

Martoch is offline   Reply With Quote
Old February 3rd, 2003, 10:57 PM     #2 (permalink)
Ultimate Member
 
Nighthawk's Avatar
 
Join Date: Oct 2001
Location: Montreal, QC
Posts: 1,950
why are you getting the user-input numbers and putting them into strings? I'd define them to be ints.

I'd also add two int variables, "biggest" and "smallest" or something along those lines.

When you get the first int from your user, set both biggest and smallest to it. Then as you keep reading in numbers, check to see if it's bigger or smaller than biggest or smallest and update the variable if it is. Then you can just do a
return("The largest number was " + biggest + " and the smallest number was " + smallest);

Nighthawk is offline   Reply With Quote
Old February 3rd, 2003, 11:02 PM     #3 (permalink)
Best To Avoid Me
 
Martoch's Avatar
 
Join Date: Mar 2002
Location: Under Your Bed
Posts: 8,596
Hmm...Nighthawk thanks for your input.

Well later in the applet I will convert the Strings to ints, that's how we were taught so that's all I know for now.

n1 = Interger.parseInt( firstNumber );

Ok, back to the drawing board for me...time to add a couple more variables as you suggested.

Thanks a ton!!!

Mike

Martoch is offline   Reply With Quote
Old February 3rd, 2003, 11:15 PM     #4 (permalink)
Best To Avoid Me
 
Martoch's Avatar
 
Join Date: Mar 2002
Location: Under Your Bed
Posts: 8,596
Ok, does this make sense? (I've declared biggest and smallest as two more int variables of course)

n1 = Integer.parseInt( firstNumber );
n2 = Integer.parseInt( secondNumber );
n3 = Integer.parseInt( thirdNumber );
n4 = Integer.parseInt( fourthNumber );
n5 = Integer.parseInt( fifthNumber );

biggest = n1;
smallest = n1;

result = "";

if ( n2 > biggest )
biggest = n2;

if ( n2 < smallest )
smallest = n2;

if ( n3 > biggest )
biggest = n3;

if ( n3 < smallest )
smallest = n3;

Please tell me I'm at least 1/2 on track here...I've never taken any programming courses before and I'm really into this so far.

Thanks again...

EDIT: added ;

Last edited by Martoch : February 3rd, 2003 at 11:36 PM.
Martoch is offline   Reply With Quote
Old February 4th, 2003, 12:11 AM     #5 (permalink)
Best To Avoid Me
 
Martoch's Avatar
 
Join Date: Mar 2002
Location: Under Your Bed
Posts: 8,596
Well I took a crack at it and here's what's going on...(if anybody is listening)

My code (important parts);


public void init()
{
String firstNumber;
String secondNumber;
String thirdNumber;
String fourthNumber;
String fifthNumber;
String result;

int n1;
int n2;
int n3;
int n4;
int n5;
int biggest;
int smallest;


firstNumber = JOptionPane.showInputDialog( "Enter first integer:" );
secondNumber = JOptionPane.showInputDialog( "Enter second integer:" );
thirdNumber = JOptionPane.showInputDialog( "Enter third integer:" );
fourthNumber = JOptionPane.showInputDialog( "Enter fourth integer:" );
fifthNumber = JOptionPane.showInputDialog( "Enter fifth integer:" );

n1 = Integer.parseInt( firstNumber );
n2 = Integer.parseInt( secondNumber );
n3 = Integer.parseInt( thirdNumber );
n4 = Integer.parseInt( fourthNumber );
n5 = Integer.parseInt( fifthNumber );

biggest = n1;
smallest = n1;

result = "";

if ( n2 > biggest )
biggest = n2;

if ( n2 < smallest )
smallest = n2;

if ( n3 > biggest )
biggest = n3;

if ( n3 < smallest )
smallest = n3;

if ( n4 > biggest )
biggest = n4;

if ( n4 < smallest )
smallest = n4;

if ( n5 > biggest )
biggest = n5;

if ( n5 < smallest )
smallest = n5;

}

public void paint( Graphics g )
{
super.paint( g );

g.drawRect( 15, 10, 270, 20 );

g.drawString( "The biggest integer is " result + biggest +
"\n The smallest integer is " result + smallest , 25, 25 );

}

}

I get 2 errors (not bad, right?)


BigSmallApplet.java [78:1] ')' expected
g.drawString( "The biggest integer is " result + biggest +


Why is it expecting a ) ???


^
BigSmallApplet.java [78:1] cannot resolve symbol
symbol : method drawString (java.lang.String)
location: class java.awt.Graphics
g.drawString( "The biggest integer is " result + biggest +




No clue on error #2
Martoch is offline   Reply With Quote
Old February 4th, 2003, 02:38 AM     #6 (permalink)
Ultimate Member
 
Nighthawk's Avatar
 
Join Date: Oct 2001
Location: Montreal, QC
Posts: 1,950
The problem it's whining at you about is that it doesn't like it when you end a line without a semicolon. You should just do ..."+ biggest + \n + "the smallest"... all in one line.

I think that's also the reason that the other error is being thrown.

Just from a code-reading standpoint, what I'd do is a :
if(n2 < smallest) {
smallest = n2;
} else if(n2 > largest){
largest = n2;
}
instead of checking the two things independently, because it can only either be smaller or larger.
Nighthawk is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2896)
The United States Debt (20)
3-days in and no threads about Gaza (160)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
hp compaq nc6000 problems (138)
Folderchat Weekday thread (441)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (15)
Recent Discussions
ACPI controller halt on boot (2)
Building a gaming computer advi.. (3)
Worth the upgrade?? (15)
Folderchat Weekday thread (441)
ADVICE (0)
How to increase my ram? (5)
Help with an Ati Radeon HD 4850.. (27)
CPU wont boot (4)
2nd video card (1)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 09:56 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28