Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE REFERENCE THE FOLLOWING CLASSES TO ANSWER QUESTION STUDENT.TESTCASE ISN'T PROVIDED, THE FOLLOWING INFORMATION ARE ALL I WAS GIVEN: SuperArray Class: import java.util.Arrays; public class

PLEASE REFERENCE THE FOLLOWING CLASSES TO ANSWER QUESTION STUDENT.TESTCASE ISN'T PROVIDED, THE FOLLOWING INFORMATION ARE ALL I WAS GIVEN:

SuperArray Class:

import java.util.Arrays;

public class SuperArray { private static final int INIT_SIZE = 10; private Object[] arr; private int size;

// ---------------------------------------------------------- /** * Create a new SuperArray object. */ SuperArray() { arr = new Object[INIT_SIZE]; size = 0; }

// ---------------------------------------------------------- /** * Create a new SuperArray object. * @param arr */ SuperArray(Object[] arr) { this.arr = arr; size = arr.length; }

// ---------------------------------------------------------- /** * Insert an object into a SuperArray object. * @param anEntry the object to be added */ public void add(Object anEntry) {

// Make sure there is room if (size >= this.arr.length) { reallocate(); }

// insert item this.arr[size] = anEntry; size++; }

// ---------------------------------------------------------- /** * Insert all the elements of an array into a SuperArray object. * @param c the array */ public void addAll(Object[] c) {

for (int i = 0; i < c.length; i++) { add(c[i]); } }

// ---------------------------------------------------------- /** * getter of field size. * @return the size of the superArray */ public int getSize(){ return size; }

// ---------------------------------------------------------- /** * Private method that doubles the size of the SuperArray object * Copies all the elements into the new array, to avoid any loss of data. * */ private void reallocate() { int capacity = this.arr.length * 2; this.arr = Arrays.copyOf(this.arr, capacity); }

}

SuperArrayTest Class:

public class SuperArrayTest extends student.TestCase {

private SuperArray SuA;

public void setUp() { String[] a = { "a", "b", "c", "d" }; SuA = new SuperArray(a); }

// ---------------------------------------------------------- /** * Test {@link SuperArray#addAll(Object[])}. */ public void testAddAll() { //The original size of SuA is 4 assertEquals(4, SuA.getSize()); String test = "testString"; SuA.add(test); //After adding the string test, the size should be 5 assertEquals(5, SuA.getSize()); }

}

QUESTION TO BE ANSWERED:

PART I: Inheritance

Create a new class SubArray that extends SuperArray. Add a new field addCount that will count the number of items added. SubArray constructor should initialize addCount to 0. Use the super reference in the constructor to call the parent's constructor to set the values.

Create an accessor method for addCount in the subArray class.

Override add : increment addCount then call add of the super class. For example to call the method "masterMethod()" implemented in the super class, do "super.masterMethod()" in the subclass' method.

Override addAll: add the length of the array to addCount , then call addAll of the super class.

Test SubArray

Create a test class SubArrayTest. Write unit tests for SubArray .

Create an instance of SubArray in your test class. To test addAll, add four elements stored in an array using the addAll method:

subArray sa; sa = new subArray(); // inside setUp() String [] a = {"a", "b", "c", "d"}; sa.addAll(a);

We would expect the getAddCount method to return four at this point, but it returns eight. What went wrong?

Internally, SuperArray's addAll() method is implemented on top of its add() method. The addAll() method in subArray added four to addCount and then invoked SuperArray's addAll() implementation using super.addAll() . This in turn invoked the add() method, as overriden in SubArray, once for each element. Each of these four invocations added one more to addCount , or a total increase of eight: Each element added with the addAll() method is double-counted! We will fix this problem in part II.

Make sure your unit tests run and pass.

Write unit tests for all the methods in subArray.

PART II: Composition

Create a new class CompArray . Instead of extending an existing class, give your new class a private field that references an instance of the existing class. In this case SuperArray. This design is called composition because the existing class becomes a component of the new one. Each instance method in the new class invokes the corresponding method on the contained instance of the existing class and returns the results. This is known as forwarding. The resulting class will be solid, with no dependencies on the implementation details of the existing class.

Here is the skeleton of CompArray:

public class CompArray { private SuperArray s; private int addCount; public CompArray() { SuperArray s = new SuperArray(); this.s = s; this.addCount = 0; } public CompArray(SuperArray s) { this.s = s; } public void add(Object o) { // your code here } public void addAll(Object[] c) { // your code here } public int getAddCount() { return this.addCount; } }

Now, in order to get a CompArray we first need to create a new SuperArray and pass it to the constructor for CompArray . The CompArray class is known as a wrapper class because each CompArray instance wraps another SuperArray instance.

Implement CompArray's add() and addAll() methods. Update addCount before call add() and addAll() on the SuperArray field.

Create a test class CompArrayTest.Write unit tests for CompArray. Run the test.

Elements added with the addAll() method should be correctly counted now, not double-counted like in part I.

PART III: Exceptions

Create a class called NotInDeansListException that extends RuntimeException. NotInDeansListException contains only two methods: one no argument constructor that calls the super class no argument constructor. And a constructor that takes a String (message) and calls the super class constructor with argument (String).

2. Create a class called NotInAcademicProbationException that extends RuntimeException. NotInAcademicProbationException contains only two methods: one no argument constructor that calls the super class no argument constructor. And a constructor that takes a String (message) and calls the super class constructor with argument (String).

3. Create a class Student that implements Comparable. Your class will have three data fields: name (String), id (int), and gpa (double). Create a constructor that takes three arguments name (String), id (int), and gpa (double) in that order and initializes the data fields. Create getters and setters. Implement compareTo, first compare gpas, then ids and then names. Paste the following method inside Student.java, this method return a Comparator object that will be used to sort an array of object. It calls the compareTo method when comparing two Students.

 

// ----------------------------------------------------------

/**

* Comparator for students objects.

* @return comparator

*/

public static Comparator studentOrder() {

return new Comparator() {

@Override

public int compare(Student o1, Student o2) {

return o1.compareTo(o2);

}

};

}

4. Add 2 methods to Student:

isInDeansList: takes nothing and returns a boolean. The method returns true if gpa >= 3.5, it returns false if 2.0 < gpa <3.5. The method should throw a NotInDeansListException if gpa <= 2.0 (e.g. throw new NotInDeansListException("message"); ).

isInAcademicProbation: takes nothing and returns a boolean. The method returns true if gpa <= 2.0, it returns false if 2.0 < gpa <3.5. The method should throw a NotInAcademicProbationException if gpa >= 3.5.

5. Test your classes: In your test classes:

Create an array of Student. Insert student (with different gpas) to the array. Use Arrays.sort(your_array_of_student, Student.studentOrder()) to sort your array. Verify that the user with the lowest gpa is at index 0 and the one with the greatest gpa is at the last position.

Create a student with gpa < 2.0 call isInDeansList and verify that the correct Exception was thrown

Create a student with gpa >= 3.5 call isInAcademicProbation and verify that the correct Exception was thrown

7. Write all of your test cases. Think through the issues for each operation carefully. Include tests for exception conditions. To test that an exception is thrown, you can use this pattern:

 Exception thrown = null; try { // Some action that is intended to produce an exception } catch (Exception exception) { thrown = exception; } assertNotNull(thrown); assertTrue(thrown instanceof /* desired exception type */); 

The piece of code above carries out an action and captures any exception that is produced so that you can test it. The assertions above check that the exception was indeed thrown (or else occurred would be null) and also show how you might check the type of the exception and its message.

need help writing this code. wrote mine, but it won't compile.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Advances In Database Technology Edbt 88 International Conference On Extending Database Technology Venice Italy March 14 18 1988 Proceedings Lncs 303

Authors: Joachim W. Schmidt ,Stefano Ceri ,Michele Missikoff

1988th Edition

3540190740, 978-3540190745

More Books

Students also viewed these Databases questions

Question

Describe the linkages between HRM and strategy formulation. page 74

Answered: 1 week ago

Question

Identify approaches to improving retention rates.

Answered: 1 week ago