Your question, your audience. Choose who sees your identityâ€”and your questionâ€”with question security.

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("Please enter stones weight:");

stns = Input.readInt();

System.out.print("Please enter pounds weight:");

lbs = Input.readInt();

kilograms = kcc.ConvToKilo(stns,lbs);

ac.InputValue (kilograms);

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

}

}

}

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("Please enter stones weight:");

stns = Input.readInt();

System.out.print("Please enter pounds weight:");

lbs = Input.readInt();

kilograms = kcc.ConvToKilo(stns,lbs);

ac.InputValue (kilograms);

System.out.println("\nAver

}

}

}

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

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;

}

}

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

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");

numToSample=Input.readInt(

for(i=1; i<=numToSample; i++){

System.out.print("Please enter stones weight:");

stns = Input.readInt();

System.out.print("Please enter pounds weight:");

lbs = Input.readInt();

kilograms = kcc.ConvToKilo(stns,lbs);

arc.RangeValue (kilograms);

arc.InputValue (kilograms);

System.out.println("\nAver

System.out.println("\nRang

}

}

}

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

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);

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

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!

LRH

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

public void RangeValue(double number){

if (count == 0){

min = number;

max = number;

}

if (number < min){

min = number;

}

if (number > max){

max = number;

}

}