2
I created three classes, an abstract class called Form, a class Retangulo
extending Forma
and a class programa
, containing the method Main
. My intention is in the "program" class to receive the parameters "width" and "length" and then use the class Retangulo
to create an object with the dimensions received and print it on the screen.
form class:
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();
}
Class Rectangle:
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 class methods Retangulo
in class programa
, print the result and treat possible exceptions?
Caio, watch out for the formatting when you put your code here. Select the code and click on the "{ }" icon, otherwise the indentation is confused with marking and becomes a mess.
– Pablo Almeida