From e03e128d703894524a0f39361b9a9897bd40601d Mon Sep 17 00:00:00 2001 From: Orange Date: Mon, 7 Nov 2016 18:01:48 +0100 Subject: [PATCH] Figure geometriche --- src/FigureGeometriche/Cerchio.java | 29 ++++++++++++++ src/FigureGeometriche/FiguraGeometrica.java | 7 ++++ src/FigureGeometriche/FiguraRegolare.java | 41 ++++++++++++++++++++ src/FigureGeometriche/Manager.java | 43 +++++++++++++++++++++ src/FigureGeometriche/Rettangolo.java | 36 +++++++++++++++++ src/FigureGeometriche/Test.java | 12 ++++++ src/FigureGeometriche/figure | 6 +++ 7 files changed, 174 insertions(+) create mode 100644 src/FigureGeometriche/Cerchio.java create mode 100644 src/FigureGeometriche/FiguraGeometrica.java create mode 100644 src/FigureGeometriche/FiguraRegolare.java create mode 100644 src/FigureGeometriche/Manager.java create mode 100644 src/FigureGeometriche/Rettangolo.java create mode 100644 src/FigureGeometriche/Test.java create mode 100644 src/FigureGeometriche/figure diff --git a/src/FigureGeometriche/Cerchio.java b/src/FigureGeometriche/Cerchio.java new file mode 100644 index 0000000..fc7b88a --- /dev/null +++ b/src/FigureGeometriche/Cerchio.java @@ -0,0 +1,29 @@ +import java.io.PrintStream; +import java.util.Scanner; + +public class Cerchio implements FiguraGeometrica{ + + public Cerchio(double raggio){ + this.raggio=raggio; + } + + public static Cerchio read(Scanner sc) throws Exception{ + if(!sc.hasNextDouble()) return null; + + return new Cerchio(sc.nextDouble()); + } + + public double calcolaPerimetro(){ + return raggio*3.14*2; + } + + public double calcolaArea(){ + return raggio*raggio*3.14; + } + + public void print(PrintStream ps){ + ps.println("Cerchio di raggio: "+raggio); + } + + private double raggio; +} diff --git a/src/FigureGeometriche/FiguraGeometrica.java b/src/FigureGeometriche/FiguraGeometrica.java new file mode 100644 index 0000000..61a61d1 --- /dev/null +++ b/src/FigureGeometriche/FiguraGeometrica.java @@ -0,0 +1,7 @@ +import java.io.PrintStream; + +public interface FiguraGeometrica{ + double calcolaPerimetro(); + double calcolaArea(); + void print(PrintStream ps); +} \ No newline at end of file diff --git a/src/FigureGeometriche/FiguraRegolare.java b/src/FigureGeometriche/FiguraRegolare.java new file mode 100644 index 0000000..5e08923 --- /dev/null +++ b/src/FigureGeometriche/FiguraRegolare.java @@ -0,0 +1,41 @@ +import java.io.PrintStream; +import java.util.Scanner; + +public class FiguraRegolare implements FiguraGeometrica{ + + public FiguraRegolare(double lato, int nlati, double numeroFisso){ + this.lato=lato; + this.nlati=nlati; + apotema=numeroFisso*lato; + } + + public static FiguraRegolare read(Scanner sc) throws Exception{ + double lato, numeroFisso; + int nlati; + + if(!sc.hasNextDouble()) return null; + lato=sc.nextDouble(); + if(!sc.hasNextDouble()) return null; + numeroFisso=sc.nextDouble(); + if(!sc.hasNextInt()) return null; + nlati=sc.nextInt(); + + return new FiguraRegolare(lato, nlati, numeroFisso); + } + + public double calcolaPerimetro(){ + return lato*nlati; + } + + public double calcolaArea(){ + return (calcolaPerimetro()*apotema)/2; + } + + public void print(PrintStream ps){ + ps.println("Figura regolare di "+nlati+" lunghi "+lato); + } + + private double lato; + private double apotema; + private int nlati; +} \ No newline at end of file diff --git a/src/FigureGeometriche/Manager.java b/src/FigureGeometriche/Manager.java new file mode 100644 index 0000000..846b061 --- /dev/null +++ b/src/FigureGeometriche/Manager.java @@ -0,0 +1,43 @@ +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Scanner; + +public class Manager{ + public Manager(Scanner sc) throws Exception{ + figure = new ArrayList(); + FiguraGeometrica fg; + String tipo=sc.next(); + + do{ + if(!sc.hasNext()) + fg=null; + else{ + tipo=sc.next(); + if(tipo.equals("Regolare")) + fg=FiguraRegolare.read(sc); + else if(tipo.equals("Rettangolo")) + fg=Rettangolo.read(sc); + else + fg=Cerchio.read(sc); + figure.add(fg); + } + }while(fg!=null); + } + + public double getAreaMax() { + FiguraGeometrica max = figure.get(0); + + for (int i = 1; i < figure.size(); i++) + if (max.calcolaArea() < figure.get(i).calcolaArea()) + max = figure.get(i); + + return max.calcolaArea(); + } + + public void print(PrintStream ps){ + for(FiguraGeometrica fg: figure) + fg.print(ps); + } + + private ArrayList figure; +} diff --git a/src/FigureGeometriche/Rettangolo.java b/src/FigureGeometriche/Rettangolo.java new file mode 100644 index 0000000..028e0c9 --- /dev/null +++ b/src/FigureGeometriche/Rettangolo.java @@ -0,0 +1,36 @@ +import java.io.PrintStream; +import java.util.Scanner; + +public class Rettangolo implements FiguraGeometrica{ + + public Rettangolo(double base, double altezza){ + this.base=base; + this.altezza=altezza; + } + + public static Rettangolo read(Scanner sc) throws Exception{ + double base, altezza; + + if(!sc.hasNextDouble()) return null; + base=sc.nextDouble(); + if(!sc.hasNextDouble()) return null; + altezza=sc.nextDouble(); + + return new Rettangolo(base, altezza); + } + + public double calcolaPerimetro(){ + return (base+altezza)*2; + } + + public double calcolaArea(){ + return base*altezza; + } + + public void print(PrintStream ps){ + ps.println("Rettangolo "+base+"x"+altezza); + } + + private double base; + private double altezza; +} \ No newline at end of file diff --git a/src/FigureGeometriche/Test.java b/src/FigureGeometriche/Test.java new file mode 100644 index 0000000..d93c754 --- /dev/null +++ b/src/FigureGeometriche/Test.java @@ -0,0 +1,12 @@ +import java.io.File; +import java.util.Scanner; + +public class Test { + public static void main(String [] args) throws Exception{ + Scanner sc = new Scanner(new File("figure")); + Manager manager = new Manager(sc); + + //manager.getAreaMax(); + manager.print(System.out); + } +} diff --git a/src/FigureGeometriche/figure b/src/FigureGeometriche/figure new file mode 100644 index 0000000..4c178f0 --- /dev/null +++ b/src/FigureGeometriche/figure @@ -0,0 +1,6 @@ +Cerchio 2 +Regolare 2 0.5 4 +Regolare 2 1.03 7 +Rettangolo 3 5 +Cerchio 3 +Regolare 2 2.35 15 \ No newline at end of file