Figure geometriche

This commit is contained in:
Orange 2016-11-07 18:01:48 +01:00
parent 54b7468ce2
commit e03e128d70
7 changed files with 174 additions and 0 deletions

View File

@ -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;
}

View File

@ -0,0 +1,7 @@
import java.io.PrintStream;
public interface FiguraGeometrica{
double calcolaPerimetro();
double calcolaArea();
void print(PrintStream ps);
}

View File

@ -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;
}

View File

@ -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>();
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<FiguraGeometrica> figure;
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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