Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class encapsulating the concept of a Student, assuming that a student ha

ID: 3745888 • Letter: W

Question

Write a class encapsulating the concept of a Student, assuming that a student has the following attributes: last name, first name, id, array of 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 create 2 student objects and test all your methods.

Remember to submit all your .java files, .class files and screenshots of your code and output.

Explanation / Answer

Student.java

import java.util.Arrays;

public class Student {
//Declaring instance variables
private String lastname;
private String firstname;
private int id;
private int grades[] = new int[10];
private static int size;

//Parameterized constructor
public Student(String lastname, String firstname, int id) {
this.lastname = lastname;
this.firstname = firstname;
this.id = id;
size = 0;
}

// getters and setters
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 int[] getGrades() {
return grades;
}

public void setGrades(int[] grades) {
this.grades = grades;
}

public void addGrade(int grade) {
grades[size] = grade;
size++;
}

//This method will calculate the GPA of Student
public double calculateGPA() {

double sum = 0, avg = 0.0, gpa;
for (int i = 0; i < size; i++) {
sum += grades[i];
}
avg = sum / size;
gpa = (avg / 20) - 1;
return gpa;
}

//toString method is used to display the contents of an object inside it
@Override
public String toString() {
System.out.println("Lastname=" + lastname);
System.out.println("Firstname=" + firstname);
System.out.println("Id=" + id);
System.out.print("Grades=");

for (int i = 0; i < size; i++) {
System.out.print(grades[i] + " ");
}
return "";
}

}

__________________

Client.java

public class Client {

public static void main(String[] args) {

Student s1 = new Student("Peterson", "Mike", 1111);
s1.addGrade(98);
s1.addGrade(77);
s1.addGrade(87);
s1.addGrade(98);
System.out.println("Student#1 Details::");
System.out.println(s1);
System.out.println("GPA of Student#1:" + s1.calculateGPA());

System.out.println();
Student s2 = new Student("Jones", "Kevin", 2222);
s2.addGrade(88);
s2.addGrade(67);
s2.addGrade(57);
s2.addGrade(78);
System.out.println("Student#2 Details::");
System.out.println(s2);
System.out.println("GPA of Student#2:" + s2.calculateGPA());

}

}

_______________

Output:

Student#1 Details::
Lastname=Peterson
Firstname=Mike
Id=1111
Grades=98 77 87 98
GPA of Student#1:3.5

Student#2 Details::
Lastname=Jones
Firstname=Kevin
Id=2222
Grades=88 67 57 78
GPA of Student#2:2.625

____________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote