Answered step by step
Verified Expert Solution
Question
1 Approved Answer
2. Write a Java program that consists of a definition for a public class called MetLog. This should have a field called data of type
2. Write a Java program that consists of a definition for a public class called MetLog. This should have a field called data of type int[], a field called index of type int, and a field called name of type String. There should be a constructor that takes an int called n, and a String called name. The constructor should set the data field to be an int array of size n. It should also set the name field to be the same as the name parameter. Finally, it should set the index value to -1. A void method called add should also be written. This should take an int called val. The method should begin by incrementing the index field. If this causes the index to be greater than or equal to the length of the data array, it should be reset to 0. The val parameter should then be assigned to the slot of the array accessed by the index. A method of type double called average should also be added. This takes an int called n, which defines the number of elements of the array which are to be averaged. The method should use a loop to add up the n most recently added elements of the array before returning the average (i.e., the total divided by the n). Finally, a public String method called toString should be added (to control printing) which constructs and returns a String consisting of the text Average NAME' (where NAME is the value of name) followed by the average of all values currently in the array (including any default values). [50 marks]
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