Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Highest & Lowest inputs 1

Status
Not open for further replies.

LittleRedHat

Instructor
Apr 20, 2001
202
GB
I am trying to write a program that, following user input (user determines no of samples),

1. converts the input to kilograms
2. calculates the average
3. identifies the range i.e. highest and lowest input
4. makes a further calculation based on both the average and the range

The input, conversion and average are working correctly, but I'm stumped as to how to identify the highest and lowest inputs.

Ideally I would like to include this in the AverageClass, but would settle for a seperate RangeClass that works! :)

I would be very grateful for any help or guidance.

WTA

LRH

/*
Java Class for Weight (Stones/Pounds:Kilograms) Converter
*/
public class KiloConvClass{
private double kilograms;
private int stones, pounds;

public double ConvToKilo(int stns, int lbs){
return (double)(stns*14+lbs)/2.2;
}
}

/*
Java Class for calculating averages
*/
public class AverageClass{
private double sum;
private int count;

public AverageClass(){
sum=0;
count=0;
}

public void InputValue(double number){
sum+=number;
count++;
}

public double Average(){
return sum / count;
}
}

/*
Java program to check samples are
maintained within a specified range,
using KiloConv class, Average class,
*/

public class SampleCheck{
public static void main(String arg[]){
double kilograms;
int stns, lbs, i, numToSample;

KiloConvClass kcc = new KiloConvClass();
AverageClass ac = new AverageClass();

System.out.println("How many samples are there?\n");
numToSample=Input.readInt();
for(i=1; i<=numToSample; i++){

System.out.print(&quot;Please enter stones weight:&quot;);
stns = Input.readInt();
System.out.print(&quot;Please enter pounds weight:&quot;);
lbs = Input.readInt();

kilograms = kcc.ConvToKilo(stns,lbs);

ac.InputValue (kilograms);


System.out.println(&quot;\nAverage=&quot;+ac.Average());
}
}
}
 
This is a well known algorithm in computer science known as the selection problem (find the kth smallest or largest key in a list). In your case, k=1.

Finding either the smallest or largest key is O(n) (i.e. takes n-1 comparisons). Finding both is also O(n) (although there is a slightly better approach than the naive approach of 2(n-1), namely 3n/2).

In any event, if you do a search you should be able to find some code or pseudocode to solve your problem.

Cheers,

Charles
 
Sorry, you've lost me totally and completely! :-( I've only been dipping my toes into Java for 2 or 3 weeks and still only have a vague understanding of OOP/Java concepts.

In the interim I've been trying to come up with a solution. I have changed the AverageClass to AverageRangeClass and added an attempt at trying to establish the first input as both max and min then have max and min adjusted as other figures are entered. (I've also updated the SampleCheck.) I do need the prgram to identify both the max and min as I need the max-min figure for the next calculation I want to include.

/*
Java Class for calculating average and range
*/
public class AverageRangeClass{
private double sum, calc, max, min;
private int count;

public AverageRangeClass(){
sum=0;
calc=0;
count=0;
}

public void InputValue(double number){
sum+=number;
count++;
}

public void RangeValue(double number){
if (count==1){
number = min;
number = max;
}
if (count+1 <=min){
number = min;
}
if (count+1 >=max){
number = max;
}

}

public double Average(){
return sum / count;

}

public double Range(){
return max-min;
}
}


/*
Java program to check samples are
maintained within a specified range,
using KiloConv class, AverageRange class,
*/

public class SampleCheck{
public static void main(String arg[]){
double kilograms, max, min;
int stns, lbs, i, numToSample;

KiloConvClass kcc = new KiloConvClass();
AverageRangeClass arc = new AverageRangeClass();

System.out.println(&quot;How many samples are there?\n&quot;);
numToSample=Input.readInt();
for(i=1; i<=numToSample; i++){

System.out.print(&quot;Please enter stones weight:&quot;);
stns = Input.readInt();
System.out.print(&quot;Please enter pounds weight:&quot;);
lbs = Input.readInt();

kilograms = kcc.ConvToKilo(stns,lbs);

arc.RangeValue (kilograms);
arc.InputValue (kilograms);



System.out.println(&quot;\nAverage=&quot;+arc.Average());
System.out.println(&quot;\nRange=&quot;+arc.Range());
}
}
}

Both compile. The Average is accurately calculated, but Range only returns 0.00.

I suspect/am guessing that my code isn't woking to establish the first input as both max and min.

Help to correct it would be appreciated.

With thanks.

LRH

 
What I am talking about is not OOP, it is computer science, specifically algorithms.

In any event, there are many ways to solve your problem. You have several bugs: you assign max and min to number and not the other way around. You check count against number and not max and min

For instance, here is a better solution (IMHO):

public class CalcRange
{
double max, min, sum;
int count;

public CalcRange()
{
max = min = sum = 0.0;
count = 0;
}

public void add(double value)
{
sum += value;
if (count == 0)
{
// first value sets the bar
max = min = value;
}
else
{
// push max and min if necessary
max = value > max ? value : max;
min = value < min ? value : min;
}
count++;
}

public double range()
{
return max - min;
}

public average()
{
// avoid divide by zero
return count > 0 ? sum / count : sum;
}
}
 
Quick thanks and apologies that I've had to put this on hold because of a crisis at this
end. I shall respond as soon as possible.

LRH
 
Hi Charles,

Apologies again for the delay and thanks again for all your advice.

I had done, as you suggested, a search on the selection problem before your second reply, which both resolved my confusion and proved helpful. I should have realised what you were referring to, but I was so focused on trying to resolve the error of my coding ways my brain didn't make the lateral leap.

Whilst I now plan to re-write my code as per your advice, what was also really helpful ... a lesson I now shan't forget!!! ... was &quot;you assign max and min to number and not the other way around. You check count against number and not max and min&quot;.

With thanks again and all best wishes ...

Maureen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top