Call methods and catch exception

Asked

Viewed 221 times

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.

1 answer

2


You have to instantiate the class by throwing the requested dice into it try-catch. To simulate the exception you can use the method that changes the value of one of the properties. You can do this:

import java.util.Scanner;

abstract class Forma {
    public void imprime() {
        System.out.println("Área: " + area());
        System.out.println("Perímetro: " + perimetro());
    }
    public abstract double area();
    public abstract double perimetro();

}

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

class Programa {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double comprimento = sc.nextDouble();
        double largura = sc.nextDouble();
        try {
            Retangulo retangulo = new Retangulo(comprimento, largura);
            retangulo.imprime();
            retangulo.setComprimento(-comprimento);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

It’s just an exercise, but some can be done better. Starting with formatting consistency. It would also be better not to use Exception to that end as stated in previous question. You also need to be careful with inheritance. A lot of people use this example of Forma/Rectangular to teach inheritance. In general this is a mistake, at least in this way. Ok, it works, in this exercise there is nothing wrong, but goes teaching inherit things that do not actually have a mother/daughter relationship. For example, others have no length and height.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.