1 - create a student class, where each student has a name, ID number and six gra
ID: 3559237 • Letter: 1
Question
1 - create a student class, where each student has a name, ID number and six grades.
2- for each student, the program should do the following: a) notify student of their highest grade and lowest grade out of six grades entered.
b) Calculate and display the student's grade average and class status.
i. if the average is less than 70, their status is poor
ii. if the average is greater than 85, their average is GREAT
iii. if the average is between 71 and 84 their average is Fair
3- The program should accept multiple students, perform above tasks for each student.
4 - Save the entered grades, the Low and high grade, the calculated average and status, to the file, named "YourName.txt."
5. put a comment for every step you make..
Explanation / Answer
import java.io.IOException;
import java.io.Serializable;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.DataInputStream;
class Student{
String name;
int id;
int []grades;
public Student()
{
name = null;
id = 0;
grades = new int[6];
}
//calculating the class status
public String CalcGrade(int avg){
if(avg<=70) return "poor";
else if(avg>=71 && avg <= 84) return "Fair";
return "Great";
}
//calculating the highest grade
public int Higest(){
for (int i = 0; i < grades.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < grades.length; j++)
if (grades[j] < grades[index])
index = j;
int smallerNumber = grades[index];
grades[index] = grades[i];
grades[i] = smallerNumber;
}
return grades[grades.length - 1];
}
// calculating the lowest grade
public int Lowest(){
for (int i = 0; i < grades.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < grades.length; j++)
if (grades[j] < grades[index])
index = j;
int smallerNumber = grades[index];
grades[index] = grades[i];
grades[i] = smallerNumber;
}
return grades[0];
}
//calculating the grade average
public int Average(){
int sum = 0;
for(int i = 0; i < grades.length; i++){
sum += grades[i];
}
return sum / grades.length;
}
}
//class to store student avg , higest , lowest grade and status
//implements Serializable so that the object can be write to file
class write implements Serializable{
int avg , high , low ;
String status;
public write(int a,int h,int l,String str){
avg = a;
high = h;
low = l;
status = str;
}
public String toString(){
return "Avg:"+avg+" high"+high+" low"+low+" Status"+status;
}
}
public class StudentDemo {
public StudentDemo() {
}
public static void main(String[] args) throws IOException{
int j = 1;
Student s;
write demo;
DataInputStream di = new DataInputStream(System.in);
//reading 5 students data
while(j!=5){
s = new Student();
System.out.print("Enter Name:");
s.name = di.readLine();
System.out.print("Enter Id:");
s.id = Integer.parseInt(di.readLine());
System.out.print("Enter grades:");
for(int i = 0; i < s.grades.length; i++)
{
System.out.println("Enter Grade For "+i+1+"Student:");
s.grades[i] = Integer.parseInt(di.readLine());
}
//creating write object
demo = new write(s.Average(),s.Higest(),s.Lowest(),s.CalcGrade(s.Average()));
System.out.println("Avg:"+s.Average()+" Status:"+s.CalcGrade(s.Average())+" Higest:"+s.Higest()+" Lowest:"+s.Lowest());
//writing data to file
writeData(demo);
j++;
}
}
//function for writing data to file
public static void writeData(write wrt){
try{
FileOutputStream fout = new FileOutputStream("d:\cimg\address.txt");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(wrt);
oos.close();
System.out.println("Done");
}catch(Exception ex){
ex.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.