Write a class called Student (classes start with a capital letter by convention)
ID: 3567351 • Letter: W
Question
Write a class called Student (classes start with a capital letter by convention). Your Student class should have the following:
Data: 5 data fields (which should all be private):
? a String called name
? an int called age
? a char called gender
? a double called gpa
? a boolean called onScholarship
Constructors: 2 overloaded constructors (means: same name, different number/type of arguments)
? A default constructor (receives no arguments) that does not do anything ( just has { } ). Normally, the default constructor would be used to set the data to "default" values (like "John Doe"), but I want you to see what values Java will assign if we do not set them ourselves.
? A parameterized constructor which receives 5 arguments in this order:
? a String which it will set its name field to
? an int which it will set its age field to
? a char which it will set its gender field to
? a double which it will set its gpa field to
? a boolean which it will set its onScholarship field to
Methods: 6 methods (which should be public)
? A method called getAge(), It will receive nothing and return the age.
? A method called setGPA. It will receive a new GPA (as a double) and set the "real" GPA to whatever is received. It returns nothing.
? A method called setOnScholarship. It will receive a boolean value and will set the "real" onScholarship data field to whatever is passed in. It returns nothing.
? A method called toString(). This method will receive no arguments and return a String with all of the information for whatever Student instance is told to return its toString. The String it should return should be made up of:
<its name><tab><its age><tab><its gender><tab><its gpa> <tab><onScholarship>
? A method called onProbation(). This method will receive no arguments and return a boolean. It will return true if the Student
Explanation / Answer
Here you go :)
I'e included the comments for each and every operation. Lemme know if u want any help.
//student class
public class Student
{
//Declaring the variables
private String name;
private int age;
private char gender;
private double gpa;
private boolean onScholarship;
//Default constructor
public Student()
{
}
//Overloading the default constructor
public Student(String name,int age,char gender,double gpa,boolean onScholarhsip)
{
//equating variables of the class to those which are passed in arguments
//this basically mean the current object under consideration
this.name=name;
this.age=age;
this.gender=gender;
this.gpa=gpa;
this.onScholarship=onScholarhsip;
}
//returning the age variable of this onject
public int getAge()
{
return this.age;
}
//setting the gpa variable of this object
public void setGPA(double gpa)
{
this.gpa=gpa;
}
//setting the onScholarship variable of this onject
public void setOnScholarship(boolean onScholarship)
{
this.onScholarship=onScholarship;
}
//returning the values of this object
public String toString()
{
return this.name+" "+this.age+" "+this.gender+" "+this.gpa+" "+this.onScholarship;
}
//checking whether this object is under probation or not
public boolean onProbation()
{
return this.gpa<2.0 && this.onScholarship;
}
//returning any complaints
public String complain()
{
return "I have no complaints yet!";
}
}
//Driver class
public class StudentDriver
{
public static void main(String[] args)
{
//creating a student instance via default constructor
Student s1=new Student();
//creating the student instance via overloaded constructor
Student s2=new Student("First name",20,'M',4.90,true);
System.out.println(s2.toString());
if(s2.onProbation())System.out.println("This student is under Probation");
//setting the gpa of s2 object
s2.setGPA(1.5);
if(s2.onProbation())System.out.println("This student is under Probation");
//setting scholarship to false for s2 object
s2.setOnScholarship(false);
System.out.println(s2.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.