Lab 11: Classes and methods 3 Introduction In this lab, we continue working on classes and...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Lab 11: Classes and methods 3 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. stat1 data = [] Example 5 Example main method: double[] datal (50.0, 60.0}; float[] data2 (70.0F, 80.0F}; int[] data3 = {90, 100}; long data4 (100L, 110L); Stat statl new Stat(); System.out.println("stat1 data="+ stat1.toString()); stat1.append(data1); System.out.println("stat1 data="+ statl.toString()); stat1.append(data2); System.out.println("statl data="+ statl.toString()); stat1.append(data3); System.out.println("stat1 data="+stat1.toString()); stat1.append(data4); System.out.println("statl data=" + statl.toString()); data1 = null; stat1.append(data1); System.out.println("statl data="+ statl.toString()); System.out.println("statl min="+stat1.min(); System.out.println("statl max="+ stat1.max()); System.out.println("statl average=" + statl average()); System.out.println("statl mode="+stat1.mode()); System.out.println("statl variance="+stat1.variance()); System.out.println("statl standard deviation=" + statl standard Deviation() + "\n"); Example output: stat1 data=[] stat1 data =[50.0, 60.0] stat1 data [50.0, 60.0, 70.0, 80.0] stat1 data [50.0, 60.0, 70.0, 80.0, 90.0, 100.0] stat1 data =[50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 100.0, 110.0] = statl data [50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 100.0, 110.0] stat1 min =50.0 stat1 max 110.0 stat1 average -82.5 stat1 mode 100.0 stat1 variance 393.75 stat1 standard deviation 19.84313483298443 Example 6 Example main method: double[] data1 = {10,10}; int[] data2 = {10,10}; Stat stat1 = new Stat(data1); Stat stat2 new Stat(data2); Stat stat3 new Stat(); Stat stat4 null; System.out.println("statl data="+stat1.toString()); System.out.println("stat2 data="+stat2.toString(); System.out.println("stat2 data="+ stat2.toString(); System.out.println("statl equals stat2="+ statl.equals(stat2)); System.out.println("statl equals stat3="+ statl.equals(stat3)); System.out.println("statl equals stat4="+ stat1.equals(stat4)); Example output: = stat1 data [10.0, 10.0] stat2 data = [10.0, 10.0] stat2 data [10.0, 10.0] stat1 equals stat2=true stat1 equals stat3 = false stat1 equals stat4 false Example 7 Example main method: double[] data = {}; double[] data2 = {25}; float[] data3 = {}; float[] data4= {25}; int[] data5 = {}; int[] data6 = {50 }; long[] data 7 = {}; long[] data8 = {12}; Stat stat1 new Stat(); stat1.append(data1); stat1.append(data2); stat1.append(data3); stat1.append(data4); stat1.append(data5); stat1.append(data); stat1.append(data); stat1.append(data); data1 = null; stat1.append(data1); System.out.println("statl data="+ statl.toString()); System.out.println("stat1 min="+ stat1.min(); System.out.println("stat1 max="+stat1.max()); System.out.println("statl average="+stat1 average()); System.out.println("stat1 mode=" + stat1.mode()); System.out.println("statl variance=" + stat1.variance()); System.out.println("statl standard deviation=" + statl standardDeviation() + "\n"); Example output: stat1 data =[25.0, 25.0, 50.0, 12.0] stat1 min 12.0 stat1 max=50.0 stat1 average = 28.0 stat1 mode = 25.0 stat1 variance 189.5 stat1 standard deviation 13.765899897936205 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.java 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. data: double[] Stat 1. + Stat() 2. + Stat (double[] d) 3. + Stat (float [] f) 4. + Stat (int[] i) 5. Stat (long[] lo) 6. + setData(float [] f): void 7. + setData (double[] d): void 8. + setData (int[] i): void 9. + setData (long [] lo): void + getData() double[] 10. 11. 12. 13. 14. 15. 16. + equals (Stat s): boolean + reset() void + append(int[] i): void + append(float [] f): void + append (long[] lo): void + append (double[] d): void +isEmpty() boolean + toString(): String 17. 18. 19. + min() double 20. 21. 22. + max() double + average (): double + 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 type). The data has the type double[], which is a reference type. This means that data itself will store a reference to the memory location where 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[] i), setData(long[] lo), setData(float[] f) 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(Stats) 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 objects 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[] i), append(long[] lo), append(float[] f) These methods will create a new double array that contains exactly the elements of data, followed by those elements of the array passed as the parameter. A reference to this array will be assigned to data. If the parameter is null, the method will do nothing. See examples at the end of the document. ⚫ (17) isEmpty() This method returns a boolean value true if the data object is empty (i.e. such an array is an empty array, which has a length 0). Otherwise, it returns false. ⚫ (18) toString() As in the previous lab, this method returns a String of the data elements stored in the Stat object. Please look at the sample output at the end of the document for formatting. See how commas and square brackets are added. ⚫ (19) min() This method returns the minimum of the data array. If the array is empty, then the method returns Double.NaN. (20) max() This method returns the maximum of the data array. If the array is empty, then the method returns Double.NaN. ⚫ (21) average() This method returns the average of the data array. The average is a double type value as the mean value of a given array of numbers. If the array is empty, then the method returns Double.NaN. =-= Hint =-= Double.NaN is what the method will return. For example, return Double.NaN; NaN stands for "Not a Number". Treat it as "an undefined value" instead of "an error". ⚫ (22) mode() Most part of the implementation will be the same as from the previous lab. This method returns the value that appears most frequently in the array. If there is no such unique, or if the data array is empty, the method will return Double.NaN. (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 Replace [CLASS/FILE NAME] with the required name according to the assignment instruction. Replace [YOUR NAME] with your actual name. Submission instruction After you have completed and thoroughly tested your program, upload and submit the modified file Stat.java. Keep your previous Stat.java file that was made since last time elsewhere. You do not have to submit the driver class file that contains the main method. We will use different test cases to test your program. Grading (1) Points are AWARDED when your program passes test cases that are determined or chosen by the grader/grading program. Test cases include checking constructors (0.5 each * 5 = 2.5 points), set methods (0.6 each *4 = 2.4 points), append methods (0.6 each * 4 = 2.4 points), reset and isEmpty methods (0.3 point), checking statistical outputs with a null value (0.2 each * 6 = 1.2 points), outputs with a non-null value (0.2 each * 6 = 1.2 points). (2) Points are DEDUCTED when your program fails to meet certain requirements: ⚫(-1 point) If the source file(s)/class(es) are named incorrectly (case matters!) ⚫ (-1 point) If your source file(s) have a package declaration at the top ⚫(-1 point) If any source file you submit is missing your Statement of Academic Honesty at the top of the source file. All submitted source code files must contain your Statement of Academic Honesty at the top of each file. ⚫(-1 point) If you have more than one instance of Scanner in your program. Your program should only have one instance of Scanner. (-1.5 points) Inconsistent I/O (input/output) that does not match our instructions or examples exactly (unless otherwise stated in an assignment's instructions). Your program's 1/0 (order, wording, formatting, etc.) must match our examples and instructions. •If your (-1 point) comments or (-1 point) variables are "lacking". "Lacking" means that your grader can find any lines of code or variables that take more than 10 seconds to understand, and there is no comment, or the variable name does not make sense (variable names like b, bb, bbb, etc. will almost never be acceptable). ⚫(-1 point) Indentation is not consistent throughout your source code (e.g. you should NOT use a combination of tabs and spaces in the files.) (-10 points) If you use a non-standard Java library or class (StringBuilder class, Arrays class, any class in java.util.stream) or other code specifically disallowed by the lab/project. Special notice regarding the submission: A. Late submission penalty. Points will be deducted from the original grade. If your submission is after the posted deadline... (1) within 24 hours: -1 (2) between 24 hours and 48 hours: -2.5 (3) after 48 hours: assignment will not be accepted. B. If the source code file is missing, partially or completely broken, unable to open (including using a wrong file format), unable to compile, irrelevant to the assignment, purposely made to contain malicious codes, or determined to be an academic misrepresentation or a case of plagiarism, you will receive 0 grade for this assignment. Below are sample inputs/outputs. This is not a comprehensive list of test cases. Example 1 Example main method: double[] data1 = {}; Stat stat1 = new Stat(data1); System.out.println("statl data="+ statl.toString()); System.out.println("stat1 min="+stat1.min0);" System.out.println("stat1 max="+stat1.max()); System.out.println("statl average=" + stat1 average()); System.out.println("statl mode=" + stat1.mode()); System.out.println("statl variance=" + stat1.variance()); System.out.println("statl standard deviation="+stat1 standardDeviation()); System.out.println("statl is empty =" + stat1.isEmpty() + "\n"); Example output: stat1 data =[] stat1 min = NaN stat1 max = NaN statl average = NaN stat1 mode NaN stat1 variance NaN stat1 standard deviation = NaN stat1 is empty = true Example 2 Example main method: double[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9 }; Stat stat1 = new Stat(data1); System.out.println("statl data="+stat1.toString()); System.out.println("stat1 min = "+ stat1.min(); System.out.println("stat1 max="+stat1.max()); System.out.println("statl average="+stat1 average()); System.out.println("stat1 mode="+stat1.mode(); System.out.println("statl variance=" + stat1.variance()); System.out.println("statl standard deviation="+statl standard Deviation()); System.out.println("statl is empty =" + stat1.isEmpty() + "\n"); stat1.reset(); System.out.println("stat1 data="+stat1.toString(); System.out.println("statl min="+ stat1.min(); System.out.println("stat1 max=" + stat1.max()); System.out.println("stat1 average="+stat1 average()); System.out.println("statl mode=" + stat1.mode()); System.out.println("statl variance=" + stat1.variance()); System.out.println("statl standard deviation=" + statl standardDeviation()); System.out.println("statl is empty =" + stat1.isEmpty() + "\n"); Example output: stat1 data [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] stat1 min = 1.0 stat1 max 9.0 stat1 average = 5.0 stat1 mode = NaN stat1 variance 6.666666666666667 stat1 standard deviation=2.581988897471611 stat1 is empty = false stat1 data = [] stat1 min = NaN stat1 max = NaN stat1 average = NaN stat1 mode=NaN stat variance NaN stat1 standard deviation - NaN stat1 is empty = true Example 3 Example main method: float[] data1 = {10.0F, 10.0F}; Stat statl new Stat(data1); System.out.println("stat1 data="+stat1.toString()); System.out.println("stat1 min="+ stat1.min()); System.out.println("stat1 max="+stat1 max()); System.out.println("statl average =" + stat1 average()); System.out.println("statl mode="+stat1.mode()); System.out.println("statl variance="+stat1.variance()); System.out.println("statl standard deviation=" + stat1 standardDeviation() + "\n"); long[] data2 (80L, 60L}; stat1.append(data2); System.out.println("statl data="+ statl.toString()); System.out.println("statl min "+stat1 min); System.out.println("statl max="+ stat1.max()); System.out.println("statl average="+stat1 average()); System.out.println("stat1 mode="+stat1.mode()); System.out.println("statl variance=" + stat1.variance()); System.out.println("statl standard deviation=" + statl standard Deviation() + "\n"); Example output: stat1 data [10.0, 10.0] stat1 min = 10.0 stat1 max = 10.0 stat1 average 10.0 stat1 mode = 10.0 stat1 variance = 0.0 stat1 standard deviation = 0.0 stat1 data [10.0, 10.0, 80.0, 60.0] stat1 min 10.0 stat1 max=80.0 stat1 average 40.0 stat1 mode = 10.0 stat1 variance 950.0 stat1 standard deviation=-30.822070014844883 Example 4 Example main method: double[] data = {50.0, 60.0}; float[] data2 (70.0F, 80.0F}; int[] data3 (90, 100}; long[] data4 ={100L, 110L}; Stat statl new Stat); System.out.println("stat1 data="+stat1.toString()); stat1.setData(data1); System.out.println("stat1 data="+ statl.toString()); stat1.setData(data2); System.out.println("stat1 data="+stat1.toString(); stat1.setData(data3); System.out.println("stat1 data="+ statl.toString()); stat1.setData(data4); System.out.println("statl data="+stat1.toString()); data1 = null; stat 1.setData(data1); System.out.println("statl data="+ statl.toString()); Example output: stat1 data= stat1 data [50.0, 60.0] = stat1 data [70.0, 80.0] stat1 data [90.0, 100.0] stat1 data [100.0, 110.0] = Lab 11: Classes and methods 3 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. stat1 data = [] Example 5 Example main method: double[] datal (50.0, 60.0}; float[] data2 (70.0F, 80.0F}; int[] data3 = {90, 100}; long data4 (100L, 110L); Stat statl new Stat(); System.out.println("stat1 data="+ stat1.toString()); stat1.append(data1); System.out.println("stat1 data="+ statl.toString()); stat1.append(data2); System.out.println("statl data="+ statl.toString()); stat1.append(data3); System.out.println("stat1 data="+stat1.toString()); stat1.append(data4); System.out.println("statl data=" + statl.toString()); data1 = null; stat1.append(data1); System.out.println("statl data="+ statl.toString()); System.out.println("statl min="+stat1.min(); System.out.println("statl max="+ stat1.max()); System.out.println("statl average=" + statl average()); System.out.println("statl mode="+stat1.mode()); System.out.println("statl variance="+stat1.variance()); System.out.println("statl standard deviation=" + statl standard Deviation() + "\n"); Example output: stat1 data=[] stat1 data =[50.0, 60.0] stat1 data [50.0, 60.0, 70.0, 80.0] stat1 data [50.0, 60.0, 70.0, 80.0, 90.0, 100.0] stat1 data =[50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 100.0, 110.0] = statl data [50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 100.0, 110.0] stat1 min =50.0 stat1 max 110.0 stat1 average -82.5 stat1 mode 100.0 stat1 variance 393.75 stat1 standard deviation 19.84313483298443 Example 6 Example main method: double[] data1 = {10,10}; int[] data2 = {10,10}; Stat stat1 = new Stat(data1); Stat stat2 new Stat(data2); Stat stat3 new Stat(); Stat stat4 null; System.out.println("statl data="+stat1.toString()); System.out.println("stat2 data="+stat2.toString(); System.out.println("stat2 data="+ stat2.toString(); System.out.println("statl equals stat2="+ statl.equals(stat2)); System.out.println("statl equals stat3="+ statl.equals(stat3)); System.out.println("statl equals stat4="+ stat1.equals(stat4)); Example output: = stat1 data [10.0, 10.0] stat2 data = [10.0, 10.0] stat2 data [10.0, 10.0] stat1 equals stat2=true stat1 equals stat3 = false stat1 equals stat4 false Example 7 Example main method: double[] data = {}; double[] data2 = {25}; float[] data3 = {}; float[] data4= {25}; int[] data5 = {}; int[] data6 = {50 }; long[] data 7 = {}; long[] data8 = {12}; Stat stat1 new Stat(); stat1.append(data1); stat1.append(data2); stat1.append(data3); stat1.append(data4); stat1.append(data5); stat1.append(data); stat1.append(data); stat1.append(data); data1 = null; stat1.append(data1); System.out.println("statl data="+ statl.toString()); System.out.println("stat1 min="+ stat1.min(); System.out.println("stat1 max="+stat1.max()); System.out.println("statl average="+stat1 average()); System.out.println("stat1 mode=" + stat1.mode()); System.out.println("statl variance=" + stat1.variance()); System.out.println("statl standard deviation=" + statl standardDeviation() + "\n"); Example output: stat1 data =[25.0, 25.0, 50.0, 12.0] stat1 min 12.0 stat1 max=50.0 stat1 average = 28.0 stat1 mode = 25.0 stat1 variance 189.5 stat1 standard deviation 13.765899897936205 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.java 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. data: double[] Stat 1. + Stat() 2. + Stat (double[] d) 3. + Stat (float [] f) 4. + Stat (int[] i) 5. Stat (long[] lo) 6. + setData(float [] f): void 7. + setData (double[] d): void 8. + setData (int[] i): void 9. + setData (long [] lo): void + getData() double[] 10. 11. 12. 13. 14. 15. 16. + equals (Stat s): boolean + reset() void + append(int[] i): void + append(float [] f): void + append (long[] lo): void + append (double[] d): void +isEmpty() boolean + toString(): String 17. 18. 19. + min() double 20. 21. 22. + max() double + average (): double + 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 type). The data has the type double[], which is a reference type. This means that data itself will store a reference to the memory location where 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[] i), setData(long[] lo), setData(float[] f) 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(Stats) 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 objects 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[] i), append(long[] lo), append(float[] f) These methods will create a new double array that contains exactly the elements of data, followed by those elements of the array passed as the parameter. A reference to this array will be assigned to data. If the parameter is null, the method will do nothing. See examples at the end of the document. ⚫ (17) isEmpty() This method returns a boolean value true if the data object is empty (i.e. such an array is an empty array, which has a length 0). Otherwise, it returns false. ⚫ (18) toString() As in the previous lab, this method returns a String of the data elements stored in the Stat object. Please look at the sample output at the end of the document for formatting. See how commas and square brackets are added. ⚫ (19) min() This method returns the minimum of the data array. If the array is empty, then the method returns Double.NaN. (20) max() This method returns the maximum of the data array. If the array is empty, then the method returns Double.NaN. ⚫ (21) average() This method returns the average of the data array. The average is a double type value as the mean value of a given array of numbers. If the array is empty, then the method returns Double.NaN. =-= Hint =-= Double.NaN is what the method will return. For example, return Double.NaN; NaN stands for "Not a Number". Treat it as "an undefined value" instead of "an error". ⚫ (22) mode() Most part of the implementation will be the same as from the previous lab. This method returns the value that appears most frequently in the array. If there is no such unique, or if the data array is empty, the method will return Double.NaN. (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 Replace [CLASS/FILE NAME] with the required name according to the assignment instruction. Replace [YOUR NAME] with your actual name. Submission instruction After you have completed and thoroughly tested your program, upload and submit the modified file Stat.java. Keep your previous Stat.java file that was made since last time elsewhere. You do not have to submit the driver class file that contains the main method. We will use different test cases to test your program. Grading (1) Points are AWARDED when your program passes test cases that are determined or chosen by the grader/grading program. Test cases include checking constructors (0.5 each * 5 = 2.5 points), set methods (0.6 each *4 = 2.4 points), append methods (0.6 each * 4 = 2.4 points), reset and isEmpty methods (0.3 point), checking statistical outputs with a null value (0.2 each * 6 = 1.2 points), outputs with a non-null value (0.2 each * 6 = 1.2 points). (2) Points are DEDUCTED when your program fails to meet certain requirements: ⚫(-1 point) If the source file(s)/class(es) are named incorrectly (case matters!) ⚫ (-1 point) If your source file(s) have a package declaration at the top ⚫(-1 point) If any source file you submit is missing your Statement of Academic Honesty at the top of the source file. All submitted source code files must contain your Statement of Academic Honesty at the top of each file. ⚫(-1 point) If you have more than one instance of Scanner in your program. Your program should only have one instance of Scanner. (-1.5 points) Inconsistent I/O (input/output) that does not match our instructions or examples exactly (unless otherwise stated in an assignment's instructions). Your program's 1/0 (order, wording, formatting, etc.) must match our examples and instructions. •If your (-1 point) comments or (-1 point) variables are "lacking". "Lacking" means that your grader can find any lines of code or variables that take more than 10 seconds to understand, and there is no comment, or the variable name does not make sense (variable names like b, bb, bbb, etc. will almost never be acceptable). ⚫(-1 point) Indentation is not consistent throughout your source code (e.g. you should NOT use a combination of tabs and spaces in the files.) (-10 points) If you use a non-standard Java library or class (StringBuilder class, Arrays class, any class in java.util.stream) or other code specifically disallowed by the lab/project. Special notice regarding the submission: A. Late submission penalty. Points will be deducted from the original grade. If your submission is after the posted deadline... (1) within 24 hours: -1 (2) between 24 hours and 48 hours: -2.5 (3) after 48 hours: assignment will not be accepted. B. If the source code file is missing, partially or completely broken, unable to open (including using a wrong file format), unable to compile, irrelevant to the assignment, purposely made to contain malicious codes, or determined to be an academic misrepresentation or a case of plagiarism, you will receive 0 grade for this assignment. Below are sample inputs/outputs. This is not a comprehensive list of test cases. Example 1 Example main method: double[] data1 = {}; Stat stat1 = new Stat(data1); System.out.println("statl data="+ statl.toString()); System.out.println("stat1 min="+stat1.min0);" System.out.println("stat1 max="+stat1.max()); System.out.println("statl average=" + stat1 average()); System.out.println("statl mode=" + stat1.mode()); System.out.println("statl variance=" + stat1.variance()); System.out.println("statl standard deviation="+stat1 standardDeviation()); System.out.println("statl is empty =" + stat1.isEmpty() + "\n"); Example output: stat1 data =[] stat1 min = NaN stat1 max = NaN statl average = NaN stat1 mode NaN stat1 variance NaN stat1 standard deviation = NaN stat1 is empty = true Example 2 Example main method: double[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9 }; Stat stat1 = new Stat(data1); System.out.println("statl data="+stat1.toString()); System.out.println("stat1 min = "+ stat1.min(); System.out.println("stat1 max="+stat1.max()); System.out.println("statl average="+stat1 average()); System.out.println("stat1 mode="+stat1.mode(); System.out.println("statl variance=" + stat1.variance()); System.out.println("statl standard deviation="+statl standard Deviation()); System.out.println("statl is empty =" + stat1.isEmpty() + "\n"); stat1.reset(); System.out.println("stat1 data="+stat1.toString(); System.out.println("statl min="+ stat1.min(); System.out.println("stat1 max=" + stat1.max()); System.out.println("stat1 average="+stat1 average()); System.out.println("statl mode=" + stat1.mode()); System.out.println("statl variance=" + stat1.variance()); System.out.println("statl standard deviation=" + statl standardDeviation()); System.out.println("statl is empty =" + stat1.isEmpty() + "\n"); Example output: stat1 data [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] stat1 min = 1.0 stat1 max 9.0 stat1 average = 5.0 stat1 mode = NaN stat1 variance 6.666666666666667 stat1 standard deviation=2.581988897471611 stat1 is empty = false stat1 data = [] stat1 min = NaN stat1 max = NaN stat1 average = NaN stat1 mode=NaN stat variance NaN stat1 standard deviation - NaN stat1 is empty = true Example 3 Example main method: float[] data1 = {10.0F, 10.0F}; Stat statl new Stat(data1); System.out.println("stat1 data="+stat1.toString()); System.out.println("stat1 min="+ stat1.min()); System.out.println("stat1 max="+stat1 max()); System.out.println("statl average =" + stat1 average()); System.out.println("statl mode="+stat1.mode()); System.out.println("statl variance="+stat1.variance()); System.out.println("statl standard deviation=" + stat1 standardDeviation() + "\n"); long[] data2 (80L, 60L}; stat1.append(data2); System.out.println("statl data="+ statl.toString()); System.out.println("statl min "+stat1 min); System.out.println("statl max="+ stat1.max()); System.out.println("statl average="+stat1 average()); System.out.println("stat1 mode="+stat1.mode()); System.out.println("statl variance=" + stat1.variance()); System.out.println("statl standard deviation=" + statl standard Deviation() + "\n"); Example output: stat1 data [10.0, 10.0] stat1 min = 10.0 stat1 max = 10.0 stat1 average 10.0 stat1 mode = 10.0 stat1 variance = 0.0 stat1 standard deviation = 0.0 stat1 data [10.0, 10.0, 80.0, 60.0] stat1 min 10.0 stat1 max=80.0 stat1 average 40.0 stat1 mode = 10.0 stat1 variance 950.0 stat1 standard deviation=-30.822070014844883 Example 4 Example main method: double[] data = {50.0, 60.0}; float[] data2 (70.0F, 80.0F}; int[] data3 (90, 100}; long[] data4 ={100L, 110L}; Stat statl new Stat); System.out.println("stat1 data="+stat1.toString()); stat1.setData(data1); System.out.println("stat1 data="+ statl.toString()); stat1.setData(data2); System.out.println("stat1 data="+stat1.toString(); stat1.setData(data3); System.out.println("stat1 data="+ statl.toString()); stat1.setData(data4); System.out.println("statl data="+stat1.toString()); data1 = null; stat 1.setData(data1); System.out.println("statl data="+ statl.toString()); Example output: stat1 data= stat1 data [50.0, 60.0] = stat1 data [70.0, 80.0] stat1 data [90.0, 100.0] stat1 data [100.0, 110.0] =
Expert Answer:
Answer rating: 100% (QA)
public class Stat private double data public Stat super data new double0 param data public Statdouble d super thisdata new doubledlength for int i 0 i dlength i thisdatai di param data public Statfloa... View the full answer
Related Book For
Systems analysis and design
ISBN: 978-0136089162
8th Edition
Authors: kenneth e. kendall, julie e. kendall
Posted Date:
Students also viewed these programming questions
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
The following items were shown on the balance sheet of Martin Corporation on December 31, 2021: Stockholders' Equity Paid-In Capital Capital Stock Common stock, $5 par value, 750,000 shares...
-
In its 2016 annual report, Poppie, Inc. reported Accounts Receivable of $1,453 million at the end of 2015 and $1,056 million at the end of 2016. Sales revenue for 2016 was $13,287 million. Required...
-
How do marketers use branded entertainment and support media such as directories, out-of-home media, and place-based media to communicate with consumers?
-
Which of the following statements is false? (a) A negative cash flow can occur in a year in which net income is positive. (b) An increase in accounts receivable represents accounts not yet collected...
-
You were recently hired to replace the manager of the Roller Division at a major conveyor-manufacturing firm, despite the managers strong external sales record. Roller manufacturing is relatively...
-
State the 2 ( two ) advantages and 3 ( three ) disadvantages of hiring an internal worker.
-
An 85-kg male and a 47 kg female figure skating pair are gliding across the ice at 6.3 m/s, preparing for a throw jump maneuver at the Shochi Olympics 2014. The male skater tosses the female skater...
-
1. What is the biggest difference between the taxation of ordinary income and capital gains? 2. What are the tax benefits related to wealth transfers? Include any limitations or thresholds in your...
-
How can countries reduce Income inequality and what can be done on a global level? Explain
-
Ryan Frazier, controller of Anstead Co., has suggested that all orders received before the end of the fiscal year be shipped by midnight, October 31, even if the shipping department must work...
-
What are the different types of strategies organizations use to promote diversity and inclusion? Explain in the context of cultural diversity.
-
A Japanese firm manufactures cars in the US. Assume that each car sells for $20,000 to a consumer in the US. Also assume that on each car the Japanese manufacturer earns $2000 in profits and remits...
-
Weber Interstate Paving Co. had $450 million of sales and $225 million of fixed assets last year, so its FA/Sales ratio was 50%. However, its fixed assets were used at only 45% of capacity. If the...
-
What is UML? Discuss.
-
What are five ways the analyst can avoid biasing output?
-
Prepare a list of activities for a systems development team for an online travel agent that is setting up a Web site for customers. Now suppose you are running out of time. Describe some of your...
-
What criteria are used to classify species in an evolutionary classification system?
-
All protists are (a) eukaryotes. (b) autotrophs. (c) heterotrophs. (d) single-celled.
-
What is a clade?
Study smarter with the SolutionInn App