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