Isso mesmo tnaires.
Obrigado! :)
Deu uma solucionada violenta nas dúvidas... sempre me "embanano" nessas modelagens.
Até acho que a classe Trapezio ficou meio mal organizada, mas não tem problema (eu espero).
O diagrama ficou assim:
[url]http://img219.imageshack.us/my.php?image=capturadatelaxm7.png[/url]
E a classe Trapezio:
/**
* Class Trapezio
*/
public class Trapezio extends Quadrilatero
{
private int minorBase = 0;
/**
* Constructor of Trapezio
* @param x X is the larger base
* @param y Y is the height
* @param w H is the minor base
*
*/
public Trapezio(int x, int y, int w)
{
super(x, y);
minorBase = w;
}
/**
* Acessor method for the minor base
* @return minorBase The minor base
*/
public int getMinorBase()
{
return minorBase;
}
/**
* Modifier method for the minor base
* @param newBase The new value of minorBase
*/
public void setMinorBase(int newBase)
{
minorBase = newBase;
}
/**
* Method that will calculate the area of the polygon
* @return area The area
*/
public double calculateArea()
{
return (((getX() * minorBase) / 2) * getY());
}
}
Também me surgiu uma dúvida na hora de fazer a função main. Eu queria colocar tipo, caso o usuário digite que queira calcular a área de um quadrado uma função é chamada pra ler as entradas dele e mostrar. Mas eu preciso do Scanner e tentei declarar ele como uma variável de instância private, mas a main não consegue acessar. É possível fazer isso? Senão meu código fica ruim de ler e tal...
Dá uma olhada:
import java.util.Scanner;
/**
* Class that will contain the main
*/
public class Controler
{
/**
* Method that will show the options for the user
*/
private void showOptions()
{
System.out.println();
System.out.println("Type the name of the shape that you want");
System.out.println("Square, Rectangle, Trapezium, Losangle or Parallelogram (or even Quit to exit)");
System.out.print("Type it here >> ");
}
/**
* Method that will ask for the rectangle values
*/
private void rectangle()
{
}
/**
* Method that answers to the user that that option doesn't exists
*/
private void dontKnow()
{
System.out.println();
System.out.println("Sorry, but I can't understand that :((");
System.out.println();
}
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
String option;
Controler cont = new Controler();
int x = 0;
int y = 0;
do {
cont.showOptions();
option = reader.nextLine();
option = option.toLowerCase();
if (option.equals("square"))
{
System.out.println();
System.out.print("side >> ");
x = Integer.parseInt(reader.nextLine());
Quadrado square = new Quadrado(x);
System.out.println("The area of the square is " + square.calculateArea());
System.out.println();
}
else if (option.equals("rectangle"))
{
System.out.println();
System.out.print("width >> ");
x = Integer.parseInt(reader.nextLine());
System.out.print("height >> ");
y = Integer.parseInt(reader.nextLine());
Retangulo rectangle = new Retangulo(x, y);
System.out.println("The area of the rectangle is " + rectangle.calculateArea());
System.out.println();
}
else if (option.equals("trapezium"))
{
int z = 0;
System.out.println();
System.out.print("larger base >> ");
x = Integer.parseInt(reader.nextLine());
System.out.print("other base >> ");
y = Integer.parseInt(reader.nextLine());
System.out.print("height >> ");
z = Integer.parseInt(reader.nextLine());
Trapezio trap = new Trapezio(x, y, z);
System.out.printf("The area of the trapezium is %.2f\n", trap.calculateArea());
System.out.println();
}
else if (option.equals("losangle"))
{
System.out.println();
System.out.print("D >> ");
x = Integer.parseInt(reader.nextLine());
System.out.print("d >> ");
y = Integer.parseInt(reader.nextLine());
Losango losangle = new Losango(x, y);
System.out.printf("The area of the losangle is %.2f\n", losangle.calculateArea());
System.out.println();
}
else if (option.equals("parallelogram"))
{
System.out.println();
System.out.print("base >> ");
x = Integer.parseInt(reader.nextLine());
System.out.print("height >> ");
y = Integer.parseInt(reader.nextLine());
Paralelogramo parallelogram = new Paralelogramo(x, y);
System.out.printf("The area of the parallelogram is %.2f\n", parallelogram.calculateArea());
System.out.println();
}
else if (option.equals("quit"))
{
System.out.println("Bye");
}
else
{
cont.dontKnow();
}
} while (!option.equals("quit"));
}
}
Horrível né? Se eu tivesse uma função como a dontKnow() pra cada formula o código ficaria muito mais legal, né?
Tem como? Eu pensei em fazer um acessador pro Scanner mas acho que não é assim que faz não...
Abraço e valeu pela ajuda!