Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 11: Classes and methods 3 Introduction In this lab, we continue working on classes and methods. The work is based on the previous

imageimageimageimageimageimageimageimageimageimageimage

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() + " "); 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() + " "); 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() + " "); 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() + " "); 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() + " "); 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() + " "); 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() + " "); 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] =

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems analysis and design

Authors: kenneth e. kendall, julie e. kendall

8th Edition

135094909, 013608916X, 9780135094907, 978-0136089162

More Books

Students also viewed these Programming questions

Question

What needs do all people have in common?

Answered: 1 week ago

Question

What is UML? Discuss.

Answered: 1 week ago

Question

What are five ways the analyst can avoid biasing output?

Answered: 1 week ago

Question

=+b. Find the probability that neither bid is successful.

Answered: 1 week ago