[Solved] CSCI1301L Lab 11: Classes and methods 3

$25

File Name: CSCI1301L_Lab_11:_Classes_and_methods_3.zip
File Size: 367.38 KB

SKU: [Solved] CSCI1301L Lab 11: Classes and methods 3 Category: Tag:
5/5 - (1 vote)

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 anybody else.

Lab objectives

After completing this lab, you should be able to create classes and gain further experience by utilizing:

  • constructors,
  • access modifiers,
  • instance variables,
  • general methods with different return types,
  • methods called another methods,
  • specific accessor and mutator methods,
  • different kinds of overloading methods, and
  • 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.

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 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() 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 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 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.

  1. 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 (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 length 0 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 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 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[] 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.

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

  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 Georgias 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 modified file Stat.java. Keep your original 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. 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 codes 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 6 tests that cover one or more methods in the Stat class. Passing each test program is worth 1 point. (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[] data1 = {}; Stat stat1 = 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(stat1 average = + stat1.average());

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

System.out.println(stat1 variance = + stat1.variance());

System.out.println(stat1 standard deviation = + stat1.standardDeviation()); System.out.println(stat1 is empty = + stat1.isEmpty() +
);

Example output: stat1 data = [] stat1 min = NaN stat1 max = NaN stat1 average = NaN stat1 mode = NaN stat1 variance = NaN stat1 standard deviation = NaN

stat1 is empty = true

Example 2

Example main method:

double[] data1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Stat stat1 = 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(stat1 average = + stat1.average());

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

System.out.println(stat1 variance = + stat1.variance());

System.out.println(stat1 standard deviation = + stat1.standardDeviation());

System.out.println(stat1 is empty = + stat1.isEmpty() +
); stat1.reset();

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 variance = + stat1.variance());

System.out.println(stat1 standard deviation = + stat1.standardDeviation());

System.out.println(stat1 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 stat1 variance = NaN stat1 standard deviation = NaN stat1 is empty = true

Example 3

Example main method:

float[] data1 = {10.0F,10.0F};

Stat stat1 = 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(stat1 average = + stat1.average());

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

System.out.println(stat1 variance = + stat1.variance());

System.out.println(stat1 standard deviation = + stat1.standardDeviation() +
); long[] data2 = {80L, 60L};

stat1.append(data2);

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 variance = + stat1.variance());

System.out.println(stat1 standard deviation = + stat1.standardDeviation() +
);

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[] data1 = {50.0, 60.0}; float[] data2 = {70.0F, 80.0F}; int[] data3 = {90, 100}; long[] data4 = {100L, 110L};

Stat stat1 = new Stat();

System.out.println(stat1 data = + stat1.toString()); stat1.setData(data1);

System.out.println(stat1 data = + stat1.toString()); stat1.setData(data2);

System.out.println(stat1 data = + stat1.toString()); stat1.setData(data3);

System.out.println(stat1 data = + stat1.toString()); stat1.setData(data4);

System.out.println(stat1 data = + stat1.toString()); data1 = null; stat1.setData(data1);

System.out.println(stat1 data = + stat1.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] stat1 data = []

Example 5

Example main method:

double[] data1 = {50.0, 60.0}; float[] data2 = {70.0F, 80.0F};

int[] data3 = {90, 100}; long[] data4 = {100L, 110L};

Stat stat1 = new Stat();

System.out.println(stat1 data = + stat1.toString()); stat1.append(data1);

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

stat1.append(data2);

System.out.println(stat1 data = + stat1.toString()); stat1.append(data3);

System.out.println(stat1 data = + stat1.toString()); stat1.append(data4);

System.out.println(stat1 data = + stat1.toString()); data1 = null; stat1.append(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(stat1 average = + stat1.average());

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

System.out.println(stat1 variance = + stat1.variance());

System.out.println(stat1 standard deviation = + stat1.standardDeviation() +
);

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] stat1 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(stat1 data = + stat1.toString());

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

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

System.out.println(stat1 equals stat2 = + stat1.equals(stat2));

System.out.println(stat1 equals stat3 = + stat1.equals(stat3));

System.out.println(stat1 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[] data1 = {}; double[] data2 = { 25 }; float[] data3 = {}; float[] data4 = { 25 }; int[] data5 = {}; int[] data6 = { 50 }; long[] data7 = {}; long[] data8 = { 12 }; Stat stat1 = new Stat(); stat1.append(data1); stat1.append(data2); stat1.append(data3); stat1.append(data4); stat1.append(data5); stat1.append(data6); stat1.append(data7); stat1.append(data8); data1 = null; stat1.append(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(stat1 average = + stat1.average());

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

System.out.println(stat1 variance = + stat1.variance());

System.out.println(stat1 standard deviation = + stat1.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

Reviews

There are no reviews yet.

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

Shopping Cart
[Solved] CSCI1301L Lab 11: Classes and methods 3
$25