Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.pg;
- interface Fabryka
- {
- Figura zwrocFigure(String typ, int a, int b, int c) throws Wyjatek,Wyjatek2;
- }
- class Wyjatek extends Exception // Nasz własny wyjątek!
- {
- public Wyjatek(String name)
- {
- super("To nie jest poprawna figura klasy "+name+"!");
- }
- }
- class Wyjatek2 extends Exception
- {
- public Wyjatek2(String name)
- {
- super("Błędna nazwa. Nie ma takiej figury."+name+"<3.");
- }
- }
- abstract class Figura
- {
- String name;
- abstract double pole();
- abstract double obwod();
- protected boolean weryfikacja(float a,float b) throws Wyjatek
- {
- boolean ok = (a > 0) && (b > 0);
- if (!ok)
- throw new Wyjatek(name);
- return ok;
- }
- protected boolean weryfikacja(float a,float b,float c) throws Wyjatek
- {
- boolean ok = (a > 0) && (b > 0) && (c > 0);
- if (!ok)
- throw new Wyjatek(name);
- return ok;
- }
- @Override
- public String toString()
- {
- String info = new String(name +", obwod: "+obwod()+", pole: "+ pole());
- return info;
- }
- }
- class Prostokat extends Figura
- {
- double a, b;
- public Prostokat(float a, float b) throws Wyjatek
- {
- name = "Prostokat";
- if(weryfikacja(a,b))
- {
- this.a = a;
- this.b = b;
- }
- }
- public double pole()
- {
- return a * b;
- }
- public double obwod()
- {
- return 2 * (a + b);
- }
- }
- class Trojkat extends Figura
- {
- double a, b,c;
- public Trojkat(float a, float b,float c) throws Wyjatek
- {
- name = "Trojkat";
- if(weryfikacja(a,b,c))
- {
- this.a = a;
- this.b = b;
- this.c = c;
- }
- }
- public double pole()
- {
- double p=(a+b+c)/2;
- double wynik=Math.sqrt(p*(p-a)*(p-b)*(p-c));
- return wynik;
- }
- public double obwod()
- {
- return (a + b + c);
- }
- }
- class FabrykaFigur implements Fabryka
- {
- public FabrykaFigur()
- {
- }
- public Figura zwrocFigure(String typ, int a, int b, int c) throws Wyjatek,Wyjatek2
- {
- Figura wynik;
- if(typ=="trojkat" || typ== "Trojkat" || typ=="Trójkąt" || typ=="trójkąt")
- {
- wynik=new Trojkat(a,b,c);
- }
- else if(typ=="prostokat" || typ== "Prostokat" || typ=="Prostokąt" || typ=="prostokąt")
- {
- wynik=new Prostokat(a,b);
- }
- else
- throw new Wyjatek2("Lubie Placki");
- return wynik;
- }
- }
- public class Figury
- {
- public static void main(String[] args) throws Wyjatek
- {
- try
- {
- //Prostokat p = new Prostokat(-2,4);
- //System.out.println(p);
- FabrykaFigur China=new FabrykaFigur();
- System.out.println(China.zwrocFigure("Trójkąt", 4, 3, 5));
- }
- catch(Exception e)
- {
- System.out.println("HAHAHA");
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement