Calculate area of a square or rectangle. Help, so I have to apply inheritance, overriding, perhaps overloading depending on the statement, here when I try to calculate the rectangle's area the result is 0.0 I believe it's because my asignar (assign) method in the subclass isn't overriding the one from the superclass. Also, does this program effectively apply inheritance and overriding(besides the issue i'm having)?
import java.io.*;
class AreaF {
private double lado; // side
public void asignar(double b) {
lado = b;
}
public double traerL() {
return lado;
}
}
class Cuadrado extends AreaF {
public double area() {
double a;
a = Math.pow(traerL(), 2);
return a;
}
}
class Rectangulo extends AreaF {
private double altura; // height
public void asignar(double h) {
double altura = h;
}
public double area() {
double a;
a = traerL() * altura;
return a;
}
}
public class Pro2 {
public static void main(String[] args) throws IOException {
double b = 0, h = 0;
int op;
Cuadrado obj = new Cuadrado(); // square
Rectangulo obj2 = new Rectangulo(); // rectangle
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Ingrese la base: ");
try {
b = Double.parseDouble(br.readLine());
} catch (NumberFormatException x) {
System.out.println(x);
}
obj.asignar(b);
/**
* 1 - for square
* 2 - for rectangle
*/
System.out.println("Ingrese 1 para saber el area de CUADRADO, 2 para RECTANGULO");
op = Integer.parseInt(br.readLine());
if (op == 1) {
System.out.println("El area del cuadrado es: " + obj.area()); // sq
// area
} else if (op == 2) {
System.out.println("Ingrese la altura del rectangulo: "); // height
try {
h = Double.parseDouble(br.readLine());
} catch (NumberFormatException x) {
System.out.println(x);
}
obj2.asignar(h);
System.out.println("El area del rectangulo es: " + obj2.area()); // rct
// area
} else
System.out.println("Error!");
}
}