Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. class Point(var x:Double=0.0, var y:Double=0.0) {
  2. }
  3.  
  4. object Point {
  5. def apply(x:Double=0.0, y:Double=0.0) = {ilosc+=1; new Point(x, y)}
  6. private var ilosc=0
  7. def iloscFigur=ilosc
  8. }
  9.  
  10. class Circle(var center:Point=Point(), var radius:Double=0.0) extends Point {
  11. def obwod():Double = {
  12. 2*3.14*radius
  13. }
  14. def pole():Double = {
  15. 3.14*radius*radius
  16. }
  17. def czyLezy(punkt:Point):Boolean = {
  18.  
  19. ((punkt.x-x)*(punkt.x-x))+((punkt.y-y)*(punkt.y-y))==(radius*radius)
  20.  
  21. }
  22. }
  23.  
  24. object Circle {
  25. def apply(center:Point=Point(), radius:Double=0.0) = {ilosc+=1; new Circle(center, radius)}
  26. private var ilosc=0
  27. def iloscFigur=ilosc
  28. }
  29.  
  30. class Cyllinder(var circle1:Circle=Circle(), var height:Double=0.0) extends Circle {
  31. def poleBoczne():Double = {
  32. circle1.obwod()*height
  33. }
  34. }
  35. object Cyllinder {
  36. def apply(c1:Circle=Circle(), height:Double=0.0) = {ilosc+=1; new Cyllinder(c1, height)}
  37. private var ilosc=0
  38. def iloscFigur=ilosc
  39. }
  40.  
  41. class Triangle(var p1:Point=Point(), var p2:Point=Point(), var h:Double) extends Point {
  42. def pole = (h/2) * Math.sqrt( ((p2.x - p1.x) * (p2.x - p1.x)) + ((p2.y - p1.y) * (p2.y - p1.y)) )
  43. }
  44.  
  45. object Triangle {
  46. def apply(p1:Point=Point(), p2:Point=Point(), h:Double) = {ilosc+=1; new Triangle(p1, p2, h)}
  47. private var ilosc=0
  48. def iloscFigur=ilosc
  49. }
  50.  
  51.  
  52.  
  53. val p1 = Point(5,4);;
  54. val p2 = Point(6,7);;
  55. val p3 = Point(1,3);;
  56.  
  57. val c1 = Circle(p1,4);;
  58. val cyllinder1 = Cyllinder(c1,10);;
  59. val tr1 = Triangle(p1,p2,2);;
  60.  
  61.  
  62. c1.obwod
  63. c1.pole
  64. c1.czyLezy( Point(1,1))
  65.  
  66. cyllinder1.poleBoczne
  67. tr1.pole
  68.  
  69. Point.iloscFigur
  70. Circle.iloscFigur
  71. Cyllinder.iloscFigur
  72. Triangle.iloscFigur
  73.  
  74.  
  75.  
  76.  
  77. /////////////////////////////////////////////////////////// zadanie 2
  78. trait plywanie
  79. {
  80. def msgP = ("\n Plywam ")
  81. }
  82.  
  83. trait latanie
  84. {
  85. var stopienL = 1;
  86. def msgL = ("\n Latam " + ( if(stopienL==1) "znakomicie"
  87. else if (stopienL==2) "dobrze"
  88. else "slabo"))
  89. }
  90.  
  91. trait nurkowanie
  92. {
  93. def msgN = ("\n Nurkuje ")
  94. }
  95.  
  96. trait bieganie
  97. {
  98. var stopienB = 1;
  99. def msgB = ("\n Biegam " + (if(stopienB==1) "znakomicie"
  100. else if (stopienB==2) "dobrze"
  101. else "slabo" ))
  102. }
  103.  
  104. object Ewidencja
  105. {
  106. var numer = 0;
  107. def nadajNr =
  108. {
  109. numer = numer+1;
  110. println("Pochodze od dinozaurów!!");
  111. numer;
  112. }
  113.  
  114. def listaPlywajacych(ls:List[Ptak with plywanie]):String=
  115. {
  116. ls match
  117. {
  118. case Nil => ""
  119. case h::t => "\n" + h.toString() + h.msgP + listaPlywajacych (t)
  120. }
  121. }
  122.  
  123. def listaLatajacych(ls:List[Ptak with latanie]):String=
  124. {
  125. ls match
  126. {
  127. case Nil => ""
  128. case h::t => "\n" + h.toString() + h.msgL + listaLatajacych (t)
  129. }
  130. }
  131.  
  132. def listaNurkujacych(ls:List[Ptak with nurkowanie]):String=
  133. {
  134. ls match
  135. {
  136. case Nil => ""
  137. case h::t => "\n" + h.toString() + h.msgN + listaNurkujacych (t)
  138. }
  139. }
  140.  
  141. def listaBiegajacych(ls:List[Ptak with bieganie]):String=
  142. {
  143. ls match
  144. {
  145. case Nil => ""
  146. case h::t => "\n" + h.toString() + h.msgB + listaBiegajacych (t)
  147. }
  148. }
  149. }
  150.  
  151.  
  152. abstract class Ptak
  153. {
  154. var nr = Ewidencja.nadajNr;
  155. override def toString = "Ptak nr: "+ nr + " - "
  156. }
  157.  
  158. class Pingwin extends Ptak with plywanie with nurkowanie
  159. {
  160. override def toString = super.toString + this.getClass().getName();
  161. }
  162.  
  163. class Golab extends Ptak with plywanie with latanie with bieganie
  164. {
  165. this.stopienL=1;
  166. this.stopienB=2;
  167. override def toString = super.toString + this.getClass().getName();
  168. }
  169.  
  170. class Strus extends Ptak with bieganie
  171. {
  172. this.stopienB=1;
  173. override def toString = super.toString + this.getClass().getName();
  174. }
  175.  
  176. class Kura extends Ptak with latanie with bieganie
  177. {
  178. this.stopienL=2;
  179. this.stopienB=2;
  180. override def toString = super.toString + this.getClass().getName();
  181. }
  182.  
  183. class Sokol extends Ptak with latanie
  184. {
  185. this.stopienL=3;
  186. override def toString = super.toString + this.getClass().getName();
  187. }
  188.  
  189.  
  190. var p1 :Ptak with plywanie with latanie with bieganie = new Golab;
  191. var p2 :Ptak with latanie with bieganie = new Kura;
  192. var p3 :Ptak with latanie = new Sokol;
  193.  
  194.  
  195. println("LATAJACE:");
  196. Ewidencja.listaLatajacych(p1::p2::p3::Nil);
  197.  
  198.  
  199.  
  200. var b1 :Ptak with plywanie with latanie with bieganie = new Golab;
  201. var b2 :Ptak with latanie with bieganie = new Kura;
  202. var b3 :Ptak with bieganie = new Strus;
  203.  
  204.  
  205. println("BIEGAJACE:");
  206. Ewidencja.listaBiegajacych(List(b1,b2,b3));
  207.  
  208.  
  209. var n1: Ptak with nurkowanie = new Pingwin
  210.  
  211.  
  212. println("NURKUJACE");
  213. Ewidencja.listaNurkujacych(n1::Nil)
  214.  
  215.  
  216. var pl1 :Ptak with plywanie with latanie with bieganie = new Golab;
  217. var pl2 :Ptak with plywanie with latanie with bieganie = new Golab;
  218. var pl3 :Ptak with plywanie = new Pingwin;
  219.  
  220. println("PLYWAJACE")
  221. Ewidencja.listaPlywajacych(List(pl1,pl2,pl3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement