Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Introduction In this lab, we will create our own class, namely Stat. The information of this class is described in the UML class diagram.
Introduction In this lab, we will create our own class, namely Stat. The information of this class is described in the UML class diagram. Recall what we have learned about the UML (Unified Modeling Language) and the class diagrams. A class diagram consists of a class name, attributes, and methods. For each member (an attribute or a method), there is an access modifier, such as public or private, to control which classes have access to it. In this lab, you will work on two source codes/files. First, you will create the class that deals with statistics on data. Then, you will create a tester program (driver class) that uses the method in your first class and test the results. -Important!- Please be aware that successfully and timely completing this lab assignment is very important. You will use what you have completed in this assignment to work on the next assignment. If you fail to make the program work for this lab assignment, you are likely to fail in the next assignment. If you have questions or unsolved issues for this assignment, we strongly suggest that you resolve all open issues and have cleared all doubts before you start working on the next lab assignment. Lab objectives After completing this lab, you should be able to create classes by utilizing: (1) constructors, (2) access modifiers, (3) instance variables, (4) general methods with different return types, (5) methods calling another methods, (6) specific accessor and mutator methods, and (7) the equals() method and other user defined methods. Assignment In this lab, you will create a Java program that deals with certain statistics of data. Details are described as follows: 1. Use the UML diagram and method descriptions below to create your Stat class. - data: double[] Stat + Stat() + Stat (double[] d) + getData() double[] + setData (double [] d) void + equals (Stat s): boolean +toString(): String +min() double + max() double + average () double + mode () double The 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 stored. However, data does not store specific array elements. According to this provided class diagram, you will implement public methods to compute the min, max, mode, and average of these values. Additionally, the program will also need the "get" and "set" methods. In this program, we ensure that each instance/object of the Stat class uses its own array of double type values. Therefore, you should define getData() to create a copy of the data array and return a reference to that copy, rather than a reference to the original (think of why?). Similarly, the method setData(double[] d) creates a copy of the array d and assigns to data a reference to the copy. 2. After you have created the skeleton code of Stat class according the UML class diagram, implement the detail of the methods. Stat() This is the default constructor for the class Stat. It should create a double array having a single element 0.0. Stat(double[] d) This is another constructor for the class Stat. This constructor should create a double array of the same length as d and hold copies of the values of d. A reference to this new array should be assigned to data. getData() This is an accessor, also known as get method. It is used to read/retrieve the values of data. This method should not return a reference to data. Instead, it should create a new array containing exactly the values contained in data, and then return a reference to this new array. setData(double[] d) This is a mutator, also known as set method. It is used to set the values of the data array. The method should create a new array containing exactly the elements of d and assign to data a reference to this new array. (Hint: the method should not simply assign d to data). equals(Stats) This 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. toString() 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. min() This method returns the minimum of the data array. max() This method returns the maximum of the data array. 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. mode() The mode, according to this lab's definition, is the value that occurs most frequently in a collection of all elements. If one element occurs more frequently in data than all others, then mode() should return that element. Otherwise, mode() should return Double.NaN, which means that no such an element appears more frequently than any others. Example 1: (1, 1, 2, 2, 2, 3, 4, 5, 6). Element "2" appears 3 times, more than any other elements ("1" twice, other just once). Therefore, mode returns 2. Example 2: {1, 1, 1, 2, 2, 2, 3, 4}. Element "1" and element "2" both appear 3 times. There is no winner. Therefore, mode() returns NaN. 3. After you have created the Stat class, write a main method in the driver class (the file other than in Stat.java) to test each 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. When you test your program, there are some considerations for the test cases (note that this is not a complete list): Can this program process an array full of numbers AND an empty array (i.e. no input of array)? Can this program handle values of integers and floating-point numbers both? Can this program handle negative values? Can this program process mode() accurately when the most frequent value is available or unavailable? Please note 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 should be added ABOVE the original first line of code. 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 file Stat.java. 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 no-arg constructor (1.5 points), 1- arg constructor (1.5 points), min() method (1.5 points), max() method (1.5 points), average() method (1 point), mode() method (1 point), equals() method (1 point), and set() & get() methods (1 point). (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 1/0 (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[] data = (-5.3, 2.5, 88.9, 0, 0.0, 28, 16.5, 88.9, 109.5, -90, 88.9}; double[] data2 = {100.34, 50.01, 50.01, -8}; Stat statl new Stat System.out.println("statl data="+statl.toString(); statl new Stat(data); System.out.println("stat1 has been altered."); System.out.println("statl data=" + statl.toString()); System.out.println("stat1 min =" + statl.min(); System.out.println("stat] max=" + statl.max()); System.out.println("statl average="+ statl.average()); System.out.println("stat] mode=" + statl mode() + " ");
Step by Step Solution
★★★★★
3.42 Rating (149 Votes )
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