#Having trouble with divide() method

10 messages · Page 1 of 1 (latest)

humble wraith
#

I keep failing the last test which should divide a and b correctly and I'm not sure why. Would very much appreciate the help.

​Error description:
divide() method is not dividing 4020 & 4035 correctly

public class ArithmeticWrapper {
    private static class Arithmetic{
        private int a;
        private int b;
     
        public Arithmetic(int a, int b){
            this.a = a;
            this.b = b;
        }
        
        public int getA(){
            return a;
        }
        
        public void setA(int a){
            this.a = a;
        }
        
        public int getB(){
            return b;
        }
        
        public void setB(int b){
            this.b = b;
        }
        
        public int add(){
            return a + b;
        }
        
        public int subtract(){
            return a - b;
        }
        
        public int multiply(){
            return a * b;
        }
        
        public double divide(){
            return (double) a / (double) b;
        }
    }
    
    public static class Calculator extends Arithmetic{
        
        public Calculator(int a, int b){
            super(a, b);
        }
    }
} ​​
rich furnaceBOT
#

This post has been reserved for your question.

Hey @humble wraith! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

ornate yarrow
#

@humble wraith

humble wraith
#

I still get the same error unfortunately.

#

Maybe I should be more specific, I am being asked this.

Write the following code in your editor below:

A private static class named Arithmetic that has two private fields int a & int b with their appropriate getters and setters. This class will also have the following member methods that do basic mathematical operations:
public int add();
public int subtract();
public int multiply();
public double divide();
A public static class named Calculator that inherits from a superclass named Arithmetic.

Test cases:
For all cases assume we initialize c = Calculator(4020, 4035)

Test0: Getters and setters for a and b work properly. (This is tested on all other test cases)
Test1: Adds a and b correctly.
Test2: Substracts a and b correctly.
Test3: Multiply a and b correctly.
Test4: Divides a and b correctly.

civic root
#

Weird. Does it look to you that it divides 4020 by 4035 fine?

humble wraith
#

It looks fine to me

civic root
#

I'd suspect the grader has a problem. And I'd try to return the result of an integer division, then cast to double. (Which would give 0.0)

humble wraith
#

How would I fix that?