Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson05 #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lesson05/src/ru/geekbrains/javaone/lesson05/hw/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.geekbrains.javaone.lesson05.hw;

public abstract class Animal {
protected String name;
protected String color;

protected Animal(String name, String color) {
this.name = name;
this.color = color;
}

protected String getName() {
return name;
}

protected String getColor() {
return color;
}

protected String run(float distance) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а зачем все животные по умолчанию бегут, если каждый наследник всё равно переопределит это поведение на сравнение со своим значением? да и поля- не логичнее ли положить в животное? они же не очень отличаются от наследника к наследнику

return name + " runs " + distance + " meters";
}

protected String jump(float heigth) {
return name + " jumps " + heigth + " meters high";
}

protected String swim(float distance) {
return name + " swims " + distance + " meters";
}
}
45 changes: 45 additions & 0 deletions lesson05/src/ru/geekbrains/javaone/lesson05/hw/Bird.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ru.geekbrains.javaone.lesson05.hw;

public class Bird extends Animal {
private static final float RUN_MAX = 5.5f;

private float maxRunDistance = 5;
private float maxJumpHeigth = 0.2f;

public Bird(String name, String color) {
super(name, color);
}

public Bird(String name, String color, float maxRunDistance) {
super(name, color);
setMaxRunDistance(maxRunDistance);
}

public void setMaxRunDistance(float maxRunDistance) {
this.maxRunDistance =
maxRunDistance > RUN_MAX ? RUN_MAX : maxRunDistance;
}

public String voice() {
return name + " whistles";
}

@Override
public String run(float distance) {
return name + " makes little jumps for "
+ ((distance < maxRunDistance) ? distance : maxRunDistance) + " meters";
}

@Override
public String jump(float heigth) {
if (heigth > maxJumpHeigth)
return voice();
else
return super.jump(heigth);
}

@Override
public String swim(float distance) {
return name + " flies away";
}
}
58 changes: 58 additions & 0 deletions lesson05/src/ru/geekbrains/javaone/lesson05/hw/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ru.geekbrains.javaone.lesson05.hw;

public class Cat extends Animal {
private static final float RUN_MAX = 500;
private static final float JUMP_MAX = 4.5f;

private float maxRunDistance = 200;
private float maxJumpHeigth = 2;

public Cat(String name, String color) {
super(name, color);
}

public Cat(String name, String color, float maxRunDistance) {
super(name, color);
setMaxRunDistance(maxRunDistance);
}

public Cat(String name, String color,
float maxRunDistance, float maxJumpHeigth) {
super(name, color);
setMaxRunDistance(maxRunDistance);
setMaxJumpHeigth(maxJumpHeigth);
}

public void setMaxRunDistance(float maxRunDistance) {
this.maxRunDistance =
maxRunDistance > RUN_MAX ? RUN_MAX : maxRunDistance;
}

public void setMaxJumpHeigth(float maxJumpHeigth) {
this.maxJumpHeigth =
maxJumpHeigth > JUMP_MAX ? JUMP_MAX : maxJumpHeigth;
}

@Override
public String run(float distance) {
if (distance > maxRunDistance)
return name + " runs only for "
+ maxRunDistance + " meters";
else
return super.run(distance);
}

@Override
public String jump(float heigth) {
if (heigth > maxJumpHeigth)
return name + " jumps only for "
+ maxJumpHeigth + " meters high";
else
return super.jump(heigth);
}

@Override
protected String swim(float distance) {
return name + " looks very dissapointed" ;
}
}
78 changes: 78 additions & 0 deletions lesson05/src/ru/geekbrains/javaone/lesson05/hw/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package ru.geekbrains.javaone.lesson05.hw;

public class Dog extends Animal {
private static final float RUN_MAX = 2000;
private static final float JUMP_MAX = 1.5f;
private static final float SWIM_MAX = 200;

protected float maxRunDistance = 500;
private float maxJumpHeigth = 0.5f;
private float maxSwimDistance = 10;

public Dog(String name, String color) { super(name, color); }

public Dog(String name, String color, float maxRunDistance) {
super(name, color);
setMaxRunDistance(maxRunDistance);
}

public Dog(String name, String color,
float maxRunDistance, float maxJumpHeigth) {
super(name, color);
setMaxRunDistance(maxRunDistance);
setMaxJumpHeigth(maxJumpHeigth);
}

public Dog(String name, String color, float maxRunDistance,
float maxJumpHeigth, float maxSwimDistance) {
super(name, color);
setMaxRunDistance(maxRunDistance);
setMaxJumpHeigth(maxJumpHeigth);
setMaxSwimDistance(maxSwimDistance);
}

public void setMaxRunDistance(float maxRunDistance) {
this.maxRunDistance =
maxRunDistance > RUN_MAX ? RUN_MAX : maxRunDistance;
}

public void setMaxJumpHeigth(float maxJumpHeigth) {
this.maxJumpHeigth =
maxJumpHeigth > JUMP_MAX ? JUMP_MAX : maxJumpHeigth;
}

public void setMaxSwimDistance(float maxSwimDistance) {
this.maxSwimDistance =
maxSwimDistance > SWIM_MAX ? SWIM_MAX : maxSwimDistance;
}

@Override
public String run(float distance) {
if (distance > maxRunDistance)
return name + " runs only for "
+ maxRunDistance + " meters";
else if (maxRunDistance/distance >= 2)
return name + " runs for "
+ distance + " meters and runs back :)";
else
return super.run(distance);
}

@Override
public String jump(float heigth) {
if (heigth > maxJumpHeigth)
return name + " jumps only for "
+ maxJumpHeigth + " meters high";
else
return super.jump(heigth);
}

@Override
public String swim(float distance) {
if (distance > maxSwimDistance)
return name + " swims only for "
+ maxSwimDistance + " meters";
else
return super.swim(distance);
}
}
77 changes: 77 additions & 0 deletions lesson05/src/ru/geekbrains/javaone/lesson05/hw/Horse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ru.geekbrains.javaone.lesson05.hw;

public class Horse extends Animal {
private static final float RUN_MAX = 10000;
private static final float JUMP_MAX = 3.5f;
private static final float SWIM_MAX = 400;

private float maxRunDistance = 1500;
private float maxJumpHeigth = 3;
private float maxSwimDistance = 100;

public Horse(String name, String color) {
super(name, color);
}

public Horse(String name, String color, float maxRunDistance) {
super(name, color);
setMaxRunDistance(maxRunDistance);
}

public Horse(String name, String color,
float maxRunDistance, float maxJumpHeigth) {
super(name, color);
setMaxRunDistance(maxRunDistance);
setMaxJumpHeigth(maxJumpHeigth);
}

public Horse(String name, String color, float maxRunDistance,
float maxJumpHeigth, float maxSwimDistance) {
super(name, color);
setMaxRunDistance(maxRunDistance);
setMaxJumpHeigth(maxJumpHeigth);
setMaxSwimDistance(maxSwimDistance);
}

public void setMaxRunDistance(float maxRunDistance) {
this.maxRunDistance =
maxRunDistance > RUN_MAX ? RUN_MAX : maxRunDistance;
}

public void setMaxJumpHeigth(float maxJumpHeigth) {
this.maxJumpHeigth =
maxJumpHeigth > JUMP_MAX ? JUMP_MAX : maxJumpHeigth;
}

public void setMaxSwimDistance(float maxSwimDistance) {
this.maxSwimDistance =
maxSwimDistance > SWIM_MAX ? SWIM_MAX : maxSwimDistance;
}

@Override
public String run(float distance) {
if (distance > maxRunDistance)
return name + " runs only for "
+ maxRunDistance + " meters";
else
return super.run(distance);
}

@Override
public String jump(float heigth) {
if (heigth > maxJumpHeigth)
return name + " jumps only for "
+ maxJumpHeigth + " meters high";
else
return super.jump(heigth);
}

@Override
public String swim(float distance) {
if (distance > maxSwimDistance)
return name + " swims only for "
+ maxSwimDistance + " meters";
else
return super.swim(distance);
}
}
20 changes: 20 additions & 0 deletions lesson05/src/ru/geekbrains/javaone/lesson05/hw/HunterDog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.geekbrains.javaone.lesson05.hw;

public class HunterDog extends Dog {
private static final float RUN_MAX = 3000;

public HunterDog(String name, String color) {
super(name, color);
}

public HunterDog(String name, String color, float maxRunDistance) {
super(name, color);
setMaxRunDistance(maxRunDistance);
}

@Override
public void setMaxRunDistance(float maxRunDistance) {
this.maxRunDistance =
maxRunDistance > RUN_MAX ? RUN_MAX : maxRunDistance;
}
}
42 changes: 42 additions & 0 deletions lesson05/src/ru/geekbrains/javaone/lesson05/hw/Lesson05HW.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ru.geekbrains.javaone.lesson05.hw;

public class Lesson05HW {
private static void printPetsInfo(Animal[] pets) {
for (int i = 1; i <= pets.length; i++)
{
System.out.printf("Our %d pet is %s. Name: %s\t Color: %s\n",
i, pets[i-1].getClass().getSimpleName(), pets[i-1].getName(), pets[i-1].getColor());
}
}

private static void makeActivity(Animal[] pets, float runDistanse, float jumpHeight, float swimDistance) {
for (Animal pet : pets) {
System.out.printf("Ask %s to run for %.1f meters:\n", pet.name, runDistanse);
System.out.println(pet.run(runDistanse));
System.out.printf("Ask %s to jump %.1f meters high:\n", pet.name, jumpHeight);
System.out.println(pet.jump(jumpHeight));
System.out.printf("Ask %s to swim for %.1f meters:\n", pet.name, swimDistance);
System.out.println(pet.swim(swimDistance));
System.out.println();
}
}

public static void main(String[] args) {
Animal[] pets = {
new Dog("Charlie", "Dark-brown"),
new Dog("Nessy", "White", 800, 1, 200),
new Horse("Caesarus","Light-brown"),
new Horse("Aphina","Black", 10000, 4),
new Bird("Nevilichka","Yellow-gray"),
new Cat("Baiyun","Gray"),
new HunterDog("Laika", "Black", 3000)
};

printPetsInfo(pets);
System.out.println("\n");
makeActivity(pets, 400, 3, 15);
System.out.println("\n");
makeActivity(pets, 3000, 4, 300);

}
}