Question: This assignment requires you to use the following specification and UML description to implement a class from scratch and have it pass the test harness

This assignment requires you to use the following specification and UML description to implement a class from scratch and have it pass the test harness tests.

Create the week10 package in your project Download the following files to the project:

TestHarness.javaThis assignment requires you to use the following, TestCaseCollection.javaThis assignment requires you to use the following, Person.javaThis assignment requires you to use the following, CollectionExample.javaThis assignment requires you to use the following Add a class called CollectionAssignment Implement the following methods // This method takes a double array, sums the data and returns the sum public double calculateSum(double[] data); // This method takes an integer array, calculates the product of all the integers and returns the result public int calculateProduct(int[] data); // This method takes a List collection, calculates the sum and returns the result public Integer calculateIntegerSum(List data); // This method sets the Person age for each person in the list to the provided value // The methods available on the Person class are person.setAge(int age); // The Person class is provided to you; you simply have to use the setAge method public void setPersonAge(List personList, int age); // This method takes a person array (Person[]), creates a new ArrayList // and populates the ArrayList instance with the person array data and // returns the new ArrayList instance public ArrayList copyArrayToList(Person[] personList);

Submit CollectionAssignment.java

Be sure it passes the test harness.

package week10;

import java.util.HashSet;

import java.util.Iterator;

import java.util.Random;

public class CollectionExample

{

public static void main(String[] args)

{

trace("Collection Example! ");

demoPrimitiveArray();

trace("---------");

demoClassArray();

trace("---------");

demoHashSet();

trace("---------");

// Passing arrays as parameters

double[] doubleArray = new double[]{1.1,2.3,4.3,4.5,6.5};

Person[] persons = new Person[] { new Person("Mickey"),

new Person("Donald"), new Person("Daisy")};

demoParameterPassing(doubleArray, persons);

trace("---------");

demo2dArray();

}

public static void demoPrimitiveArray()

{

trace("demoPrimitiveArray");

double[] doubleArray = new double[]{1.1, 2.3, 4.3, 4.5,

6.5};

double sum = 0.0;

for(double d : doubleArray)

{

sum += d;

}

trace("Array size: " + doubleArray.length);

trace("Sum: " + sum);

}

public static void demoClassArray()

{

trace("demoClassArray");

Person[] persons = new Person[] { new Person("Mickey"),

new Person("Donald"), new Person("Daisy")};

Person[] persons2 = { new Person("Mickey1"), new

Person("Donald1"), new Person("Daisy1")};

trace("Array size: " + persons.length);

trace("Array size: " + persons2.length);

for(Person p : persons)

{

trace(p.getName());

}

for(Person p : persons2)

{

trace(p.getName());

}

}

public static void demoHashSet()

{

trace("demoHashSet");

int size;

HashSet collection = new HashSet();

String str1 = "Yellow";

String str2 = "White";

String str3 = "Green";

String str4 = "Blue";

Iterator iterator;

collection.add(str1);

collection.add(str2);

collection.add(str3);

collection.add(str4);

trace("Collection data: ");

iterator = collection.iterator();

while(iterator.hasNext())

{

trace(iterator.next() + " ");

}

System.out.println();

size = collection.size();

if(collection.isEmpty())

{

trace("Collection is empty");

}

else

{

trace("Collection size: " + size);

}

System.out.println();

}

public static void demoParameterPassing(double[] doubleList,

Person[] personList)

{

trace("demoParameterPassing");

double[] doubleArray = doubleList;

double sum = 0.0;

for(double d : doubleArray)

{

sum += d;

}

trace("Sum: " + sum);

Person[] persons = personList;

trace("Array size: " + persons.length);

for(Person p : persons)

{

trace(p.getName());

}

}

public static void demo2dArray()

{

double[][] payScaleTable = new double[4][5];

Random rand = new Random();

// Initialize the array

for(int i = 0; i

{

for(int j = 0; j

j++)

{

payScaleTable[i][j] =

rand.nextInt(100);

}

}

// dump the contents of the array

for(int i = 0; i

{

for(int j = 0; j

j++)

{

trace(Double.toString(payScaleTable[i][j]));

}

}

}

private static void trace(String msg)

{

System.out.println(msg);

}

}

package week10;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import test.AbstractTestCase;

/**

* Tests the Fermi game implementation

*

* @author slachanc

*

*/

public class TestCaseCollection extends AbstractTestCase

{

CollectionAssignment m_collection;

/**

* Constructor

*/

public TestCaseCollection()

{

super("TestCaseCollection");

}

@Override

protected boolean runTest()

{

m_results = "";

m_collection = new CollectionAssignment();

boolean test1 = testCalculateSum();

boolean test2 = calculateProduct();

boolean test3 = calculateIntegerSum();

boolean test4 = setPersonAge();

boolean test5 = copyArrayToList();

return test1 && test2 && test3 && test4 && test5;

}

@Override

protected String results()

{

return m_results;

}

private boolean testCalculateSum()

{

trace("Testing calculateSum");

double[] doubleArray = new double[] { 1.1, 2.3, 4.3, 4.5,

6.5 };

double expected = 0.0;

for(double d : doubleArray)

{

expected += d;

}

double actual = m_collection.calculateSum(doubleArray);

boolean result = expected == actual;

if(!result)

{

trace("Failed - Expected: " + expected + ",

actual: " + actual);

}

return result;

}

private boolean calculateProduct()

{

trace("Testing calculateProduct");

int[] array = new int[] { 1, 1, 2, 3, 4, 3, 4, 5, 6, 5 };

int expected = 1;

for(int d : array)

{

expected *= d;

}

int actual = m_collection.calculateProduct(array);

boolean result = expected == actual;

if(!result)

{

trace("Failed - Expected: " + expected + ",

actual: " + actual);

}

return result;

}

private boolean calculateIntegerSum()

{

trace("Testing calculateIntegerSum");

Integer[] array = { 1, 1, 2, 3, 4, 3, 4, 5, 6, 5 };

List arrayList = new ArrayList();

for(Integer i : array)

{

arrayList.add(i);

}

Integer expected = 0;

for(Integer d : arrayList)

{

expected += d;

}

Integer actual =

m_collection.calculateIntegerSum(arrayList);

boolean result = expected == actual;

boolean result2 = expected.equals(actual);

if(!result)

{

trace("Failed - Expected: " + expected + ",

actual: " + actual);

}

if(!result2)

{

trace("Failed 2 - Expected: " + expected + ",

actual: " + actual);

}

return result;

}

private boolean setPersonAge()

{

trace("Testing setPersonAge");

boolean result = true;

List persons = new ArrayList();

persons.add(new Person("Dave"));

persons.add(new Person("Todd"));

persons.add(new Person("Barry"));

persons.add(new Person("Jim"));

persons.add(new Person("Gerald"));

persons.add(new Person("Tim"));

int expected = 45;

m_collection.setPersonAge(persons, expected);

for(Person p : persons)

{

int actual = p.getAge();

if(expected != actual)

{

trace("Failed - expected: " + expected

+ ", actual: " + actual);

result = false;

break;

}

}

return result;

}

private boolean copyArrayToList()

{

trace("Testing copyArrayToList");

boolean result = true;

Person[] persons = { new Person("Dave", 45), new

Person("Todd", 47),

new Person("Barry", 44), new

Person("Jim", 50), new Person("Gerald", 42),

new Person("Tim", 48) };

ArrayList actual =

m_collection.copyArrayToList(persons);

if(persons.length == actual.size())

{

for(int i = 0; i

{

Person p1 = persons[i];

Person p2 = actual.get(i);

if( !p1.equals(p2))

{

trace("Failed - Person.

Expected: " + p1.getName() + ", " + p1.getAge() + ", actual: " +

p2.getName() + ", " + p2.getAge());

result = false;

break;

}

}

}

else

{

trace("Failed - array size mismatch.

Expected: " + persons.length + ", actual: " + actual.size());

result = false;

}

trace("Completed testing copyArrayToList");

return result;

}

private String m_results;

}

package week10;

import test.TestEngine;

/**

* File: TestHarness.java

*/

class TestHarness

{

public static void main(String[] args)

{

trace("Starting test...");

trace(" -- setup test data");

TestEngine engine = new TestEngine();

engine.addTest(new TestCaseCollection());

engine.runTests();

trace("Completed test");

}

static private void trace(String msg)

{

System.out.println(msg);

}

}

package week10;

/**

* Represents a Person

* Basic class to support week10 assignment

*

* @author scottl

*

*/

public class Person

{

// Constructor

public Person(String name)

{

// call the other constructor with a default age

this(name, 0);

}

/**

* The main constructor

*

* @param name Person name

* @param age Person age

*/

public Person(String name, int age)

{

m_name = name;

m_age = age;

}

// Setters/Getters

public String getName() {return m_name;}

public int getAge() {return m_age;}

public void setAge(int age) { m_age = age;}

/**

* This overrides the Object.equals method so

* we can accurately determine whether the person object

* is equal at the data level (name and age).

*/

@Override

public boolean equals(Object obj)

{

boolean equal = false;

if( obj instanceof Person) // must be an instance of a

Person object

{

Person p = (Person)obj; // cast to a person

object

// Use the Person methods to determine

equality.

// remebmer to use equals for strings

boolean equalName =

this.getName().equals(((Person)p).getName());

boolean equalAge = this.getAge() ==

p.getAge();

equal = equalName && equalAge;

}

return equal;

}

private String m_name;

private int m_age;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!