Figuras 1 public abstract class Figura { protected String nombre; public abstract double getPerimetro(); public abstract double getArea(); public String getNombre(){return nombre;} } .................................. public class Circulo extends Figura { public static final double pi=3.1416; double radio; public Circulo(double r) {radio=r;} public double getPerimetro() { // TODO Auto-generated method stub return pi*2*radio; } @Override public double getArea() { // TODO Auto-generated method stub return pi*radio*radio; } } ......................................... public class PoligonoRegular extends Figura { private int numlado; private double radio; public static final double pi=3.1416; public PoligonoRegular(int nl, double rr) { numlado=nl; radio=rr; } @Override public double getPerimetro() { return numlado*2*radio*Math.sin(pi/numlado); } @Override public double getArea() { return getPerimetro()*0.5*Math.cos(pi/numlado); Page 1 Figuras 1 } } .............................................. import java.util.LinkedList; public class Catalogo { LinkedList lis; public Catalogo(){lis = new LinkedList();} public void agregaPR(int n, double r) { lis.add(new PoligonoRegular(n,r)); } public Figura busca(int n) {return (Figura)lis.get(n);} public void agregaCirc(double r) { lis.add(new Circulo(r)); } public double dimePerimetro(int n) { Figura f=busca(n); if (f==null) return -1.0; else return f.getPerimetro(); } public double dimeArea(int n) { Figura f=busca(n); if (f==null) return -1.0; else return f.getArea(); } } ............................................. import junit.framework.TestCase; public class CatalogoTest extends TestCase { private Catalogo kat; protected void setUp() throws Exception { super.setUp(); kat=new Catalogo(); } public void testagrega1() { kat.agregaCirc(10.0); System.out.println("Círculo con radio 10.0 tiene area= "+kat.dimeArea(0)); assertEquals(314.16,kat.dimeArea(0),0.01); Page 2 Figuras 1 } public void testagrega2() { kat.agregaPR(4,10.0); System.out.println("Cuadrado con radio 10.0 tiene area= "+kat.dimeArea(0)); assertEquals(20.0,kat.dimeArea(0),0.01); } } ................................... import java.io.*; import java.util.StringTokenizer; public class IUTexto { String deli=",;:-+ "; BufferedReader ent; String lin, tok; StringTokenizer st; boolean sigue; private Catalogo cata; public IUTexto(){cata=new Catalogo();} /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub IUTexto inter=new IUTexto(); inter.accion(); } public boolean accion() { boolean regreso=true; System.out.println("*** Comienza prueba de figuras ***"); System.out.println("Comandos:"); System.out.println("agrega poligono <nl> <radio>"); System.out.println("agrega circulo <radio>"); System.out.println("perimetro <cual>"); System.out.println("area <cual>"); System.out.println("fin"); //abrimos archivo de lectura sigue=true; try{ ent=new BufferedReader(new InputStreamReader(System.in)); while((lin=ent.readLine())!=null &&sigue) { Page 3 Figuras 1 st=new StringTokenizer(lin,deli,false); while (st.hasMoreTokens()) { tok=st.nextToken(); if (tok.equals("fin"))sigue=false; else if (tok.equals("area")) { tok=st.nextToken(); int n=Integer.parseInt(tok); double ar=cata.dimeArea(n); System.out.println("Area de figura "+n+" = "+ar); } else if (tok.equals("perimetro")) { int n=Integer.parseInt(st.nextToken()); System.out.println("Perimetro de figura "+n+" = "+cata.dimePerimetro(n)); } else if (tok.equals("circulo")) { tok=st.nextToken(); double d=Double.parseDouble(tok); cata.agregaCirc(d); System.out.println("Agregamos círculo de radio "+d); } else if (tok.equals("poligono")) { int n=Integer.parseInt(st.nextToken()); double d=Double.parseDouble(st.nextToken()); cata.agregaPR(n, d); System.out.println("Agregamos polígono de "+n+" lados y radio "+d); } else System.out.println("Error en comando. Repita"); } } Page 4 Figuras 1 System.out.println("*** Terminamos. Adiós. ***"); }catch (Exception e){System.out.println("Problema en lectura: "+e);return false;} return regreso; } } ............................................................. import junit.framework.TestCase; public class IUTextoTest extends TestCase { private IUTexto textin; protected void setUp() throws Exception { super.setUp(); textin=new IUTexto(); } /* * Test method for 'IUTexto.accion()' */ public void testAccion() { assertEquals(true,textin.accion()); } } ............................................ Page 5