Question
Can anybody help me with this lab question? Here's the introduction A LIST OF REFERENCES TO CLASSES THAT IMPLEMENT PERSON For this lab you will
Can anybody help me with this lab question?
Here's the introduction
A LIST OF REFERENCES TO CLASSES THAT IMPLEMENT PERSON For this lab you will be given an interface called Person. We have provided you with the ExamplePerson class that implements that interface. First, you'll need to complete the AnotherExamplePerson class so that it also implements Person. There are some important comments in that file telling you how it is different from the ExamplePerson class. The MyPersonList class will use an ArrayList behind the scenes. Some things are just like they were in the previous lab, but some are different this time. Part of this is because MyPersonList implements the Collection interface. Some of the code has been provided, and specifications are written in the comment above each method you need to complete. To make deep copies, we can no longer use a copy constructor. Look at the Person interface to think about what it requires and how it can be utilized. The StarterTests.java file contains some examples of JUnit testing for this type of coding problem. When you check the lab out, most of the starter tests are commented out. This is because the AnotherExamplePerson class needs to be written before those tests can compile.
and here's the code of the lab
package studentCode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class MyPersonList implements Collection {
//Keeping this package for easier testing...
ArrayList people;
/**
* Instantiates the "people" variable as a new ArrayList of
* references to Person objects.
*/
public MyPersonList(){
people = new ArrayList(0);
}
/**
* A copy constructor which makes the right kind of copy considering
* a Person is mutable.
*/
public MyPersonList(MyPersonList other) {
throw new RuntimeException("You need to implement this one!");
}
/** Adds the parameter to the end of the list.
*
* Note that you want to make sure that you don't just do a reference copy
* of the newMember Person since it is mutable but also you don't know
* what specific type of Person it is, so you can't use a copy constructor.
*/
@Override
public boolean add(Person newMember){
throw new RuntimeException("You need to implement this one!");
}
/** Gives each person in the list a raise of $1000. Note that the objects
* are mutable, which means you can use a for-each style loop as you update.
*/
public void giveRaises(){
for (Person currPerson : people) {
currPerson.acceptRaise(1000);
}
}
/** Returns the sum of the salaries of all people in the list.
*/
public int getTotalOfSalaries(){
int total = 0;
for (Person currPerson : people) {
total += currPerson.getSalary();
}
return total;
}
/** Returns the number of people in the list with a name
* that matches the parameter (possibly 0).
*/
public int count(String searchName){
int count = 0;
for (Person currPerson : people) {
if (currPerson.getName().equals(searchName)) {
count++;
}
}
return count;
}
/** Returns the number of people in the list.
*/
@Override
public int size() {
throw new RuntimeException("You need to implement this one!");
}
/** Returns whether the list is empty.
*/
@Override
public boolean isEmpty() {
throw new RuntimeException("You need to implement this one!");
}
/** Removes ALL people from the list whose names match the parameter
* (possibly more than one person). Consider that a for-each style
* loop cannot be used unless you create a shadow. There are code
* examples in the posted slides that will be useful here.
*/
public void remove(String name){
ArrayList peopleShadow = new ArrayList(people);
for (Person currPerson : peopleShadow) {
if (currPerson.getName().equals(name)) {
people.remove(currPerson);
}
}
}
/** Removes all people from the list.
*/
@Override
public void clear() {
throw new RuntimeException("You need to implement this one!");
}
/** Returns a String which is a concatenation of each individual
* person in the list being turned into a String.
*/
@Override
public String toString() {
String result = "";
for (Person currPerson : people) {
result += currPerson.toString() + " ";
}
return result;
}
// ========= showing how to "pass the buck" to the ArrayList =========
//Since the people list is an ArrayList, it already has methods to
// do the following ones so we show here how you could "pass the
// buck" to it. We do it for these for you because there are
// things in the parameters we have not yet discussed, and are
// unlikely to until CMSC132.
@Override
public boolean addAll(Collection otherCollection) {
return people.addAll(otherCollection);
}
@Override
public boolean contains(Object other) {
return people.contains(other);
}
@Override
public boolean containsAll(Collection otherCollection) {
return people.containsAll(otherCollection);
}
@Override
public boolean remove(Object other) {
return people.remove(other);
}
@Override
public boolean removeAll(Collection otherCollection) {
return people.removeAll(otherCollection);
}
@Override
public boolean retainAll(Collection otherCollection) {
return people.retainAll(otherCollection);
}
@Override
public Iterator iterator() {
return people.iterator();
}
@Override
public Object[] toArray() {
return people.toArray();
}
@Override
public T[] toArray(T[] a) {
return people.toArray(a);
}
}
StarterTest
package studentCode;
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.Test;
import studentCode.AnotherExamplePerson;
import studentCode.MyPersonList;
import studentCode.Person;
import studentCode.ExamplePerson;
public class StarterTests {
@Test
public void testConstructor() {
MyPersonList localList = new MyPersonList();
assertTrue(localList != null);
assertTrue(localList.people != null);
assertTrue(localList.people.size() == 0);
}
/*
@Test
public void testAdd() {
Person p1 = new ExamplePerson("Sam");
Person p2 = new AnotherExamplePerson("Pat");
Person p3 = new ExamplePerson("Sasha");
MyPersonList localPersonList = new MyPersonList();
localPersonList.add(p1);
localPersonList.add(p2);
localPersonList.add(p3);
assertEquals(p1, localPersonList.people.get(0));
assertEquals(p2, localPersonList.people.get(1));
assertEquals(p3, localPersonList.people.get(2));
}
@Test
public void testAddUsingGiveRaisesAndTotalSalaries() {
MyPersonList localList = new MyPersonList();
Person p1 = new ExamplePerson("Sam");
Person p2 = new AnotherExamplePerson("Pat");
Person p3 = new ExamplePerson("Sasha");
localList.add(p1);
localList.add(p2);
localList.add(p3);
localList.giveRaises();
localList.giveRaises();
assertEquals(31000, localList.getTotalOfSalaries());
assertEquals(10000, p1.getSalary());
assertEquals( 5000, p2.getSalary());
assertEquals(10000, p3.getSalary());
}
@Test
public void testSize() {
MyPersonList localList = new MyPersonList();
Person p1 = new ExamplePerson("Sam");
Person p2 = new AnotherExamplePerson("Pat");
Person p3 = new ExamplePerson("Sasha");
localList.add(p1);
localList.add(p2);
localList.add(p3);
assertEquals(3, localList.size());
localList.add(new ExamplePerson("Sam"));
localList.add(new AnotherExamplePerson("Sam"));
localList.add(new ExamplePerson("Sasha"));
assertEquals(6, localList.size());
}
@Test
public void testClearL8() {
MyPersonList localList = new MyPersonList();
Person p1 = new ExamplePerson("Sam");
Person p2 = new AnotherExamplePerson("Pat");
Person p3 = new ExamplePerson("Sasha");
localList.add(p1);
localList.add(p2);
localList.add(p3);
assertEquals(3, localList.size());
localList.add(new ExamplePerson("Sam"));
localList.add(new AnotherExamplePerson("Sam"));
localList.add(new ExamplePerson("Sasha"));
assertEquals(6, localList.size());
localList.clear();
assertEquals(0, localList.size());
}
@Test
public void testPrinting() {
MyPersonList localList = new MyPersonList();
Person p1 = new ExamplePerson("Sam");
Person p2 = new AnotherExamplePerson("Pat");
Person p3 = new ExamplePerson("Sasha");
localList.add(p1);
localList.add(p2);
localList.add(p3);
assertEquals(
"EP:Sam(10000)AEP:Pat(5000)EP:Sasha(10000)",
localList.toString().replace(" ","")
);
}
*/
}
Person
package studentCode;
public interface Person {
/** Returns a copy of the Person */
public Person copyMe();
/** Returns the person's name */
public String getName();
/** Returns the person's annual salary */
public int getSalary();
/** Increases this person's salary by the amount of the parameter */
public void acceptRaise(int amount);
/** Turns the person into a String */
public String toString();
}
Sorry for my mistake Thanks.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started