Create a class of students in a college. Make sure you include the following ins
ID: 3824442 • Letter: C
Question
Create a class of students in a college. Make sure you include the following instance/member variables:
Name
Phone Number
Student Number
Current GPA
Create the default constructor. Assign some default values to the instance variables.
Create a non-default constructor. This non-default constructor will take all four variables as parameters, and set them to the instance variables.
Create a "displayValues()" member function in the class. This class will display the instance variables.
Finally, in the "main()" function, declare two Student objects, one that uses the default constructor, and one that uses the non-default constructor. Then, use the objects to call the "displayValues()" function to display the values of the student objects.
Explanation / Answer
Create the Student Class, the following is the code for creating the Student class and build-in function Display() that lets it to display the inputted values for the variables.
public class Student {
private String Name;
private String Phone_Number;
private int Student_Number;
private float Current_GPA;
public Student(){
this.Name="John Doe";
this.Phone_Number="567-977";
this.Student_Number=101;
this.Current_GPA=3.6f;
}
public Student(String Name,String Phone_Number,int Student_Number,float Current_GPA){
this.Name=Name;
this.Phone_Number=Phone_Number;
this.Student_Number=Student_Number;
this.Current_GPA=Current_GPA;
}
public void Display(){
System.out.println("Name:"+this.Name);
System.out.println("Phone Number:"+this.Phone_Number);
System.out.println("Student Number:"+this.Student_Number);
System.out.println("Current_GPA:"+this.Current_GPA);
}
}
Next is the main function, where the Student class is called and values are entered in the class using the required constructor call.
public class StudentMain {
public static void main(String[] args){
Student stu1=new Student();
Student stu2=new Student("Jack Moe","969-381-182",102,3.2f);
stu1.Display();
stu2.Display();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.