Write the following java program: a) Define a class Student which extends Person
ID: 3664638 • Letter: W
Question
Write the following java program:
a) Define a class Student which extends Person. It adds the attributes int test1, test2, test3, Double average, String grade
It has methods computeaverage() and calculategrade(). The grades are based on the average, with above 90 an A, 80 to 90 a B, 70 to 80 a C etc. All other attributes have a set and a get.
b) Write an application that uses an array of type student of size 20. The program uses a menu that has options
1. Enter the Students
2. Enter grades for test 1
3. Enter grades for test 2
4. Enter grades for test 3
5. Calculate Grades
6. List all Students final grades and Quit
The application saves the data to a text file upon quitting and restores the data when it restarts
Explanation / Answer
import java.util.Scanner;
// Person Class
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// Student Class
class Student extends Person{
int test1, test2, test3;
double average;
String grade;
public Student(String name, int test1, int test2, int test3) {
setName(name);
this.test1 = test1;
this.test2 = test2;
this.test3 = test3;
}
public int getTest1() {
return test1;
}
public void setTest1(int test1) {
this.test1 = test1;
}
public int getTest2() {
return test2;
}
public void setTest2(int test2) {
this.test2 = test2;
}
public int getTest3() {
return test3;
}
public void setTest3(int test3) {
this.test3 = test3;
}
public double getAverage() {
return average;
}
public void setAverage(double average) {
this.average = average;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public void computeAverage() {
average = (getTest1()+getTest2()+getTest3())/3;
}
public void computeGrade() {
String grade="";
computeAverage();
double avg = getAverage();
if(avg > 90)
grade = "A";
else if(avg > 80)
grade = "B";
else if(avg > 70)
grade = "C";
else if(avg > 60)
grade = "D";
else
grade = "F";
this.grade = grade;
}
}
//App Class
class App {
public static void main(String[] args) {
Student[] students = new Student[20];
Scanner sc = new Scanner(System.in);
int test1,test2,test3;
double average;
String name, grade;
for(int i=0; i<20; i++) {
System.out.println("Enter name of "+i+"th student: ");
name = sc.nextLine();
System.out.println("Enter marks of test1 of "+i+"th student: ");
test1 = sc.nextInt();
System.out.println("Enter marks of test2 of "+i+"th student: ");
test2 = sc.nextInt();
System.out.println("Enter marks test3 of "+i+"th student: ");
test3 = sc.nextInt();
students[i] = new Student(name, test1, test2, test3);
students[i].computeGrade();
}
for(int i=0; i<20; i++) {
System.out.println("Student "+students[i].getName()+" has "+students[i].getGrade());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.