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<Double> marks;
/**
* 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+")\n";
}
/**
* 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<Double> marks) {
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<Double>(Arrays.asList(1.5, 4.5, 2.5, 8.5)));
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);
}
Answer:
GradebookEntry.java
import java.util.*;
public class GradebookEntry {
private String name;
private int id;
private ArrayList<Double> marks;
/** 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 + ")\n";
}
public double getTotal() {
double total = 0;
for (int i = 0; i < marks.size(); i++) {
total = total + marks.get(i);
}
return total;
}
public double getMark(int index) {
return marks.get(index);
}
/** 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<Double> marks) {
this.name = name;
this.setId(id);
//this.marks = marks;
this.marks = makeDeepCopyInteger(marks);
// Stuck on setting marks, there is no setMarks method either
}
private ArrayList<Double> makeDeepCopyInteger(ArrayList<Double> old) {
ArrayList<Double> copy = new ArrayList<Double>(old.size());
for (Double i : old) {
copy.add(new Double(i));
}
return copy;
}
}
TestGradebookEntry.java
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.Test;
import com.gradbook.GradebookEntry;
public class TestGradebookEntry {
@Test
public void testGradebookEntry() {
GradebookEntry entry = new GradebookEntry("Joshua Lyman", 5,
new ArrayList<Double>(Arrays.asList(1.5, 4.5, 2.5, 8.5)));
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);
}
}
Output:
Hi, I am having trouble solving the constructor, it requires to ArrayList marks to be a...
Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...
C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employees to be compared by the Name, Age, and Pay, respectively. Code: Employee.Core.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Employees { partial class Employee { // Field data. private string empName; private int empID; private float currPay; private int empAge; private string empSSN = ""; private string empBene...
Ive got this started but im really struggling its a c# program, I have to modify the class Purse to implement the interface ICloneable . create a main program that demonstrates that the Purse method Clone works. using System; using System.Collections; namespace TestingProject { /// <summary> /// A coin with a monetary value. /// </summary> public class Coin { /// Constructs a coin. /// @param aValue the monetary value of the coin ...
Hey guys I am having trouble getting my table to work with a java GUI if anything can take a look at my code and see where I am going wrong with lines 38-49 it would be greatly appreciated! Under the JTableSortingDemo class is where I am having erorrs in my code! Thanks! :) import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import javax.swing.*; public class JTableSortingDemo extends JFrame{ private JTable table; public JTableSortingDemo(){ super("This is basic sample for your...
Given attached you will find a file from Programming Challenge 5 of chapter 6 with required you to write a Payroll class that calculates an employee’s payroll. Write exception classes for the following error conditions: An empty string is given for the employee’s name. An invalid value is given to the employee’s ID number. If you implemented this field as a string, then an empty string could be invalid. If you implemented this field as a numeric variable, then a...
I am having trouble finding any logical errors...Any advise? Thank you public class Brick { // Constant. private static final int WEIGHT_PER_CM3 = 2; // weight per cubic cm in grams private int height; private int width; private int depth; /** * Create a Brick given edge lengths in centimeters. * @param height The brick's height. * @param width The brick's width. * @param depth The brick's depth. */ public Brick(int height, int width, int depth) { this.height = height;...
Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed from block-letters of size 7. Each block-letter is composed of 7 horizontal line-segments of width 7 (spaces included): SOLID as in block-letters F, I, U, M, A, S and the blurb RThere are six distinct line-segment types: TRIPLE as in block-letter M DOUBLE as in block-letters U, M, A LEFT_DOT as in block-letters F, S CENTER_DOT as in block-letter...
This is a simple C++ class using inheritance, I have trouble getting the program to work. I would also like to add ENUM function to the class TeachingAssistant(Derived class) which is a subclass of Student(Derived Class) which is also a subclass of CourseMember(Base Class). The TeachingAssistant class uses an enum (a user-defined data type) to keep track of the specific role the TA has: enum ta_role {LAB_ASSISTANT, LECTURE_ASSISTANT, BOTH}; You may assume for initialization purposes that the default role is...
I was wondering if I could get some help with a Java Program that I am currently working on for homework. When I run the program in Eclipse nothing shows up in the console can you help me out and tell me if I am missing something in my code or what's going on? My Code: public class Payroll { public static void main(String[] args) { } // TODO Auto-generated method stub private int[] employeeId = { 5658845, 4520125, 7895122,...