Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with this Java code, I am having issues with the methods getWorth and sortRecords they are giving a NullpointerException for some reason and not

Help with this Java code, I am having issues with the methods getWorth and sortRecords they are giving a NullpointerException for some reason and not too sure the methods after that either. Please dont change the method headers. Thank you

CODE-

ASSESSMENT CLASS-

package toBeCompleted.stage1;

public class Assessment {

private String title;

private double worth;

/**

* DO NOT MODIFY

* @return worth

*/

public double getWorth() {

return worth;

}

/**

* DO NOT MODIFY

* @return title

*/

public String getTitle() {

return title;

}

/**

* IMPORTANT setWorth is set to be a private method as

* we don't want its value to change after instantiation.

* set the instance variable worth to the parameter provided.

* if w is less than 0, worth should become 0.

* if w is more than 100, worth should become 100.

* if w is in the range [0, 100], worth should become w.

* @param w

*/

private void setWorth(double w) {

if (w < 0)

worth = 0;

else if (w > 100)

worth = 100;

else

worth = w;

//to be completed

}

/**

* set title to upper case version of t,

* set worth to w using the setter

* @param t

* @param w

*/

public Assessment(String t, double w) {

title = t.toUpperCase(); //Using a built-in method to make the string appear in upper case

setWorth(w);

//to be completed

}

/**

* return String representation of the object.

* format example: if title is "A1" and worth is 25,

* method should return "A1 (25.0)"

*/

public String toString() {

String s="";

s =this.title+ " " + "(" + worth + ")";

return s;

//to be completed

}

/**

*

* @param other

* @return 1 if calling object is worth more than parameter object

* @return -1 if calling object is worth less than parameter object

* @return 0 if calling object is worth the same as parameter object

*/

public int compareTo(Assessment other) {

if(this.worth > other.worth)

return 1;

if (this.worth < other.worth)

return -1;

else

return 0; //if both conditions pass then both objects are the same

//to be completed

}

/**

*

* @param marks

* @return true if the parameter is a valid mark for the assessment, false otherwise

*/

public boolean isValidMark(double marks) {

if (marks >= 0 && marks <= worth) //checking if is a valid mark

return true;

else

return false;

//to be completed

}

}

GRADEBOOKENTRY CLASS-

package toBeCompleted.stage2;

import java.util.*;

public class GradebookEntry {

private String name;

private int id;

private ArrayList 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+") ";

}

/**

* 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) //Checking condition

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 marks) {

this.name = name;

setId(id);

this.marks = new ArrayList(); //Deep copy for marks

for(int i=0;i

this.marks.add(marks.get(i)); //Going through the ArrayList and adding entries

//to be completed

}

/**

*

* @return total marks (0 if 0 assessments)

*/

public double getTotal() {

double totalMarks= 0;

for(int i=0;i

totalMarks+=marks.get(i); //Adding total number of marks from ArrayList of marks

}

return totalMarks;

//to be completed

}

/**

*

* @return average marks (null if 0 assessments)

*/

public Double getAverage() {

if(numberOfAssessments()== 0)

return null;

else

return (getTotal()/numberOfAssessments()); //Getting the average value of the marks

}

/**

*

* @param assessmentNumber

* @return true if assessmentNumber is valid, false otherwise.

* For example if there were 5 assessments, the valid assessment numbers are from 0 to 4 (including 0 and 4).

*/

public boolean isValidAssessmentNumber(int assessmentNumber) {

if(assessmentNumber>=0 && assessmentNumber

return true;

else

return false;

}

/**

*

* @param assessmentNumber

* @return mark in assessment with given index

* return null if no such assessment exists

*/

public Double getMark(int assessmentNumber) {

if(!isValidAssessmentNumber(assessmentNumber))

return null;

else

return (marks.get(assessmentNumber));

}

/**

*

* @param other

* @return 1 if calling object's total is more than parameter's total

* @return -1 if calling object's total is less than parameter's total

* @return 0 if calling object's total is equal to parameter's total

*/

public int compareTo(GradebookEntry other) {

if(getTotal() > other.getTotal())

return 1;

else if(getTotal() < other.getTotal())

return -1;

else

return 0; //to be completed

}

/**

*

* @param other

* @param assessmentBasis: index of assessment on the basis of which comparison is being made

* @return null if assessmentBasis is an invalid index (for calling object or parameter object)

* @return 1 if calling object's mark in the concerned assessment is more than parameter's mark in the concerned assessment

* @return -1 if calling object's mark in the concerned assessment is less than parameter's mark in the concerned assessment

* @return 0 if calling object's mark in the concerned assessment is equal to parameter's mark in the concerned assessment

*/

public Integer compareTo(GradebookEntry other, int assessmentBasis) {

if(isValidAssessmentNumber(assessmentBasis) && other.isValidAssessmentNumber(assessmentBasis)){

if(getMark(assessmentBasis) > other.getMark(assessmentBasis))

return 1;

else if(getMark(assessmentBasis) < other.getMark(assessmentBasis))

return -1;

else

return 0;

}

else

return null;//to be completed

}

/**

* swap marks of the assessments at the two given indices.

* return without doing anything if either index is invalid

* @param idx1

* @param idx2

*/

public void swap(int idx1, int idx2) {

if(isValidAssessmentNumber(idx1) && isValidAssessmentNumber(idx2)){

double tempMarks = getMark(idx1); //Creating an object to temporarily store the value of idx1

marks.add(idx1, marks.get(idx2));

marks.remove(idx1+1); // Since the marks at idx2 has been inserted in idx1 therefore the original marks at idx1 is moved to idx1+1

marks.add(idx2, tempMarks);

marks.remove(idx2+1);

}

//to be completed

}

/**

* if the assessment number is a valid index, and the mark is in the range [0, maxMark],

* set the mark of the given assessment number to the mark supplied

* @param assessmentNumber

* @param mark

* @param maxMark

*/

public void setMarks(int assessmentNumber, double mark, double maxMark) {

if(isValidAssessmentNumber(assessmentNumber) && mark >=0 && mark <=maxMark)

marks.add(assessmentNumber,mark);

}

}

GRADEBOOK CLASS-

package toBeCompleted.stage3;

import java.util.*;

import toBeCompleted.stage1.Assessment;

import toBeCompleted.stage2.GradebookEntry;

public class Gradebook {

private ArrayList assessments = new ArrayList();

private ArrayList records = new ArrayList();

/**

* DO NOT MODIFY

* @return number of records

*/

public int numberOfRecords() {

return records.size();

}

/**

* DO NOT MODIFY

* @return number of assessments

*/

public int numberOfAssessments() {

return assessments.size();

}

/**

* DO NOT MODIFY

*/

public String toString() {

String header1 = "Asssessment title: \t";

String header2 = "Asssessment worth: \t";

double totalWorth = 0;

for(Assessment a: assessments) {

header1 = header1 + a.getTitle() + "\t";

header2 = header2 + "(" + a.getWorth() + ")\t";

totalWorth += a.getWorth();

}

header2 = header2 + "(Total "+totalWorth+")";

String data = records.toString();

data = data.replace("[", "");

data = data.replace("]", "");

data = data.replaceAll(", ", "");

return header1+" "+header2+" "+data;

}

/**

* make a deep copy of the parameters into the instance variables

* @param c

* @param r

*/

public Gradebook(ArrayList c, ArrayList r) {

//to be completed

for (Assessment a : c) {

assessments.add(a);

}

for (GradebookEntry g : r) {

records.add(g);

}

}

/**

*

* @param recordNumber

* @return true if the given record number is valid.

* For example, if there are 9 records, valid record numbers

* are from 0 to 8 (including 0 and 8)

*/

public boolean isValidRecordNumber(int recordNumber) {

if (recordNumber >= 0 && recordNumber < records.size())

return true;

else

return false;

//to be completed

}

/**

*

* @param assessmentNumber

* @return true if assessmentNumber is valid, false otherwise

*/

public boolean isValidAssessmentNumber(int assessmentNumber) {

if (assessmentNumber >= 0 && assessmentNumber < assessments.size())

return true;

else

return false; // to be completed

}

/**

*

* @param assessmentTitle

* @return how much is the assessment worth. return null if assessment not found

*/

public Double getWorth(String assessmentTitle) {

for (Assessment a : assessments) {

if (assessmentTitle == a.getTitle())

return a.getWorth();

}

return null;

}// to be completed }

/**

* sort the records in descending order of totals.

* hint: you can use the method compareTo from class GradebookEntry

*/

public void sortRecords() {

for (int i = 0; i < records.size(); i++) {

for (int j = i + 1; j < records.size(); j++) {

if (records.get(i).compareTo(records.get(j))==1) {

GradebookEntry t = records.get(i);

records.set(i, records.get(j));

records.set(j, t);

}

}

}

}

/**

*

* @param title

* @return index of assessment with given title. return -1 if no such title exists

*/

public int getAssessmentIndex(String title) {

return 0; //to be completed

}

/**

* scale marks of a particular assessment, whose title is provided

* by the given factor (1.2 means increase by 20%, 0.75 means decrease by 25%).

* note that marks should have an upper bound of what that particular assessment is worth,

* and a lower bound of 0. Thus, a mark of 4 out of 5, with a factor of 0.5, should become 5 (and not 6).

* Similarly, a mark of 2 out of 5, with a factor = -1.5, should become 0 (and not -1).

* do nothing if there is no assessment with the given title

* @param title

* @param factor

*/

public void scale(String title, double factor) {

//to be completed

}

/**

* D/HD

* sort the assessments in ascending order of how much they are worth.

* Note that each record should be updated as well.

*/

public void sortAssessments() {

//to be completed

}

/**

* D/HD

* set the marks of student with the given name in the assessment with the

* title of assessmentTitle to given marks

* @param name: name of student

* @param assessmentTitle: title of assessment

* @param marks: marks to be set for the given student and assignment

*/

public void setMarks(String name, String assessmentTitle, double marks) {

//to be completed

}

/**

*

* @param idx

* @return GradebookEntry at given index if index is valid, return null otherwise

*/

public GradebookEntry getRecord(int idx) {

return null; //to be completed

}

/**

*

* @param idx

* @return Assessment at given index if index is valid, return null otherwise

*/

public Assessment getAssessment(int idx) {

return null; //to be completed

}

/**

*

* @param name

* @param title

* @return mark of student with given name in an assessment with given title.

* return null if no such record exists.

*/

public Double getMark(String name, String title) {

return null; //to be completed

}

/**

*

* @param name

* @return record for student with given name. return null if no such record exists

*/

public GradebookEntry getRecord(String name) {

return null; //to be completed

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Privacy In Statistical Databases International Conference Psd 2022 Paris France September 21 23 2022 Proceedings Lncs 13463

Authors: Josep Domingo-Ferrer ,Maryline Laurent

1st Edition

3031139445, 978-3031139444

More Books

Students also viewed these Databases questions

Question

Different formulas for mathematical core areas.

Answered: 1 week ago

Question

a valuing of personal and psychological privacy;

Answered: 1 week ago