Question

In: Computer Science

Write a method that, given an array of grades stored as double values, computes and return...

Write a method that, given an array of grades stored as double values, computes and return their mean.
   Write another method that will return an array of the mean grade obtained by an arbitrary number of student.
   This method will take a 2-dimensional array of double values in which each row corresponds to the grade vector
   for a different student. You can compute the mean for each row as you did before...
   Once you are done, be [good] lazy and just reuse your previous method.

   If you are looking for yet another practice opportunity, what about a method that, given the whole matrix,
   would bump up a specific grade of a specific student by a given number of points?
   Signature of this method is not provided below.

*/
public class TinkeringWithArraysLive {
   public static void main(String[] args){
double[] s1 = {90.1, 80.2, 70.3, 30.4, 99.5};
System.out.println("Computing average of grades " + Arrays.toString(s1) + " result is " + mean(s1));

double[][] all = { {50.0, 60.0, 52.0, 58.0, 55.0},
   {60.0, 70.0, 62.0, 68.0, 65.0},
   {70.0, 80.0, 72.0, 78.0, 75.0}
   };
  
System.out.println("Averages for all students are " + Arrays.toString(mean(all)));
   } // end main
  
   public static double mean(double[] grades){
return 0.0;
   }// end method mean for vector of grades
  
   public static double[] mean(double[][] grades){
double[] result = new double[grades.length];
return result;
   }// end method mean for matrix of grades
} // end class

Solutions

Expert Solution

Code:

import java.util.Arrays;

public class TinkeringWithArraysLive {
public static void main(String[] args)
{
   double[] s1 = {90.1, 80.2, 70.3, 30.4, 99.5};
   System.out.println("Computing average of grades " + Arrays.toString(s1) + " result is " + mean(s1));

   double[][] all = { {50.0, 60.0, 52.0, 58.0, 55.0},
   {60.0, 70.0, 62.0, 68.0, 65.0},
   {70.0, 80.0, 72.0, 78.0, 75.0}
   };
  
   System.out.println("Averages for all students are " + Arrays.toString(mean(all)));
} // end main
  
public static double mean(double[] grades)
{
   double total = 0.0;   
   for(int i = 0;i < grades.length;i++)
   {
       total += grades[i];
   }
   return total/grades.length;
}// end method mean for vector of grades
  
public static double[] mean(double[][] grades)
{
   double[] result = new double[grades.length];
   for(int j = 0;j < grades.length;j++)
   {
       double total = 0.0;
       for(int i = 0;i < grades[j].length;i++)
       {
           total += grades[j][i];
       }
       result[j] = total/grades[j].length;
   }
  
  
   return result;
}// end method mean for matrix of grades
} // end class

OUTPUT:


Related Solutions

Write a function that will take in an array (of type double), and will return the...
Write a function that will take in an array (of type double), and will return the array with all of the elements doubled. You must use pass-by-reference and addressing and/or pointers to accomplish this task. C++
Assume that you have an array of 100 elements representing the grades students are stored in...
Assume that you have an array of 100 elements representing the grades students are stored in memory. Suppose the grades are in IEEE single precision format. Write a MIPS program to compute the average of the students grades and store the result in register $f0. Assume the array base address is stored in register $s0 and a floating point value of 100 is stored in memory with it address given in register $s2.
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
Write a Java method that computes the future investment value at a given interest rate for...
Write a Java method that computes the future investment value at a given interest rate for a specified number of years. Your method should return the future investment value after calculation. The future investment is determined using the formula below:     futureInvestmentValue = investmentAmount * (1+monthlyInterestRate)numberOfYears*12 You can use the following method header: public static double futureInvestmentValue (double investmentAmount, double monthlyInterestRate, int years);
Write a method named area with one double parameter name radius. The method needs to return...
Write a method named area with one double parameter name radius. The method needs to return a double value that represents the area of a circle. If the parameter radius is negative , then return -1.0 to present an invalid value Write another overload method with 2 parameters side1 and side2 (both double) where side1 and side2 represent the sides of a rectangle. The method needs to return an area of a rectangle . If either or both parameters is/...
Write a method named area with one double parameter name radius, the method needs to return...
Write a method named area with one double parameter name radius, the method needs to return a double value that represents the area of a circle. If the parameter radius is negative, then return -1.0 to represents an invalid value write another overload method with 2 parameter side 1 and side 2 (double) where side 1 and side 2 represents the sides of a rectangle. The method needs to return an area of a rectangle. If either or both parameters...
Using the implementation of the array based list given, write a CLIENT method (that is NOT...
Using the implementation of the array based list given, write a CLIENT method (that is NOT part of the class) called replace, that given a value and a position replaces the value of the element at that position. REMEMBER error checking public static void replace( List aList, int newValue, int position) public class ArraybasedList implements MyListInterface{ private static final int DEFAULTSIZE = 25; // Data members: private int currentSize; private int maxSize; private S[] elements; //default constructor has NO parameters...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
//   Given an array of size 9, with the values of 1-9, determine if the array...
//   Given an array of size 9, with the values of 1-9, determine if the array //   is valid or not. //   Display a message stating the row is VALId, or state its INVALID and what //   was the index number that determined the data invalid. // //   Use this java code as a start of your code. //   Test the array data by changing the values. //============================================================================= import java.util.*;    public class Soduko_ValidateRow    { public static void main(String...
Given an array, return the element at which the "pattern" breaks. For example, in the array...
Given an array, return the element at which the "pattern" breaks. For example, in the array [2,4,6,8,11,13,15,17], the algorithm should return 11 because the difference between every previous node was 2, and the difference between 8 and 11 is 3. PLEASE USE DIVIDE AND CONQUER. Thank you. Python please or just a description of the algorithm
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT