Question
Here will we again build some methods, but in a slightly different context, to help build the programming links between the code and your conception
Here will we again build some methods, but in a slightly different context, to help build the programming links between the code and your conception of it, rather than just understanding how a bank account should work.
In the skeleton you have a class called SummerStatic. The class has two data members sum and count. You do not need to do anything to these declarations. The purpose of the class is to receive numbers one by one, and update the current sum and count, and thus be able to report on the sum, average and count. The sequence can also be reset if desired.
It contains one completed method: a main method which runs at least one example of each method, you can change the main as much as you like, as long as it compiles, it will not affect the marking.
Your task is to complete the following six methods:
-
putNumber
-
reset
-
a different reset
-
getSum
-
getCount
-
getAverage
You should read the description of each method in the associated comment block carefully as it describes the purpose of each method, and what needs to be completed for the method.
/** * This "Summer" class is a simple introduction to * methods and variables / fields / private data members. * * @author Raymond Lister * @version April 2015; a "static" version for a Lab Test */
public class SummerStatic { /* * The variables follow. These are also * called "fields" or "private data members". */
private static int sum; // sum of all integers received private static int count; // number of integers received public static void main(String [] args) { /* * Note: This method "main" has been provided in * its entirety. You do NOT have to make any * changes to it. * * You could use this "main" method to test your code. */ System.out.println("First we call reset so that th evalues of sum and count are set to zero."); reset(); System.out.println("Now we assign numbers 17 and 1."); putNumber(17); putNumber(1); System.out.println("Now we call getCount, getSum and getAverage in three print statements to check the values."); System.out.println("count should be 2, sum should be 18 and average should be 9.00:"); System.out.println("Now we call getCount, getSum and getAverage in three print statements to check the values."); System.out.println(""); System.out.println(""); System.out.print("count = " + getCount() + " "); System.out.print("sum = " + getSum() + " "); System.out.println("average = " + getAverage()); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); // Repeat for a second set of numbers System.out.println("We run the program again, this time we start by calling reset(3)"); reset(3); System.out.println("Now we assign numbers 5 and 7."); putNumber(5); putNumber(7); System.out.println("Now we call getCount, getSum and getAverage in three print statements to check the values."); System.out.println("count should be 3, sum should be 15 and average should be 5.00:"); System.out.println(""); System.out.println(""); System.out.print("count = " + getCount() + " "); System.out.print("sum = " + getSum() + " "); System.out.println("average = " + getAverage()); } // method main /** * Receives and processes a new number in the series * This method needs to be completed. It needs: * - a proper return type * - a proper list of parameters (maybe empty) * - code to actually do what it should */ public static returntype putNumber(parametersIfAny) { add whatever code is required here in the method body }
/** * Resets so all numbers previously forgotten. This is * dangerous since average is now undefined. * This method needs to be completed. It needs: * - a proper return type * - a proper list of parameters (maybe empty) * - code to actually do what it should */ public static returntype reset(parametersIfAny) { add whatever code is required here in the method body } /** * Resets with the first number of a new series. This is * safer reset since the average remains defined. * This method needs to be completed. It needs: * - a proper return type * - a proper list of parameters (maybe empty) * - code to actually do what it should */ public static returntype reset(parametersIfAny) { add whatever code is required here in the method body } /** * This method needs to be completed. It needs: * - a proper return type * - a proper list of parameters (maybe empty) * - code to actually do what it should */ public static returntype getSum(parametersIfAny) { add whatever code is required here in the method body } /** * This method needs to be completed. It needs: * - a proper return type * - a proper list of parameters (maybe empty) * - code to actually do what it should */ public static returntype getCount(parametersIfAny) { add whatever code is required here in the method body }
/** * This method needs to be completed. It needs: * - a proper return type * - a proper list of parameters (maybe empty) */ public static returntype getAverage(parametersIfAny) { /* * Note: This body of this method "getAverage" has * been provided in its entirety. You do NOT have * to make any changes to it. */
return ( (double) sum / (double) count ); } } // class SummerStatic
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started