Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* package whatever; // don't place package name! */
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- class Student implements Cloneable{
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSurname() {
- return surname;
- }
- public void setSurname(String surname) {
- this.surname = surname;
- }
- public String getPatronymic() {
- return patronymic;
- }
- public void setPatronymic(String patronymic) {
- this.patronymic = patronymic;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- if(age>70||age<18){
- System.out.println("Invalid age for student "+this.name);
- return;
- }
- this.age = age;
- }
- public int getCredits() {
- return credits;
- }
- public void setCredits(int credits) {
- this.credits = credits;
- }
- public Subject getFavSubj(){
- return favSubj;
- }
- public void setFavSubj(Subject val){
- this.favSubj = val;
- }
- private String name = "Unknown";
- private String surname = "Unknown";
- private String patronymic = "Unknowovich";
- private int age = 18;
- private int credits;
- Subject favSubj;
- public Student(){
- }
- public Student(String name, String surname, String patronymic, int age, int credits, Subject favSubj) {
- this.name = name;
- this.surname = surname;
- this.patronymic = patronymic;
- this.setAge(age);
- this.credits = credits;
- this.favSubj = favSubj;
- }
- public String toString(){
- return "Student["+
- "name: "+this.name +
- ", surname: "+this.surname +
- ", patronymic: "+this.patronymic +
- ", age: "+this.age +
- ", credits: "+this.credits +
- ", favSubj: "+this.favSubj
- +"]";
- }
- public int hashCode(){
- int result = 0;
- int c = 0;
- if(name==null)c = 0; else
- c = name.hashCode();
- result = 37*result + c;
- if(surname==null)c = 0; else
- c = surname.hashCode();
- result = 37*result + c;
- if(patronymic==null)c = 0; else
- c = patronymic.hashCode();
- result = 37*result + c;
- c = (int)age;
- result = 37*result + c;
- c = (int)credits;
- result = 37*result + c;
- if(favSubj==null)c = 0; else
- c = favSubj.hashCode();
- result = 37*result + c;
- return result;
- }
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o instanceof Student) {
- if(hashCode() == o.hashCode()){
- Student s = (Student)o;
- return
- (this.name==s.name)&&
- (this.surname==s.surname)&&
- (this.patronymic==s.patronymic)&&
- (this.age==s.age)&&
- (this.credits==s.credits)&&
- (this.favSubj==s.favSubj);
- }else return false;
- } else {
- return false;
- }
- }
- public Student clone() throws CloneNotSupportedException{
- Student s = (Student)super.clone();
- s.favSubj = favSubj.clone();
- return s;
- }
- public int compareTo(Student s){
- if(this.getCredits()==s.getCredits())
- return 0;
- else if(this.getCredits()>s.getCredits())
- return 1;
- else
- return -1;
- }
- }
- class Subject implements Cloneable{
- String name;
- Boolean isNormative = false;
- int hours;
- Boolean isExam = false;
- OKR sOKR;
- public Subject clone() throws CloneNotSupportedException{
- return (Subject)super.clone();
- }
- public int compareTo(Subject s){
- if(this.getHours()==s.getHours())
- return 0;
- else if(this.getHours()>s.getHours())
- return 1;
- else
- return -1;
- }
- public String toString(){
- return "Subject["+
- "name: "+this.name +
- ", isNormative: "+this.isNormative +
- ", hours: "+this.hours +
- ", isExam: "+this.isExam +
- ", sOKR: "+this.sOKR+
- "]";
- }
- public int hashCode(){
- int result = 0;
- int c = 0;
- if(name==null)c = 0; else
- c = name.hashCode();
- result = 37*result + c;
- c = (isNormative? 0 : 1);
- result = 37*result + c;
- c = (int)hours;
- result = 37*result + c;
- c = (isExam? 0 : 1);
- result = 37*result + c;
- if(sOKR==null)c = 0; else
- c = sOKR.hashCode();
- result = 37*result + c;
- return result;
- }
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o instanceof Subject) {
- if(hashCode() == o.hashCode()){
- Subject s = (Subject)o;
- return
- (this.name==s.name)&&
- (this.isNormative==s.isNormative)&&
- (this.hours==s.hours)&&
- (this.isExam==s.isExam)&&
- (this.sOKR==s.sOKR);
- }else return false;
- } else {
- return false;
- }
- }
- public String getName(){
- return name;
- }
- public void setName(String val){
- this.name = val;
- }
- public Boolean getIsNormative(){
- return isNormative;
- }
- public void setIsNormative(Boolean val){
- this.isNormative = val;
- }
- public int getHours(){
- return hours;
- }
- public void setHours(int val){
- this.hours = val;
- }
- public Boolean getIsExam(){
- return isExam;
- }
- public void setIsExam(Boolean val){
- this.isExam = val;
- }
- public OKR getSOKR(){
- return sOKR;
- }
- public void setSOKR(OKR val){
- this.sOKR = val;
- }
- public Subject(){
- }
- public Subject(String n, Boolean isNorm, int h, Boolean isExam, OKR sOKR){
- this.name = n;
- this.isNormative = isNorm;
- this.hours = h;
- this.isExam = isExam;
- this.sOKR = sOKR;
- }
- public enum OKR{
- master,
- bachelor,
- specialist
- }
- }
- class Main
- {
- public static void main (String[] args) throws java.lang.Exception
- {
- /*
- У даному пакеті створити клас Main з головним методом проекту.
- У даному методі створити 2 студенти (student1, student2), 2 предмети (subject1,
- subject2). Продемонструвати роботу з усіма виществореними методами,
- зокрема:
- 1. У створених об’єктів викликати метод toString();
- 2. Реалізувати перевірку еквівалентності об’єктів у наступному
- форматі:
- if (subject1.equals(subject2)) {
- System.out.println("subjectsareequals!");
- }
- elseSystem.out.println("subjectsaren'tequals!");
- аналогічну перевірку зробити для об’єктів класу Student.
- 3. Засобами клонування створити об’єкти student3 та subject3, аналогічні до student1 та subject1 відповідно. Потім у об’єктах student3 та subject3 внести зміни до полів ім’я / назва.
- 4. Продемонструвати порівняння об’єктів методом compareTo() у наступному форматі
- if (subject1.compareTo(subject2) > 0) {
- System.out.println(subject1.toString() + " isbetter!");
- }
- elseif (subject1.compareTo(subject2) < 0) {
- System.out.println(subject2.toString() + " isbetter!");
- }
- elseSystem.out.println("Theyaresimilar!");
- аналогічно продемонструвати роботу з методом compareTo() для об’єктів класу Student.
- 5. Отримати список всіх елементів enum-класу під час виконання. Для цих цілей в кожному enum-класі компілятор створює метод:
- publicstaticEnumClass [] values ().
- Приклад використання:
- System.out.println(Arrays.toString(zminnaEnumClass.values()));
- */
- Subject subject1 = new Subject("Math",true,30,true,Subject.OKR.bachelor);
- Subject subject2 = new Subject("Physics",true,20,false,Subject.OKR.bachelor);
- Student student1 = new Student("Taras","Tarasov","Tarasovich",22,80,subject1);
- Student student2 = new Student("Roman","Romanov","Romanovich",21,50,subject2);
- System.out.println(subject1.toString());
- System.out.println(subject2.toString());
- System.out.println(student1.toString());
- System.out.println(student2.toString());
- if (subject1.equals(subject2)) {
- System.out.println("subjects are equal!");
- }
- else
- System.out.println("subjects aren't equal!");
- if (student1.equals(student2)) {
- System.out.println("students are equal!");
- }
- else
- System.out.println("students aren't equal!");
- Student student3 = student1.clone();
- Subject subject3 = subject1.clone();
- subject3.setName("Arts");
- student3.setAge(30);
- System.out.println(student3);
- System.out.println(subject3);
- if (subject1.compareTo(subject2) > 0) {
- System.out.println(subject1.toString() + " is better!");
- }
- else if (subject1.compareTo(subject2) < 0) {
- System.out.println(subject2.toString() + " is better!");
- }
- if (student1.compareTo(student2) > 0) {
- System.out.println(student1.toString() + " is better!");
- }
- else if (student1.compareTo(student2) < 0) {
- System.out.println(student2.toString() + " is better!");
- }
- System.out.println(Arrays.toString(Subject.OKR.values()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement