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: 3730408 • 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.
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.
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

public class Student {

private String firstName;

private String lastName;

private int id;

private char[] grades;

// Constructor to allow creation of Student objects

public Student(String firstName, String lastName, int id, char[] grades) {

this.firstName = firstName;

this.lastName = lastName;

this.id = id;

this.grades = grades;

}

/**

* @return the firstName

*/

public String getFirstName() {

return firstName;

}

/**

* @param firstName the firstName to set

*/

public void setFirstName(String firstName) {

this.firstName = firstName;

}

/**

* @return the lastName

*/

public String getLastName() {

return lastName;

}

/**

* @param lastName the lastName to set

*/

public void setLastName(String lastName) {

this.lastName = lastName;

}

/**

* @return the id

*/

public int getId() {

return id;

}

/**

* @param id the id to set

*/

public void setId(int id) {

this.id = id;

}

/**

* @return the grades

*/

public char[] getGrades() {

return grades;

}

/**

* @param grades the grades to set

*/

public void setGrades(char[] grades) {

this.grades = grades;

}

// This method will calculate GPA

public double getGPA() {

// Initializing gpa to zero

double gpa = 0.0;

for(char grade:grades) {

// Checking if grade is either 'A', 'B', 'C' or 'D'

if(grade == 'A') {

// Adding score to gpa

gpa += 4.0;

} else if(grade == 'B') {

// Adding score to gpa

gpa += 3.0;

} else if(grade == 'C') {

// Adding score to gpa

gpa += 2.0;

} else if(grade == 'D') {

// Adding score to gpa

gpa += 1.0;

}

}

// Returning the gpa after calculating and formatting it

return Double.parseDouble(new DecimalFormat("0.0").format(gpa/this.grades.length));

}

// This method will add grade to existing grades

public void addGrade(char grade) {

// Arrays.copyOf method will copy all the grades from existing array and return the new array with additional space to add new grade

this.setGrades(Arrays.copyOf(grades, grades.length+1));

// Adding grade after expanding the original array

grades[grades.length-1] = grade;

}

@Override

public String toString() {

// Using StringBuilder to concatenate all the information for current student

StringBuilder sb = new StringBuilder();

sb.append("First Name:""+this.getFirstName()+""");

sb.append(" ");

sb.append("Last Name:""+this.getLastName()+""");

sb.append(" ");

sb.append("ID:""+this.getId()+""");

sb.append(" ");

sb.append("Grades:");

for(char grade:this.getGrades()) {

sb.append("""+grade+""");

sb.append(" ");

}

sb.append(" ");

sb.append("GPA:""+this.getGPA()+""");

sb.append(" ");

// Returning StringBuilder in the form of String

return sb.toString();

}

}

Client.java

public class Client {

public static void main(String[] args) {

// Creating an array of grades for first student

char[] firstGrades = {'A', 'C', 'B', 'A', 'A'};

// Creating an array of grades for second student

char[] secondGrades = {'A', 'A', 'C', 'C', 'B'};

// Creating an object of first student using constructor

Student firstStudent = new Student("John", "Cena", 1, firstGrades);

// Creating an object of second student using constructor

Student secondStudent = new Student("Shawn", "Cena", 2, secondGrades);

// Displaying the details of First student before adding new grades

System.out.println("Details of first student before adding new grades");

System.out.println(firstStudent);

// Adding new grades

firstStudent.addGrade('A');

firstStudent.addGrade('A');

// Displaying the details of First student after adding new grades

System.out.println("Details of first student after adding new grades");

System.out.println(firstStudent);

// Displaying the details of Second student before adding new grades

System.out.println("Details of second student before adding new grades");

System.out.println(secondStudent);

// Adding new grades

secondStudent.addGrade('D');

secondStudent.addGrade('D');

// Displaying the details of Second student after adding new grades

System.out.println("Details of second student after adding new grades");

System.out.println(secondStudent);

}

}

Output:

Details of first student before adding new grades

First Name:"John" Last Name:"Cena" ID:"1" Grades:"A" "C" "B" "A" "A" GPA:"3.4"

Details of first student after adding new grades

First Name:"John" Last Name:"Cena" ID:"1" Grades:"A" "C" "B" "A" "A" "A" "A" GPA:"3.6"

Details of second student before adding new grades

First Name:"Shawn" Last Name:"Cena" ID:"2" Grades:"A" "A" "C" "C" "B" GPA:"3.0"

Details of second student after adding new grades

First Name:"Shawn" Last Name:"Cena" ID:"2" Grades:"A" "A" "C" "C" "B" "D" "D" GPA:"2.4"

NOTE: If you are facing any issues do let me know in comments section

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