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

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

LittleRedHatAuthor Commented:

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

LittleRedHatAuthor Commented:

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

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

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

LittleRedHatAuthor Commented:

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!!! :-)

change your RangeValue method:
public void RangeValue(double number){
if (count == 0){
min = number;
max = number;
}
if (number < min){
min = number;
}
if (number > max){
max = number;
}
}

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!

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

LittleRedHatAuthor Commented:

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

LittleRedHatAuthor Commented:

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

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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