-
Notifications
You must be signed in to change notification settings - Fork 0
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
a-tikhomirov
wants to merge
3
commits into
master
Choose a base branch
from
lesson05
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lesson05 #6
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
lesson05/src/ru/geekbrains/javaone/lesson05/hw/Animal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
lesson05/src/ru/geekbrains/javaone/lesson05/hw/HunterDog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
lesson05/src/ru/geekbrains/javaone/lesson05/hw/Lesson05HW.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а зачем все животные по умолчанию бегут, если каждый наследник всё равно переопределит это поведение на сравнение со своим значением? да и поля- не логичнее ли положить в животное? они же не очень отличаются от наследника к наследнику