In your main class, make a method called makeStudent that accepts 5 parameters f
ID: 3595168 • Letter: I
Question
In your main class, make a method called makeStudent that accepts 5 parameters for the initial values of a student object, and returns a new Student object. It would create a new temporary student object, use setters to set that object’s initial values, and return the temporary student object.
Question: How would I make this method with these paramters? I've tried doing this below: But it is throwing a void error. Can you write me the method?
----------------------------------------------------------------------------------------------------
Class Student: public class Student
{
main class:
Explanation / Answer
// Main.java
public class Main {
public static void main(String[] args) {
Student s1 = makeStudent("Jack", "CSE", 12345, 21, 9.8); //Calling static method makeStudent
Student s2 = makeStudent("Rose", "CSE", 12346, 20, 9.7); // Calling static method makeStudent
String studentInfo1 = s1.displayStudent(s1); //Passing student object into displayStudent
String studentInfo2 = s2.displayStudent(s2); //Returns String
System.out.println(studentInfo1);
System.out.println(studentInfo2);
}
//makeStudent method takes details of student and return object
public static Student makeStudent(String name, String major, int studentID, int age, double GPA){
Student st = new Student(); // Student Object and sets data
st.setName(name);
st.setMajor(major);
st.setStudentID(studentID);
st.setAge(age);
st.setGPA(GPA);
return st; //Returns Student Object st
}
}
//Student class
class Student {
//Properties
private String name;
private String major;
private int studentID;
private int age;
private double GPA;
//Setters and Getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getGPA() {
return GPA;
}
public void setGPA(double gPA) {
GPA = gPA;
}
//returns String with student details
public String displayStudent(Student fs){ //Printing Data using object Student fs
return "First Name:" + fs.name + " Major:" + fs.major + " Student ID:" + fs.studentID + " Age:" + fs.age + " GPA:" + fs.GPA;
}
}
/* Sample Output
First Name:Jack Major:CSE Student ID:12345 Age:21 GPA:9.8
First Name:Rose Major:CSE Student ID:12346 Age:20 GPA:9.7
*/