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

Автор Тема: Java_OOP_Online_Reciepthack 24.03.2020  (Прочитано 25 раз)

VladimirShevchuk

  • Пользователи
  • *
  • Сообщений: 33
    • Просмотр профиля
    • E-mail
Java_OOP_Online_Reciepthack 24.03.2020
« : Марта 24, 2020, 09:52:24 pm »
Задача с котами, но я взял Собак)
package com.gmail.reciepthack;

public class Dog {
private String name;
private String breed;
private String color;
private int age;
private double leangth;
private double weigth;


public Dog(String name, String breed, String color, double weigth, double leangth, int age) {
super();
this.name = name;
this.breed = breed;
this.color = color;
this.weigth = weigth;
this.leangth = leangth;
this.age = age;
}

public Dog() {
super();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getBreed() {
return breed;
}

public void setBreed(String breed) {
this.breed = breed;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public double getWeigth() {
return weigth;
}

public void setWeigth(double weigth) {
this.weigth = weigth;
}

public double getLeangth() {
return leangth;
}

public void setLeangth(double leangth) {
this.leangth = leangth;
}

public int getAge() {
return age;
}

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

@Override
public String toString() {
return "Dog [name=" + name + ", breed=" + breed + ", color=" + color + ", weigth=" + weigth + ", leangth="
+ leangth + ", age=" + age + "]";
}

}

package com.gmail.reciepthack;

public class Main {

public static void main(String[] args) {
Dog dogOne = new Dog();

dogOne.setName("Bella");
dogOne.setBreed("York");
  dogOne.setColor("brown");
  dogOne.setAge(6);
  dogOne.setLeangth(27.4);
  dogOne.setWeigth(15.8);
System.out.println(dogOne);

Dog dogTwo = new Dog("Jack","Вull terrier","white",20,35.3,3);
System.out.println(dogTwo);

Dog dogThee = new Dog();
dogThee.setName("Pushok");
dogThee.setBreed("Pomeranian");
  dogThee.setColor("White");
  dogThee.setAge(2);
  dogThee.setLeangth(20);
  dogThee.setWeigth(17);
  System.out.println(dogThee);
 
  Dog dogFour = new Dog("Pinky","Husky","Gray",60,50,4);
System.out.println(dogFour);
}

}


Alexander Ts

  • Преподаватели
  • Пользователи
  • *
  • Сообщений: 13429
    • Просмотр профиля
    • E-mail
Re: Java_OOP_Online_Reciepthack 24.03.2020
« Ответ #1 : Марта 25, 2020, 12:22:32 am »

VladimirShevchuk

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

VladimirShevchuk

  • Пользователи
  • *
  • Сообщений: 33
    • Просмотр профиля
    • E-mail
Re: Java_OOP_Online_Reciepthack 24.03.2020
« Ответ #2 : Марта 26, 2020, 09:13:51 pm »
Лекция 2 задание 1-3
package com.gmail.reciepthack;

public abstract class Shape {
abstract double calcPerimetr();

abstract double calcArea();
}
package com.gmail.reciepthack;

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

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

public Point() {
super();
}

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;
}

@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}

}
package com.gmail.reciepthack;

public class Triangle extends Shape {
private Point p1;
private Point p2;
private Point p3;

public Triangle(Point p1, Point p2, Point p3) {
super();
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}

public Triangle() {
super();
}

public Point getP1() {
return p1;
}

public void setP1(Point p1) {
this.p1 = p1;
}

public Point getP2() {
return p2;
}

public void setP2(Point p2) {
this.p2 = p2;
}

public Point getP3() {
return p3;
}

public void setP3(Point p3) {
this.p3 = p3;

}

@Override
double calcPerimetr() {
double side1 = Math.sqrt(Math.pow((p1.getX() - p2.getX()), 2) + Math.pow((p1.getY() - p2.getY()), 2));
double side2 = Math.sqrt(Math.pow((p2.getX() - p3.getX()), 2) + Math.pow((p2.getY() - p3.getY()), 2));
double side3 = Math.sqrt(Math.pow((p1.getX() - p3.getX()), 2) + Math.pow((p1.getY() - p3.getY()), 2));
return side1 + side2 + side3;
}

@Override
double calcArea() {
double side1 = Math.sqrt(Math.pow((p1.getX() - p2.getX()), 2) + Math.pow((p1.getY() - p2.getY()), 2));
double side2 = Math.sqrt(Math.pow((p2.getX() - p3.getX()), 2) + Math.pow((p2.getY() - p3.getY()), 2));
double side3 = Math.sqrt(Math.pow((p1.getX() - p3.getX()), 2) + Math.pow((p1.getY() - p3.getY()), 2));
double polPer = (side1 + side2 + side3) / 2;
return Math.sqrt(polPer * (polPer - side1) * (polPer - side2) * (polPer - side3));
}

@Override
public String toString() {
return "Triangle [p1=" + p1 + ", p2=" + p2 + ", p3=" + p3 + "]";
}

}
package com.gmail.reciepthack;

public class Rectangl extends Shape {
private Point p1;
private Point p2;
private Point p3;
public Rectangl(Point p1, Point p2, Point p3) {
super();
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public Rectangl() {
super();
// TODO Auto-generated constructor stub
}
public Point getP1() {
return p1;
}
public void setP1(Point p1) {
this.p1 = p1;
}
public Point getP2() {
return p2;
}
public void setP2(Point p2) {
this.p2 = p2;
}
public Point getP3() {
return p3;
}
public void setP3(Point p3) {
this.p3 = p3;
}
@Override
double calcPerimetr() {
double side1 = Math.sqrt(Math.pow((p1.getX() - p2.getX()), 2) + Math.pow((p1.getY() - p2.getY()), 2));
double side2 = Math.sqrt(Math.pow((p2.getX() - p3.getX()), 2) + Math.pow((p2.getY() - p3.getY()), 2));
return (side1+side2)*2;
}
@Override
double calcArea() {
double side1 = Math.sqrt(Math.pow((p1.getX() - p2.getX()), 2) + Math.pow((p1.getY() - p2.getY()), 2));
double side2 = Math.sqrt(Math.pow((p2.getX() - p3.getX()), 2) + Math.pow((p2.getY() - p3.getY()), 2));
return side1*side2;
}
@Override
public String toString() {
return "Rectangl [p1=" + p1 + ", p2=" + p2 + ", p3=" + p3 + "]";
}

}
package com.gmail.reciepthack;

public class Circle extends Shape {
private Point p1;
private Point p2;

public Circle(Point p1, Point p2) {
super();
this.p1 = p1;
this.p2 = p2;
}

public Circle() {
super();
// TODO Auto-generated constructor stub
}

public Point getP1() {
return p1;
}

public void setP1(Point p1) {
this.p1 = p1;
}

public Point getP2() {
return p2;
}

public void setP2(Point p2) {
this.p2 = p2;
}

@Override
double calcPerimetr() {
double rad = Math.sqrt(Math.pow((p1.getX() - p2.getX()), 2) + Math.pow((p1.getY() - p2.getY()), 2));
return 2 * Math.PI * rad;
}

@Override
double calcArea() {
double rad = Math.sqrt(Math.pow((p1.getX() - p2.getX()), 2) + Math.pow((p1.getY() - p2.getY()), 2));
return Math.PI * rad * rad;
}

@Override
public String toString() {
return "Circle [p1=" + p1 + ", p2=" + p2 + "]";
}

}
package com.gmail.reciepthack;

public class Main {
public static void main(String[] args) {
Shape c = new Circle(new Point(0, 3), new Point(6, 1));
Shape sq = new Rectangl(new Point(0, 1), new Point(2, 5), new Point(3, 5));
Shape tr = new Triangle(new Point(0, 0), new Point(4, 1), new Point(5, 0));

System.out.println("Perimetr of Cirlce =" + c.calcPerimetr());
System.out.println("Area of Cirlce =" + c.calcArea());

System.out.println("Perimetr of Rectangle =" + sq.calcPerimetr());
System.out.println("Area of Rectangle =" + sq.calcArea());

System.out.println("Perimetr of Triangle =" + tr.calcPerimetr());
System.out.println("Area of Triangle =" + tr.calcArea());
}
}

Alexander Ts

  • Преподаватели
  • Пользователи
  • *
  • Сообщений: 13429
    • Просмотр профиля
    • E-mail
Re: Java_OOP_Online_Reciepthack 24.03.2020
« Ответ #3 : Марта 27, 2020, 12:06:08 am »
VladimirShevchuk

Решение верное. Хотя если бы вынесли метод вычисления расстояния между двумя точками в отдельный класс (например в ту же точку) то не пришлось бы его повторять по много раз во всех фигурах.

VladimirShevchuk

  • Пользователи
  • *
  • Сообщений: 33
    • Просмотр профиля
    • E-mail
Re: Java_OOP_Online_Reciepthack 24.03.2020
« Ответ #4 : Марта 29, 2020, 09:54:25 pm »
Лекция 3
package com.gmail.reciepthack;

public class Students extends Human {
static public final String STUDENT = "Student";
private int course;
private String group;

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

public Students() {
super();
}

public Students(String name, int age, String sex, int height, String surname) {
super(name, age, sex, height, surname);
}

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 static String getStudent() {
return STUDENT;
}

@Override
public String toString() {
return "Students [course=" + course + ", group=" + group + "]";
}

}
package com.gmail.reciepthack;

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

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

public Human() {
super();
}

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;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public String getSurname() {
return surname;
}

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

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

}
package com.gmail.reciepthack;

public class MyExeptions extends Exception {
@Override
public String getMessage() {
return "Group of students is full";
}

}
package com.gmail.reciepthack;

import java.util.Arrays;

public class Group {
private Students[] gr = new Students[10];

public Group(Students[] gr) {
super();
this.gr = gr;
}

public Group() {
super();
}

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

public void setGr(Students[] gr) {
this.gr = gr;
}

public Students[] addNewStudent(Students x) throws MyExeptions {
for (int i = 0; i < gr.length; i++) {
if (i == gr.length) {
throw new MyExeptions();
}
if (gr[i] == null) {
gr[i] = x;
break;
}

}
return gr;
}

public Students[] expelStudent(String surname) {
for (int i = 0; i < gr.length; i++) {
if (gr[i].getSurname().equalsIgnoreCase(surname)) {
gr[i] = null;
break;
}

}
return gr;

}

public Students findStudent(String surname) {

for (int i = 0; i < gr.length; i++) {
if (gr[i].getSurname().equalsIgnoreCase(surname)) {
return gr[i];
}
}
return null;
}

@Override
public String toString() {
for (int i = 0; i < gr.length - 1; i++) {
for (int j = i + 1; j < gr.length; j++) {
if (gr[i] == null || gr[j] == null) {
continue;
}
if (gr[i].getSurname().compareTo(gr[j].getSurname()) > 0) {
Students x = gr[i];
gr[i] = gr[j];
gr[j] = x;
}
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < gr.length; i++) {
if (gr[i] != null) {
sb.append(gr[i].toString() + "\n");
}
}
return sb.toString();
}

}
package com.gmail.reciepthack;

public class Main {

public static void main(String[] args) {
Students rojkov = new Students("Piter", 16, "man", 182, "Rojkov", 1, "Java");
Students melnikovich = new Students("Leonid", 18, "man", 176, "Melnikovich", 2, "Java");
Students sidorov = new Students("Egor", 19, "man", 190, "Sidorov", 3, "Java");
Students gavrilov = new Students("Petr", 21, "man", 185, "Gavrilov", 4, "Java");
Students pupkin = new Students("Ivan", 17, "man", 180, "Abramov", 3, "Java");
Students vasutina = new Students("Irina", 16, "woman", 160, "Vasutina", 1, "Java");
Students ysach = new Students("Olya", 22, "woman", 160, "Ysach", 4, "Java");
Students sviridov = new Students("Vasya", 19, "man", 165, "Sviridov", 3, "Java");
Students aliev = new Students("Petr", 21, "man", 175, "Aliev", 2, "Java");
Students ivanchyk = new Students("Ivan", 17, "man", 180, "Ivanchyk", 3, "Java");
Students selesneva = new Students("Alisa", 19, "woman", 160, "Selesneva", 3, "Java");

Group gr = new Group();
try {
gr.addNewStudent(selesneva);
gr.addNewStudent(ivanchyk);
gr.addNewStudent(aliev);
gr.addNewStudent(sviridov);
gr.addNewStudent(ysach);
gr.addNewStudent(pupkin);
gr.addNewStudent(gavrilov);
gr.addNewStudent(sidorov);
gr.addNewStudent(vasutina);
gr.addNewStudent(rojkov);
gr.addNewStudent(melnikovich);
} catch (MyExeptions e) {
System.out.println(e.getMessage());
}

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

System.out.println(gr.toString());

}


}


Alexander Ts

  • Преподаватели
  • Пользователи
  • *
  • Сообщений: 13429
    • Просмотр профиля
    • E-mail
Re: Java_OOP_Online_Reciepthack 24.03.2020
« Ответ #5 : Марта 31, 2020, 12:22:23 am »
VladimirShevchuk

Метод
        public Students findStudent(String surname) {
Не устойчив. Добавьте в группу только 3 Студента и вызовите метод поиска для несуществующего в этой группе фамилии. Ваша программа завершится с NullPointerException так как рано или поздно вы у null вызовете метод получения фамилии

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