JAVA OOP Thank you Modify Assignment #10 so that it can take positive, zero, and
ID: 3839205 • Letter: J
Question
JAVA OOP Thank you
Modify Assignment #10 so that it can take positive, zero, and negative floating point numbers and calculate the mean and standard deviation. Input ends when the user enters a string that can not be interpreted as a floating point number. Use exception handling to detect improper inputs. This was my assignment 10 code
Paper ViewPlain Text View
import java.util.Scanner;
public class DataSet {
static double value;
static double count ;
static double sum;
static double sumOfSquares;
public DataSet(double value, double count, double sum, double sumOfSquares){
this.value=value;
this.count=count;
this.sum=sum;
this.sum=sumOfSquares;
}
public void addValue(double value){
count++;
sum+=value;
sumOfSquares+=value*value;
}
public static double getAverage(double sum, double count){
return sum/count;
}
public static double getStandardDeviation(double someOfSquares){
return Math.sqrt((count*sumOfSquares-sum*sum)/(count*(count-1)));
}
public static void main(String[] args){
DataSet d=new DataSet(value, count, sum, sumOfSquares);
Scanner sc=new Scanner(System.in);
System.out.print("Enter Data :");
while((value=sc.nextDouble())>0){
d.addValue(value);
}
System.out.println("Average is :"+getAverage(sum,count));
System.out.println("Standard Deviation :"+getStandardDeviation(sumOfSquares));
}
}
Explanation / Answer
HI below is your code: -
package airpoty;
import java.util.InputMismatchException;
import java.util.Scanner;
public class DataSet {
static double value;
static double count;
static double sum;
static double sumOfSquares;
public DataSet(double value, double count, double sum, double sumOfSquares) {
this.value = value;
this.count = count;
this.sum = sum;
this.sum = sumOfSquares;
}
public void addValue(double value) {
count++;
sum += value;
sumOfSquares += value * value;
}
public static double getAverage(double sum, double count) {
return sum / count;
}
public static double getStandardDeviation(double someOfSquares) {
return Math.sqrt((count * sumOfSquares - sum * sum) / (count * (count - 1)));
}
public static void main(String[] args) {
DataSet d = new DataSet(value, count, sum, sumOfSquares);
Scanner sc = new Scanner(System.in);
System.out.print("Enter Data :");
boolean loop = true;
while (loop == true) { // running the loop till a NotANumber or string is entered
try{
value = sc.nextDouble();
d.addValue(value);
}catch (InputMismatchException e) { // catch block to catch string
loop = false;
break;
}
}
sc.close();
System.out.println("Average is :" + getAverage(sum, count));
System.out.println("Standard Deviation :" + getStandardDeviation(sumOfSquares));
}
}
Sample Run;-
Enter Data :-34.3
43
0
32.32
3.42
-323
End
Average is :-46.42666666666667
Standard Deviation :138.18694332919688
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.