Question: Hi, I am having trouble solving the constructor, it requires to ArrayList marks to be a deep copy of the instance variable. I have also
Hi,
I am having trouble solving the constructor, it requires to ArrayList marks to be a deep copy of the instance variable. I have also provided the J-Unit test I have been provided.
import java.util.*;
public class GradebookEntry {
private String name;
private int id;
private ArrayList
/**
* DO NOT MODIFY
*/
public String toString() {
String result = name+" (ID "+id+")\t";
for(int i=0; i < marks.size(); i++) {
/**
* display every mark rounded off to two digits.
* adding the 0.00001 to compensate for any rounding-off errors.
*/
double displayMark = (int)((marks.get(i)+0.00001)*100)/100.0;
result+=displayMark+"\t";
}
double displayTotal = (int)(getTotal()*100)/100.0;
return result.substring(0, result.length()-1)+"\t(Total "+displayTotal+") ";
}
/**
* DO NOT MODIFY
* @return name of the student
*/
public String getName() {
return name;
}
/**
* DO NOT MODIFY
* @return id
*/
public int getId() {
return id;
}
/**
* DO NOT MODIFY
* @return number of assessments
*/
public int numberOfAssessments() {
return marks.size();
}
/**
* set the instance variable id to the parameter provided.
* if i is less than 1, id should become 1.
* otherwise, id should become i.
* @param i
*/
private void setId(int i) {
if( i < 1){
id = 1;
} else {
id = i;
}
}
/**
* set instance variables name, id and marks to corresponding parameter provided.
* use the setter to set the id.
* you should make a deep copy for marks, NOT A SHALLOW COPY
* @param name
* @param id
* @param marks
*/
public GradebookEntry(String name, int id, ArrayList
this.name = name;
this.setId(id);
//Stuck on setting marks, there is no setMarks method either
}
}
This is the J-Unit Test they provided as well:
@Test
public void testGradebookEntry() {
GradebookEntry entry = new GradebookEntry("Joshua Lyman", 5, new ArrayList
assertEquals("Joshua Lyman", entry.getName());
assertEquals(5, entry.getId());
assertEquals(4, entry.numberOfAssessments());
assertEquals(1.5, entry.getMark(0), 0.0001);
assertEquals(4.5, entry.getMark(1), 0.0001);
assertEquals(2.5, entry.getMark(2), 0.0001);
assertEquals(8.5, entry.getMark(3), 0.0001);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
