Question

6. Analyzable Interface Modify the CourseGrades class you created in Programming Challenge 5 so it implements...

6. Analyzable Interface

Modify the CourseGrades class you created in Programming Challenge 5 so it implements

the following interface:

public interface Analyzable

{

double getAverage();

GradedActivity getHighest();

GradedActivity getLowest();

}

The getAverage method should return the average of the numeric scores stored in the

grades array. The getHighest method should return a reference to the element of the grades

array that has the highest numeric score. The getLowest method should return a reference

to the element of the grades array that has the lowest numeric score. Demonstrate the new

methods in a complete program.

Homework Answers

Answer #1

Here i am providing the answer. Hope it helps, please give me a like. it helps me a lot.

Program Code:

public class CourseGradeDemo

{

/*Main method to demonstrate the classes CourseGrade,

* GradedActivity, PassFailExam, Essay, FinalExam.

*/

// start main method

public static void main(String[] args)

{

GradedActivity lowest;

GradedActivity highest;

// create an object for the GradedActivity class

GradedActivity ga = new GradedActivity();

// set score of the GradedActivity

ga.setScore(80);

// create an object for the PassFailExam class

PassFailExam pfe = new PassFailExam(10, 3, 90);

// create an object for the Essay class

Essay e = new Essay(28, 19, 18, 30);

// create an object for the FinalExam class

FinalExam fe = new FinalExam(50, 12);

// create an object for the CourseGrades class

CourseGrades coursegrade = new CourseGrades();

/* call the "setLab" method with the

GradedActivity object */

coursegrade.setLab(ga);

/* call the "setPassFailExam" method with the

PassFailExam object */

coursegrade.setPassFailExam(pfe);

/* call the "setEssay " method with the Essay

object */

coursegrade.setEssay(e);

/* call the "setFinalExam " method with the

FinalExam object */

coursegrade.setFinalExam(fe);

// call the "toString" method of courseGrade

System.out.println(coursegrade);

System.out.println("\n");

double average;

average= coursegrade.getAverage();

System.out.println("Average of the scores is:\t"

+average);

System.out.println("Lowest of the scores is:\t" +coursegrade.getLowest().getScoreOf());

System.out.println("Highest of the scores is:\t" +coursegrade.getHighest().getScoreOf());

} // end of main method

} // end of CourseGradeDemo class

  1. //Create an Interface Analyzable

    public interface Analyzable

    {

    double getAverage();

    GradedActivity getHighest();

    GradedActivity getLowest();

    }

    Comments (1)

  2. Step 5 of 13

    //File: CourseGrades.java

    // CourseGrades class implementation

    public class CourseGrades implements Analyzable

    {

    //create an array grades of 4 GradedActivity objects

    private GradedActivity[] grades = new GradedActivity[4];

    // Method setLAb() which assigns the gradedActivity

    //to first array element

    /*

    * setLab method accepts a GradedActivity object as

    * a parameter and then stores this object in the

    * grades array as the 0th element.*/

    // setLab method implementation

    public void setLab(GradedActivity ga)

    {

    grades[0] = ga;

    } // end of setLab method

    // setPassFailExam() which assigns the GradedActivity

    //to second array element

    /*

    * The following setPassFailExam method accepts a

    * PassFailExam object as a parameter and then stores

    * this object in the grades array as the 1st

    * element.

    */

    // setPassFailExam method implementation

    public void setPassFailExam(PassFailExam fe)

    {

    grades[1] = fe;

    } // end of setPassFailExam method

    // Method setEssay() which assigns the gradedActivity

    //to third array element

    /*

    * The following setEssay method accepts an Essay

    * object as a parameter and then stores this object

    * in the grades array as the 2nd element.

    */

    // setEssay method implementation

    public void setEssay(Essay e)

    Comment

Step 6 of 13

{

grades[2] = e;

} // end of setEssay method

// Method setFinalExam() which assigns the

//gradedActivity to fourth array element

/*

* The following setFinalExam method accepts

* a FinalExam object as a parameter and then stores

* this object in the grades array as the 3rd

* element.

*/

// setFinalExam method implementation

public void setFinalExam(FinalExam fe)

{

grades[3] = fe;

} // end of setFinalExam method

// Method string() which calls all the methods and

//return the values

/* The following toString method returns a string

* representation of the numeric scores and grades of

* all elements in the grades array. */

// toString method implementation

public String toString()

{

return "Lab:\n" + " Score: "

+ grades[0].getScoreOf()

+ ", Grade: " + grades[0].getGradeOf()

+ "\n\nPassFailExam:\n" + " Score: "

+ grades[1].getScoreOf() + ", Grade: "

+ grades[1].getGradeOf()

+ "\n\nEssay:\n"

+ " Score: " + grades[2].getScoreOf()

+ ", Grade: " + grades[2].getGradeOf()

+ "\n\nFinalExam:\n" + " Score: "

+ grades[3].getScoreOf() + ", Grade: "

+ grades[3].getGradeOf();

} // end of toString method

/*

* The following getAverage method computes and returns the average score

* of all elements in the grades array.

*/

// getAverage method implementation

public double getAverage()

{

double total = 0;

for (int i = 0; i < grades.length; i++)

{

total += grades[i].getScoreOf();

}

double average = total / grades.length;

return average;

} // end of getAverage method

/* The following getHighest method finds and returns

* the reference to the element with the highest

* score in the grades array. */

// getHighest method implementation

public GradedActivity getHighest()

{

GradedActivity highest = grades[0];

for (int i = 0; i < grades.length; i++)

{

if (grades[i].getScoreOf() > highest

.getScoreOf())

highest = grades[i];

}

return highest;

} // end of getHighest method

/* The following getLowest method finds and returns

* the reference to the element with the lowest score

* in the grades array.*/

// getLowest method implementation

public GradedActivity getLowest()

{

GradedActivity lowest = grades[0];

for (int i = 0; i < grades.length; i++)

{

if (grades[i].getScoreOf() < lowest.getScoreOf())

lowest = grades[i];

}

return lowest;

} // end of getLowest method

} // end of CourseGrades class

/**********************************************************

* The Following GradedActivity class demonstrates *

* the numeric score and calculating lettergrade with *

* the corresponding numeric score values *

*********************************************************/

//File: GradedActivity.java

//Implemetation of GradedActivity Class

/*Class to demonstrate the numeric score and the letter

grade calculation*/

public class GradedActivity

{

//declaring the variable scoreValue

private double scoreValue;

//setter to set a value for the scoreValue

public void setScore(double s1)

{

//setting value

scoreValue = s1;

}

//getter to get score

public double getScoreOf()

{

//return statement

return scoreValue;

}

//getter to get grade

public char getGradeOf()

{

//variable declaration

char letterGrade;

//checking each scoreValue to get a letter grade

/*Calculating the letter grade by comparing score

values of each stage*/

if (scoreValue >= 90)

letterGrade = 'A';

else if (scoreValue >= 80)

letterGrade = 'B';

else if (scoreValue >= 70)

letterGrade = 'C';

else if (scoreValue >= 60)

letterGrade = 'D';

else

letterGrade = 'F';

//return statement

return letterGrade;

}

}

Comment

public class PassFailActivity extends GradedActivity

{

//Declare a variable for Minimum Passing Score

private double minPassScore;

public PassFailActivity(double mp)

{

minPassScore = mp;

}

@Override

public char getGradeOf()

{

char letterGrade;

if(super.getScoreOf()>=minPassScore)

letterGrade='P';

else

letterGrade='F';

return letterGrade;

}

}

//File: PassFailExam.java

//Implemetation of PassFailExam Class

public class PassFailExam extends PassFailActivity

{

// Number of questions

private int numOfQuestions;

// Points for each question

private double pointsOfEach;

// Number of questions missed

private int numOfMissed;

/*Constructor which is passing the parameters

Questions,missed, minpassing and that can be

assigned for the private data members which is

already declared above.*/

//parameterised constructor which is passing

//Questions, missed and minPassing

public PassFailExam(int questions, int missed,

double minPassing)

{

// Call the superclass constructor.

super(minPassing);

// Declare a local variable for the score.

double numericScoreValue;

// Set the numQuestions and numMissed fields.

numOfQuestions = questions;

numOfMissed = missed;

// Calculate the points for each question and

// the numeric score for this exam.

/*Calculating the points of each and numeric score value

* which can be subtracted with 100 and multiplied by the

* points of each*/

pointsOfEach = 100.0 / questions;

numericScoreValue = 100.0 - (missed *

pointsOfEach);

// Call the superclass's setScore method to

// set the numeric score.

setScore(numericScoreValue);

}

//Get method to get each points

public double getPointsOfEach()

{

//return statement

return pointsOfEach;

}

//Get method to get nummissed

public int getNumOfMissed()

{

//return statement

return numOfMissed;

}

}

public class FinalExam extends GradedActivity

{

// Number of questions

private int numOfQuestions;

// Points for each question

private double pointsOfEach;

// Questions missed

private int numOfMissed;

//Constructor for the class FinalExam

public FinalExam(int questions, int missed)

{

// To hold a numeric score

double numericScoreValue;

// Set the numQuestions and numMissed fields.

numOfQuestions = questions;

numOfMissed = missed;

/*Calculating the points of each and numeric

score value which can be subtracted with 100 and

multiplied by the points of each*/

// the numeric score for this exam.

pointsOfEach = 100.0 / questions;

numericScoreValue = 100.0 - (missed * pointsOfEach);

// Call the inherited setScore method to

// set the numeric score.

setScore(numericScoreValue);

}

//Get method to get each points

//Method to get points

public double getPointsOfEach()

{

//return statement

return pointsOfEach;

}

//Method which is getting the num of missed

//Get method to get nummissed

public int getNumOfMissed()

{

//return statement

return numOfMissed;

}

}

//File: Essay.java

// Essay class implementation

public class Essay extends GradedActivity

{

/* declare the variables to store the points for the

grammar, spelling, correct length, and content */

private int grammars;

private int spellings;

private int correctLengths;

private int contents;

/*Constructors which is passing the parameters egrammer,

* espelling, ecorrectlength and econtent */

// parameterized constructor

public Essay(int egrammar, int espelling,

int ecorrectLength, int econtent)

{

grammars = egrammar;

spellings = espelling;

correctLengths = ecorrectLength;

contents = econtent;

/*Summing the grammars, spelling, correct length

* and the contents*/

setScore(grammars + spellings + correctLengths

+ contents);

} // end of constructor

} // end of Essay class

Sample Output:

Lab:

Score: 80.0, Grade: B

PassFailExam:

Score: 70.0, Grade: F

Essay:

Score: 95.0, Grade: A

FinalExam:

Score: 76.0, Grade: C

The average of the scores is: 80.25

The lowest of the scores is: 70.0

The highest of the scores is: 95.0

Thank you. please like.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor...
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor should accept an integer argument and dynamically allocate the array to hold that many numbers. The private data members of the class should include the integer argument in a variable to hold the size of the array and a pointer to float type to hold the address of the first element in the array. The destructor should free the memory held by the array....
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...
Casting class objects 1.2 Compile and execute the code listed below as it is written. Run...
Casting class objects 1.2 Compile and execute the code listed below as it is written. Run it a second time after uncommenting the line obj.speak();. public class AnimalRunner {    public static void main(String[] args)    {       Dog d1 = new Dog("Fred");       d1.speak();       Object obj = new Dog("Connie");       // obj.speak();    } } The uncommented line causes a compile error because obj is an Object reference variable and class Object doesn’t contain a speak() method. This...
Assignment Overview This programming exercise introduces generics and interfaces. The students must create methods that accept...
Assignment Overview This programming exercise introduces generics and interfaces. The students must create methods that accept generic parameters and perform operation on them. Deliverables A listing of the fully commented, working source code of the Java program Test data for the code A screen shot of the application in execution Step 1 Create a new project. Name it "Assignment_2_1". Step 2 Build a solution. Write the Java source code necessary to build a solution for the problem below:You have just...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes a contestant’s name, talent code, and talent description. The competition has become so popular that separate contests with differing entry fees have been established for children, teenagers, and adults. Modify the Contestant class to contain a field that holds the entry fee for each category, and add get and set accessors. Extend the Contestant class to create three subclasses: ChildContestant, TeenContestant, and AdultContestant. Child...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. Program Description: This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm. A total of seven classes are required. Employee (From previous assignment) HourlyEmployee (From previous assignment) SalaryEmployee (From previous assignment) CommissionEmployee (From previous assignment) EmployeeManager (Altered from previous...
Lab Objectives This lab was designed to inforce the following programming concepts: • Using classes to...
Lab Objectives This lab was designed to inforce the following programming concepts: • Using classes to create a data type SimpleCalculator capable of performing arithmetic operations. • Creating const member functions to enforce the principle of least privilege. The follow-up questions and activities also will give you practice: • Using constructors to specify initial values for data members of a programmer-defined class. Description of the Problem Write a SimpleCalculator class that has public methods for adding, subtracting, multiplying and dividing...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT