PLEASE REFERENCE THE FOLLOWING CLASSES TO ANSWER QUESTION STUDENT.TESTCASE ISN\'
ID: 3747626 • Letter: P
Question
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:
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:
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:
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.
Explanation / Answer
CompArray.java
public class CompArray {
private SuperArray s;
private int addCount;
/**
*
*/
public CompArray() {
this.s = new SuperArray();
this.addCount = 0;
}
/**
* @param s the wrapped superarray
*/
public CompArray(SuperArray s)
{
this.s = s;
}
/**
* @param o the object to add
*/
public void add(Object o)
{
addCount++;
s.add(o);
}
/**
* @param c the array to add
*/
public void addAll(Object[] c)
{
addCount += c.length;
s.addAll(c);
}
/**
* @return count of additions
*/
public int getAddCount()
{
return this.addCount;
}
}
CompArrayTest.java
import org.junit.Before;
import org.junit.Test;
import student.TestCase;
public class CompArrayTest extends TestCase {
private CompArray ca;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
ca = new CompArray();
}
/**
* Test method for {@link CompArray#add(java.lang.Object)}.
*/
@Test
public void testAdd() {
ca.add(3);
assertEquals(1, ca.getAddCount());
}
/**
* Test method for {@link CompArray#addAll(java.lang.Object[])}.
*/
@Test
public void testAddAll() {
String [] a = {"a", "b", "c", "d"};
ca.addAll(a);
assertEquals(4, ca.getAddCount());
}
/**
* Test method for {@link CompArray#getAddCount()}.
*/
@Test
public void testGetAddCount() {
assertEquals(0, ca.getAddCount());
}
/**
* for testing the other constructor
*/
public void testCompArraySuperArray() {
SuperArray s = new SuperArray();
CompArray a = new CompArray(s);
assertNotNull(a);
}
}
NotInAcademicProbationException.java
//We don't care about serialization for this project.
//Adding a UID would just complicate things.
@SuppressWarnings("serial")
public class NotInAcademicProbationException extends RuntimeException {
/**
*
*/
public NotInAcademicProbationException() {
super();
}
/**
* @param message for the user
*/
public NotInAcademicProbationException(String message) {
super(message);
}
}
NotInDeansListException.java
@SuppressWarnings({ "serial" })
public class NotInDeansListException extends RuntimeException {
/**
*
*/
public NotInDeansListException() {
super();
}
/**
* @param arg0 message for the user
*/
public NotInDeansListException(String arg0) {
super(arg0);
}
}
Student.java
import java.util.Comparator;
public class Student implements Comparable<Student> {
private String name;
private int id;
private double gpa;
/**
* @param name
* @param id
* @param gpa
*
*/
public Student(String name, int id, double gpa) {
this.name = name;
this.id = id;
this.gpa = gpa;
}
/**
* compareTo implementation
* @param s the other student to compare
* @return an integer wrt. the compareTo def.
*/
public int compareTo(Student s) {
if (gpa != s.getGpa()) {
return Double.compare(gpa, s.getGpa());
}
if (id != s.getId()) {
return Integer.compare(id, s.getId());
}
if (!name.equals(s.getName())) {
return name.compareTo(s.getName());
//changed implementation to satisfy webcat? nvm...
//return name.length() - s.getName().length();
}
return 0;
}
/**
* Comparator for students objects.
* @return comparator
*/
public static Comparator<Student> studentOrder() {
return new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.compareTo(o2);
}
};
}
/**
* @return true if the student is in the list
*/
public boolean isInDeansList() {
if (gpa <= 0) {
throw new NotInDeansListException("not in deans list");
//for coverage
}
if (gpa <= 2.0) {
throw new NotInDeansListException();
}
return gpa >= 3.5;
}
/**
* @return true if the student is on probation
*/
public boolean isInAcademicProbation() {
if (gpa >= 10.0) {
throw new NotInAcademicProbationException("message for coverage");
//for coverage
}
if (gpa >= 3.5) {
throw new NotInAcademicProbationException();
}
return gpa <= 2.0;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the gpa
*/
public double getGpa() {
return gpa;
}
/**
* @param gpa the gpa to set
*/
public void setGpa(double gpa) {
this.gpa = gpa;
}
}
StudentTest.java
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import student.TestCase;
public class StudentTest extends TestCase {
private Student d;
private Student b;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
d = new Student("dragon", 1000, 1.00);
b = new Student("baron", 3000, 20.00);
}
/**
* Test method for {@link Student#compareTo(Student)}.
* A lexicographic implemenation didn't change the error.
* I'll try using the pure java comparison operators.
*/
@Test
public void testCompareTo() {
assertTrue(b.compareTo(d) > 0);
//recall, double field is casted to int
b = new Student("baron", 50, 1.00);
assertTrue(b.compareTo(d) < 0);
b = new Student("baron", 1000, 1.00);
assertEquals(-2, b.compareTo(d));
//assertEquals(-1, b.compareTo(d));
assertEquals(0, b.compareTo(b));
}
/**
* Test method for {@link Student#studentOrder()}.
*/
@Test
public void testStudentOrder() {
Student r = new Student("rift herald", 2000, 9.30);
Student[] obj = {d, b, r};
Arrays.sort(obj, Student.studentOrder());
assertEquals(d, obj[0]);
assertEquals(b, obj[2]);
}
/**
* Test method for {@link Student#isInDeansList()}.
*/
@Test
public void testIsInDeansList() {
assertEquals(true, b.isInDeansList());
try {
d.isInDeansList();
}
catch (Exception e) {
assertTrue(e instanceof NotInDeansListException);
}
try {
d = new Student("dragon", 30, -5.0);
d.isInDeansList();
}
catch (Exception e) {
assertTrue(e instanceof NotInDeansListException);
}
}
/**
* Test method for {@link Student#isInAcademicProbation()}.
*/
@Test
public void testIsInAcademicProbation() {
assertEquals(true, d.isInAcademicProbation());
try {
b.isInAcademicProbation();
}
catch (Exception e) {
assertTrue(e instanceof NotInAcademicProbationException);
}
//now without a message
try {
b = new Student("baron", 10, 5.00);
b.isInAcademicProbation();
}
catch (Exception e) {
assertTrue(e instanceof NotInAcademicProbationException);
}
}
/**
* Test method for {@link Student#getName()}.
*/
@Test
public void testGetName() {
assertEquals("dragon", d.getName());
}
/**
* Test method for {@link Student#setName(java.lang.String)}.
*/
@Test
public void testSetName() {
d.setName("infernal");
assertEquals("infernal", d.getName());
}
/**
* Test method for {@link Student#getId()}.
*/
@Test
public void testGetId() {
assertEquals(1000, d.getId());
}
/**
* Test method for {@link Student#setId(int)}.
*/
@Test
public void testSetId() {
d.setId(5);
assertEquals(5, d.getId());
}
/**
* Test method for {@link Student#getGpa()}.
*/
@Test
public void testGetGpa() {
assertEquals(20.00, b.getGpa(), 0.01);
}
/**
* Test method for {@link Student#setGpa(double)}.
*/
@Test
public void testSetGpa() {
d.setGpa(2.0);
assertEquals(2.0, d.getGpa(), 0.01);
}
}
SubArray.java
public class SubArray extends SuperArray {
/**
*
*/
private int addCount;
/**
* @param arr the object to base on
*/
public SubArray(Object[] arr) {
super(arr);
setAddCount(0);
}
/**
* No parameter creation
*/
public SubArray() {
super();
setAddCount(0);
}
/**
* @return the addCount
*/
public int getAddCount() {
return addCount;
}
/**
* @param addCount the addCount to set
*/
public void setAddCount(int addCount) {
this.addCount = addCount;
}
/**
* Insert an object into a SuperArray object.
* @param anEntry the object to be added
*/
public void add(Object anEntry) {
addCount++;
super.add(anEntry);
}
/**
* Insert all the elements of an array into a SuperArray object.
* @param c the array
*/
public void addAll(Object[] c) {
addCount += c.length;
super.addAll(c);
}
}
SubArrayTest.java
import org.junit.Before;
import org.junit.Test;
import student.TestCase;
public class SubArrayTest extends TestCase {
private SubArray sa;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
sa = new SubArray();
}
/**
* Test method for {@link SubArray#add(java.lang.Object)}.
*/
@Test
public void testAdd() {
sa.add(3);
assertEquals(1, sa.getAddCount());
}
/**
* Test method for {@link SubArray#addAll(java.lang.Object[])}.
*/
@Test
public void testAddAll() {
String [] a = {"a", "b", "c", "d"};
sa.addAll(a);
assertEquals(8, sa.getAddCount());
}
/**
* Test method for {@link SubArray#getAddCount()}.
*/
@Test
public void testGetAddCount() {
assertEquals(0, sa.getAddCount());
}
/**
* Test method for {@link SubArray#setAddCount(int)}.
*/
@Test
public void testSetAddCount() {
sa.setAddCount(5);
assertEquals(5, sa.getAddCount());
}
/**
* for the other constructor
*/
public void testSubArrayObjectArray() {
String [] a = {"a", "b", "c", "d"};
SubArray s = new SubArray(a);
assertNotNull(s);
}
}
SuperArray.java
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 to base on
*/
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.java
import org.junit.Before;
import org.junit.Test;
import student.TestCase;
public class SuperArrayTest extends TestCase {
private SuperArray sua;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
String[] a = { "a", "b", "c", "d" };
sua = new SuperArray(a);
}
/**
* Test method for {@link SuperArray#add(java.lang.Object)}.
*/
@Test
public void testAdd() {
sua.add("e");
assertEquals(5, sua.getSize());
//there isn't a way to access the array directly
//so I'm using getSize as a proxy check.
}
/**
* Test method for {@link SuperArray#addAll(java.lang.Object[])}.
*/
@Test
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());
}
/**
* Test method for {@link SuperArray#getSize()}.
*/
@Test
public void testGetSize() {
assertEquals(4, sua.getSize());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.