繁体   English   中英

如何使用toString检索我的班级中的信息,以及如何获取用于计算此类中的“ GPA”的方法?

[英]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?

我的问题是如何获取在构造函数类中列出的gpa进行编译。 每次尝试编译它时,都会收到.class预期错误。 我是Java的新手,所以请原谅我的无能。 我的老师希望我们通过将构造函数类中的点数除以类来计算aa学生的gpa。 然后她希望我们使用toString来检索我们的学生信息,因为我们已经阅读过toString一次,所以我不知道该怎么做。 抱歉,如果我的解释有点混乱,但很难在不听起来像我正在教的班级的情况下解释这堂课。 这是我的代码:

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; 
   }

}

calcGpa方法可以是这样的:

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

对于更改toString方法,您可以覆盖它。 像这样的东西:

@Override
public String toString() {

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM