Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Identify Highest & Lowest Inputs

Posted on 2002-03-17
Medium Priority
504 Views
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");
for(i=1; i<=numToSample; i++){

kilograms = kcc.ConvToKilo(stns,lbs);

ac.InputValue (kilograms);

System.out.println("\nAverage="+ac.Average());
}
}
}

0
Question by:LittleRedHat
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

Expert Comment

ID: 6873792
If you would like to identify the biggest/lowest number in an array that's very simple.

int highest = 0;
for (int i=0; i<yourArray.length; i++)  {
if yourArray[i] > highest
highest = yourArray[i];
}

Just change the type of highest to what's suitable. You shoul also set it to be the lowest possible. If the values are ranging from 1 to 1000, set it to 1.

To find the smallest, just set the variable to be as high as possible and change one row of code:
if yourArray[i] < lowest

-toffe
0

Author Comment

ID: 6873993
Hi Toffe

Thanks for your reply.  I'm afraid though I haven't been able to work out how to use it/where to put it. I'm only a week or two into dipping my toes into OOP/Java and haven't yet fully grasped the concepts.  Sorry! :-(

In between times I was trying to resolve the problem as below.  I'm down to 2 errors, both appearing to be related to my (presumably incorrect) use of "if".  Will this work if I can resolve the errors? ... and advice about how to resolve these and how to use your coding would be appreciated.

With thanks again.

LRH

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

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

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

public void RangeValue(double number){
rcount=1;
number = min;
number = max;

if rcount+1 <={
number = min;
}
if rcount+1 >={
number = max;
}

calc=(max-min);
}

public double Average(){
return sum / count;

}

public double Range(){
return calc;
}
}

0

Author Comment

ID: 6874085
To save wasting more of your time ... I'm now up and running on my own code, having resolved my careless error!  I hadn't included max/min for the <= and >=

In case it's any help to anyone else here's the corrected code

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

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

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

public void RangeValue(double number){
rcount=1;
number = min;
number = max;

if (rcount+1 <=min){
number = min;
}
if (rcount+1 >=max){
number = max;
}

calc=(max-min);
}

public double Average(){
return sum / count;

}

public double Range(){
return calc;
}
}

With thanks again for your help.

LRH
0

Author Comment

ID: 6875196
Whoops!  I was somewhat premature at thinking I was up and running ... I was only compiling!!!

Following is my latest attempt at trying to establish the first input as both max and min and then have max and min adjusted as other figures are entered. (I've also updated the SampleCheck.)  I do need the program 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("How many samples are there?\n");
for(i=1; i<=numToSample; i++){

kilograms = kcc.ConvToKilo(stns,lbs);

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

System.out.println("\nAverage="+arc.Average());
System.out.println("\nRange="+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 again.

LRH
0

LVL 9

Expert Comment

ID: 6875840
You don't need the RangeValue() method. Determine the max and min when you call InputValue():

public void InputValue(double number){
min = Math.min(min, number);
max = Math.max(max, number);
sum+=number;
count++;
}

// arc.RangeValue (kilograms); // Remove this also
arc.InputValue (kilograms);
0

Author Comment

ID: 6876542
Many thanks for your help.  I now have a figure showing for the max-min range, but [[isn't there always a but! :-)]] the max-min calculation most times isn't coming up with the correct answer.

I'm sure there must be a pattern somewhere, but I can't fathom it out.  Sometimes it gives the figure of the maximum amount input. Sometimes it gives the calculation based on the last two figures input.  Sometimes it gives a figure from I don't know where!  Occassionally it gives the correct figure.

I've tried all the coding variations, combinations and permutations I can think of without any success and am open to any/all suggestions!!! :-)

WTA

LRH
0

LVL 9

Accepted Solution

Venci75 earned 1200 total points
ID: 6877139
public void RangeValue(double number){
if (count == 0){
min = number;
max = number;
}
if (number < min){
min = number;
}
if (number > max){
max = number;
}
}

0

LVL 86

Expert Comment

ID: 6879536

1. As a point of coding style, your classes should begin with a capital letter, but their methods should begin with a lower case letter:

public class Average {
public double computeAverage(){
...
}
}

This will mean that these are easily distinguished when your code is viewed. Thus, your current 'suffix' of 'Class' in your class names is redundant.

2. As regards your third functional requirement (identifying your highest and lowest inputs), you *could*, as has been suggested, simply save the highest and lowest inputs as you go along and this will work fine. But one of the important principles of object oriented programming is reusability. What if, in the future, you wanted to know the second highest number? Let's call this 'functionality 3b' as opposed to your current functionality 3a. You couldn't do that with the class you have. Yes, you could create a subclass that did this, but you would find you would have to alter your approach quite significantly.
Think about what you need to do here, and what you need to do in the first case. What are you doing? Are you not *sorting* the numbers essentially?
If the answer to the above is affirmative, why not try and develop a generic class for sorting a range of numbers and computing their average. This would then satisfy functionality 3a *and* 3b.

Finally, as far as point 1 is concerned, two more principles of good programming:
1. Do it properly
2. Do it NOW!
0

LVL 86

Expert Comment

ID: 6879648
If you find my previous second point a little abstract for now, I could possibly help with smaller, more concrete aspects of your code.
0

Author Comment

ID: 6882208
Quick thanks to you all 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
0

Author Comment

ID: 6904857
Apologies again for the delay and my thanks to you all.  I found all your replies helpful and have taken note of your useful advice CEHJ ... thanks for that too.

I'm sorry I can't share the points.  I don't know if this is the fairest solution, but Venci75's advice to use min/max=number and NOT number=min/max taught me the best lesson pro rata to my level of knowledge/learning ... ... a lesson I now shan't forget!!!

With best wishes and all round thanks again ...

LRH
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectioâ€¦
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Downâ€¦