[Solved] CSCI1301L Lab 10: Classes and methods 2

30 $

SKU: [Solved] CSCI1301L Lab 10: Classes and methods 2 Category: Tag:

Introduction

In this lab, we will create our own class, namely Stat. The information of this class is described in the UML class diagram. Recalled 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:

  • constructors,
  • access modifiers,
  • instance variables,
  • general methods with different return types,
  • methods called another methods,
  • specific accessor and mutator methods, and
  • 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
Stat
– data: double[]
+ 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 store. However, data does not store the array.

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 defined 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.

  1. 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 holding 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(Stat s)

This method returns the boolean value true if the data objects of both the calling Stat object and the passing Stat object s 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. In 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 that 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.

  1. After you have created the Stat class, write a main method in a separate driver class 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.

  1. 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.

/*

  • [CLASS/FILE NAME].java * Author: [YOUR NAME]
  • Statement of Academic Honesty:

*

  • The following code represents my own work. I have neither
  • received nor given inappropriate assistance. I have not copied
  • or modified code from anywhere other than the authorized
  • I recognize that any unauthorized sharing, assistance,
  • or plagiarism will be handled in accordance with both the
  • University of Georgia’s Academic Honesty Policy and the
  • policies of this course. I recognize that my work is based on
  • an assignment created by the Department of Computer
  • Science at the University of Georgia. Any publishing or posting * of source code at any time for this project is prohibited. */

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. However, you may include it if you really want, but we will use different test cases to test your program.

Grading

A score between 0 and 10 will be assigned, with a minimum of 1 point increment (i.e. only integers).

  1. The source code is correctly provided, including the right file format/name extension. The file name is consistent with the source code’s class name. (1 point)
  2. Correct data types for variables are used, correct return types for the method are used, and correct access modifiers (public or private) are used. (1 point)
  3. Good programming practices are followed, including variable naming, coding layout, and comments. (1 point)
  4. No package declaration at the top of the program and no non-standard Java library or class is used, such as Arrays class, stream. (1 point)
  5. The program can pass the tests for each of these methods: equals, toString, min, max, average, and mode. (6 points)

Special notice regarding the submission:

  1. Late submission penalty. Points will be deducted from the original grade. If your submission is after the posted deadline…
    • within 24 hours: -3
    • between 24 hours and 48 hours: -6
    • between 48 hours and 72 hours: -9
    • after 72 hours: assignment will not be accepted.
  1. 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 stat1 = new Stat();

System.out.println(“stat1 data = ” + stat1.toString());

stat1 = new Stat(data);

System.out.println(“stat1 has been altered.”);

System.out.println(“stat1 data = ” + stat1.toString());

System.out.println(“stat1 min = ” + stat1.min());

System.out.println(“stat1 max = ” + stat1.max());

System.out.println(“stat1 average = ” + stat1.average());

System.out.println(“stat1 mode = ” + stat1.mode() + “
”);

Stat stat2 = new Stat(); stat2.setData(data2);

Stat stat3 = new Stat(stat1.getData());

System.out.println(“stat2 data = ” + stat2.toString());

System.out.println(“stat3 data = ” + stat3.toString());

System.out.println();

System.out.println(“stat1 is equal to stat2 using ”equals()”? ” + stat1.equals(stat2));

System.out.println(“stat1 is equal to stat3 using ”equals()”? ” + stat1.equals(stat3));

System.out.println(“stat1 is equal to stat3 using ”==”? ” + (stat1 == stat3));

Example output: stat1 data = [0.0] stat1 has been altered.

stat1 data = [-5.3, 2.5, 88.9, 0.0, 0.0, 28.0, 16.5, 88.9, 109.5, -90.0, 88.9] stat1 min = -90.0 stat1 max = 109.5 stat1 average = 29.80909090909091 stat1 mode = 88.9

stat2 data = [100.34, 50.01, 50.01, -8.0]

stat3 data = [-5.3, 2.5, 88.9, 0.0, 0.0, 28.0, 16.5, 88.9, 109.5, -90.0, 88.9]

stat1 is equal to stat2 using “equals()”? false stat1 is equal to stat3 using “equals()”? true

stat1 is equal to stat3 using “==”? false

Example 2

Example main method:

double[] data = {10.0, 20.0, 30.0};

Stat stat1 = new Stat(data);

data[0] = 100.0; data[1] = 200.0; data[2] = 300.0;

Stat stat2 = new Stat(data);

System.out.println(“stat1 data = ” + stat1.toString());

System.out.println(“stat2 data = ” + stat2.toString());

System.out.println(“The two arrays should be different”);

Example output:

stat1 data = [10.0, 20.0, 30.0] stat2 data = [100.0, 200.0, 300.0] The two arrays should be different

Example 3

Example main method:

double[] data1 = {10.0, 20.0, 30.0};

Stat stat1 = new Stat(data1);

double[] data2 = stat1.getData();

System.out.println(“The arrays are identical: ” + (data1 == data2));

Example output:

The arrays are identical: false

Example 4

Example main method:

double[] data1 = {10.0, 20.0, 30.0}; Stat stat1 = new Stat(); stat1.setData(data1); Stat stat2 = new Stat(data1); double[] data2 = stat1.getData();

System.out.println(“The arrays are identical: ” + (data1 == data2));

System.out.println(“stat2 equals stat1: ” + stat2.equals(stat1));

System.out.println(“stat1 equals stat2: ” + stat2.equals(stat1));

Example output:

The arrays are identical: false stat2 equals stat1: true stat1 equals stat2: true

Example 5

Example main method:

Stat stat1 = new Stat();

System.out.println(“stat1 data = ” + stat1.toString());

System.out.println(“stat1 min = ” + stat1.min());

System.out.println(“stat1 max = ” + stat1.max());

System.out.println(“stat1 average = ” + stat1.average());

System.out.println(“stat1 mode = ” + stat1.mode());

System.out.println(“stat1 data = ” + stat1.toString());

Example output:

stat1 data = [0.0] stat1 min = 0.0 stat1 max = 0.0 stat1 average = 0.0 stat1 mode = 0.0

stat1 data = [0.0]

Example 6

Example main method:

double[] data = {1,2,2,3,3,4};

Stat stat1 = new Stat(data);

System.out.println(“stat1 data = ” + stat1.toString());

System.out.println(“stat1 min = ” + stat1.min());

System.out.println(“stat1 max = ” + stat1.max());

System.out.println(“stat1 average = ” + stat1.average());

System.out.println(“stat1 mode = ” + stat1.mode());

System.out.println(“stat1 data = ” + stat1.toString());

Example output:

stat1 data = [1.0, 2.0, 2.0, 3.0, 3.0, 4.0]

stat1 min = 1.0 stat1 max = 4.0 stat1 average = 2.5 stat1 mode = NaN

stat1 data = [1.0, 2.0, 2.0, 3.0, 3.0, 4.0]

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CSCI1301L Lab 10: Classes and methods 2
30 $