Within Java, please write a class encapsulating the concept of a Student, assumi
ID: 3851814 • Letter: W
Question
Within Java, please write a class encapsulating the concept of a Student, assuming that a student has the following attributes: last name, first name, id, array of 4 grades. Include a constructor, the accessors and mutators, and method toString. Also code the following methods: one returning the GPA using the array of grades (assuming each grade represents a course grade and all courses have the same number of credit hours) and a method to add a course grade to the array of grades (this means creating a larger array). Write a client class to test all your methods.
Explanation / Answer
1)First creating Student class and declaring its members(variables)
import java.util.Arrays;
public class Student
{
private String lastName;
private String firstName;
private int id;
private char grade[]=new char[4];
//Constructor to initialize data members of the class
public Student(String lastName, String firstName, int id, char[] grade)
{
super();
this.lastName = lastName;
this.firstName = firstName;
this.id = id;
this.grade = grade;
}
//default constructor
public Student()
{
}
//getter and setter methods
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public char[] getGrade() {
return grade;
}
public void setGrade(char[] grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Student [lastName=" + lastName + ", firstName=" + firstName + ", id=" + id + ", grade="
+ Arrays.toString(grade) + "]";
}
}
2)Create a class to test Student class
public class StudentTestClass
{
public static void main(String[]args)
{
Student ob= new Student();
char[] chars = {'A','B','C','D'};
ob.setGrade(chars);
ob.setLastName("Joe");
ob.setFirstName("Root");
ob.setId(123);
System.out.println(ob.toString());
}
}
Output:
Student [lastName=Joe, firstName=Root, id=123, grade=[A, B, C, D]]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.