1. Design and implement a class that will represent a student; call the class St
ID: 3685881 • Letter: 1
Question
1. Design and implement a class that will represent a student; call the class Student. Name your instance variables firstName, lastName, GPA (double) and credits (integer). Have a function called addCredits. This function will add some number of credits that is passed to it to the student’s current credits.
Implement Accessors and Mutators for all instance variables of the class. Have two constructors; the first will be the default constructor (set all instance variables to 1) and the second will be a constructor that will pass arguments to set the instance variables of the object.
Develop a function calculateLetterGrade which will return the correct letter grade according to the table below: GPA Letter Grade < 1.0 F >=1.0 and < 2.0 D >=2.0 and < 3.0 C >=3.0 and < 3.6 B >=3.6 A Develop a function printStudentInfo which will print all student data in neatly formatted output on the console. In the main instantiate three Student objects called Dick, Jane and Harry. The values to be passed are in the table below: Object Dick, Jane, Harry Instance Variables
firstName Dick, Jane, Harry
lastName Smith, Jones, Adams
GPA 3.62, 2.98, 1.13
credits 27, 36, 49
The main is to complete the following additional details:
1. Instantiate the object Dick by using the default constructor; then use your setters to put the data in the above table into the object. 2. Instantiate the object Jane by using the constructor with parameters and passing all data to instantiate the object through that constructor. 3. Instantiate the object Harry by using the constructor with parameters and passing all data to instantiate the object through that constructor. 4. Print the details of all objects using the printStudentInfo method. 5. Assume this semester is now over, and you need to update the student records. And 12 credits to Dick, add 16 credits to Jane and add 15 credits to Harry. 6. Print the details of all objects using the printStudentInfo method.
Explanation / Answer
class Student
{
String firstName, lastName;
double GPA=0.0;
int credits=0;
Student()
{
GPA=1;
credits=1;
firstName="1";
lastName="1";
}
Student(String fname,String lname,double GPA,int credits)
{
this.firstName=fname;
this.lastName=lname;
this.GPA=GPA;
this.credits=credits;
}
public void setFirstName(String fname)
{
this.firstName=fname;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String lname)
{
this.lastName=lname;
}
public String getLastName()
{
return lastName;
}
public void setGPA(double GPA)
{
this.GPA=GPA;
}
public double getGPA()
{
return GPA;
}
public void setCredits(int credits)
{
this.credits=credits;
}
public int getCredits()
{
return credits;
}
public char calculateLetterGrade()
{
if(GPA>=3.6)
return 'A';
else if(GPA >=3.0 && GPA < 3.6)
return 'B';
else if(GPA>=2.0 && GPA < 3.0)
return 'C';
else if(GPA >=1.0 && GPA< 2.0)
return 'D';
else
return 'F';
}
public void addCredits(int credits)
{
this.credits+=credits;
}
public void printStudentInfo()
{
System.out.println("Info of student"+firstName);
System.out.print("First Name: "+firstName);
System.out.print(" Last Name: "+lastName);
System.out.print(" GPA: "+GPA);
System.out.print(" Credits: "+credits);
System.out.print(" Grade: "+calculateLetterGrade());
System.out.println(" ");
}
}
public class HelloWorld{
public static void main(String []args){
Student s1=new Student();
s1.setFirstName("Dick");
s1.setLastName("Smith");
s1.setGPA(3.62);
s1.setCredits(27);
Student s2=new Student("Jane","Jones",2.98,36);
Student s3=new Student("Harry","Adams",1.13,49);
s1.printStudentInfo();
s2.printStudentInfo();
s3.printStudentInfo();
s1.addCredits(12);
s2.addCredits(15);
s3.addCredits(16);
System.out.println("After semester :");
s1.printStudentInfo();
s2.printStudentInfo();
s3.printStudentInfo();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.