Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Point(var x:Double=0.0, var y:Double=0.0) {
- }
- object Point {
- def apply(x:Double=0.0, y:Double=0.0) = {ilosc+=1; new Point(x, y)}
- private var ilosc=0
- def iloscFigur=ilosc
- }
- class Circle(var center:Point=Point(), var radius:Double=0.0) extends Point {
- def obwod():Double = {
- 2*3.14*radius
- }
- def pole():Double = {
- 3.14*radius*radius
- }
- def czyLezy(punkt:Point):Boolean = {
- ((punkt.x-x)*(punkt.x-x))+((punkt.y-y)*(punkt.y-y))==(radius*radius)
- }
- }
- object Circle {
- def apply(center:Point=Point(), radius:Double=0.0) = {ilosc+=1; new Circle(center, radius)}
- private var ilosc=0
- def iloscFigur=ilosc
- }
- class Cyllinder(var circle1:Circle=Circle(), var height:Double=0.0) extends Circle {
- def poleBoczne():Double = {
- circle1.obwod()*height
- }
- }
- object Cyllinder {
- def apply(c1:Circle=Circle(), height:Double=0.0) = {ilosc+=1; new Cyllinder(c1, height)}
- private var ilosc=0
- def iloscFigur=ilosc
- }
- class Triangle(var p1:Point=Point(), var p2:Point=Point(), var h:Double) extends Point {
- def pole = (h/2) * Math.sqrt( ((p2.x - p1.x) * (p2.x - p1.x)) + ((p2.y - p1.y) * (p2.y - p1.y)) )
- }
- object Triangle {
- def apply(p1:Point=Point(), p2:Point=Point(), h:Double) = {ilosc+=1; new Triangle(p1, p2, h)}
- private var ilosc=0
- def iloscFigur=ilosc
- }
- val p1 = Point(5,4);;
- val p2 = Point(6,7);;
- val p3 = Point(1,3);;
- val c1 = Circle(p1,4);;
- val cyllinder1 = Cyllinder(c1,10);;
- val tr1 = Triangle(p1,p2,2);;
- c1.obwod
- c1.pole
- c1.czyLezy( Point(1,1))
- cyllinder1.poleBoczne
- tr1.pole
- Point.iloscFigur
- Circle.iloscFigur
- Cyllinder.iloscFigur
- Triangle.iloscFigur
- /////////////////////////////////////////////////////////// zadanie 2
- trait plywanie
- {
- def msgP = ("\n Plywam ")
- }
- trait latanie
- {
- var stopienL = 1;
- def msgL = ("\n Latam " + ( if(stopienL==1) "znakomicie"
- else if (stopienL==2) "dobrze"
- else "slabo"))
- }
- trait nurkowanie
- {
- def msgN = ("\n Nurkuje ")
- }
- trait bieganie
- {
- var stopienB = 1;
- def msgB = ("\n Biegam " + (if(stopienB==1) "znakomicie"
- else if (stopienB==2) "dobrze"
- else "slabo" ))
- }
- object Ewidencja
- {
- var numer = 0;
- def nadajNr =
- {
- numer = numer+1;
- println("Pochodze od dinozaurów!!");
- numer;
- }
- def listaPlywajacych(ls:List[Ptak with plywanie]):String=
- {
- ls match
- {
- case Nil => ""
- case h::t => "\n" + h.toString() + h.msgP + listaPlywajacych (t)
- }
- }
- def listaLatajacych(ls:List[Ptak with latanie]):String=
- {
- ls match
- {
- case Nil => ""
- case h::t => "\n" + h.toString() + h.msgL + listaLatajacych (t)
- }
- }
- def listaNurkujacych(ls:List[Ptak with nurkowanie]):String=
- {
- ls match
- {
- case Nil => ""
- case h::t => "\n" + h.toString() + h.msgN + listaNurkujacych (t)
- }
- }
- def listaBiegajacych(ls:List[Ptak with bieganie]):String=
- {
- ls match
- {
- case Nil => ""
- case h::t => "\n" + h.toString() + h.msgB + listaBiegajacych (t)
- }
- }
- }
- abstract class Ptak
- {
- var nr = Ewidencja.nadajNr;
- override def toString = "Ptak nr: "+ nr + " - "
- }
- class Pingwin extends Ptak with plywanie with nurkowanie
- {
- override def toString = super.toString + this.getClass().getName();
- }
- class Golab extends Ptak with plywanie with latanie with bieganie
- {
- this.stopienL=1;
- this.stopienB=2;
- override def toString = super.toString + this.getClass().getName();
- }
- class Strus extends Ptak with bieganie
- {
- this.stopienB=1;
- override def toString = super.toString + this.getClass().getName();
- }
- class Kura extends Ptak with latanie with bieganie
- {
- this.stopienL=2;
- this.stopienB=2;
- override def toString = super.toString + this.getClass().getName();
- }
- class Sokol extends Ptak with latanie
- {
- this.stopienL=3;
- override def toString = super.toString + this.getClass().getName();
- }
- var p1 :Ptak with plywanie with latanie with bieganie = new Golab;
- var p2 :Ptak with latanie with bieganie = new Kura;
- var p3 :Ptak with latanie = new Sokol;
- println("LATAJACE:");
- Ewidencja.listaLatajacych(p1::p2::p3::Nil);
- var b1 :Ptak with plywanie with latanie with bieganie = new Golab;
- var b2 :Ptak with latanie with bieganie = new Kura;
- var b3 :Ptak with bieganie = new Strus;
- println("BIEGAJACE:");
- Ewidencja.listaBiegajacych(List(b1,b2,b3));
- var n1: Ptak with nurkowanie = new Pingwin
- println("NURKUJACE");
- Ewidencja.listaNurkujacych(n1::Nil)
- var pl1 :Ptak with plywanie with latanie with bieganie = new Golab;
- var pl2 :Ptak with plywanie with latanie with bieganie = new Golab;
- var pl3 :Ptak with plywanie = new Pingwin;
- println("PLYWAJACE")
- Ewidencja.listaPlywajacych(List(pl1,pl2,pl3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement