Calling Methods and Exception Catching

0

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?

    
asked by anonymous 19.08.2016 / 23:50

1 answer

2

You have to instantiate the class by throwing the requested data into it within a 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());
        }
    }
}

See running on ideone .

It's just an exercise, but some can be done better. Starting with formatting consistency. It would also be best not to use Exception for this as I said in the previous question . You also need to be careful about inheritance. Many people use this example of Form / Rectangle to teach inheritance. Usually this is a mistake, at least in that way. Okay, it works, there's nothing wrong with this exercise, but it's teaching you to inherit from things that do not actually have a mother / daughter relationship . For example, others do not have length and height.

    
20.08.2016 / 00:30