1

So I was given the following 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;
   }
}

and I was tasked with generating a constructor that accepts values for points Obtaned and pointsTotal as arguments, initializes them, and sets the corresponding score (points obtained divided by points total), accessors and mutators for pointsobtained and total.

So here is what I came up with:

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

Everything compiles without error, but getScore isn't computing obtained/total (it comes back 0) in my test 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');  
 }
 }

How do I obtain the score (points obtained/points total) with getScore()

Test class returns:

28
30
0.0
F
0.0
F
0.0
true

  • Didn't read everything yet but you override the `GradedActivity` but dismiss completely the previous `score` logic. Isn't an assignment would be a group of `GradedActivity` instead ? – AxelH Aug 29 '19 at 06:50
  • It would be helpful if you wrote what is the result of these println() methods. – TomekK Aug 29 '19 at 06:52
  • 1
    You are doing the following operation `int / int` which always give an `int`. Now `28/30` give `0.93...`, which is rounded to `0`. – AxelH Aug 29 '19 at 06:52
  • And I will do a bit of self-promotion ... I wrote this [answer](https://stackoverflow.com/a/41822430/4391450) to understand the result of any math operation – AxelH Aug 29 '19 at 06:54
  • pointObtain and pointTotal should be double dataType – MangduYogii Aug 29 '19 at 06:55
  • @TomekK sorry. Edited with print results –  Aug 29 '19 at 07:00

3 Answers3

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

I have added the comments in code itself, where you are doing wrong. In case of an integer division, an integer quotient is returned. Eg., 28/30 will yield 0 & not 0.9333... So we divide with double i.e. 28/30.0 which will give you 0.9333..

Yes you can change the variable types to double if you want. But only if you want - it uses more memory.

Kris
  • 5,560
  • 3
  • 31
  • 55
0

Either you declare a variable double

   private double pointsObtained;
   private double pointsTotal;

or else explicitly converts the result into a double in a getter getScore()

   public double getScore()
       {
         return ((double)pointsObtained / pointsTotal);
       }
MangduYogii
  • 773
  • 6
  • 22
0

By the looks of it, the simplest thing to fix your problem would be to change getScore() to:

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

What your code is doing is that it's giving you 0.93 instead of 93, and 0.93 gets truncated to 0 when put into an integer(as pointed out in the comments).

the_ritz
  • 282
  • 1
  • 14