Create a Java project called Lab8-Q1. Inside this project create a class named \
ID: 3741480 • Letter: C
Question
Create a Java project called Lab8-Q1. Inside this project create a class named "Q1" with a
main function. Also create a class named "Student".
The main function should have the following lines of code:
Student Corey = new Student("Corey Park", 4.0, 90);
Student Don = new Student("Don Draper", 3.6, 60);
Corey.displayStudentProfile();
Don.displayStudentProfile();
And the program should output the following:
First Name: Corey
Last Name: Park
GPA: 4.0
Year: Senior
First Name: Don
Last Name: Draper
GPA: 3.6
Year: Junior
The class Student should have the following variables.
private String fullName;
private String firstName;
private String lastName;
private String gradeYear;
private double gpa;
private int creditsComplete;
The constructer will initialize the variables, fullName, gpa and creditsComplete.
The constructer will also parse the the students fullName and split it into the variables
firstName and lastName based on white space.
The constructor will use the number of credits completed to compute if the student is a
Freshmen (less then 30 credits), Sophomore (less then 60 credits), Junior (less then 90 credits),
or a Senior.
The class Student should have a method displayStudentProfile() that prints to the standard
output the students First Name, Last Name, GPA, and Year.
Explanation / Answer
Q1.java
public class Q1 {
public static void main(String[] args) {
Student Corey = new Student("Corey Park", 4.0, 90);
Student Don = new Student("Don Draper", 3.6, 60);
Corey.displayStudentProfile();
Don.displayStudentProfile();
}
}
Student.java
public class Student {
private String fullName;
private String firstName;
private String lastName;
private String gradeYear;
private double gpa;
private int creditsComplete;
public Student(String fullName, double gpa, int creditsComplete) {
this.fullName=fullName;
String name[] = fullName.split(" ");
firstName=name[0];
lastName=name[1];
this.gpa = gpa;
this.creditsComplete=creditsComplete;
}
public void displayStudentProfile() {
System.out.println("First Name: "+firstName);
System.out.println("Last Name: "+lastName );
System.out.println("GPA: "+gpa);
if(creditsComplete<30) {
System.out.println("Year: Freshmen");
} else if(creditsComplete>=30 && creditsComplete<60) {
System.out.println("Year: Sophomore");
} else if(creditsComplete>=60 && creditsComplete<90) {
System.out.println("Year: Junior");
} else {
System.out.println("Year: Senior");
}
}
}
Output:
First Name: Corey
Last Name: Park
GPA: 4.0
Year: Senior
First Name: Don
Last Name: Draper
GPA: 3.6
Year: Junior