Modify the student class presented in this chapter as follow. Each student objec
ID: 3687834 • Letter: M
Question
Modify the student class presented in this chapter as follow. Each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the constructor such that each test score is assumed to be initially zero. Provide a method called setTestScore that accepts two parameters: the test number (1 through 3) and the score. Also Provide a method called getTestScore that accepts the test number and returns the appropriate score. Provide a method called average that computes and returns the average test score for this student. Modify the toString method such that the test scores and average are included in the description of the student. Modify the driver class main method to exercise the new Student methods.
Explanation / Answer
import java.text.DecimalFormat;
import java.util.Arrays;
/**
* @author Srinivas Palli
*
*/
public class Student {
String firstName;
String lastName;
int testScores[] = new int[3];
/**
* @param firstName
* @param lastName
* @param testScores
*/
public Student(String firstName, String lastName, int[] testScores) {
this.firstName = firstName;
this.lastName = lastName;
this.testScores = testScores;
}
/**
* @param firstName
* @param lastName
*/
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.testScores[0] = 0;
this.testScores[1] = 0;
this.testScores[2] = 0;
}
/**
* to sets testscore with score according testNumber(index)
*
* @param testNumber
* @param score
*/
public void setTestScore(int testNumber, int score) {
if (testNumber >= 1 && testNumber <= 3) {
testScores[testNumber - 1] = score;
}
}
/**
* accepts the test number and returns the appropriate score
*
* @param testNumber
* @return
*/
public int getTestScore(int testNumber) {
if (testNumber >= 1 && testNumber <= 3) {
return testScores[testNumber - 1];
} else {
return -1;
}
}
/**
* computes and returns the average test score
*
* @return
*/
public double average() {
double average = (double) (testScores[0] + testScores[1] + testScores[2]) / 3.0d;
return average;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
DecimalFormat decimalFormat = new DecimalFormat("#.##");
return "Student [firstName=" + firstName + ", lastName=" + lastName
+ ", testScores=" + Arrays.toString(testScores)
+ ", average()=" + decimalFormat.format(average()) + "]";
}
/**
* @param args
*/
public static void main(String[] args) {
try {
Student student1 = new Student("Rajesh", "Kumar");
student1.setTestScore(1, 90);
student1.setTestScore(2, 95);
student1.setTestScore(3, 93);
System.out.println("First test Score:" + student1.getTestScore(1));
System.out.println(student1);
} catch (Exception e) {
// TODO: handle exception
}
}
}
OUTPUT:
First test Score:90
Student [firstName=Rajesh, lastName=Kumar, testScores=[90, 95, 93], average()=92.67]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.