Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.34 KB | None | 0 0
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class Student implements Cloneable{
  8. public String getName() {
  9. return name;
  10. }
  11.  
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15.  
  16. public String getSurname() {
  17. return surname;
  18. }
  19.  
  20. public void setSurname(String surname) {
  21. this.surname = surname;
  22. }
  23.  
  24. public String getPatronymic() {
  25. return patronymic;
  26. }
  27.  
  28. public void setPatronymic(String patronymic) {
  29. this.patronymic = patronymic;
  30. }
  31.  
  32. public int getAge() {
  33. return age;
  34. }
  35.  
  36. public void setAge(int age) {
  37. if(age>70||age<18){
  38. System.out.println("Invalid age for student "+this.name);
  39. return;
  40. }
  41. this.age = age;
  42. }
  43.  
  44. public int getCredits() {
  45. return credits;
  46. }
  47.  
  48. public void setCredits(int credits) {
  49. this.credits = credits;
  50. }
  51.  
  52. public Subject getFavSubj(){
  53. return favSubj;
  54. }
  55. public void setFavSubj(Subject val){
  56. this.favSubj = val;
  57. }
  58.  
  59. private String name = "Unknown";
  60. private String surname = "Unknown";
  61. private String patronymic = "Unknowovich";
  62. private int age = 18;
  63. private int credits;
  64. Subject favSubj;
  65.  
  66. public Student(){
  67.  
  68. }
  69.  
  70. public Student(String name, String surname, String patronymic, int age, int credits, Subject favSubj) {
  71. this.name = name;
  72. this.surname = surname;
  73. this.patronymic = patronymic;
  74. this.setAge(age);
  75. this.credits = credits;
  76. this.favSubj = favSubj;
  77. }
  78.  
  79. public String toString(){
  80. return "Student["+
  81. "name: "+this.name +
  82. ", surname: "+this.surname +
  83. ", patronymic: "+this.patronymic +
  84. ", age: "+this.age +
  85. ", credits: "+this.credits +
  86. ", favSubj: "+this.favSubj
  87. +"]";
  88. }
  89.  
  90. public int hashCode(){
  91. int result = 0;
  92. int c = 0;
  93.  
  94. if(name==null)c = 0; else
  95. c = name.hashCode();
  96. result = 37*result + c;
  97.  
  98. if(surname==null)c = 0; else
  99. c = surname.hashCode();
  100. result = 37*result + c;
  101.  
  102. if(patronymic==null)c = 0; else
  103. c = patronymic.hashCode();
  104. result = 37*result + c;
  105.  
  106. c = (int)age;
  107. result = 37*result + c;
  108.  
  109. c = (int)credits;
  110. result = 37*result + c;
  111.  
  112. if(favSubj==null)c = 0; else
  113. c = favSubj.hashCode();
  114. result = 37*result + c;
  115.  
  116.  
  117. return result;
  118. }
  119.  
  120. public boolean equals(Object o) {
  121. if (this == o) return true;
  122. if (o instanceof Student) {
  123. if(hashCode() == o.hashCode()){
  124. Student s = (Student)o;
  125. return
  126. (this.name==s.name)&&
  127. (this.surname==s.surname)&&
  128. (this.patronymic==s.patronymic)&&
  129. (this.age==s.age)&&
  130. (this.credits==s.credits)&&
  131. (this.favSubj==s.favSubj);
  132. }else return false;
  133. } else {
  134. return false;
  135. }
  136. }
  137.  
  138. public Student clone() throws CloneNotSupportedException{
  139. Student s = (Student)super.clone();
  140. s.favSubj = favSubj.clone();
  141.  
  142. return s;
  143. }
  144.  
  145. public int compareTo(Student s){
  146. if(this.getCredits()==s.getCredits())
  147. return 0;
  148. else if(this.getCredits()>s.getCredits())
  149. return 1;
  150. else
  151. return -1;
  152. }
  153. }
  154.  
  155. class Subject implements Cloneable{
  156. String name;
  157. Boolean isNormative = false;
  158. int hours;
  159. Boolean isExam = false;
  160. OKR sOKR;
  161.  
  162. public Subject clone() throws CloneNotSupportedException{
  163.  
  164. return (Subject)super.clone();
  165. }
  166.  
  167. public int compareTo(Subject s){
  168. if(this.getHours()==s.getHours())
  169. return 0;
  170. else if(this.getHours()>s.getHours())
  171. return 1;
  172. else
  173. return -1;
  174. }
  175.  
  176. public String toString(){
  177. return "Subject["+
  178. "name: "+this.name +
  179. ", isNormative: "+this.isNormative +
  180. ", hours: "+this.hours +
  181. ", isExam: "+this.isExam +
  182. ", sOKR: "+this.sOKR+
  183. "]";
  184. }
  185.  
  186. public int hashCode(){
  187. int result = 0;
  188. int c = 0;
  189.  
  190. if(name==null)c = 0; else
  191. c = name.hashCode();
  192. result = 37*result + c;
  193.  
  194. c = (isNormative? 0 : 1);
  195. result = 37*result + c;
  196.  
  197. c = (int)hours;
  198. result = 37*result + c;
  199.  
  200. c = (isExam? 0 : 1);
  201. result = 37*result + c;
  202.  
  203. if(sOKR==null)c = 0; else
  204. c = sOKR.hashCode();
  205. result = 37*result + c;
  206.  
  207. return result;
  208. }
  209.  
  210. public boolean equals(Object o) {
  211. if (this == o) return true;
  212. if (o instanceof Subject) {
  213. if(hashCode() == o.hashCode()){
  214. Subject s = (Subject)o;
  215. return
  216. (this.name==s.name)&&
  217. (this.isNormative==s.isNormative)&&
  218. (this.hours==s.hours)&&
  219. (this.isExam==s.isExam)&&
  220. (this.sOKR==s.sOKR);
  221. }else return false;
  222. } else {
  223. return false;
  224. }
  225. }
  226.  
  227. public String getName(){
  228. return name;
  229. }
  230. public void setName(String val){
  231. this.name = val;
  232. }
  233. public Boolean getIsNormative(){
  234. return isNormative;
  235. }
  236. public void setIsNormative(Boolean val){
  237. this.isNormative = val;
  238. }
  239. public int getHours(){
  240. return hours;
  241. }
  242. public void setHours(int val){
  243. this.hours = val;
  244. }
  245. public Boolean getIsExam(){
  246. return isExam;
  247. }
  248. public void setIsExam(Boolean val){
  249. this.isExam = val;
  250. }
  251. public OKR getSOKR(){
  252. return sOKR;
  253. }
  254. public void setSOKR(OKR val){
  255. this.sOKR = val;
  256. }
  257.  
  258. public Subject(){
  259.  
  260. }
  261.  
  262. public Subject(String n, Boolean isNorm, int h, Boolean isExam, OKR sOKR){
  263. this.name = n;
  264. this.isNormative = isNorm;
  265. this.hours = h;
  266. this.isExam = isExam;
  267. this.sOKR = sOKR;
  268. }
  269.  
  270. public enum OKR{
  271. master,
  272. bachelor,
  273. specialist
  274. }
  275. }
  276.  
  277. class Main
  278. {
  279. public static void main (String[] args) throws java.lang.Exception
  280. {
  281. /*
  282. У даному пакеті створити клас Main з головним методом проекту.
  283. У даному методі створити 2 студенти (student1, student2), 2 предмети (subject1,
  284. subject2). Продемонструвати роботу з усіма виществореними методами,
  285. зокрема:
  286. 1. У створених об’єктів викликати метод toString();
  287. 2. Реалізувати перевірку еквівалентності об’єктів у наступному
  288. форматі:
  289. if (subject1.equals(subject2)) {
  290. System.out.println("subjectsareequals!");
  291. }
  292. elseSystem.out.println("subjectsaren'tequals!");
  293. аналогічну перевірку зробити для об’єктів класу Student.
  294. 3. Засобами клонування створити об’єкти student3 та subject3, аналогічні до student1 та subject1 відповідно. Потім у об’єктах student3 та subject3 внести зміни до полів ім’я / назва.
  295. 4. Продемонструвати порівняння об’єктів методом compareTo() у наступному форматі
  296. if (subject1.compareTo(subject2) > 0) {
  297. System.out.println(subject1.toString() + " isbetter!");
  298. }
  299. elseif (subject1.compareTo(subject2) < 0) {
  300. System.out.println(subject2.toString() + " isbetter!");
  301. }
  302. elseSystem.out.println("Theyaresimilar!");
  303. аналогічно продемонструвати роботу з методом compareTo() для об’єктів класу Student.
  304. 5. Отримати список всіх елементів enum-класу під час виконання. Для цих цілей в кожному enum-класі компілятор створює метод:
  305. publicstaticEnumClass [] values ().
  306. Приклад використання:
  307. System.out.println(Arrays.toString(zminnaEnumClass.values()));
  308.  
  309. */
  310.  
  311. Subject subject1 = new Subject("Math",true,30,true,Subject.OKR.bachelor);
  312. Subject subject2 = new Subject("Physics",true,20,false,Subject.OKR.bachelor);
  313.  
  314. Student student1 = new Student("Taras","Tarasov","Tarasovich",22,80,subject1);
  315. Student student2 = new Student("Roman","Romanov","Romanovich",21,50,subject2);
  316.  
  317. System.out.println(subject1.toString());
  318. System.out.println(subject2.toString());
  319. System.out.println(student1.toString());
  320. System.out.println(student2.toString());
  321.  
  322. if (subject1.equals(subject2)) {
  323. System.out.println("subjects are equal!");
  324. }
  325. else
  326. System.out.println("subjects aren't equal!");
  327.  
  328. if (student1.equals(student2)) {
  329. System.out.println("students are equal!");
  330. }
  331. else
  332. System.out.println("students aren't equal!");
  333.  
  334. Student student3 = student1.clone();
  335. Subject subject3 = subject1.clone();
  336.  
  337. subject3.setName("Arts");
  338. student3.setAge(30);
  339.  
  340. System.out.println(student3);
  341. System.out.println(subject3);
  342.  
  343. if (subject1.compareTo(subject2) > 0) {
  344. System.out.println(subject1.toString() + " is better!");
  345. }
  346. else if (subject1.compareTo(subject2) < 0) {
  347. System.out.println(subject2.toString() + " is better!");
  348. }
  349.  
  350. if (student1.compareTo(student2) > 0) {
  351. System.out.println(student1.toString() + " is better!");
  352. }
  353. else if (student1.compareTo(student2) < 0) {
  354. System.out.println(student2.toString() + " is better!");
  355. }
  356.  
  357. System.out.println(Arrays.toString(Subject.OKR.values()));
  358. }
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement