Question
Here is the problem I am working on: Write a class encapsulating the concept of a Student, assuming that a student has the following attributes:
Here is the problem I am working on: 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.
Here is the code I have written:
(1st java file, Student.java)
import java.util.Arrays;
import java.util.Scanner;
public class Student
{
public String lastName;
public String firstName;
public int id;
public double grades[]=new double[4];
public double gpa;
//Constructor to initialize data members of the class
public Student(String lastName, String firstName, int id, double[] grades){
super();
this.lastName = lastName;
this.firstName = firstName;
this.id = id;
this.grades = grades;
}
//default constructor
public Student(){
}
//set and get methods
public void setLastName (String lastName){this.lastName = lastName;}
public String getLastName () {return lastName;}
public void setFirstName (String firstName){this.firstName = firstName;}
public String getFirstName () {return firstName;}
public void setId (int id) {this.id = id;}
public int getId () {return id;}
public void setGrades (double[] grades) {this.grades = grades;}
public double[] getGrades () {return grades;}
public void setGpa (double gpa) {this.gpa = gpa;}
public double gpa () {return gpa;}
@Override
public String toString() {
return "Student: Last Name is " + lastName + ", First Name is " + firstName + ", Student ID number is " + id + ", Grades are "+ Arrays.toString(grades);
}
//method to return the GPA using the array of grades
public static double calcGpa(double[] grades){
double gpa = 0;
for (int i=0;i
gpa += grades[i];
}
gpa = (gpa/grades.length);
return gpa;
}
//method to add a new grade to the array of grades
public static double[] addGrade (double[] grades){
double newEntry;
System.out.println ("Enter the value of the new grade.");
Scanner in=new Scanner(System.in);
newEntry = in.nextDouble();
double[] newGrades = new double[grades.length+1];
for (int i=0;i
newGrades[i]=grades[i];
}
newGrades[newGrades.length-1]=newEntry;
grades=newGrades;
return grades;
}
}
(2nd java file, TestStudent.java)
public class TestStudent{
//Create a student object.
public static void main(String[] args) {
Student a1 = new Student();
double[] grades = {4.0,3.0,2.0,1.0};
a1.setGrades(grades);
a1.setLastName("Smith");
a1.setFirstName("Jenny");
a1.setId(8675309);
System.out.println(a1.toString());
//Test calcGPA
double gpa=calcGpa(grades[]);
System.out.println("Current GPA is " +gpa);
//Test addGrade
double[] addGrade=grades;
for (int i=0;i
System.out.print(grades[i]+" ");
}
}
}
When trying to compile the TestStudent.java file, I get the erro "TestStudent.java:19: error '.class' expected double gpa=calcGpa(grades[]);
Any help with solving this would be highly appreciated, and my apologies for the bad formatting.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started