1. Define a class Student which extends Person defined in class. It adds the att
ID: 3888708 • Letter: 1
Question
1. Define a class Student which extends Person defined in class. It adds the attributes Int Testl, test2, test3 Double average String grade Define a constructor which takes all the attributes of person and uses a default value of 0 for the test scores, "unknown" for grade and 0 for aversge. It has methods computeaverage0 and calculategrade(). The grades are based on the average, with above 92 an A, 85 to 92 a B, 70 to 85 a C, 60 to 70 a D etc. Grades only has a getter. All other attributes have a setter and a getter. 2. Write an application that uses an array of type student of size 20. The program prompts the user for the name of a text file and reads from the file to start the program. Then it gives a menu. 1. Enter a student 2. List the names of all the students 3. Enter the test scores for a student specified by name 4. Find the grade of a student s. Quit 3. When the user quits the students are written to the textExplanation / Answer
Note: Plz let me know if u face any problemn whiel running it..so that I will rectify it..thank You
testscores.txt
sachin 90 89 87
Pointing 76 89 72
Rahul 67 56 45
Raman 67 78 91
John 78 34 56
Williams 78 89 87
______________
Person.java
public class Person {
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
____________________
Student.java
public class Student extends Person {
private int test1;
private int test2;
private int test3;
private String grade;
private double average;
public Student() {
this.test1 = 0;
this.test2 = 0;
this.test3 = 0;
this.grade = "Unkown";
this.average = 0;
}
public Student(String name, int test1, int test2, int test3) {
super(name);
this.test1 = test1;
this.test2 = test2;
this.test3 = test3;
this.grade = grade;
this.average = average;
}
public double computeAverage() {
this.average = (test1 + test2 + test3) / 3.0;
return average;
}
public String calculateGrade() {
if (average > 92)
grade = "A";
else if (average > 85 && average <= 92)
grade = "B";
else if (average > 70 && average <= 85)
grade = "C";
else if (average > 60 && average <= 70)
grade = "D";
else if (average <= 60)
grade = "F";
this.grade = grade;
return grade;
}
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 String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public double getAverage() {
return average;
}
public void setAverage(double average) {
this.average = average;
}
}
______________________
Test.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
String filename,name;
int tst1,tst2,tst3;
int i=0;
Student starr[]=new Student[20];
Scanner s1 = null;
Student st1;
FileOutputStream fout = null;
PrintWriter writer = null;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name of the text file :");
filename=sc.next();
try {
s1=new Scanner(new File(filename));
while(s1.hasNext())
{
name=s1.next();
tst1=s1.nextInt();
tst2=s1.nextInt();
tst3=s1.nextInt();
st1=new Student(name, tst1, tst2, tst3);
starr[i]=st1;
i++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
s1.close();
int choice ;
while(true)
{
System.out.println(":: Menu ::");
System.out.println("1.Enter a Student");
System.out.println("2.List the names of all students");
System.out.println("3.Enter the test scores for a student speciad by name");
System.out.println("4.Find the grade of the Student");
System.out.println("5.Quit");
System.out.print("Enter Choice :");
choice=sc.nextInt();
switch(choice)
{
case 1:{
System.out.println("Enter the name of the Student :");
name=sc.next();
if(i<20)
{
System.out.println("Enter score in test#1 :");
tst1=sc.nextInt();
System.out.println("Enter score in test#2 :");
tst2=sc.nextInt();
System.out.println("Enter score in test#3 :");
tst3=sc.nextInt();
st1=new Student(name, tst1, tst2, tst3);
starr[i]=st1;
i++;
}
else
{
System.out.println("** Student Array is full **");
}
continue;
}
case 2:{
System.out.println("List of all student names are :");
for(int k=0;k<i;k++)
{
System.out.println(starr[k].getName());
}
continue;
}
case 3:{
int index=0;
System.out.print("Enter student name:");
name=sc.next();
while(index<i)
{
if(name.equalsIgnoreCase(starr[index].getName()))
{
System.out.println("Enter score in test#1 :");
tst1=sc.nextInt();
System.out.println("Enter score in test#2 :");
tst2=sc.nextInt();
System.out.println("Enter score in test#3 :");
tst3=sc.nextInt();
starr[index].setTest1(tst1);
starr[index].setTest2(tst2);
starr[index].setTest3(tst3);
break;
}
index++;
}
continue;
}
case 4:{
int index=0;
System.out.print("Enter the name of the Stduent :");
name=sc.next();
while(index<i)
{
if(name.equalsIgnoreCase(starr[index].getName()))
{
System.out.println("The Grade of the Stduent is :"+starr[index].calculateGrade());
break;
}
index++;
}
continue;
}
case 5:{
/* Instantiate a FileOutputStream object for file "dataOutput.txt"
* Do not specify a path .Your file will be created in the project folder
*/
fout = new FileOutputStream("dataOutput.txt");
/*
* Instantiate a PrintWriter Object for writing data to your FileOutputStream
*/
writer = new PrintWriter(fout);
DecimalFormat df=new DecimalFormat("#.##");
for(int h=0;h<i;h++)
{
writer.write(starr[h].getName()+" ");
writer.write(starr[h].getTest1()+" ");
writer.write(starr[h].getTest2()+" ");
writer.write(starr[h].getTest3()+" ");
writer.write(df.format(starr[h].computeAverage())+" ");
writer.write(starr[h].calculateGrade()+" ");
writer.println();
}
System.out.println("** Data Written to outfile **");
System.out.println("** Program Exit **");
writer.close();
break;
}
default:{
System.out.println("** Invalid Choice **");
continue;
}
}
break;
}
}
}
______________________
Output:
Enter the name of the text file :testscores.txt
:: Menu ::
1.Enter a Student
2.List the names of all students
3.Enter the test scores for a student speciad by name
4.Find the grade of the Student
5.Quit
Enter Choice :5
** Data Written to outfile **
** Program Exit **
_______________________
dataOutput.txt
sachin 90 89 87 88.67 B
Pointing 76 89 72 79 C
Rahul 67 56 45 56 F
Raman 67 78 91 78.67 C
John 78 34 56 56 F
Williams 78 89 87 84.67 C
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.