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

Introduce Student, UndergradStudent and GradStudent classes as defined in the di

ID: 3708310 • Letter: I

Question

Introduce Student, UndergradStudent and GradStudent classes as defined in the diagram above. Also define a StudentFactory class as explained below.

Student is an abstract class with abstract getGPA() method. UndergradStudent and GradStudent are final classes. Information about students is entered by the user using the following format:

G|Adam Smith|123-4567|CISC|Network Management

U|Chris Smith|987-6543|MATH|ENGL|3.98

The fields are separated by the | character. The first field is either a G representing a graduate student or a U representing an undergraduate student. Graduate student entries have name, id, major and research area fields, whereas the undergraduate entries have name, id, major, minor and gpa fields. The getGPA() method for the graduate students should always return 4.0.

Introduce the StudentFactory class with the main method and another static method of the form:

This function should take the string entered by the user representing the student info and parse it to extract the student’s information and create either a Graduate or Undergraduate Student objects based on the entry (G or U). The method should return the newly created Student object.

Explanation / Answer

Below is your code: -

Student.java

public abstract class Student {

private String name;

private String Id;

private String major;

public Student(String name, String id, String major) {

super();

this.name = name;

Id = id;

this.major = major;

}

@Override

public String toString() {

return "Student Name = " + name + ", Id = " + Id + ", major = " + major + ", GPA = " + getGPA();

}

public abstract double getGPA();

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getId() {

return Id;

}

public void setId(String id) {

Id = id;

}

public String getMajor() {

return major;

}

public void setMajor(String major) {

this.major = major;

}

}

UnderGradStudent.java

public final class UndergradStudent extends Student {

private String minor;

private double gpa;

@Override

public double getGPA() {

return this.gpa;

}

public UndergradStudent(String name, String id, String major, String minor, double gpa) {

super(name, id, major);

this.minor = minor;

this.gpa = gpa;

}

public String getMinor() {

return minor;

}

public void setMinor(String minor) {

this.minor = minor;

}

public double getGpa() {

return gpa;

}

public void setGpa(double gpa) {

this.gpa = gpa;

}

}

GradStudent.java

public final class GradStudent extends Student {

private String researchArea;

@Override

public double getGPA() {

return 4.0;

}

public GradStudent(String name, String id, String major, String researchArea) {

super(name, id, major);

this.researchArea = researchArea;

}

public String getResearchArea() {

return researchArea;

}

public void setResearchArea(String researchArea) {

this.researchArea = researchArea;

}

}

StudentFactory.java

public class StudentFactory {

public static Student createStudent(String studentInfo) {

String arr[] = studentInfo.split("\|");

if (arr[0].equals("G")) {

Student student = new GradStudent(arr[1], arr[2], arr[3], arr[4]);

return student;

} else if (arr[0].equals("U")) {

Student student = new UndergradStudent(arr[1], arr[2], arr[3], arr[4], Double.parseDouble(arr[5]));

return student;

}

return null;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

ArrayList<Student> arrayList = new ArrayList<>();

String input = sc.nextLine();

arrayList.add(createStudent(input));

input = sc.nextLine();

arrayList.add(createStudent(input));

for (Student student : arrayList) {

System.out.println(student);

}

sc.close();

}

}

Output

G|Adam Smith|123-4567|CISC|Network Management
U|Chris Smith|987-6543|MATH|ENGL|3.98
Student Name = Adam Smith, Id = 123-4567, major = CISC, GPA = 4.0
Student Name = Chris Smith, Id = 987-6543, major = MATH, GPA = 3.98

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