Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MAIN
- public static void main(String[] args) {
- try {
- Grupa G = new Grupa(20);
- Okrag o1 = new Okrag(2);
- G.UstawFigure(0, o1);
- Okrag o2 = new Okrag(6.66);
- G.UstawFigure(1, o2);
- Okrag o3 = new Okrag(7.2);
- G.UstawFigure(2, o3);
- Trojkat t1 = new Trojkat(2,3,4);
- G.UstawFigure(6, t1);
- Trojkat t2 = new Trojkat(3.12,4.1,5.86);
- G.UstawFigure(7, t2);
- Trojkat t3 = new Trojkat(6.66,6.66,6.66);
- G.UstawFigure(8, t3);
- Prostokat p1 = new Prostokat(2,3);
- G.UstawFigure(9, p1);
- Prostokat p2 = new Prostokat(1.12,2.22);
- G.UstawFigure(10, p2);
- Prostokat p3 = new Prostokat(6.66,6.66);
- G.UstawFigure(11, p3);
- System.out.println("Grupa figur: "+G.KlasaFigury());
- System.out.printf("Suma pĂłl: %.2f\n" , G.PolePowierzchni());
- System.out.printf("Suma obwodĂłw: %.2f\n",G.Obwod());
- System.out.println();
- G.Wyswietl();
- }
- catch(NiepoprawneDane n){
- System.out.println("BĹ‚Ä…d w utworzeniu nowej figury - niepoprawne dane!");
- }
- //FIGURA
- public abstract class Figura {
- abstract String KlasaFigury();
- abstract double PolePowierzchni();
- abstract double Obwod();
- }
- //OKRĄG
- public class Okrag extends Figura{
- private double promien;
- public Okrag(double promien) throws NiepoprawneDane {
- if(promien > 0) this.promien = promien;
- else throw new NiepoprawneDane();
- }
- public String KlasaFigury(){
- return "Okrąg";
- };
- public double PolePowierzchni(){
- return Math.PI * promien * promien;
- };
- public double Obwod(){
- return 2 * Math.PI * promien;
- };
- }
- // PROSTOKĄT
- public class Prostokat {
- private double a, b;
- public Prostokat(double a, double b) throws NiepoprawneDane {
- if(a > 0 || b > 0) {
- this.a = a;
- this.b = b;
- }
- else throw new NiepoprawneDane();
- }
- public String KlasaFigury() {
- return "Prostokat";
- };
- public double PolePowierzchni(){
- return a*b;
- };
- public double Obwod(){
- return (2*a)+(2*b);
- };
- }
- // TRÓJKĄT
- public class Trojkat {
- private double a, b, c;
- public Trojkat(double a, double b, double c) throws NiepoprawneDane {
- if(a > 0 || b > 0 || c > 0){
- this.a = a;
- this.b = b;
- this.c = c;
- }
- else throw new NiepoprawneDane();
- }
- public String KlasaFigury(){
- return "Trojkat";
- }
- public double PolePowierzchni(){
- double p = (a+b+c)/2;
- return Math.sqrt(p*(p-a)*(p-b)*(p-c));
- }
- public double Obwod(){
- return a+b+c;
- }
- }
- // GRUPA
- public class Grupa extends Figura {
- Figura[] tab;
- public Grupa(int n) throws NiepoprawneDane {
- if(n > 0) tab = new Figura[n];
- else throw new NiepoprawneDane();
- }
- public void UstawFigure(int i, Figura figura) throws NiepoprawneDane {
- if(i>0 || i<= tab.length){
- tab[i] = figura;
- }
- else throw new NiepoprawneDane();
- }
- public void Wyswietl(){
- System.out.println("Numer | Nazwa figury | Obwod | Pole powierzchni |");
- for(int i = 0;i<tab.length;i++)
- {
- if(tab[i]!=null)System.out.printf("[%2d] | %9s | %6.2f | %6.2f |\n",i,tab[i].KlasaFigury(),tab[i].Obwod(),tab[i].PolePowierzchni());
- }
- }
- public String KlasaFigury(){
- String nazwy = "";
- for(int i=0; i<tab.length; i++){
- if(tab[i]!= null) nazwy += tab[i].KlasaFigury() + " ";
- }
- return nazwy;
- }
- public double Obwod(){
- double suma = 0;
- for(Figura nr:tab){
- if(nr!=null) suma+= nr.Obwod();
- }
- return suma;
- }
- public double PolePowierzchni(){
- double pole = 0;
- for(int i=0; i<tab.length; i++){
- if(tab[i]!=null) pole+= tab[i].PolePowierzchni();
- }
- }
- }
- // NIEPOPRAWNEDANE
- public class NiepoprawneDane extends Exception {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement