Hi guys I need help with this assignment... I have to make changes to this code.
ID: 3606948 • Letter: H
Question
Hi guys I need help with this assignment... I have to make changes to this code. Would really appreciate it if someone can help me out... The instructions are as follows ->
1: In the constructor, as well as in the setLetterGrade method, when the grade parameter is A, B, C, D, or F, use it to set the letterGrade instance variable. Otherwise, throw an IllegalArgumentException. The message for the exception would be: "Invalid value for the letter grade.”
2: Make the class implement the Comparable<CourseGrade> interface.
3: Provide an implementation for the compareTo method such that it compares the value of the courseTaken variable. If both objects have the same value, it returns 0; if the courseTaken variable of the calling object is greater, it returns a positive number; else it returns a negative number.
package acadadminsystem_phase1;
public class CourseGrade
{
// instance variables
private Course courseTaken;
private String letterGrade;
/**
* This constructor initializes the fields to the passed values.
* @param course Course number.
* @param grade Grade received.
*/
public CourseGrade(Course course, String grade)
{
courseTaken = course;
letterGrade = grade;
}
/**
* This is a copy constructor. It initializes the fields of the object being created to the same
* values as the fields in the object passed as an argument.
* @param courseGradeObj The object being copied.
*/
public CourseGrade(CourseGrade courseGradeObj)
{
if( courseGradeObj != null )
{
courseTaken = courseGradeObj.courseTaken;
letterGrade = courseGradeObj.letterGrade;
}
}
/**
* The getCourseTaken method returns the course that the student has taken.
* @return A copy of the course object.
*/
public Course getCourseTaken()
{
return courseTaken;
}
/**
* The getLetterGrade method returns the grade received for a course.
* @return The value in the letterGrade field.
*/
public String getLetterGrade()
{
return letterGrade;
}
/**
* The setCourseTaken method stores a value in the courseTaken field.
* @param course the new Course object to store in courseTaken.
*/
public void setCourseTaken(Course course)
{
courseTaken = course;
}
/**
* The setLetterGrade method stores a value in the letterGrade field.
* @param grade the value to store in letterGrade.
*/
public void setLetterGrade(String grade)
{
letterGrade = grade;
}
/**
* The toString method returns a string representing the state of a CourseGrade object.
* @return A string containing the information for the course that has been taken: course info,
* and grade received.
*/
@Override
public String toString()
{
return String.format("%s %-30s %s",
courseTaken,
"Grade Received:", letterGrade);
}
/**
* The equals method compares two CourseGrade objects. The result is true if the argument
* is not null and is a CourseGrade object with the same course and grade as the calling
* object.
* @param obj The object to compare this CourseGrade with.
* @return true if the given object has the same value for the courseTaken and letterGrade
* fields.
*/
@Override
public boolean equals(Object obj)
{
// check that the type of the parameter is CourseGrade
if( !(obj instanceof CourseGrade) )
return false;
// we already know that obj is of type CourseGrade, so it's safe to cast
CourseGrade courseGradeObj = (CourseGrade) obj;
// return true or false depending on whether courseTaken and letterGrade have the same value
return courseGradeObj.courseTaken.equals(this.courseTaken)
&& courseGradeObj.letterGrade.equals(this.letterGrade);
}
public int compareTo(CourseGrade courseGrade)
{
}
}
Explanation / Answer
Program:
package acadadminsystem_phase1;
public class CourseGrade
{
// instance variables
private Course courseTaken; //name of the course taken
private String letterGrade;
/**
* This constructor initializes the fields to the passed values.
* @param course Course number.
* @param grade Grade received.
*/
public CourseGrade(Course course, String grade)
{
courseTaken = course;
letterGrade = grade;
}
/**
* This is a copy constructor. It initializes the fields of the object being created to the same
* values as the fields in the object passed as an argument.
* @param courseGradeObj The object being copied.
*/
public CourseGrade(CourseGrade courseGradeObj)
{
if( courseGradeObj != null )
{
courseTaken = courseGradeObj.courseTaken;
letterGrade = courseGradeObj.letterGrade;
}
}
/**
* The getCourseTaken method returns the course that the student has taken.
* @return A copy of the course object.
*/
public Course getCourseTaken()
{
return courseTaken;
}
/**
* The getLetterGrade method returns the grade received for a course.
* @return The value in the letterGrade field.
*/
public String getLetterGrade()
{
return letterGrade;
}
/**
* The setCourseTaken method stores a value in the courseTaken field.
* @param course the new Course object to store in courseTaken.
*/
public void setCourseTaken(Course course)
{
courseTaken = course;
}
/**
* The setLetterGrade method stores a value in the letterGrade field.
* @param grade the value to store in letterGrade.
*/
public void setLetterGrade(String grade)
{
letterGrade = grade;
}
/**
* The toString method returns a string representing the state of a CourseGrade object.
* @return A string containing the information for the course that has been taken: course info,
* and grade received.
*/
@Override
public String toString()
{
StringBuffer stringBuffer=new StringBuffer();
DecimalFormat decimalformat=new DecimalFormat("###.##");
int index=0;
double percentage;
String grade="";
for(letterGrade assignment : grade)
{
percentage = assignment.getpercentage();
if(percentage<50)
{
grade="F";
}
else if(percentage<=60)
{
grade="D";
}
else if(percentage<=70)
{
grade="C";
}
else if(percentage<=80)
{
grade="B";
}
else if(percentage>=90)
{
grade="A";
}
else if(percentage<=20)
system.out.println("Illegal value for the letter grade");
return String.format("%s %-30s %s",
courseTaken,
"Grade Received:", letterGrade);
}
}
/**
* The equals method compares two CourseGrade objects. The result is true if the argument
* is not null and is a CourseGrade object with the same course and grade as the calling
* object.
* @param obj The object to compare this CourseGrade with.
* @return true if the given object has the same value for the courseTaken and letterGrade
* fields.
*/
@Override
public boolean equals(Object obj)
{
// check that the type of the parameter is CourseGrade
if( !(obj instanceof CourseGrade) )
return false;
// we already know that obj is of type CourseGrade, so it's safe to cast
CourseGrade courseGradeObj = (CourseGrade) obj;
// return true or false depending on whether courseTaken and letterGrade have the same value
return courseGradeObj.courseTaken.equals(this.courseTaken)
&& courseGradeObj.letterGrade.equals(this.letterGrade);
}
public interface Comparable<CourseGrade> //comparable interface implementation provides a compareTo ethod that takes an object of Class CourseGrade,compares and returns an integer.
{
public int compareTo(CourseGrade courseGrade)
{
if(CourseTaken().equals(CourseGrade.CourseTaken())) //if coursetaken value equals coursegrade value, return 0
{
return 0;
}
else if(courseGrade>>CourseTaken) //if coursegrade is greater than coursetaken, then it returns 1
return 1;
}
else //if coursegrade<coursetaken, then return -1
{
return -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.