views:

94

answers:

5

The outcome of this code should be "huugiin zardal: 3", but it doesn't work.
Please help. And also is there any easy way to write it?

class Cost3{

 int a;

 int u;
 int x;
 Cost3(int a,int u,int x){ 
 }
}

class FC1 extends Cost3{
   FC1(int a1, int u1, int x1){
   super(a1,u1,x1);
   a=a1;
   u=u1;
   x=x1;
   }

  public int huugiin_zardal(){
   return(((a+u)/2)*(x/100));
  }
}

public class Cost2{
 public static void main(String args[]){
  FC1 h_z=new FC1(3,4,20);
  System.out.println("huugiin zardal: "+h_z.huugiin_zardal());  
 } 
}
+4  A: 

Your problem is that ((3 + 4) / 2 ) * (20 / 100) is not 3. It is 0 because (20 / 100) is zero ... in integer arithmetic.

Indeed, even ((3 + 4) / 2 ) * 20 / 100 is 60 / 100 which is also 0. So I suspect that your root problem is that you have completely the wrong formula.

In addition, the initialization of a, u, and x should probably be in the supertype constructor not the subtype constructor. (This doesn't fix your current problem, but the way that you have the code at the moment, creating an instance of Cost3 will return an object in which the fields have not been initialized.)

Stephen C
+1  A: 

In your huugiin_zardal() method, all your variables are declared as int. So the division operator is performing integer division, which will silently truncate any remainder. For integer division, 20 / 100 = 0.

Just cast one of your variables to double and it will promote the entire expression to be evaluated as floating-point, only rounding to integer at the end.

Daniel Pryden
+1  A: 

How are you getting 3, and what exactly are you trying to achieve, your question is not very clear?

As for the code it will execute the following with the parameters you provided.

(((3+4)/2)*(20/100))

((7/2)*(0) // 10/100 will give you 0 unless you cast this calculation

3*0 //7/2 gets set to 3 since this is an int calculation

final answer 0.

Depending on what you are trying to achieve, you may just need to use something other than int calculations.

broschb
+3  A: 

((3+4)/2)*(20/100)
(7/2) * (0)
0

7/2 = 3 not 3.5 since it is truncated in integer division. the same for 20/100 = 0 not 0.2

Indent your code better too,it helps a lot.

William
+2  A: 

This is a nicer and simpler version of your code, however it will print out 0, for the integer division reasons already discussed by others:

class Cost3 {
    int a;
    int u;
    int x;

    public Cost3(int a, int u, int x) {
        this.a = a;
        this.u = u;
        this.x = x;
    }
}

class FC1 extends Cost3 {
    public FC1(int a, int u, int x) {
        super(a, u, x);
    }

    public int huugiin_zardal(){
        return (((a + u) / 2) * (x / 100));
    }
}

public class Cost2 {
    public static void main(String args[]){
        FC1 h_z = new FC1(3, 4, 20);
        System.out.println("huugiin zardal: " + h_z.huugiin_zardal());
    }
}

Notice that if the superclass handles the instance variables (a, u, and x), then there is no need to set them in the subclass, you just invoke the correct constructor via super().

It is hard to know exactly what your intention is, but one guess is that you want a base class (Cost3) that can handle particular variables, and have extensions (e.g. FC1) that return values based on different calculations. If so, it would be more logical to provide the base class as abstract, as follows:

class abstract Cost3 {
    int a;
    int u;
    int x;

    public Cost3(int a, int u, int x) {
        this.a = a;
        this.u = u;
        this.x = x;
    }

    public abstract int calculate();
}

class FC1 extends Cost3 {
    public FC1(int a, int u, int x) {
        super(a, u, x);
    }

    public int calculate() {
        return (((a + u) / 2) * (x / 100));
    }
}
Chris Carruthers