Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In java: Design and write a class named Student with attributes that store a nam

ID: 3855498 • Letter: I

Question

In java:

Design and write a class named Student with attributes that store a name, social security number, number of courses completed, grades for each course, and credits for each course. Include constructors, getter and setter methods, and a method that returns a student’s grade point average.
Write a program that accepts data for three students, and store the three Student objects in a binary file called students.dat.
Write a second program that displays the name and grade point average of each student.

Explanation / Answer

StudentWrite.java

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.util.Scanner;

public class StudentWrite {

public static void main(String[] args) throws IOException {

File file = new File("D:\students.dat");

FileOutputStream fout = new FileOutputStream(file);

ObjectOutputStream oos = new ObjectOutputStream(fout);   

Scanner scan = new Scanner(System.in);

int grade;

String credit;

Student stu[] = new Student[3];

for(int i=0;i<stu.length;i++) {

System.out.println("Enter student name: ");

String name = scan.next();

System.out.println("Enter student ssn:");

int ssn = scan.nextInt();

System.out.println("Enter number of courses: ");

int numOfCourses = scan.nextInt();

int grades[] = new int[numOfCourses];

String credits[]=new String[numOfCourses];

for(int j=0;j<grades.length;j++) {

System.out.println("Enter the grade: ");

grade = scan.nextInt();

System.out.println("Enter the credit: ");

credit = scan.next();

grades[j]=grade;

credits[j] = credit;

}

Student s = new Student(name, ssn);

s.setGrades(grades);

s.setCredits(credits);

s.setNumOfCourses(numOfCourses);

stu[i]=s;

}

oos.writeObject(stu);

System.out.println("File has been generated");

oos.close();

}

}

Student.java

import java.io.Serializable;

public class Student implements Serializable{

private String name, credits[];

private int ssn;

private int numOfCourses, grades[];

public Student(String name, int ssn) {

this.name=name;

this.ssn=ssn;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String[] getCredits() {

return credits;

}

public void setCredits(String[] credits) {

this.credits = credits;

}

public int getSsn() {

return ssn;

}

public void setSsn(int ssn) {

this.ssn = ssn;

}

public int getNumOfCourses() {

return numOfCourses;

}

public void setNumOfCourses(int numOfCourses) {

this.numOfCourses = numOfCourses;

}

public int[] getGrades() {

return grades;

}

public void setGrades(int[] grades) {

this.grades = grades;

}

public double getAverage() {

int total = 0;

for(int i=0;i<numOfCourses; i++) {

total+=grades[i];

}

return total/numOfCourses;

}

}

Output:

Enter student name:
Suresh
Enter student ssn:
1111
Enter number of courses:
2
Enter the grade:
45
Enter the credit:
ttt
Enter the grade:
55
Enter the credit:
yyy
Enter student name:
Sekhar
Enter student ssn:
2222
Enter number of courses:
2
Enter the grade:
66
Enter the credit:
hhh
Enter the grade:
77
Enter the credit:
jjj
Enter student name:
Anshu
Enter student ssn:
33333
Enter number of courses:
2
Enter the grade:
88
Enter the credit:
iii
Enter the grade:
99
Enter the credit:
ooo
File has been generated

StudentDisplay.java

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

public class StudentDisplay {

public static void main(String[] args) throws IOException, ClassNotFoundException {

File file = new File("D:\students.dat");

FileInputStream fin = new FileInputStream(file);

ObjectInputStream ois = new ObjectInputStream(fin);

Student[] c = (Student[]) ois.readObject();

for(int i=0; i<c.length; i++){

System.out.println("Name: "+c[i].getName()+" Average: "+c[i].getAverage());

}

ois.close();

}

}

Output:

Name: Suresh Average: 55.0
Name: Sekhar Average: 71.0
Name: Anshu Average: 82.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote