CrypticZero CrypticZero - 2 months ago 5
Java Question

How do I use toString to retrieve my information in my class and how do I get my method to calculate the "GPA" in this class?

My question is how do I get the gpa that I have listed in my constructor class to compile. Every time I try to compile it, I get a .class expected error. I'm rather new to java so please forgive my incompetence. My teacher wants us to calculate a a students gpa by dividing the number of points by classes in our constructor class. Then she wants us to use toString to retrieve our students information, which I do not know how to do as we have gone over toString once. Sorry if my explanation is a little confusing, but its hard to explain this class without sounding like its a class I'm teaching. Here is my code:

public class student
{
private String name;
private int year;
private int age;
private double gpa;
/*
* Default Constructor
*/
public student()
{
name = "John";
year = 2016;
age = 16;
gpa = 4.0;
}
/*
* other constructor
*/
public student(String name, int year, int age, double gpa)
{
this.name = name;
this.year = year;
this.age = age;
this.gpa = gpa;
}
/*
* accessors
*/
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int getYear()
{
return year;
}
public double getGpa()
{
return gpa;
}
/*
* mutators
*/
public void setName(String name)
{
this.name=name;
}
public void setGpa(double gpa)
{
this.gpa=gpa;
}
public void setYear(int year)
{
this.year=year;
}
public void setAge(int age)
{
this.age=age;
}
/*
* calculate GPA
*/
public double calcGpa(double points, int classes)
{
return double gpa;
gpa = points / classes;
}

}

Answer

calcGpa method can be something like this:

public double calcGpa(double points, int classes)
   {
      gpa = points / classes;
      return gpa;
   }

and for change the toString method you can override it. something like this:

@Override
public String toString() {

    return getName() + " : " + getGpa() 
}