I've created three classes, an abstract class called Form, a Retangulo
class that extends Forma
and a programa
class, which contains the Main
method. My intention is in the "program" class to receive the "width" and "length" parameters and then use the Retangulo
class to create an object with the received dimensions and print it on the screen.
class Form:
public abstract class Forma {
public void imprime(){
System.out.print("Área: "+area());
System.out.print("Perímetro: "+perimetro());
}
public abstract double area();
public abstract double perimetro();
}
Rectangle class:
public class Retangulo extends Forma {
private double comprimento;
private double largura;
Retangulo(double comprimento, double largura) throws Exception{
if(comprimento <= 0 || largura <= 0){
throw new Exception("ONDE JÁ SE VIU LARGURA E/OU COMPRIMENTO NEGATIVO?");
}
this.comprimento = comprimento;
this.largura = largura;
}
@Override
public double area() {
return comprimento*largura;
}
@Override
public double perimetro() {
return 2*(largura + comprimento);
}
public double getComprimento(){
return comprimento;
}
public double getLargura(){
return largura;
}
public void setComprimento(double comprimento) throws Exception{
if(comprimento < 0){
throw new Exception("ONDE JÁ SE VIU COMPRIMENTO NEGATIVO?");
}
this.comprimento = comprimento;
}
public void setLargura(double largura) throws Exception{
if(largura < 0){
throw new Exception("ONDE JÁ SE VIU LARGURA NEGATIVA?");
}
this.comprimento = largura;
}
}
Program class:
import java.util.Scanner;
public class programa {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double comprimento = sc.nextDouble();
double largura = sc.nextDouble();
}
}
How to call the methods of class Retangulo
in class programa
, print the result and handle possible exceptions?