User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 391,582 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,664 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2829 | Replies: 1
Reply
Join Date: Sep 2004
Posts: 2
Reputation: KCC1982 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
KCC1982 KCC1982 is offline Offline
Newbie Poster

Pls Help JAVA Program Headaches Not Giving me the right output for user input

  #1  
Sep 24th, 2004
Hi,
I just want to say thank you in advance to anyone that replies to this. I'm new to JAVA & my 4th assignment has got me stumped. I have to write a program that reads 4 integers and prints the largest and smallest value that the user entered (using joption pane) with no if statements. I also have to modify the addValue method with Math.min & Math.max methods. So far the computer is not responding to the users' input and giving me -.2147483648 for the smallest and .2147483647 for the largest. Please do not laugh I am just starting out. Once again any insight or help will be greatly appreciated. My soucre code is as follows:

class DataSet
{

private int smallest;
private int largest;


//The constructor

public DataSet()
{
smallest = Integer.MIN_VALUE;
largest = Integer.MAX_VALUE;
}

public void addValue(int x)
{
smallest = Math.min(smallest,largest);
largest = Math.max(smallest,largest);
}

public int getSmall()
{

return Math.min(smallest,largest);
}

public int getLarge()
{

return Math.max(smallest,largest);
}

}

import javax.swing.JOptionPane;

class DataSetTest
{
public static void main(String[] args)
{

DataSet myDataSet = new DataSet();

String input = JOptionPane.showInputDialog(
"Please enter the first number:");
int number1 = Integer.parseInt(input);

input = JOptionPane.showInputDialog(
"Please enter the second number:");
int number2 = Integer.parseInt(input);

input = JOptionPane.showInputDialog(
"Please enter the third number:");
int number3 = Integer.parseInt(input);

input = JOptionPane.showInputDialog(
"Please enter the fourth number:");
int number4 = Integer.parseInt(input);

myDataSet.addValue(number1);
myDataSet.addValue(number2);
myDataSet.addValue(number3);
myDataSet.addValue(number4);

System.out.println("The smallest number is " + myDataSet.getSmall());
System.out.println("The largest number is " + myDataSet.getLarge());

System.exit(0);
}
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2006
Posts: 1
Reputation: morans is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
morans morans is offline Offline
Newbie Poster

Re: Pls Help JAVA Program Headaches Not Giving me the right output for user input

  #2  
Mar 15th, 2006
public void addValue(int x)
{
smallest = Math.min(smallest,largest);
largest = Math.max(smallest,largest);
}

should be

public void addValue(int x)
{
smallest = Math.min(smallest, x);
largest = Math.max(largest, x);
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

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

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 10:52 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC