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: 3871917 • 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 test all your methods.

Explanation / Answer

Student.java

import java.text.DecimalFormat;
import java.util.ArrayList;

public class Student {
//Declaring instance variables
private String firstname;
private String lastname;
private int id;
private ArrayList < String > grade;

//Parameterized constructor
public Student(String firstname, String lastname, int id,
ArrayList < String > grade) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.id = id;
this.grade = grade;
}

//getters and setters
public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public ArrayList < String > getGrade() {
return grade;
}

public void setGrades(ArrayList < String > grade) {
this.grade = grade;
}
//This method will calculate tge GPA
double CalGPA() {
double tot = 0;
double gradeValue = 0;
for (int i = 0; i < grade.size(); i++) {
if (grade.get(i).equals("A"))
gradeValue = 4.00;
else if (grade.equals("A-"))
gradeValue = 3.67;
else if (grade.equals("B+"))
gradeValue = 3.33;
else if (grade.equals("B"))
gradeValue = 3.00;
else if (grade.equals("B-"))
gradeValue = 2.67;
else if (grade.equals("C+"))
gradeValue = 2.33;
else if (grade.equals("C"))
gradeValue = 2.00;
else if (grade.equals("D+"))
gradeValue = 1.33;
else if (grade.equals("D"))
gradeValue = 1.00;
else if (grade.equals("F"))
gradeValue = 0;

tot += gradeValue;
}

return tot / grade.size();
}

//This method will Add course grade to the array
void addCourseGrade(String grade) {
this.grade.add(grade);
}

//toString method is used to display the contents of an object inside it
@Override
public String toString() {
//DecimalFormat class is used to format the output
DecimalFormat df = new DecimalFormat("#.#");
return "First Name=" + firstname + " Last Name=" + lastname + " Id=" + id + " GPA =" + df.format(CalGPA());
}


}

_____________________

Test.java

import java.util.ArrayList;

public class Test {

public static void main(String[] args) {
//Creating an Array of Grades
ArrayList < String > grades = new ArrayList < String > ();

//Adding Grades to the ArrayList
grades.add("A+");
grades.add("B");
grades.add("A");
grades.add("C");
grades.add("D");
grades.add("A-");

//Creating Student class object by passing values as arguments
Student s1 = new Student("Kane", "Williams", 111, grades);

//Displaying the info about the student
System.out.println(s1.toString());


System.out.println("** After Adding Few Grades **");
//Adding few grades to the array
s1.addCourseGrade("F");
s1.addCourseGrade("C+");
s1.addCourseGrade("D");

//Displaying the Updates GPA
System.out.printf("GPA is :%.2f" ,s1.CalGPA());


}

}

___________________________

Output:

First Name=Kane
Last Name=Williams
Id=111
GPA =2.7
** After Adding Few Grades **
GPA is :3.11


_____________Could you rate me well.Plz .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