Can someone please help me write this program? Derive a class named Student from
ID: 3585065 • Letter: C
Question
Can someone please help me write this program?
Derive a class named Student from the class Person . A Student object has a name and an identification number(both inherited from the class Person), two test scores(int), the average score(double) and a letter grade(char). Write the definitions for the constructors, a number of appropriate accessor and mutator methods as well as an equals and a toString method for the Student class.
Write a Java program to read in the name, the id and the two test scores for 5 students from the keyboard and then compute the letter grade for each student according to the following scale:
average >=90 is an A
80<=average<90 is a B
70<=average<80 is a C
60<=average<70 is a D
average<60 is an E
Use an array to store the student records and then sort the records in an ascending order based on their test score averages. Display the sorted array in a nice format.
The Person class I will be using is as follows:
public class Person
{
private String name;
private int id;
public Person( )
{
name = "";
id = 0;
}
public Person(String newName, int newId)
{
name = newName;
id = newId;
}
public Person(Person otherPerson)
{
this.name = otherPerson.name;
this.id = otherPerson.id;
}
public void setName(String name)
{
this.name = name;
}
public static void setName(Person p, String newName)
{
p.name = newName;
}
public String getName( )
{
return name;
}
public void setId(int id)
{
this.id = id;
}
public static void setId(Person p, int newId)
{
p.id = newId;
}
public int getId( )
{
return id;
}
public boolean equals(Person otherPerson)
{
if (this.name.equals(otherPerson.name) && this.id == otherPerson.id)
return true;
else
return false;
}
public String toString( )
{
String nwStr = "";
nwStr = nwStr + "name: " + this.getName();
nwStr = nwStr + " id: " + this.getId();
return nwStr;
}
}
You should create a project, named Project3 and the project should contain the definitions of the Person class, the Student class as well as a class named Program3 containing the main method for this application.
Explanation / Answer
Person.java
public class Person {
//Declaring instance variables
private String name;
private int id;
//Parameterized constructor
public Person(String name, int id) {
super();
this.name = name;
this.id = id;
}
//getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Name=" + name + " Id=" + id;
}
}
_____________________
Student.java
public class Student extends Person {
//Zero argumented constructor
private int testscore1;
private int testscore2;
private double average;
private char gradeLetter;
//Parameterized constructor
public Student(String name, int id, int testscore1, int testscore2) {
super(name, id);
this.testscore1 = testscore1;
this.testscore2 = testscore2;
}
//getters and setters
public int getTestscore1() {
return testscore1;
}
public void setTestscore1(int testscore1) {
this.testscore1 = testscore1;
}
public int getTestscore2() {
return testscore2;
}
public void setTestscore2(int testscore2) {
this.testscore2 = testscore2;
}
//This method will calculate the average
public double CalAverage() {
this.average = (testscore1 + testscore2) / 2;
return average;
}
//This method will find the garde letter
public char findtGradeLetter() {
if (average >= 90)
gradeLetter = 'A';
else if (average >= 80 && average < 90)
gradeLetter = 'B';
else if (average >= 70 && average < 80)
gradeLetter = 'C';
else if (average >= 60 && average < 70)
gradeLetter = 'D';
else if (average < 60)
gradeLetter = 'E';
return gradeLetter;
}
public boolean equals(Student other) {
if (testscore1 != other.testscore1)
return false;
if (testscore2 != other.testscore2)
return false;
if (!getName().equals(other.getName()))
return false;
if (getId() == other.getId())
return false;
return true;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString() + " Test score1=" + testscore1 + " Test score2=" + testscore2 + " Average=" + CalAverage() + " Grade Letter=" + findtGradeLetter();
}
}
____________________
TestClass.java
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
//Declaring variables
int id, test1, test2;
String name;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Creating an Array of 5 students
int size = 5;
Student students[] = new Student[size];
//Getting each student info and create student object and populate it into an array
for (int i = 0; i < size; i++) {
System.out.println(":: Student#" + (i + 1) + " ::");
System.out.print("Enter Student Id :");
id = sc.nextInt();
sc.nextLine();
System.out.print("Enter Student Name :");
name = sc.nextLine();
System.out.print("Enter Score in Test#1 :");
test1 = sc.nextInt();
System.out.print("Enter Score in Test#2 :");
test2 = sc.nextInt();
students[i] = new Student(name, id, test1, test2);
}
sortBasedOnAvg(students);
System.out.println("After Sortingbased on average :");
int i = 0;
for (Student st: students) {
System.out.println(" " + st.toString());
}
}
private static void sortBasedOnAvg(Student[] students) {
//This Logic will Sort the Array of students in Ascending order based on average
Student temp;
for (int i = 0; i < students.length; i++) {
for (int j = i + 1; j < students.length; j++) {
if (students[i].CalAverage() < students[j].CalAverage()) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
}
_____________________
Output:
:: Student#1 ::
Enter Student Id :111
Enter Student Name :Sachin
Enter Score in Test#1 :89
Enter Score in Test#2 :98
:: Student#2 ::
Enter Student Id :222
Enter Student Name :Rick
Enter Score in Test#1 :78
Enter Score in Test#2 :87
:: Student#3 ::
Enter Student Id :333
Enter Student Name :Bob
Enter Score in Test#1 :76
Enter Score in Test#2 :54
:: Student#4 ::
Enter Student Id :444
Enter Student Name :Johnson
Enter Score in Test#1 :65
Enter Score in Test#2 :54
:: Student#5 ::
Enter Student Id :555
Enter Student Name :Mitchel
Enter Score in Test#1 :76
Enter Score in Test#2 :65
After Sortingbased on average :
Name=Sachin
Id=111
Test score1=89
Test score2=98
Average=93.0
Grade Letter=A
Name=Rick
Id=222
Test score1=78
Test score2=87
Average=82.0
Grade Letter=B
Name=Mitchel
Id=555
Test score1=76
Test score2=65
Average=70.0
Grade Letter=C
Name=Bob
Id=333
Test score1=76
Test score2=54
Average=65.0
Grade Letter=D
Name=Johnson
Id=444
Test score1=65
Test score2=54
Average=59.0
Grade Letter=E
_____________Could you rate me well.Plz .Thank You