Haifisch7734

JAVALAB6

Apr 2nd, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. package pl.kielce.tu.lab6;
  2.  
  3. class Person {
  4. int id;
  5. String firstName, lastName;
  6.  
  7. Person(int id, String firstName, String lastName) {
  8. this.id = id;
  9. this.firstName = firstName;
  10. this.lastName = lastName;
  11. }
  12. }
  13.  
  14. class Student extends Person {
  15. String group;
  16.  
  17. Student(int id, String firstName, String lastName, String group) {
  18. super(id, firstName, lastName);
  19. this.group = group;
  20. }
  21.  
  22. @Override
  23. public String toString(){
  24. String ans = "S";
  25. ans += this.id;
  26. return ans;
  27. }
  28.  
  29. @Override
  30. public boolean equals(Object s){
  31. Student st = (Student)s;
  32. if(this.id == st.id && this.firstName == st.firstName && this.lastName == st.lastName && this.group == st.group)
  33. return true;
  34. else
  35. return false;
  36. }
  37.  
  38. @Override
  39. public int hashCode(){
  40. Integer id = this.id;
  41. String imie = this.firstName;
  42. String nazwisko = this.lastName;
  43. String grupa = this.group;
  44. int hash = id.hashCode() + imie.hashCode() + nazwisko.hashCode() + grupa.hashCode();
  45. return hash;
  46. }
  47. }
  48.  
  49. class Teacher extends Person {
  50. String title;
  51.  
  52. Teacher(int id, String title, String firstName, String lastName) {
  53. super(id, firstName, lastName);
  54. this.title = title;
  55. }
  56.  
  57. @Override
  58. public String toString(){
  59. String ans = "T";
  60. ans += this.id;
  61. return ans;
  62. }
  63.  
  64. @Override
  65. public boolean equals(Object t){
  66. Teacher tr = (Teacher)t;
  67. if(this.id == tr.id && this.firstName == tr.firstName && this.lastName == tr.lastName && this.title == tr.title)
  68. return true;
  69. else
  70. return false;
  71. }
  72.  
  73. @Override
  74. public int hashCode(){
  75. Integer id = this.id;
  76. String imie = this.firstName;
  77. String nazwisko = this.lastName;
  78. String title = this.title;
  79. int hash = id.hashCode() + imie.hashCode() + nazwisko.hashCode() + title.hashCode();
  80. return hash;
  81. }
  82. }
  83.  
  84. public class TestMyCollection {
  85.  
  86. private static void testIntegers() {
  87. MyHashCollection list = new MyHashCollection(4, 4);
  88. list.add(1);
  89. list.add(1);
  90. list.add(9);
  91. list.add(2);
  92. list.add(3);
  93. System.out.println(list);
  94. list.delete(3);
  95. System.out.println(list);
  96. list.add(5);
  97. System.out.println(list);
  98. }
  99.  
  100. private static void testStudentsAndTeachers() {
  101. MyHashCollection list = new MyHashCollection(4, 4);
  102. list.add(new Student(1, "Jan", "Kowalski", "Group_1"));
  103. list.add(new Student(1, "Jan", "Kowalski", "Group_1"));
  104. list.add(new Teacher(9, "prof.", "Adam", "Mickiewicz"));
  105. list.add(new Student(2, "Piotr", "Nowak", "Group_1"));
  106. list.add(new Student(3, "Tomasz", "Kowal", "Group_1"));
  107. System.out.println(list);
  108. list.delete(new Student(3, "Piotr", "Nowak", "Group_1"));
  109. System.out.println(list);
  110. list.add(new Student(5, "Wojciech", "Król", "Group_1"));
  111. System.out.println(list);
  112. }
  113.  
  114. public static void main(String[] args) {
  115. testIntegers();
  116. testStudentsAndTeachers();
  117. }
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. package pl.kielce.tu.lab6;
  127.  
  128. class PojazdGasienicowy extends PojazdKolowy {
  129. int iloscKolNosnych;
  130.  
  131. public PojazdGasienicowy(String naz, int m, int ik, int ikn, int pl) {
  132. super(naz, m, ik, pl);
  133. this.iloscKolNosnych = ikn;
  134. }
  135. //przeciazenie metody z klasy bazowej
  136. @Override
  137. public void skrecaj(char kier) {
  138. if (kier == 'L')
  139. System.out
  140. .println("Lewa gasienica jedzie do tylu, prawa do przodu, skrecam w lewo");
  141. if (kier == 'P')
  142. System.out
  143. .println("Prawa gasienica jedzie do tylu, lewa do przodu, skrecam w prawo");
  144. }
  145.  
  146. }
  147.  
  148. class WozBojowy extends PojazdKolowy {
  149. int kaliberDziala;
  150.  
  151. public WozBojowy(String naz, int m, int ik, int kal, int pl) {
  152. super(naz, m, ik, pl);
  153. this.kaliberDziala = kal;
  154.  
  155. }
  156.  
  157. public void laduj(int x, int y) {
  158. System.out.println("Zaladowalem " + x + " osob oraz " + y
  159. + " sztuk amunicji");
  160. }
  161.  
  162. public void strzelaj() {
  163. System.out.println("Strzelam");
  164. }
  165.  
  166. class Czolg extends PojazdGasienicowy {
  167. public Czolg(String naz, int m, int ik, int ikn, int kal, int pl) {
  168. super(naz, m, ik, ikn, pl);
  169. WozBojowy.this.kaliberDziala = kal;
  170.  
  171. }
  172. }
  173. }
  174.  
  175. public class PojazdKolowy {
  176. int masa;
  177. int iloscKol;
  178. int pojemnoscLudzka;
  179. String nazwa;
  180.  
  181. public PojazdKolowy(String naz, int m, int ik, int pl) {
  182. this.masa = m;
  183. this.iloscKol = ik;
  184. this.pojemnoscLudzka = pl;
  185. this.nazwa = naz;
  186. }
  187.  
  188. public void jedz() {
  189. System.out.println("Jade");
  190. }
  191.  
  192. public void skrecaj(char kier) {
  193. if (kier == 'L')
  194. System.out.println("Kieruje przednie kola w lewo, skrecam w lewo");
  195. if (kier == 'P')
  196. System.out
  197. .println("Kieruje przednie kola w prawo, skrecam w prawo");
  198. }
  199.  
  200. public void laduj(int x) {
  201. System.out.println("Zaladowalem " + x + " osob");
  202. }
  203.  
  204. public static void main(String[] args) {
  205. PojazdKolowy poj = new PojazdKolowy("Nissan", 25000, 4, 5);
  206. PojazdGasienicowy gas = new PojazdGasienicowy("Czolg", 25000, 4, 10, 4);
  207. WozBojowy woz = new WozBojowy("Czolg", 25000, 4, 10, 4);
  208. WozBojowy.Czolg cz = woz.new Czolg("IS3",56000,4,12,122,4);
  209. cz.WozBojowy.strzelaj();
  210.  
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment