• Июля 03, 2020, 01:03:45 pm
• Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Не получили письмо с кодом активации?
Новости: Вебинар по выбору IT профессии: http://bit.ly/2S4kcC6

Автор Тема: Java OOP Alexander 15_06_2020 online (Пн-Ср 19.00-21.00)  (Прочитано 153 раз)

MaryKlymenko

  • Пользователи
  • *
  • Сообщений: 9
    • Просмотр профиля
Re: Java OOP Alexander 15_06_2020 online (Пн-Ср 19.00-21.00)
« Ответ #15 : Июня 24, 2020, 12:21:35 pm »
HomeTask#3 :)
1) Создайте класс, описывающий человека (создайте метод,
выводящий информацию о человеке).
2) На его основе создайте класс студент (переопределите
метод вывода информации).
3) Создайте класс Группа, который содержит массив из 10
объектов класса Студент. Реализуйте методы добавления,
удаления студента и метод поиска студента по фамилии. В
случае попытки добавления 11 студента, создайте
собственное исключение и обработайте его. Определите
метод toString() для группы так, что бы он выводил список
студентов в алфавитном порядке.

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Random random = new Random();
long numberOfStudentRecordBook = random.nextInt(1000000000);
Group group = new Group();

try {
group.addNewStudentToGroup(new Student("Humphrey", "Bogart", "m", 19, numberOfStudentRecordBook));
group.addNewStudentToGroup(new Student("Marlon", "Brando", "m", 18, numberOfStudentRecordBook));
group.addNewStudentToGroup(new Student("John", "Wayne", "m", 17, numberOfStudentRecordBook));
group.addNewStudentToGroup(new Student("Grace", "Kelly", "f", 20, numberOfStudentRecordBook));
group.addNewStudentToGroup(new Student("Laurence", "Olivier", "m", 21, numberOfStudentRecordBook));
group.addNewStudentToGroup(new Student("Barbara", "Stanwyck", "f", 18, numberOfStudentRecordBook));
group.addNewStudentToGroup(new Student("Gary", "Cooper", "m", 19, numberOfStudentRecordBook));
group.addNewStudentToGroup(new Student("Charlie", "Chaplin", "m", 20, numberOfStudentRecordBook));
group.addNewStudentToGroup(new Student("Marlene", "Dietrich", "f", 21, numberOfStudentRecordBook));
group.addNewStudentToGroup(new Student("Clark", "Gable", "m", 18, numberOfStudentRecordBook));
group.addNewStudentToGroup(new Student("Ingrid", "Bergman", "f", 17, numberOfStudentRecordBook));
} catch (ArrayExeption e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Student student = group.search("Charlie");
System.out.println(String.format("Student %s was found!", student.getName() + student.getSurname()));
group.printSortBySurnameGroupInfo();

}
}

public class Human implements Comparable {
protected String name;
protected String surname;
protected String sex;
protected int age;

public Human() {
super();
}

public Human(String name, String surname, String sex, int age) {
super();
this.name = name;
this.surname = surname;
this.sex = sex;
this.age = age;
}

@Override
public int compareTo(Object obj) {
Human tmp = (Human) obj;
return this.surname.compareTo(tmp.surname);
}

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 getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "Human [name=" + name + ", surname=" + surname + ", sex=" + sex + ", age=" + age + "]";
}

}


public class Student extends Human {

private long numberOfStudentRecordBook;

public Student() {
super();
}

public Student(String name, String surname, String sex, int age, long numberOfStudentRecordBook) {
super(name, surname, sex, age);
this.numberOfStudentRecordBook = numberOfStudentRecordBook;
}

@Override
public String toString() {
return "Student [numberOfStudentRecordBook=" + numberOfStudentRecordBook + ", name=" + name + ", surname="
+ surname + ", sex=" + sex + ", age=" + age + "]";
}

public long getNumberOfStudentRecordBook() {
return numberOfStudentRecordBook;
}

public void setNumberOfStudentRecordBook(long numberOfStudentRecordBook) {
this.numberOfStudentRecordBook = numberOfStudentRecordBook;
}

}


public class Group {

@Override
public String toString() {
return "Group [students=" + Arrays.toString(students);
}

public Group() {
super();
}

private Student[] students = new Student[10];

public void addNewStudentToGroup(Student student) throws ArrayExeption {
if (student == null) {
throw new IllegalArgumentException("Null student");
}
for (int i = 0; i < students.length; i++) {
if (students[i] == null) {
students[i] = student;
return;
}
}
throw new ArrayExeption();
}

public Student search(String studentName) {
for (Student student : students) {
if (student != null && student.getName().equals(studentName)) {
return student;
}
}
return null;
}

public void printSortBySurnameGroupInfo() {
Arrays.sort(students);
for (Student std : students) {
try {
System.out.println(std.getSurname() + " " + std.getName());
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
}

public class ArrayExeption extends Exception {

/**
*
*/
private static final long serialVersionUID = -7341685345596930008L;

@Override
public String toString() {
return "You can't add 11s student to group.";
}
}

Alexander Ts

  • Преподаватели
  • Пользователи
  • *
  • Сообщений: 13854
    • Просмотр профиля
    • E-mail
Re: Java OOP Alexander 15_06_2020 online (Пн-Ср 19.00-21.00)
« Ответ #16 : Июня 25, 2020, 12:34:19 am »
Sllawwkoo
Решение верное

MaryKlymenko
В классе Группа перепутан порядок описания членов класса. Сначала идут поля, потом конструкторы, потом методы. У вас же совершенно не так.

Метод сортировки Студентов не устойчивый, попробуйте например добавить только 5 Студентов в группу, и отсортировать. Ваша программа завершиться по NullPointerException

В остальном решение верное

sllawwkoo

  • Пользователи
  • *
  • Сообщений: 88
    • Просмотр профиля
Re: Java OOP Alexander 15_06_2020 online (Пн-Ср 19.00-21.00)
« Ответ #17 : Июня 28, 2020, 12:43:03 am »
Урок 3 Задание 1-3

Human
package com.gmail.sllawwkoo;

public class Human {

private String name;
private String surname;
private String sex;
private int age;

public Human(String name, String surname, String sex, int age) {
super();
this.name = name;
this.surname = surname;
this.sex = sex;
this.age = age;
}

public Human() {
super();

}

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 getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "Human [name=" + name + ", surname=" + surname + ", sex=" + sex + ", age=" + age + "]";
}



}


Student
package com.gmail.sllawwkoo;

public class Student extends Human {

private String faculty;
private String specialty;

public Student(String name, String surname, String sex, int age, String faculty, String specialty) {
super(name, surname, sex, age);
this.faculty = faculty;
this.specialty = specialty;
}

public Student() {
super();

}

public Student(String name, String surname, String sex, int age) {
super(name, surname, sex, age);

}

public String getFaculty() {
return faculty;
}

public void setFaculty(String faculty) {
this.faculty = faculty;
}

public String getSpecialty() {
return specialty;
}

public void setSpecialty(String specialty) {
this.specialty = specialty;
}

@Override
public String toString() {
return "Student [faculty=" + faculty + ", specialty=" + specialty + "," + super.toString();
}

}


Group
package com.gmail.sllawwkoo;

import java.util.Arrays;

public class Group {

private Student[] studentArray = new Student[10];

public Group(Student[] studentArray) {
super();
this.studentArray = studentArray;
}

public Group() {
super();

}

public Student[] getStudentArray() {
return studentArray;
}

public void setStudentArray(Student[] studentArray) {
this.studentArray = studentArray;
}

public void addStudent(Student student) throws MyException {
if (student == null) {
throw new IllegalArgumentException("Null student");
}
for (int i = 0; i < studentArray.length; i++) {

if (studentArray[i] == null) {
studentArray[i] = student;
return;
}
}
throw new MyException();
}

public void deleteStudent(String surname) {
for (int i = 0; i < studentArray.length; i++) {
if (studentArray[i] != null && studentArray[i].getSurname().equals(surname)) {
studentArray[i] = null;
break;
}
}

}

public Student searchStudent(String surname) {
for (Student one : studentArray) {
if (one != null && one.getSurname().equals(surname)) {
return one;
}
}
System.out.println("We do not have such a student!");
return null;

}

@Override
public String toString() {
return "Group [studentArray=" + Arrays.toString(studentArray) + "]";
}

}


MyException
package com.gmail.sllawwkoo;

public class MyException extends Exception {

@Override
public String getMessage() {

return "Group formed, student not accepted!!";

}

}


Main
package com.gmail.sllawwkoo;

public class Main {

public static void main(String[] args) {

Student student1 = new Student("Wayne", "Rooney", "male", 17, "Economic", "Audit");
Student student2 = new Student("Rio", "Ferdinand", "male", 18, "Economic", "Audit");
Student student3 = new Student("Carlos", "Tevez", "male", 19, "Economic", "Audit");
Student student4 = new Student("Michelle", "Akers", "female", 18, "Economic", "Audit");
Student student5 = new Student("Alexandra", "Morgan", "female", 19, "Economic", "Audit");
Student student6 = new Student("Darren", "Fletcher", "male", 19, "Economic", "Audit");
Student student7 = new Student("Kristine", "Lilly", "female", 19, "Economic", "Audit");
Student student8 = new Student("Gerard", "Pique", "male", 18, "Economic", "Audit");
Student student9 = new Student("Brandi", "Smith", "female", 18, "Economic", "Audit");
Student student10 = new Student("Paul", "Scholes", "male", 19, "Economic", "Audit");
Student student11 = new Student("Michael", "Carrick", "male", 19, "Economic", "Audit");

Group groupA = new Group();

try {
groupA.addStudent(student1);
groupA.addStudent(student2);
groupA.addStudent(student3);
groupA.addStudent(student4);
groupA.addStudent(student5);
groupA.addStudent(student6);
groupA.addStudent(student7);
groupA.addStudent(student8);
groupA.addStudent(student9);
groupA.addStudent(student10);

} catch (MyException e) {
e.printStackTrace();
}
System.out.println(groupA);

try {
groupA.addStudent(student11);
} catch (MyException e) {
System.out.println(e.getMessage());
}
System.out.println();

System.out.println(groupA);
System.out.println();

groupA.deleteStudent("Pique");
System.out.println(groupA);
System.out.println(groupA.searchStudent("Rooney"));

}

}


Alexander Ts

  • Преподаватели
  • Пользователи
  • *
  • Сообщений: 13854
    • Просмотр профиля
    • E-mail
Re: Java OOP Alexander 15_06_2020 online (Пн-Ср 19.00-21.00)
« Ответ #18 : Июня 28, 2020, 10:55:48 pm »
Sllawwkoo

Решение верное

Ksenia

  • Пользователи
  • *
  • Сообщений: 12
    • Просмотр профиля
Re: Java OOP Alexander 15_06_2020 online (Пн-Ср 19.00-21.00)
« Ответ #19 : Июня 29, 2020, 04:38:28 pm »
Лекция 1
Описать класс "Кот" ....

Задание 1
package net.bigmir.gfd;

public class Cat {
String color;
double weight;
int age;
public Cat (String color, double weight, int age ) {
this.color = color;
this.weight = weight;
this.age = age;
}
void  voice1 () {
System.out.println("May - May");
}
void  voice2 () {
System.out.println("Nay - Nay");
}
void  voice3 () {
System.out.println("Doesn`t like speak");

}
public Cat () {
super();
}
public  String getColor () {
return color;
}
public void setColor (String color) {
this.color = color;
}
public  double getWeight () {
return weight;
}
public void setWeight (double weight) {
this.weight = weight;
}
public  int getAge () {
return age;
}
public void setAge (int age) {
this.age = age;
}
public  void getVoice1 () {
System.out.println("May - May");
}
public void getVoice2 () {
System.out.println("Nay - Nay");
}
public void getVoice3 () {
System.out.println("Cat doesn`t like speak");
}
public String toString () {
return  "Cat[color=" + color + ",  weight =" + weight +", age = " + age + "]";
}
}

package net.bigmir.gfd;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Cat  filya = new Cat ("White", 5, 15);
Cat tosha = new Cat ("Grey", 7, 2  );
Cat topa = new Cat ("Black", 6, 8);

filya.getVoice1 ();
System.out.println (filya);

tosha.getVoice2 ();
System.out.println (tosha);

topa.getVoice3 ();
System.out.println (topa);
}
}

Задание 2.

Описать класс "Треугольник" со свойствами...

package net.bigmir.fgh;

public class Triangle {
private double a;
private double b;
private double c;


public Triangle(double a, double b, double c) {
super();
this.a = a;
this.b = b;
this.c = c;
}
public Triangle () {
super();
}


public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
private double area() {
double p =(a+b+c)/2;
double s = Math.sqrt (p*(p-a)*(p-b)*(p-c));
return s;

}



public String toString () {
return " [a =" + a + ", b = "+b+", c= "+ c +",  = " + area()+"]";
}
}


package net.bigmir.fgh;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Triangle trone = new Triangle (3, 5, 4);
Triangle trtwo = new Triangle (9, 16, 16);
Triangle trthree = new Triangle (3.5, 4, 7);


System.out.println(" Triangleone    " + trone);
System.out.println(" Triangletwo    " + trtwo);
System.out.println(" Trianglethree  " + trthree);

}
}

Ksenia

  • Пользователи
  • *
  • Сообщений: 12
    • Просмотр профиля
Re: Java OOP Alexander 15_06_2020 online (Пн-Ср 19.00-21.00)
« Ответ #20 : Июня 29, 2020, 04:44:52 pm »
Лекция 2.
Создать классы "Форма, точка, фигуры"...
Задание (1,2,3)

package net.bigmir.hgf;

public abstract class Shape {
abstract double getPerimetr ();
abstract double getArea ();

public Shape() {
super();

}


}

package net.bigmir.hgf;

public class Point {
private double x;
private double y;

public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}

public double getX() {
return x;
}

public void setX(double x) {
this.x = x;
}

public double getY() {
return y;
}

public void setY(double y) {
this.y = y;
}

//public double getDist(Point a)
//{
//double xa =this.x - a.getX();
//double ya =this.y - a.getY();
//return Math.sqrt(xa*xa+ ya*ya);
//}
public static double getDistance ( Point a,  Point b) {
return Math.sqrt((a.x -b.x)* (a.x -b.x) +(a.y -b.y)* (a.y -b.y));
}

@Override
public String toString() {

return " [x =" + x + ", y = "+ y + "]";
}


}


package net.bigmir.hgf;

public class Triangular extends Shape {

private Point   a;
private Point   b;
private Point   c;


public Triangular(Point a, Point b, Point c) {
super();
this.a = a;
this.b = b;
this.c = c;
}


public Triangular() {
super();

}


public Point getA() {
return a;
}


public void setA(Point a) {
this.a = a;
}


public Point getB() {
return b;
}


public void setB(Point b) {
this.b = b;
}


public Point getC() {
return c;
}


public void setC(Point c) {
this.c = c;
}


@Override
double getPerimetr () {
double p = Point.getDistance(a, b)+ Point.getDistance(a, c) + Point.getDistance(b, c);
return p;
}
@Override
double getArea () {
double p = getPerimetr()/2;
return Math.sqrt(p* (p-Point.getDistance(a, b))*(p-Point.getDistance(a, c))*(p-Point.getDistance(b, c)));
}

public String toString () {
return " [a =" + a + ", b = "+b+", c= "+ c +", S = " + getArea()+" P = " + getPerimetr () +"]";

}

}



package net.bigmir.hgf;

public class Circle extends Shape{
private Point a;
private Point b;
public Circle(Point a, Point b) {
super();
this.a = a;
this.b = b;
}
public Circle() {
super();

}
public Point getA() {
return a;
}
public void setA(Point a) {
this.a = a;
}
public Point getB() {
return b;
}
public void setB(Point b) {
this.b = b;
}
@Override
double getPerimetr() {
double p = 2*Math.PI*Point.getDistance(a, b);
return p;
}
@Override
double getArea() {
double s = Math.PI * Math.pow(Point.getDistance(a, b), 2);
return s;
}
@Override
public String toString() {

return " [a =" + a + ", b = " + b + ", s = " + getArea() + " P = " + getPerimetr () +"]";
}


}


package net.bigmir.hgf;


public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Point a = new Point (1, 2);
Point b = new Point (2, 4);
Point c = new Point (2, 3);

Triangular triangle1 = new Triangular (a,b,c);
Circle circle1 = new Circle (a,b);

System.out.println(triangle1);

System.out.println(circle1);

}

}

fr1kky

  • Пользователи
  • *
  • Сообщений: 11
    • Просмотр профиля
    • E-mail
Re: Java OOP Alexander 15_06_2020 online (Пн-Ср 19.00-21.00)
« Ответ #21 : Июня 29, 2020, 07:18:41 pm »
Дз 3-4
Human
package com.gmail.tso;

public class Human {
private String name;
private String surname;
private int age;
private String sex;

public Human() {super();}

public Human(String name, String surname, int age, String sex) {
super();
this.name = name;
this.surname = surname;
this.age = age;
this.sex = sex;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}

@Override
public String toString() {
return "Human [name=" + name + ", surname=" + surname + ", age=" + age + ", sex=" + sex + "]";
}

}

Student
package com.gmail.tso;

public class Student extends Human {
private int course;
private String group;
private String faculty;

public Student() {}

public Student(String name, String surname, int age, String sex, int course, String group, String faculty) {
super(name ,surname , age, sex);
this.course = course;
this.group = group;
this.faculty = faculty;
}

public int getCourse() {
return course;
}

public void setCourse(int course) {
this.course = course;
}

public String getGroup() {
return group;
}

public void setGroup(String group) {
this.group = group;
}

public String getFaculty() {
return faculty;
}

public void setFaculty(String faculty) {
this.faculty = faculty;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString() + "Student [course=" + course + ", group=" + group + ", faculty=" + faculty + "]";
}
}

OverfillException
package com.gmail.tso;

public class Student extends Human {
private int course;
private String group;
private String faculty;

public Student() {}

public Student(String name, String surname, int age, String sex, int course, String group, String faculty) {
super(name ,surname , age, sex);
this.course = course;
this.group = group;
this.faculty = faculty;
}

public int getCourse() {
return course;
}

public void setCourse(int course) {
this.course = course;
}

public String getGroup() {
return group;
}

public void setGroup(String group) {
this.group = group;
}

public String getFaculty() {
return faculty;
}

public void setFaculty(String faculty) {
this.faculty = faculty;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString() + "Student [course=" + course + ", group=" + group + ", faculty=" + faculty + "]";
}
}

Group
package com.gmail.tso;

import javax.swing.JOptionPane;
import java.util.Scanner;

public class Group implements Voencom {
private Student[] gr = new Student[10];

public Group() {
};

public Student[] getGr() {
return gr;
}

public void add(Student st) {
for (int i = 0; i < gr.length; i++) {
try {
if (gr[i] == null) {
gr[i] = st;
break;
}
if (i == 9) {
throw new OverfillExeption();
}
if (gr[i] != null) {
continue;
}
break;
} catch (OverfillExeption e) {
JOptionPane.showMessageDialog(null, "Cant add more Students");
}
}
}

public void addNew() {
Student st = new Student();
for (int i = 0; i < gr.length; i++) {
try {
if (gr[i] == null) {
break;
}
if (i == 9) {
throw new OverfillExeption();
}
} catch (OverfillExeption e) {
JOptionPane.showMessageDialog(null, "Cant add more Students");
}
}
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
st.setName(name);
System.out.println();

System.out.print("Enter surname: ");
String surname = sc.nextLine();
st.setSurname(surname);
System.out.println();

System.out.print("Enter faculty: ");
String faculty = sc.nextLine();
st.setFaculty(faculty);

System.out.print("Enter sex: ");
String sex = sc.nextLine();
st.setSex(sex);
System.out.println();

System.out.print("Enter group: ");
String group = sc.nextLine();
st.setGroup(group);
System.out.println();

System.out.print("Enter age: ");
int age = sc.nextInt();
st.setAge(age);
System.out.println();

System.out.print("Enter course: ");
int course = sc.nextInt();
st.setCourse(course);
System.out.println();

add(st);
}

public Student dell(String surname) {
for (int i = 0; i < gr.length; i++) {
if (gr[i] == null) {
continue;
}
if (gr[i].getSurname() == surname) {
Student st = new Student();
st = gr[i];
gr[i] = null;
return st;
}
}
return null;
}

public Student serch(String surname) {
for (int i = 0; i < gr.length; i++) {
if (gr[i] == null) {
continue;
}
if (gr[i].getSurname() == surname) {
return gr[i];
}
}
return null;
}

@Override
public Student[] pora() {
// TODO Auto-generated method stub
int n = 0;
for (int i = 0; i < gr.length; i++) {
if (gr[i] == null) {
continue;
}
if(gr[i].getAge() >= 18 &&  gr[i].getSex().compareTo("male") == 0) {
n++;
}
}
Student[] recGroup = new Student[n];
for (int i = 0; i < gr.length; i++) {
if (gr[i] == null) {
continue;
}
if (gr[i].getAge() >= 18 &&  gr[i].getSex().compareTo("male") == 0) {
for (int j = 0; j < recGroup.length; j++) {
if(recGroup[j] != null) {continue;}
recGroup[j] = gr[i];
break;
}
}
}
return recGroup;
}
}

StudentAgeComparator
package com.gmail.tso;

import java.util.Comparator;

public class StudentAgeComparator implements Comparator {

@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub

if (o1 != null && o2 == null) {
return 1;
}
if (o1 == null && o2 != null) {
return -1;
}
if (o1 == null && o2 == null) {
return 0;
}

Student st1 = (Student) o1;
Student st2 = (Student) o2;

int age1 = st1.getAge();
int age2 = st2.getAge();

if (age1 > age2) {
return 1;
}
if (age1 < age2) {
return -1;
}
return 0;
}

}

StudentSurnameComparator
package com.gmail.tso;

import java.util.Comparator;

public class StudentSurnameComparator implements Comparator {

@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub

if (o1 != null && o2 == null) {
return 1;
}
if (o1 == null && o2 != null) {
return -1;
}
if (o1 == null && o2 == null) {
return 0;
}

Student st1 = (Student) o1;
Student st2 = (Student) o2;

String name1 = st1.getSurname();
String name2 = st2.getSurname();

if (name1.compareTo(name2) > 0) {
return 1;
}
if (name1.compareTo(name2) < 0) {
return -1;
}
return 0;
}

}

Voencom
package com.gmail.tso;

import java.util.Comparator;

public interface Voencom{

public Student[] pora();

}

Main
package com.gmail.tso;

import java.util.Arrays;

public class Main {

public static void main(String[] args) {

Student a = new Student("Igor", "Popov", 17, "male", 1, "FI-24", "FTI");
Student a1 = new Student("Olya", "Markov",18, "female", 1, "FI-22", "FTI");
Student a2 = new Student("Stas", "Smirnov",17, "male", 1, "FI-23", "FTI");
Student a3 = new Student("Kolya", "Petrov",16, "male", 1, "FI-21", "FTI");
Student a4 = new Student("Nastya", "Sidorov",18, "female", 1, "FI-24", "FTI");
Student a5 = new Student("Oleg", "Ivanov",17, "male", 1, "FI-22", "FTI");
Student a6 = new Student("Mark", "Golovach",18, "male", 1, "FI-21", "FTI");
Student a7 = new Student("Katya", "Kazakov",16, "female", 1, "FI-23", "FTI");
Student a8 = new Student("Polina", "Ryazanov",17, "female", 1, "FI-24", "FTI");
Group myGr = new Group();
myGr.add(a);
myGr.add(a1);
myGr.add(a2);
myGr.add(a3);
myGr.add(a4);
myGr.add(a5);
myGr.add(a6);
myGr.add(a7);
myGr.add(a8);
//myGr.addNew();

for (int i = 0; i < myGr.getGr().length; i++) {
System.out.println(myGr.getGr()[i]);
}

System.out.println();
System.out.println(myGr.dell("Popov"));
myGr.dell("Popovahaha");
System.out.println(myGr.serch("Popovdsadawda"));

System.out.println(myGr.serch("Kazakov"));

for (int i = 0; i < myGr.getGr().length; i++) {
System.out.println(myGr.getGr()[i]);
}

Arrays.sort(myGr.getGr(), new StudentSurnameComparator());
System.out.println();
for (int i = 0; i < myGr.getGr().length; i++) {
System.out.println(myGr.getGr()[i]);
}

Arrays.sort(myGr.getGr(), new StudentAgeComparator());
System.out.println();
for (int i = 0; i < myGr.getGr().length; i++) {
System.out.println(myGr.getGr()[i]);
}
System.out.println("Voencom");
for (int i = 0; i < myGr.pora().length; i++) {
System.out.println(myGr.pora()[i]);
}
}

}

sllawwkoo

  • Пользователи
  • *
  • Сообщений: 88
    • Просмотр профиля
Re: Java OOP Alexander 15_06_2020 online (Пн-Ср 19.00-21.00)
« Ответ #22 : Июля 01, 2020, 01:11:20 am »
Урок 4 Задание 1-4

Human
package com.gmail.sllawwkoo;

public class Human {

private String name;
private String surname;
private String sex;
private int age;

public Human(String name, String surname, String sex, int age) {
super();
this.name = name;
this.surname = surname;
this.sex = sex;
this.age = age;
}

public Human() {
super();

}

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 getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "Human [name=" + name + ", surname=" + surname + ", sex=" + sex + ", age=" + age + "]";
}

}


Student
package com.gmail.sllawwkoo;

public class Student extends Human {

private String faculty;
private String specialty;

public Student(String name, String surname, String sex, int age, String faculty, String specialty) {
super(name, surname, sex, age);
this.faculty = faculty;
this.specialty = specialty;
}

public Student() {
super();

}

public Student(String name, String surname, String sex, int age) {
super(name, surname, sex, age);

}

public String getFaculty() {
return faculty;
}

public void setFaculty(String faculty) {
this.faculty = faculty;
}

public String getSpecialty() {
return specialty;
}

public void setSpecialty(String specialty) {
this.specialty = specialty;
}

@Override
public String toString() {
return "Student [faculty=" + faculty + ", specialty=" + specialty + "," + super.toString();
}

}


MyException
package com.gmail.sllawwkoo;

public class MyException extends Exception {

@Override
public String getMessage() {

return "Group formed, student not accepted!!";

}

}


StudentAgeComparator
package com.gmail.sllawwkoo;

import java.util.Comparator;

public class StudentAgeComparator implements Comparator {

@Override
public int compare(Object o1, Object o2) {
if (o1 != null && o2 == null) {
return 1;
}
if (o1 == null && o2 != null) {
return -1;
}
if (o1 == null && o2 == null) {
return 0;
}
Student student1 = (Student) o1;
Student student2 = (Student) o2;

int age1 = student1.getAge();
int age2 = student2.getAge();

if (age1 > age2) {
return 1;
}
if (age1 < age2) {
return -1;
}
return 0;
}

}

StudentNameComparator
package com.gmail.sllawwkoo;

import java.util.Comparator;

public class StudentNameComparator implements Comparator {

@Override
public int compare(Object o1, Object o2) {
if (o1 != null && o2 == null) {
return 1;
}
if (o1 == null && o2 != null) {
return -1;
}
if (o1 == null && o2 == null) {
return 0;
}
Student student1 = (Student) o1;
Student student2 = (Student) o2;

String name1 = student1.getName();
String name2 = student2.getName();

if (name1.compareTo(name2) > 0) {
return 1;
}
if (name1.compareTo(name2) < 0) {
return -1;
}
return 0;
}

}

Voencom
package com.gmail.sllawwkoo;

public interface Voencom {

public Student[] getRecruter();
}

Group
package com.gmail.sllawwkoo;

import java.util.Arrays;
import javax.swing.JOptionPane;

public class Group implements Voencom {

private Student[] studentArray = new Student[10];

public Group(Student[] studentArray) {
super();
this.studentArray = studentArray;
}

public Group() {
super();

}

public Student[] getStudentArray() {
return studentArray;
}

public void setStudentArray(Student[] studentArray) {
this.studentArray = studentArray;
}

public void addStudent(Student student) throws MyException {
if (student == null) {
throw new IllegalArgumentException("Null student");
}
for (int i = 0; i < studentArray.length; i++) {

if (studentArray[i] == null) {
studentArray[i] = student;
return;
}
}
throw new MyException();
}

public void deleteStudent(String surname) {
for (int i = 0; i < studentArray.length; i++) {
if (studentArray[i] != null && studentArray[i].getSurname().equals(surname)) {
studentArray[i] = null;
break;
}
}

}

public Student searchStudent(String surname) {
for (Student one : studentArray) {
if (one != null && one.getSurname().equals(surname)) {
return one;
}
}
System.out.println("We do not have such a student!");
return null;

}

public void addStudentManually() throws MyException {
String name = String.valueOf(JOptionPane.showInputDialog("Input student name"));
String surname = String.valueOf(JOptionPane.showInputDialog("Input student lastname"));
String sex = String.valueOf(JOptionPane.showInputDialog("Input sex:  male or female"));
int age = Integer.valueOf(JOptionPane.showInputDialog("Input age"));
String faculty = String.valueOf(JOptionPane.showInputDialog("Input faculty"));
String specialty = String.valueOf(JOptionPane.showInputDialog("Input specialty"));
Student st = new Student(name, surname, sex, age, faculty, specialty);
this.addStudent(st);
System.out.println("Student added to group");
return;
}

public void showStudents() {
for (Student i : studentArray) {
System.out.println(i);
}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();

int i = 0;
for (Student student : studentArray) {
if (student != null) {
sb.append(student);
sb.append(System.lineSeparator());
}
}
return sb.toString();
}

@Override
public Student[] getRecruter() {
int n = 0;
for (Student student : studentArray) {
if (student != null  && student.getAge() >= 18 && student.getSex().compareTo("male") == 0) {
n += 1;
}
}
Student[] recruterArray = new Student[n];
int i = 0;
for (Student student : this.studentArray) {
if (student != null  && student.getAge() >= 18 && student.getSex().compareTo("male") == 0) {
recruterArray[i++] = student;
}
}
return recruterArray;
}

}


Main
package com.gmail.sllawwkoo;

import java.util.Arrays;

public class Main {

public static void main(String[] args) {
Student student1 = new Student("Wayne", "Rooney", "male", 17, "Economic", "Audit");
Student student2 = new Student("Rio", "Ferdinand", "male", 18, "Economic", "Audit");
Student student3 = new Student("Carlos", "Tevez", "male", 19, "Economic", "Audit");
Student student4 = new Student("Michelle", "Akers", "female", 18, "Economic", "Audit");
Student student5 = new Student("Alexandra", "Morgan", "female", 19, "Economic", "Audit");
Student student6 = new Student("Darren", "Fletcher", "male", 19, "Economic", "Audit");
Student student7 = new Student("Kristine", "Lilly", "female", 19, "Economic", "Audit");
Student student8 = new Student("Gerard", "Pique", "male", 18, "Economic", "Audit");
Student student9 = new Student("Brandi", "Smith", "female", 18, "Economic", "Audit");
Student student10 = new Student("Paul", "Scholes", "male", 19, "Economic", "Audit");
Student student11 = new Student("Michael", "Carrick", "male", 19, "Economic", "Audit");

Group groupA = new Group();

try {
groupA.addStudent(student1);
groupA.addStudent(student2);
groupA.addStudent(student3);
groupA.addStudent(student4);
groupA.addStudent(student5);
groupA.addStudent(student6);
groupA.addStudent(student7);
groupA.addStudent(student8);

} catch (MyException e) {
e.printStackTrace();
}

groupA.showStudents();
System.out.println();

try {
groupA.addStudent(student11);
groupA.addStudentManually();
} catch (MyException e) {
System.out.println(e.getMessage());
}
System.out.println();

Arrays.sort(groupA.getStudentArray(), new StudentNameComparator());
for (int i = 0; i < groupA.getStudentArray().length; i++) {

System.out.println(groupA.getStudentArray()[i]);
}

System.out.println();

Arrays.sort(groupA.getStudentArray(), new StudentAgeComparator());
for (int i = 0; i < groupA.getStudentArray().length; i++) {

System.out.println(groupA.getStudentArray()[i]);
}

System.out.println();

groupA.deleteStudent("Pique");
System.out.println(groupA);
System.out.println(groupA.searchStudent("Rooney"));
System.out.println();

Student[] recruterArray = groupA.getRecruter();
for (Student student : recruterArray) {
System.out.println("It's time for the army" + student);
}
}

}

Alexander Ts

  • Преподаватели
  • Пользователи
  • *
  • Сообщений: 13854
    • Просмотр профиля
    • E-mail
Re: Java OOP Alexander 15_06_2020 online (Пн-Ср 19.00-21.00)
« Ответ #23 : Июля 01, 2020, 12:40:55 pm »
Ksenia
В Java не используют открытые свойства класса, так что такой код неудачен:
public class Cat {
        String color;
        double weight;
        int age;
Так же не используют default модификатор доступа. Так, что такой код также неудачен:
void  voice1 () {
                        System.out.println("May - May");
                }

Перепутан порядок следования членов класса. У вас конструктор идет после методов

Задание 2.
Метод который вычисляет площадь, у вас объявлен как private
         private double area() {
И как же его тогда вызывать? Он же закрыт от внешнего доступа

Так что тут есть над чем поработать.

Ksenia
Решение верное

Fr1kky
У вас что то класс исключения, ну прям один в один класс Студент. Наверное перепутали.
Решение верное

Sllawwkoo
Решение верное