Was wondering what im doing wrong cant get the correct numeric output for My pro
ID: 3688001 • Letter: W
Question
Was wondering what im doing wrong cant get the correct numeric output for My problems here is my output:
Lab Score: 85.0 Grade: B
Pass/Fail Exam Score: 85.0 Grade: P
Essay Score: 80.0 Grade: B
Final Exam Score: 80.0 Grade: B
Average Score: 82.50
GradedActivity@69c67db
The Lowest: Essay@6665e41
//interface analyazble
public interface Analyzable
{
double getAverage ();
GradedActivity getHighest ();
GradedActivity getLowest();
}
//courseGradeDemo
// CourseGradeDemo
public class CourseGradeDemo
{
public static void main(String[] args)
{
GradedActivity ga = new GradedActivity ();
//Set score of Graded activity
ga.setScore(85);
//create an objec for teh PassFailExam clas
PassFailExam pfe = new PassFailExam (20, 3, 70);
//create an object for the Essay class
Essay e = new Essay (25,18,17,20);
//Create an object for the finalExam class
FinalExam fe = new FinalExam (50,10);
//create an object for the CouseGrades class
CourseGrades cg = new CourseGrades ();
/*Call the "setLab method with the GradedActivity object */
cg.setLab (ga);
/*call the "setPassFailExam" method with the PPassFailExam object */
cg.setPassFailExam(pfe);
/*call the "setEssay" method with the Essay object */
cg.setEssay(e);
/*call teh "setFinalExam" method with the FinalExam object */
cg.setFinalExam(fe);
// call the "toString " mehtod of cg
System.out.println (cg);
/*call the getAverage method to get the average*/
System.out.printf("Average Score: %.2f", cg.getAverage());
System.out.println();
System.out.println("" + cg.getHighest ());
System.out.println("The Lowest: " + cg.getLowest());
}
}
//my CourseGrades that i
public class CourseGrades implements Analyzable
{
private GradedActivity[] grades = new GradedActivity [4];
public void setLab(GradedActivity ga)
{
grades[0] =ga;
}
public void setPassFailExam(PassFailExam aPassFailExam)
{
grades[1] = aPassFailExam;
}
public void setEssay(Essay anEssay)
{
grades[2] = anEssay;
}
public void setFinalExam(FinalExam aFinalExam)
{
grades[3] = aFinalExam;
}
public String toString()
{
return
"Lab Score: " + grades[0].getScore() + " Grade: " + grades[0].getGrade () + " Pass/Fail Exam Score: " + grades[1].getScore() + " Grade: " + grades[1].getGrade()
+ " Essay Score: " + grades[2].getScore() + " Grade: " + grades[2].getGrade() + " Final Exam Score: "
+ grades[3].getScore() + " Grade: " + grades[3].getGrade();
}
/*the following getAverage method computes and returns the average score of all elemnts in the grades array. */
// getAverage method implementation
public double getAverage ()
{
double total = 0 ;
for (int i = 0; i < grades.length ; i++)
{
total += grades [i].getScore ();
}
double average = total/grades.length;
return average;
}
/*the follwoing getHighest methods finds a returns the reference to the element with the highest score in the grades array. */
//getHighest method implementation
public GradedActivity getHighest()
{
double max = Double.MIN_VALUE;
GradedActivity output = null;
for(GradedActivity g : grades)
{
if(g.getScore() > max)
{
output = g;
max = g.getScore();
}
}
return output;
}
public GradedActivity getLowest ()
{
GradedActivity lowest= grades [0];
for (int i = 0; i < grades.length; i++)
{
if (grades[i].getScore()<lowest.getScore())
lowest = grades [i];
}
return lowest;
}
}
//Essay
// Eassy.java
public class Essay extends GradedActivity
{
private int grammar;
private int spelling;
private int correctLength;
private int content;
//parmatezid a constructor
public Essay (int egrammar, int espelling, int ecorrectLength, int econtent)
{
grammar = egrammar;
spelling = espelling;
correctLength = ecorrectLength;
content = econtent;
setScore ( grammar + spelling + correctLength + content);
}
private void setGrammar(int g)
{
if (g <= 30)
grammar = g;
else
grammar = 0;
}
private void setSpelling(int s)
{
if (s <= 20)
spelling = s;
else
spelling = 0;
}
private void setCorrectLength(int c)
{
if (c <= 20)
correctLength = c;
else
correctLength = 0;
}
private void setContent(int c)
{
if (c <= 30)
content = c;
else
content = 0;
}
public int getGrammar()
{
return grammar;
}
public int getSpelling()
{
return spelling;
}
public int getCorrectLength()
{
return correctLength;
}
public int getContent()
{
return content;
}
public double getScore()
{
return grammar + spelling + correctLength + content;
}
}
//passFailExam
/**
This class determines a passing or failing grade for
an exam.
*/
public class PassFailExam extends PassFailActivity
{
private int numQuestions; // Number of questions
private double pointsEach; // Points for each question
private int numMissed; // Number of questions missed
/**
The constructor sets the number of questions, the
number of questions missed, and the minimum passing
score.
@param questions The number of questions.
@param missed The number of questions missed.
@param minPassing The minimum passing score.
*/
public PassFailExam(int questions, int missed,
double minPassing)
{
// Call the superclass constructor.
super(minPassing);
// Declare a local variable for the score.
double numericScore;
// Set the numQuestions and numMissed fields.
numQuestions = questions;
numMissed = missed;
// Calculate the points for each question and
// the numeric score for this exam.
pointsEach = 100.0 / questions;
numericScore = 100.0 - (missed * pointsEach);
// Call the superclass's setScore method to
// set the numeric score.
setScore(numericScore);
}
/**
The getPointsEach method returns the number of
points each question is worth.
@return The value in the pointsEach field.
*/
public double getPointsEach()
{
return pointsEach;
}
/**
The getNumMissed method returns the number of
questions missed.
@return The value in the numMissed field.
*/
public int getNumMissed()
{
return numMissed;
}
}
//passFailActivity
/**
This class holds a numeric score and determines
whether the score is passing or failing.
*/
public class PassFailActivity extends GradedActivity
{
private double minPassingScore; // Minimum passing score
/**
The constructor sets the minimum passing score.
@param mps The minimum passing score.
*/
public PassFailActivity(double mps)
{
minPassingScore = mps;
}
/**
The getGrade method returns a letter grade determined
from the score field. This method overrides the
superclass method.
@return The letter grade.
*/
@Override
public char getGrade()
{
char letterGrade;
if (super.getScore() >= minPassingScore)
letterGrade = 'P';
else
letterGrade = 'F';
return letterGrade;
}
}
//finalExam
/**
This class holds a numeric score and determines
whether the score is passing or failing.
*/
public class PassFailActivity extends GradedActivity
{
private double minPassingScore; // Minimum passing score
/**
The constructor sets the minimum passing score.
@param mps The minimum passing score.
*/
public PassFailActivity(double mps)
{
minPassingScore = mps;
}
/**
The getGrade method returns a letter grade determined
from the score field. This method overrides the
superclass method.
@return The letter grade.
*/
@Override
public char getGrade()
{
char letterGrade;
if (super.getScore() >= minPassingScore)
letterGrade = 'P';
else
letterGrade = 'F';
return letterGrade;
}
}
//
gradedActivity
/**
A class that holds a grade for a graded activity.
*/
public class GradedActivity
{
private double score; // Numeric score
/**
The setScore method sets the score field.
@param s The value to store in score.
*/
public void setScore(double s)
{
score = s;
}
/**
The getScore method returns the score.
@return The value stored in the score field.
*/
public double getScore()
{
return score;
}
/**
The getGrade method returns a letter grade
determined from the score field.
@return The letter grade.
*/
public char getGrade()
{
char letterGrade;
if (score >= 90)
letterGrade = 'A';
else if (score >= 80)
letterGrade = 'B';
else if (score >= 70)
letterGrade = 'C';
else if (score >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
return letterGrade;
}
}
Explanation / Answer
I'm unable to compile the provided code. Anyhow seeing the error,I think what could be the possible issue.
In getHighest() method, return type is GradedActivity but at the end of the method, you are just returning a doule value.' In Java, double won't be converted automatically to a new class instance. Hence to resolve, we need to create a new instance of GradedActivity and set the highest score to it.
So, try modifying your logic for getHIghest() as below:
public GradedActivity getHighest()
{
double max = Double.MIN_VALUE;
GradedActivity output = null;
for(GradedActivity g : grades)
{
if(g.getScore() > max)
{
output = g;
max = g.getScore();
}
}
}
Try this and let me know and moreover your code is not compiling at all..
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.