Question

In: Computer Science

Create a new Java file, containing this code public class DataStatsUser { public static void main...

Create a new Java file, containing this code

public class DataStatsUser {
    public static void main (String[] args) { 
      DataStats d = new DataStats(6); 
      d.append(1.1);
      d.append(2.1);
      d.append(3.1);
      System.out.println("final so far is: " + d.mean()); d.append(4.1);
      d.append(5.1);
      d.append(6.1);
      System.out.println("final mean is: " + d.mean());
    } 
  }

This code depends on a class called DataStats, with the following API:

public class DataStats {
    public DataStats(int N) {
    }
       // set up an array (to accept up to N doubles) and other member variables
    public double mean() {
        return 0;
    }
       //compute and return the mean of the set of numbers added so far
    public void append(double in) {
    }
       //append number to the set; throw error if more than N numbers added
 }

Your job: implement DataStats, so that it correctly works when used by DataStatsUser.

Solutions

Expert Solution

Below is the solution:

import java.util.ArrayList;

public class DataStatsUser {
   public static void main(String[] args) {
       DataStats d = new DataStats(6);
       d.append(1.1);
       d.append(2.1);
       d.append(3.1);
       System.out.println("final so far is: " + d.mean());
       d.append(4.1);
       d.append(5.1);
       d.append(6.1);
       System.out.println("final mean is: " + d.mean());
   }
}

public class DataStats {
   ArrayList<Double> arrlist; //declare arraylist

   public DataStats(int N) {
       arrlist = new ArrayList<Double>(N); //declare the size of arraylist
   }

   // set up an array (to accept up to N doubles) and other member variables
   public double mean() {
       int sum = 0, i;
       for (i = 0; i < arrlist.size(); i++) { //iterate through loop
           sum += arrlist.get(i); //sum of all the arraylist
       }
       double mean = (float)sum / arrlist.size(); //calculate the mean
       return mean;
   }

   // compute and return the mean of the set of numbers added so far
   public void append(double in) {
       arrlist.add(in); //append the arraylist
   }
   // append number to the set; throw error if more than N numbers added
}

sample output:

final so far is: 2.0
final mean is: 3.5


Related Solutions

Task 2/2: Java program Based upon the following code: public class Main {   public static void...
Task 2/2: Java program Based upon the following code: public class Main {   public static void main( String[] args ) {     String alphabet = "ABCDEFGHIJKLMNMLKJIHGFEDCBA";     for( <TODO> ; <TODO> ; <TODO> ) {       <TODO>;     } // Closing for loop   } // Closing main() } // Closing class main() Write an appropriate loop definition and in-loop behavior to determine if the alphabet string is a palindrome or not. A palindrome is defined as a string (or more generally, a token) which...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer,...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer, String>(); ssnMap.put (8675309,"Jenney"); ssnMap.put (42, "Answer to Everything"); ssnMap.put (8675309, "Stacy"); ssnMap.put (1006, "Peter"); System.out.println(ssnMap.get (8675309)); } } What is the output of the above code. Why?
public class StackTest { public static void main(String[] args) { StackX theStack = new StackX(10); //...
public class StackTest { public static void main(String[] args) { StackX theStack = new StackX(10); // make new stack theStack.push(20); // push items onto stack theStack.push(30); theStack.push(40); theStack.push(40); theStack.push(60); theStack.push(80); theStack.showStack(); System.out.println("removeDownTo(40)"); theStack.removeDownTo(40); theStack.showStack(); } // end main() } public class QueueTest { public static void main(String[] args) { Queue theQueue = new Queue(20); // queue holds 5 items theQueue.insert(10); // insert 4 items theQueue.insert(20); theQueue.insert(30); theQueue.insert(40); theQueue.showQueue(); System.out.println("Removing 3 items"); theQueue.remove(); // remove 3 items theQueue.remove(); // (10, 20,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT