关于在子类中重写的困惑

Confusion about overriding in the subclass

所以我获得了以下 GradedActivity class:

 public class GradedActivity
 {
    private double score;  // Numeric score

    public void setScore(double s)
    {
        if (s < 0)
        score = 0.0;
    else if (s > 100)
        score = 100.0;
        else
            score = s;
     }

    public double getScore()
    {
       return score;
    }


    public char getGrade()
   {
      char letterGrade;

      if (score >= 90)
         letterGrade = 'A';
      else if (score >= 80)
         letterGrade = 'B';
      else if (score >= 70)
         letterGrade = 'C';
      else if (score >= 60)
         letterGrade = 'D';
      else
         letterGrade = 'F';

      return letterGrade;
   }
}

我的任务是生成一个构造函数,该构造函数接受 points Obtaned 和 pointsTotal 的值作为参数,初始化它们,并设置相应的分数(获得的分数除以总分数)、获取的分数和总分数的访问器和增变器。

所以这是我想出的:

public class ProgrammingAssignment extends GradedActivity 
{
   public int pointsObtained;
   public int pointsTotal;

   public ProgrammingAssignment(int p, int t)
   {

      pointsObtained = p;
      pointsTotal = t;
   } 

   public int getPointsObtained()
   {
      return pointsObtained;
   }

   public int getPointsTotal()
   {
      return pointsTotal;
   }

   public double getScore()
   {
     return pointsObtained / pointsTotal;
   }

   public void setPointsObtained(int p)
   {
      pointsObtained = p;

   }

   public void setPointsTotal(int t)
   {
      pointsTotal = t;
   }     
  }

一切编译都没有错误,但 getScore 在我的测试中没有计算 obtained/total(它返回 0)class:

 public class PADemo
 {
   public static void main(String[] args)
   {
        ProgrammingAssignment p1 = new ProgrammingAssignment(28,30);
            GradedActivity p2 = new ProgrammingAssignment(0,30);

      System.out.println (p1.getPointsObtained());
      System.out.println (p1.getPointsTotal());
      System.out.println (p1.getScore());  
      System.out.println (p1.getGrade());  

      System.out.println (p2.getScore());  
      System.out.println (p2.getGrade());  

      p1.setPointsObtained(25);
      p1.setPointsTotal(40);
      System.out.println (p1.getScore());  
      System.out.println (p1.getGrade() == 'F');  
 }
 }

如何使用 getScore()

获得分数(总分 obtained/points)

测试classreturns:

28
30
0.0
F
0.0
F
0.0

public class ProgrammingAssignment extends GradedActivity 
{

   //Use private for instance variables, never make them public - good practices
   private int pointsObtained;
   private int pointsTotal;

   public ProgrammingAssignment(int p, int t)
   {

      pointsObtained = p;
      pointsTotal = t;
   } 

   public int getPointsObtained()
   {
      return pointsObtained;
   }

   public int getPointsTotal()
   {
      return pointsTotal;
   }

   public double getScore()
   {
     //if you want the result to be double, the division should be done with double precision. So you can type cast the divisor {or change the divident and divisor types to double.}
     return pointsObtained / (double)pointsTotal;
   }

   public void setPointsObtained(int p)
   {
      pointsObtained = p;

   }

   public void setPointsTotal(int t)
   {
      pointsTotal = t;
   }     
  }

我已经在代码本身添加了注释,你哪里做错了。在整数除法的情况下,返回整数商。例如,28/30 将产生 0 而不是 0.9333...所以我们除以双倍即 28/30.0 将得到 0.9333..

是的,如果需要,您可以将变量类型更改为双精度。但前提是您需要 - 它会占用更多内存。

或者你声明一个变量double

   private double pointsObtained;
   private double pointsTotal;

或者在 getter getScore()

中显式地将结果转换为双精度数
   public double getScore()
       {
         return ((double)pointsObtained / pointsTotal);
       }

从表面上看,解决问题的最简单方法是将 getScore() 更改为:

public double getScore()
   {
     return (pointsObtained * 100) / pointsTotal;
   }

你的代码正在做的是它给你 0.93 而不是 93,并且 0.93 在放入整数时被截断为 0(如评论中指出的那样)。