Introduction In this lab, we continue working on classes and methods. The work is based on the previous lab, the Stat class. You will use existing code that you should have made since last lab and add or modify codes to the Stat class. You will practice on implementing method overloading and design a few algorithms to operate on the arrays. Since this lab assignment relies on the work from the previous lab. You must ensure that the code from the previous assignment works. You are not allowed to use any code that was not made by yourself to work on this lab. Moreover, you should not provide any of your code from the previous assignment to any other student. Lab objectives After completing this lab, you should be able to create classes and gain further experience by utilizing: (1) constructors, (2) access modifiers, (3) instance variables, (4) general methods with different return types, (5) methods calling another method, (6) specific accessor and mutator methods, (7) different kinds of overloading methods, and (8) one-dimensional arrays of various data types. Assignment During method calls, automatic type conversion of primitive data types does not apply to arrays of primitive data types. We understand that an array with a narrower ranged type of data will not automatically convert to a wider ranged type of data, e.g. from int to double. Moreover, automatic type conversion can be performed on individual elements of an array, but not on the array itself. By considering this background, you will add some methods that specifically deal with certain type of data in an array. Additionally, you will also write some methods to operate on an array and to obtain additional statistical data from the array.
Image transcription text
Sometimes, we may handle methods invoked with null as a parameter. In this assignment, you will modify several methods to check that value passed to the methods is not null. We will ensure the robustness of the program so that all calculations are performed on some values, not on "nothing", which could trigger errors. In this lab, you will modify the existing code from Stat. jova that deals with additional statistics of data, including variance and standard deviation. Details are described as follows: 1. Use the UML diagram and method descriptions below to modify your Stat class. Numbers in front of the methods in the class diagram are not part of the code. They are for explaining the program requirements. Stat data: double [ ] 1. stat ( ) + Stat (double d) 3. Stat (float f) Stat (int i) 5. Stat (long 10) 6. setData (float f): void 7. setData (double d) : void 8. setData (int i) : void 9. setData (long lo] : void 10. + getData () : double 11. equals (Stat s) : boolean 12. reset () : void 13. append (int i) : void 14. + append (float f) : void 15. + append (long lo) : void 16. append (double d) : void 17. isEmpty () : boolean 18 toString () : String 19 min () : double 20. max () : double 21. average () : double 22. mode () : double 23. occursNumberOfTimes (double value] : int 24. variance () : double 25 standardDeviation () : double Texts in red will be the methods that you need to add Into the existing class. Texts In black are existing methods that you should have made since last lab, but they may need to be modified according to the new program need. Recall the previous lab, Stat class stores an array of double type values called data (private
Image transcription text
the array is store. However, data does not store the array. 2. After you have created the skeleton code of Stat class according the UML class diagram, Implement the detail of the methods. . (1) Stat() This is the default constructor for the class Stat. It should create a double array having a length 0. It is possible to create an array of length 0 in Java. Consider having a variable to hold a reference to such an array of length 0 (though another way is to assign null to that variable). Clearly, you will modify the method of Stat class that you did since last time to reflect the fact that a zero-length array is created. =-= Hint =-= Pay attention to the difference between these three kinds of arrays: . An array with null property means "there is a so-called array of no-array". For example, int array = null; . An empty array means "there is an array with no element". For example, int array – new int[0]; . An array with null values means "there is an array but its elements have no value(yet)". For example, String array = new String[50]; // if no value will be assigned later . (2, 3, 4, 5) Stat(double d), Stat(int I), Stat[long lo), Stat(float f) These are all constructors for the class Stat. By using the constructor, the program will create a double array of the same length as the parameter array and holding copies of its values. A reference to the new array should be assigned to data. If the parameter is null, an empty array will be assigned to data. (6, 7, 8, 9) setData(double d), setData(int [), setData(long lo), setData(float () These are the set methods (mutator). They are used to set the values of the data array. If the array used as parameter is not null, the method should create a new array containing exactly the elements of d and assign to data a reference to this new array. If the parameter is null, an empty array should be assigned to data. (10) getData() This method should be left unchanged from the previous lab. The method should be able to handle empty array (i.e. the array length is 0). (11) equals(Stat s) This method should be left unchanged from the previous lab. The method returns the boolean value true if the data objects of both the calling Stat object and the passing Stat
Image transcription text
object s have all the same values in the same order, or false otherwise. If the parameter s is null, the
method returns false. (12) reset() This method clears the data array. A new empty double array will be
created and assigned to data. (13, 14, 15, 16) append(double d), append(int 1), append(long l…
Image transcription text
. (23) occursNumberOfTimes(double value) This method returns the number of times the value occurs in the data array. This is private method for the mode() method. It is very likely that the mode() method that you have written contains many lines of code. A part of the mode() method includes the processing of getting the number of times such a value occurs in the array. Therefore, It is better to make code decomposition so that each method is handling one specific task. This method implementation is optional. . (24) variance() This method returns the variance of the data in the data array. We define variance as the sum of [squared (difference between each element and the average)], divided by the total number of elements. If the data array is empty, the method returns Double.NaN. For example, if there is an array of elements (1.0, 3.0, 5.0). The difference between each element and the average (which is 3.0) is 2.0, 0.0, -2.0. Then the squared values of these three differences are 4.0, 0, 4.0. Next, the sum of these squared values is 4.0+0+4.0 = 8.0. This sum is divided by the total number of elements (which is 3). The result will be 8.0 / 3 = 2.6667. (25) standardDeviation() This method returns the square root of the variance. If the data array is empty, the method returns Double. NaN. 3. After you have completed all above steps for modifying the Stat class, write a main method in a separate driver class to test each public method of the Stat class. For each method, there should be several test cases to use. Refer to the sample program outputs for select test cases. It is very important that you thoroughly test your program by using multiple different types of test cases. For example (example only! Not a comprehensive list!), you should have arrays of different data types to test if method overloading works. You should also have arrays with 0, 1, 2, and multiple elements to test if the program can process them correctly. You should consider how arrays are connected using append() method, when they have different lengths (including zero-length arrays) and with different data types. It is very important that you are NOT allowed to use any methods from java.util.Arrays or any Java Stream APIs throughout the entire program code. 4. Once you make the program working correctly. Understand the following statement for Academic Honesty and add it into the top of your source code to submit. The following lines
Image transcription text
Below are sample inputs/outputs. This is not a comprehensive list of test cases. Example 1 Example main method: double[ ] datal = (1: Stat stall = new Stat[datal); System.out.printing"stall data = " + stall.toString()); System.our.printIn("stall min – "+ statl.min()); System out.printin("stat) max = " + stat l.max()); System.out.printIn("stall average – " + stall.average()); System out printin("stat] mode = " + statl.mode()); System out-printIn["statl variance = * + stall. variance()); System.our.printin("statl standard deviation – "+ stat l standardDeviation()); System.out.printin("statI is empty = "+stall.isEmpty() + "in]: Example output stall data = [ statl min – NON stall max = NoN catl average = NAN stall mode – NaN mail variance = NIN stat I standard deviation – NON statl is empty = true Example 1 Example main method: double[ ] datal – ( 1, 2, 3, 4, 5, 6, 7. 8.9 ); Stat stall – new Standatal); System.out.printin("statl data = "+ stall,toString(); Syrier.our printin("stat] min = " + statl min()); System our printin( "stall max -" + stall max(): System.our printin("stall average ="+statl.average()); System.our.printlo["stat I mode = " + stall mode(); System out printlo("stall variance = " + stat l. variance()]. System.our printlo("stall standard deviation ="+ stat ! standardDeviation()K System our printin("stall is empty – " + statl isEmpty() + "\"); Ball reset(): System.out println("stall data – " + atsil,inString(); System out printing"stall min – " + stall min(Il System our printin("stall max -" + stall.max(); System.out.printin(‘stall average = " + stall.average(); System out printIn("stat ! mode " + stall mode(). System out printin("stall variance – " + stall,varianceOj System our printin("stall standard deviation – " + stall standardDeviation(). System out printin( "stall is empty -" + stall.isEmpty( + ".");
Image transcription text
Systemn.out. printin["statl standard deviation =" + stat 1 standardDeviation()); System.out.printin("stall is empty = " + statl,isEmpty( # "); Example output stall data = [ stall min = NaN stall max – NaN statl average – NIN stall mode – NaN stall variance – NIN stall standard deviation – NaN stall is empty = true Example 2 Example mah method: double datal = ( 1, 2, 3, 4, 5, 6, 7, 8, 9 ); Stat statl – new Stat(datal); System out.printIn("starl data -" + statl.toString()); System our printin(‘stail min -" +statl.min(): System.out.printin("stall max = " + stail.max(); System.out printin("stall average – "+statl,average()); System.our printin("statl mode -" + statl.mode()); System,our printin("statl variance = "+ statl.variance()); System out printla("statl standard deviation – " + statl standardDeviation()); System.our printin("stat] is empty = " + stath isEmpty() + "in"); stall reset(): System out.printin("stall data – " + statl toString(); System out printIn("statl min – "+ statl.min(); System.our.printin("statl max – " + statl .max()); System.our.printin("statl average = "+ stat L.average(). System.our printin("statl mode -"+statl.mode()) System.our printin("statl variance " "+statl.variance()): System.our printle("statl standard deviation -" + stall.standardDeviation()); System our printin("statl is empty -" + statl,isEmpty() + "In7); Example curt stall data – [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] statl min – 1.0 stath max – 9.0 statl average = 5.0 atall mode = NaN stall variance = 6,566665665666657 statI standard deviation – 2.581986897471611 tail is empty = false stall data = Hall min – NEN stall max – NEN Hall average = NaN Hall mode = NoN Rail variance – NaN stat I Mandard deviation – NaN stat ! is emply = true Example 1
plz help and this the code that I need to edit along with
public class Stat{
private double[] data;
public Stat() {
data = new double[1];
data[0] = 0.0;
}
public Stat (double[] d){
this.data = new double[d.length];
for(int o = 0; o < d.length; o ++) {
this.data[o] = d[o];
}
}
public void setData(double [] d){
this.data = new double[d.length];
for(int o = 0; o < d.length; o ++) {
data[o] = d[o];
}
}
public double[] getData(){
double[] d = new double[this.data.length];
for(int o = 0; o < d.length; o ++) {
d[o] = data[o];
}
return d;
}
public boolean equals(Stat s){
boolean onGoing = false;
if(s.getData().length == getData().length) {
onGoing = true;
for(int o = 0; o < getData().length && onGoing; o++) {
if(Math.abs(s.data[o] – this.data[o]) > 0.001)
onGoing = false;
}
}
return onGoing;
}
public String toString() {
String dataDatas = “”;
for(int o = 0; o < data.length; o++) {
if(o == data.length – 1){
dataDatas += data[o];
}else
dataDatas += data[o] + “, “;
}
return “[” + dataDatas + “]”;
}
public double min(){
double min = data[0];
for(int o = 0; o < data.length; o++) {
if(data[o] < min)
min = data[o];
}
return min;
}
public double max(){
double max = data[0];
for(int o = 0; o < data.length; o++) {
if(data[o] > max)
max = data[o];
}
return max;
}
public double average() {
double average = 0;
for(int o = 0; o < this.data.length; o++) {
average += this.data[o];
}
average /= (this.data.length);
return average;
}
public double mode() {
double mode = data[0];
int numberInAll = 0,
numberIt = 0,
specialNumnum = 0;
for(int o = 0; o < this.data.length; o++) {
numberIt = 0;
for(int f = 0; f < this.data.length; f++) {
if (Math.abs(this.data[o] – data[f]) < 0.000000001) {