we are going to review some of the concepts of OOP .. We’ll create an interface,
ID: 3736115 • Letter: W
Question
we are going to review some of the concepts of OOP .. We’ll create an interface, a super class, a subclass, and test it with an application.
StudentInterface.java (interface)
Create an interface, StudentInterface with the following methods:
String getName();
int getId();
double getAvgGrade();
boolean addScore( int grade );
Also create a constant, INIT_NUM_GRADES set to 5.
Student.java (super class)
Create Student.java which will implement the StudentInterface. This class will have the following protected fields:
ID
Name
Array of integer test scores. This array will initially store INIT_NUM_GRADES. If the array fills up, the method addScore() will create a new array that is double the length, copy the existing grades, and then set this reference to the new array.
An index to the location of where the next score will be stored in the array defined above.
The next ID to use for a new student. This is a static variable and is incremented after each new student is created.
In addition to implementing the StudentInterface, the Student should have the following methods:
A one argument constructor with the name. The ID will be set to the static next ID and that variable will be incremented. Create the array of 5 integer test scores.
A two argument constructor with the name and ID. If the specified ID is greater than the next ID, set next ID to the specified ID plus one. Create the array test scores.
A static method to display the toString() message headers. This method is defined below.
A toString() method, which matches the data defined within the getHdr method: name, id, average test score, list of all tests.
public static String getHdr()
{
return String.format("%15s %3s %6s %6s",
"name", "id", "tests", "scores");
}
GradStudent.java (subclass)
GradStudent will inherit everything from Student. In addition, a GradStudent has an additional essayScore (double) field.
Add the following method:
Add 2 constructors, which will call the super class’s constructor (with super(....) ).
Override the getAvgGrade() method with the following weighted average: essayScore * 25% + averageTestScore * 75%. Call the super class’s getAvgGrade() with a reference to super: super.getAvgGrade(). If the essayScore was not set (equal to 0), count the 100% of the averageTestScore. If no testScores have been added (average = 0), use 100% of essayScore. Note to get the testScore grade, call the super class’s method:
double tests = super.getAvgGrade();
Add a mutator to set the essayScore: void setEssayScore( int score )
Add a static method to print the header for the toString(), shown below.
Add the corresponding toString() method to display the name, ID, essayScore, average test score, overall grade (call getAvgGrade()), and list all the test scores.
public static String getHdr()
{
return String.format("%15s %3s %6s %6s %6s %6s",
"name", "id", "essay", "tests",
"grade", "scores");
}
School.java (application) you do not need to modify this file
School is the application and creates an array of Students and GradStudents. The grades for each student are populated with random values. The array is then traversed and each Student is display by calling the toString().
Question: Why can we populate the array with both Students and GradStudents?
A sample run of School
name id tests scores
Mary Smith 1 80.3 75, 79, 78, 84, 87, 70, 84, 85,
Sam Adams 2 72.0 41, 41, 41, 100, 96, 101, 84,
Bill Butcher 3 74.6 75, 78, 71, 76, 75, 72, 74, 75, 75,
Sue Thomas 4 86.6 76, 81, 84, 97, 94, 86, 98, 77,
Sarah Billings 5 78.8 111, 84, 53, 63, 76, 86,
name id essay tests grade scores
Bob Jones 6 84 87.6 86.7 79, 87, 99, 89, 84,
Mary Parker 7 84 74.5 76.9 77, 67, 81, 71, 85, 72, 75, 68,
Explanation / Answer
Hi,
I have attached the complete code. Since School.java is not provided so please test this code with School.java.
Question:Why can we populate the array with both Students and GradStudents?
Answer: An array reference of parent class can contain objects of parent as well as child class. This is because of isA relationship between Student and Grad Student. In other words, each GradStudent is A Student.
PS: If you are satisifed with the answer, please take a couple of seconds to rate the answer. Thanks. if you have any questions or comments do specify so that I can help. :)
StudentInterface.java
public interface StudentInterface {
String getName();//Create a method to get the name
int getId();
double getAvgGrade();
boolean addScore(int grade);
int INIT_NUM_GRADES = 5;//Constant for number of grades
}
Student.java
import java.util.Arrays;
public class Student implements StudentInterface {
//Attributes in protected mode and attribute for next ID to be used and index where next score must be added
protected int ID;
protected String Name;
protected int scores[];
protected int nextScore = 0;
static int nextID = 0;
public Student(String Name) {
this.Name = Name;
ID = Student.nextID++;
scores = new int[INIT_NUM_GRADES];
}
public Student(int ID, String Name) {
if (ID > nextID) {
nextID = ID + 1;
}
this.ID = ID;
this.Name = Name;
scores = new int[INIT_NUM_GRADES];
}
@Override
public String toString() {//Show the Output using toString
System.out.println(Student.getHdr());
return String.format("%15s %3s %6s %6s", Name, ID, getAvgGrade(), Arrays.toString(scores));
}
public static String getHdr() {//Get the header
return String.format("%15s %3s %6s %6s",
"name", "id", "tests", "scores");
}
@Override
public String getName() {
return Name;//Return name
}
@Override
public int getId() {
return ID;//Return ID
}
@Override
public double getAvgGrade() {
double total = 0;
for (int i = 0; i < nextScore; i++) {
total += scores[i];
}
return total / nextScore;//find the grade, average grade of student
}
@Override
public boolean addScore(int grade) {
if (nextScore > scores.length) {
scores = Arrays.copyOf(scores, scores.length * 2);//If next score is more than length of array then do this
}
scores[nextScore++] = grade;
return true;
}
}
GradStudent.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Arrays;
public class GradStudent extends Student {
double essayScore;//Create a variable for essayScore
public GradStudent(String Name) {
super(Name);
essayScore = 0;
}
public GradStudent(int ID, String Name, int essayScore) {
super(ID, Name);
this.essayScore = essayScore;
}
public void setEssayScore(double essayScore) {
this.essayScore = essayScore;
}
@Override
public double getAvgGrade() {
double tests = super.getAvgGrade();
if (tests == 0) {//If test score is 0 then only show essayScore
return essayScore;
} else if (essayScore == 0) {//If essayScore is 0 then only show test scores
return tests;
}
return ((essayScore * 0.25) + (tests * 0.75));//75% of test scoress and 25% for essay Score
}
@Override
public String toString() {
System.out.println(getHdr());
return String.format("%15s %3s %6s %6s %6s %6s",
Name, ID, essayScore, super.getAvgGrade(), getAvgGrade(), Arrays.toString(scores));
}
public static String getHdr() {
return String.format("%15s %3s %6s %6s %6s %6s",
"name", "id", "essay", "tests",
"grade", "scores");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.