find minimum and maximum

find the minimum and maximum of a group of numers

Page 1 of 1

5 Replies - 3019 Views - Last Post: 22 October 2009 - 11:46 PM Rate Topic: -----

#1 yingkang  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 24-March 09

find minimum and maximum

Post icon  Posted 22 October 2009 - 10:06 PM

the program should find the minimum andhe maxmum from the 1000 ramdom numbers. It needs to be done in the method of enter( double dblItem ) , in Statistics class. can anyone help please? thanks
public class StatisticsTest {

	/**
	 * @param args
	 */ 
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		double dblNum;
		Statistics statisticsObj = new Statistics();
		
		for(int i = 0; i < 1000; i++)
		{
			dblNum = Math.random() * 100;
			statisticsObj.enter(dblNum);
		}
		System.out.println(statisticsObj);
			

	}

}
import java.text.DecimalFormat;
public class Statistics 
{
	private int	intCount; 
	private double dblMax; 
	private double dblMin; 
	private double dblSum; 
	private double dblMean;
	
	public Statistics()
	{
		this.intCount = 0;
		this.dblMax = 0;
		this.dblMin = 0;
		this.dblSum = 0;		
	}
   public int count() 
   {
	   return this.intCount;   
   
   }
	public double min() 
	{
		return this.dblMin;		
		
	}
	public double max()
	{
		return this.dblMax;
	}
	
	public double sum()
	{
		return this.dblSum;		
	}
	public double mean() 
	{
		
		return dblMean;
	}
	
	 void enter( double dblItem )
	{
		
		dblSum += dblItem;
		intCount++;
		// here I need to find the minimum and maxmum, but i cant figure out how
		
		
		dblMean = this.dblSum / (double)this.intCount; 
	}
	
	public String toString()
	{
		DecimalFormat fmt = new DecimalFormat("0.####");
		return "Counter = " + intCount + "\nMinimum = " + fmt.format(dblMin) 
			+ "\nMaximum = " + fmt.format(dblMax) + "\nSum = " + fmt.format(dblSum) +
			"\nAverage = " + fmt.format(dblMean);
	} 
}
 




Is This A Good Question/Topic? 0
  • +

Replies To: find minimum and maximum

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: find minimum and maximum

Posted 22 October 2009 - 10:17 PM

What problem(s) are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 yingkang  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 24-March 09

Re: find minimum and maximum

Posted 22 October 2009 - 10:21 PM

View PostLocke, on 22 Oct, 2009 - 09:17 PM, said:

What problem(s) are you encountering?


the ramdom generator generate 1000 numbers, and I need to find the minimum and maxmum. how can I do that?. by the way, i havent learnt array yet, so it wont use any array stuff. thank you for help :)
Was This Post Helpful? 0
  • +
  • -

#4 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: find minimum and maximum

Posted 22 October 2009 - 10:38 PM

no need for an array, all you need is a variable that is a min and max variable, youll probably want to initialize them such that they WILL be edited

int min = 9001, max = -9001;//unless you got some impossible values this should be just fine
//...code...\\
//youll want to compare your values in your for loop	ex.

for(int i = 0; i < 1000; i++)
		{
			dblNum = Math.random() * 100;
			statisticsObj.enter(dblNum);
			if(min > dblNum)
				   min = dblNum;
			if(max < dblNum)
				   max = dblNum;

		}


Was This Post Helpful? 1
  • +
  • -

#5 yingkang  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 24-March 09

Re: find minimum and maximum

Posted 22 October 2009 - 10:57 PM

View Postvirgul, on 22 Oct, 2009 - 09:38 PM, said:

no need for an array, all you need is a variable that is a min and max variable, youll probably want to initialize them such that they WILL be edited

int min = 9001, max = -9001;//unless you got some impossible values this should be just fine
//...code...\\
//youll want to compare your values in your for loop	ex.

for(int i = 0; i < 1000; i++)
		{
			dblNum = Math.random() * 100;
			statisticsObj.enter(dblNum);
			if(min > dblNum)
				   min = dblNum;
			if(max < dblNum)
				   max = dblNum;

		}


Thanks Virgul :) I revised my program according to your suggestion. It works perfectly now. thank you very much
Was This Post Helpful? 0
  • +
  • -

#6 Munkon  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 61
  • Joined: 13-August 09

Re: find minimum and maximum

Posted 22 October 2009 - 11:46 PM

Hi virgul,

The random method will give you only numbers between 0 and 100 and then you are in trouble..

I'd do something else:


for(int i = 0; i < 1000; i++)
		{
			dblNum = Math.random() * 100;
			if (i = 0)
		   {
			min = dblNum;
			max = dblNum;
			}
			else
			{

			statisticsObj.enter(dblNum);
			if(min > dblNum)
				   min = dblNum;
			if(max < dblNum)
				   max = dblNum;
			}
		}





Good luck,

Munkon.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1