Question

This is the assignment..... Write a class DataSet that stores a number of values of type...

This is the assignment.....

Write a class DataSet that stores a number of values of type double. Provide a constructor

public DataSet(int maxNumberOfValues)

and a method

public void addValue(double value)

that add a value provided there is still room.

Provide methods to compute the sum, average, maximum and minimum value.

​This is what I have, its suppose to be using arrays also the double smallest = Double.MAX_VALUE; and double largest = Double.MIN_VALUE;​ are there so I don't need to create an element. I need to use enhanced for loop, not if else statments. thank you

import java.util.Scanner;
public class DataSet1 {
    double[]values;
    int sum;
    double average;
    double maximum;
    double minimum;
    public DataSet1(int maxNumberOfValues){
        values = new double[maxNumberOfValues];
}
public void addValue(int[]values){
    for(int i=0; i<values.length;i++){
        sum+= values[i];
    }
}
public double getSum(double[] values){
    for(int i=0; i<values.length;i++){
        sum+= values[i];
}
    return sum;
}
public double getAverage(){
double total =0;
for(double element : values){
total = total + element;
}
double average = 0;
if(values.length>0){
average = total/values.length;
}
return sum/values.length;
}
public double getMaximum(){
     double largest = Double.MIN_VALUE;
     for(int i = 1;i<values.length;i++){
     if(values[i]<largest)
     largest=values[i];
    }
    return Math.max(largest, largest);
}
public double getMinimum(){
    double smallest = Double.MAX_VALUE;
    for(int i =0;i<values.length;i++){
    if(values[i]<smallest)
    smallest = values[i];
    }
    return Math.min(smallest, smallest);
}

public static void main(String[] args){
final int LENGTH = 100;
double[]values = new double[LENGTH];
DataSet1 d1 = new DataSet1(LENGTH);
int currentSize =0;   
Scanner in=new Scanner(System.in);
System.out.print("Enter number : ");
while(in.hasNextDouble()){
    values[currentSize]=in.nextDouble();
        currentSize++;
}

        System.out.println("Sum is : "+d1.getSum(values));
        System.out.println("Average is :"+d1.getAverage());
        System.out.println("Maximum is :"+d1.getMaximum());
        System.out.println("Minimum is :"+d1.getMinimum());
}   
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA :

import java.util.*;

import java.util.Arrays;

class DataSet1 {

double[] values;
int sum;
double average;
double maximum;
double minimum;

public DataSet1(int maxNumberOfValues) {
values = new double[maxNumberOfValues];
}

public void addValue(int[] values) {
for (int i = 0; i < values.length; i++) {
sum += values[i];
}
}

public double getSum(double[] values) {
sum = 0;
for (int i = 0; i < values.length; i++) {
sum += values[i];
}
return sum;
}

public double getAverage() {
double total = 0;
for (double element : values) {
total = total + element;
}
double average = 0;
if (values.length > 0) {
average = total / values.length;
}
return sum / values.length;
}

public double getMaximum() {
double largest = Double.MIN_VALUE;
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
largest = Math.max(largest, values[i]);
}
return largest;
}

public double getMinimum() {
double smallest = Double.MAX_VALUE;
for (int i = 0; i < values.length; i++) {
smallest = Math.min(smallest, values[i]);
}
return smallest;
}

public static void main(String[] args) {
final int LENGTH = 100;
int currentSize = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter number : ");
int size = in.nextInt();
DataSet1 d1 = new DataSet1(size);
  
for(int i=0;i<size;i++){
d1.values[currentSize] = in.nextDouble();
currentSize++;
}
// d1.values = values;
System.out.println("Sum is : " + d1.getSum(d1.values));
System.out.println("Average is :" + d1.getAverage());
System.out.println("Maximum is :" + d1.getMaximum());
System.out.println("Minimum is :" + d1.getMinimum());
}
}

OUTPUT:

Enter number : 5
2
1
3
5
4
Sum is : 15.0
Average is :3.0
2.0
1.0
3.0
5.0
4.0
Maximum is :5.0
Minimum is :1.0

Add a comment
Know the answer?
Add Answer to:
This is the assignment..... Write a class DataSet that stores a number of values of type...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I keep getting an Error after I ask the user for test scores. What is missing?...

    I keep getting an Error after I ask the user for test scores. What is missing? package testscores; import java.util.Scanner; public class TestScores { /** * @param args the command line arguments */ private double[] scores;    public TestScores(double[] score) throws IllegalArgumentException { scores = new double[scores.length]; for (int i = 0; i < scores.length; i++) { if (score[i] < 0 || score[i] > 100){ throw new IllegalArgumentException(); } scores[i]=score[i];    }    } public double getAverage() { int sum=0;...

  • Consider the following classes: Dollhouse, Legos, TeddyBear, Toy, ToyBox Which should not be included in a...

    Consider the following classes: Dollhouse, Legos, TeddyBear, Toy, ToyBox Which should not be included in a class hierarchy?    ToyBox     Dollhouse     TeddyBear     Legos     Toy __________________ Questions 6 - 9 Refer to the following code: public class WhatsIt { private int[] values; private double average; public WhatsIt () { values = new int[10]; findAvg(); } public WhatsIt (int[] n) { values = n; findAvg(); } public void findAvg () { double sum = 0; for (int i...

  • USE JAVA Using recursion, find the largest element in an array. Skeleton code is provided. Please...

    USE JAVA Using recursion, find the largest element in an array. Skeleton code is provided. Please do not change the DataSetDemo code. Hint: Find the largest element in the subset containing all but the last element. Then compare that maximum to the value of the last element. Skeleton Code: DataSet: /** Computes the maximum of a set of data values. */ public class DataSet { private int[] values; private int first; private int last; /** Constructs a DataSet object. @param...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right...

    Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right corner to the bottom left corner and i dont know how to do it please help me thank you dd another method to the bottom of the "TwoDimArraysMethods.java" file called "printDiagonalRL()"                                         public static void printDiagonalRL(int[][] matrix) 4. Call this method in the main file ("TwoDimArraysAsParam.java") passing it "board." e.g. TwoDimArraysMethods.printDiagonal(board); 5. Your new method should print any numbers along the diagonal from...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT