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...
I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...
Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java. Create two child classes named Cat.java and Dog.java. Cat.java should add attributes for color and breed. Dog.java should add attributes for breed and size (use String for small, medium, large). Add appropriate constructors, getter/setter methods and toString() methods. In a separate file named PetDriver.java, create a main. In the main, create an ArrayList of Pet. Add an instance of Cat and an instance of Dog to the ArrayList....
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...
I need help with the last method listed in the problem: Implement a class Grid that stores measurements in a rectangular grid. The grid has a given number of rows and columns, and a description string can be added for any grid location. Supply the following constructor and methods: public Grid(int numRows, int numColumns) public void add(int row, int column, String description) public String getDescription(int row, int column) public ArrayList getDescribedLocations() Here, Location is an inner class that encapsulates the...