According to my code, please provide a Tester that can prompt a user for input.
ID: 3857108 • Letter: A
Question
According to my code, please provide a Tester that can prompt a user for input.
public class P6_2{
int count ;
double sum;
double sumSquares ;
public P6_2(){
this.count = 0;
this.sum = 0;
this.sumSquares = 0 ;
}
public void add (double num){
sum = sum +num;
sumSquares = sumSquares + num * num;
count ++;
}
public double getAverage(){
return sum / count;
}
public int getCount(){
return count;
}
public double getStandardDeviation(){
return Math.sqrt((sumSquares - Math.pow(sum,2)/count)/(count-1));
}
}
Explanation / Answer
public class P6_2{
int count ;
double sum;
double sumSquares ;
public P6_2(){
this.count = 0;
this.sum = 0;
this.sumSquares = 0 ;
}
public void add (double num){
sum = sum +num;
sumSquares = sumSquares + num * num;
count ++;
}
public double getAverage(){
return sum / count;
}
public int getCount(){
return count;
}
public double getStandardDeviation(){
return Math.sqrt((sumSquares - Math.pow(sum,2)/count)/(count-1));
}
}
class Test
{
public static void main (String[] args)
{
P6_2 obj = new P6_2(); //create object of class
//add double numbers to obj
obj.add(56.7);
obj.add(34.5);
obj.add(56.7);
obj.add(34.5);
obj.add(56.7);
obj.add(34.5);
//calculate average, count and standard deviation of numbers
System.out.println("Average = "+obj.getAverage());
System.out.println("Count = "+obj.getCount());
System.out.println("Standard Deviation = "+obj.getStandardDeviation());
}
}
Output:
Average = 45.6
Count = 6
Standard Deviation = 12.159440776614689
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.