Cálculo de área de formas geométricas
Autor: Sama Assuero | Publicado: 2012-05-04T08:55:00-07:00

ontribuo abaixo com um código que encontrei no Javafree e me ajudou muito. Um aplicativo que vai calcular a área de uma circunferência ou retângulo conforme fluxo de entrada de um objeto Scanner.



import java.util.Scanner; &nbsp;
&nbsp; &nbsp;
&nbsp;public abstract class Forma { &nbsp;
&nbsp; &nbsp; private double medida[]; &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp; public Forma(int numMedidas) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;medida = new double[numMedidas]; &nbsp;
&nbsp; &nbsp; } &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp; public double getMedida(int i) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;if (i &lt; 0 || i &gt;= medida.length) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException("Numero inválido de medida!"); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;} &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;return medida[i]; &nbsp;
&nbsp; &nbsp; } &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp; protected void setMedida(int i, double m) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;if (i &lt; 0 || i &gt;= medida.length) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException("Numero inválido de medida!"); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;} &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;if (m &lt; 0) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException("Medida #" + i + "inválida."); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;} &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;medida[i] = m; &nbsp;
&nbsp; &nbsp; } &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp; public String toString() { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;StringBuffer sb = new StringBuffer(getClass().getName()); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;sb.append("medidas : "); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;for (int i = 0; i &lt; medida.length; i++) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(medida[i]); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(","); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;} &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;sb.append(medida[medida.length - 1]); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;sb.append("}"); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;return sb.toString(); &nbsp;
&nbsp; &nbsp; } &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp; public int getNumMedidas() { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;return medida.length; &nbsp;
&nbsp; &nbsp; } &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp; public abstract double area(); &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp; public static void main(String[] args) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Que área você quer calcular?\n" &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ "1 para Circunferência\n" + "2 para Retângulo\n &nbsp; "); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;try { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner abc = new Scanner(System.in); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int ler = abc.nextInt(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ler == 1) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Digite a altura do retângulo:"); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;double altura = abc.nextDouble(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Digite a largura do retângulo:"); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;double largura = abc.nextDouble(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Retangulo ret = new Retangulo(altura, largura); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Área do retângulo: " + ret.area()); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.println("Perímetro do retângulo: " + ret.perimetro()); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (ler == 2) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Digite o raio da circunferência:"); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;double raio = abc.nextDouble(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Circunferencia cir = new Circunferencia(raio); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Área da circunferência: " + cir.area()); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;} catch (Exception e) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;} &nbsp;
&nbsp; &nbsp; } &nbsp;
&nbsp;} &nbsp;
&nbsp; &nbsp;
&nbsp;public class Circunferencia extends Forma { &nbsp;
&nbsp; &nbsp; public Circunferencia(double raio) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;super(1); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;setMedida(0, raio); &nbsp;
&nbsp; &nbsp; } &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp; public double area() { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;return Math.PI * Math.pow(getMedida(0), 2); &nbsp;
&nbsp; &nbsp; } &nbsp;
&nbsp;} &nbsp;
&nbsp; &nbsp;
&nbsp;public class Retangulo extends Forma { &nbsp;
&nbsp; &nbsp; public Retangulo(double altura, double largura) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;super(2); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;setMedida(0, altura); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;setMedida(1, largura); &nbsp;
&nbsp; &nbsp; } &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp; public double area() { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;return getMedida(0) * getMedida(1); &nbsp;
&nbsp; &nbsp; } &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp; public double perimetro() { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;return 2 * (getMedida(0) + getMedida(1)); &nbsp;
&nbsp; &nbsp; } &nbsp;
&nbsp;} &nbsp;