Added Kotik class and Application with main method
This commit is contained in:
commit
ef1e6396ec
39
.gitignore
vendored
Normal file
39
.gitignore
vendored
Normal file
@ -0,0 +1,39 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
.kotlin
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea/modules.xml
|
||||
.idea/jarRepositories.xml
|
||||
.idea/compiler.xml
|
||||
.idea/libraries/
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
17
pom.xml
Normal file
17
pom.xml
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>PW1</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
35
src/main/java/Application.java
Normal file
35
src/main/java/Application.java
Normal file
@ -0,0 +1,35 @@
|
||||
import animals.Kotik;
|
||||
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
Kotik kotik1 = new Kotik("Вася","громкий", 10, 3);
|
||||
|
||||
Kotik kotik2 = new Kotik();
|
||||
kotik2.setName("Рыжик");
|
||||
kotik2.setVoice("тихий");
|
||||
kotik2.setSatiety(15);
|
||||
kotik2.setWeight(4);
|
||||
|
||||
String[] day1 = kotik1.liveAnotherDay();
|
||||
for (String hour : day1){
|
||||
System.out.println(hour);
|
||||
}
|
||||
|
||||
System.out.println(kotik1.getName() + " весит " + kotik1.getWeight() + " кг.");
|
||||
|
||||
String comparison = "Котики " + kotik1.getName() + " и " + kotik2.getName() + " разговаривают ";
|
||||
if (compareVoice(kotik1, kotik2)) System.out.println(comparison + "одинакого!");
|
||||
else System.out.println(comparison + "по-разному!");
|
||||
|
||||
System.out.println("Всего котиков было создано: " + Kotik.getCount());
|
||||
}
|
||||
public static boolean compareVoice(Kotik kotik1, Kotik kotik2) {
|
||||
if (kotik1 == null || kotik2 == null) return false;
|
||||
String voice1 = kotik1.getVoice();
|
||||
String voice2 = kotik2.getVoice();
|
||||
if (voice1 == null || voice2 == null) return false;
|
||||
return voice1.equals(voice2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
147
src/main/java/animals/Kotik.java
Normal file
147
src/main/java/animals/Kotik.java
Normal file
@ -0,0 +1,147 @@
|
||||
package animals;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Kotik {
|
||||
private String name;
|
||||
private String voice;
|
||||
private int satiety;
|
||||
private int weight;
|
||||
|
||||
private static final int METHODS = 5;
|
||||
private static int count;
|
||||
|
||||
public Kotik(){
|
||||
count ++;
|
||||
}
|
||||
|
||||
public Kotik(String name, String voice, int satiety, int weight){
|
||||
Kotik.count ++;
|
||||
this.name = name;
|
||||
this.voice = voice;
|
||||
this.satiety = satiety;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getVoice(){
|
||||
return this.voice;
|
||||
}
|
||||
|
||||
public void setVoice(String voice){
|
||||
this.voice = voice;
|
||||
}
|
||||
|
||||
public int getSatiety(){
|
||||
return this.satiety;
|
||||
}
|
||||
|
||||
public void setSatiety(int satiety){
|
||||
this.satiety = satiety;
|
||||
}
|
||||
|
||||
public int getWeight(){
|
||||
return this.weight;
|
||||
}
|
||||
|
||||
public void setWeight(int weight){
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public static int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
private boolean performAction(String action) {
|
||||
if (satiety > 0) {
|
||||
System.out.println("Котик " + action);
|
||||
satiety--;
|
||||
return true;
|
||||
} else {
|
||||
System.out.println("Котик просит есть");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean play() {
|
||||
return performAction("играет");
|
||||
}
|
||||
|
||||
public boolean sleep() {
|
||||
return performAction("спит");
|
||||
}
|
||||
|
||||
public boolean wash() {
|
||||
return performAction("умывается");
|
||||
}
|
||||
|
||||
public boolean walk() {
|
||||
return performAction("гуляет");
|
||||
}
|
||||
|
||||
public boolean hunt() {
|
||||
return performAction("охотится");
|
||||
}
|
||||
|
||||
public void eat(int amount) {
|
||||
System.out.println("Котик ест");
|
||||
this.satiety += amount;
|
||||
}
|
||||
|
||||
public void eat(int amount, String food) {
|
||||
System.out.println("Котик ест" + food);
|
||||
this.satiety += amount;
|
||||
}
|
||||
|
||||
public void eat() {
|
||||
eat(3,"корм");
|
||||
}
|
||||
|
||||
public String[] liveAnotherDay(){
|
||||
String[] day = new String[24];
|
||||
Random rand = new Random();
|
||||
boolean success = false;
|
||||
|
||||
for (int i=0; i<24; i++){
|
||||
int random = rand.nextInt(METHODS);
|
||||
String activity = "";
|
||||
switch (random){
|
||||
case 0:
|
||||
success = play();
|
||||
activity = "играл";
|
||||
break;
|
||||
case 1:
|
||||
success = sleep();
|
||||
activity = "спал";
|
||||
break;
|
||||
case 2:
|
||||
success = wash();
|
||||
activity = "умывался";
|
||||
break;
|
||||
case 3:
|
||||
success = walk();
|
||||
activity = "гулял";
|
||||
break;
|
||||
case 4:
|
||||
success = hunt();
|
||||
activity = "охотился";
|
||||
break;
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
eat();
|
||||
activity = "ел";
|
||||
}
|
||||
|
||||
day[i] = i + " - " + activity;
|
||||
}
|
||||
return day;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user