From ef1e6396eca3e7fcdeea9b03f31a28f2ee586a2c Mon Sep 17 00:00:00 2001 From: valerpan Date: Wed, 15 Jul 2026 18:54:53 +0300 Subject: [PATCH] Added Kotik class and Application with main method --- .gitignore | 39 ++++++++ pom.xml | 17 ++++ src/main/java/Application.java | 35 ++++++++ src/main/java/animals/Kotik.java | 147 +++++++++++++++++++++++++++++++ 4 files changed, 238 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/Application.java create mode 100644 src/main/java/animals/Kotik.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..480bdf5 --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..2d1b1a9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + PW1 + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 0000000..889b123 --- /dev/null +++ b/src/main/java/Application.java @@ -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); + } +} + + diff --git a/src/main/java/animals/Kotik.java b/src/main/java/animals/Kotik.java new file mode 100644 index 0000000..28a8baa --- /dev/null +++ b/src/main/java/animals/Kotik.java @@ -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; + } +}