From 35c5adb9126cd5aa91afb73b3d9cc3d38098c03d Mon Sep 17 00:00:00 2001 From: Shuulker Date: Sat, 13 Apr 2024 20:21:05 -0700 Subject: [PATCH] Finished Java Assignment 010 --- .idea/misc.xml | 2 +- .idea/modules.xml | 1 + README.md | 23 +++++++++---- src/src.iml | 11 ++++++ src/src/Dog.java | 87 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 src/src.iml create mode 100644 src/src/Dog.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..172df7b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index bdef184..496a538 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/README.md b/README.md index 833cd8b..7028b86 100644 --- a/README.md +++ b/README.md @@ -10,23 +10,33 @@ 6. All the instance methods for the class **Student** ```java +// Class: Student class Student{ + // Instance Variables/Fields + // String: 'name' + // int: 'rollNo' private String name; private int rollNo; - + + // Constructor + // Constructor Parameters: String: 's', int: 'r' Student(String s, int r) { - name = s; - rollNo = r; + // Where instance variables get set + name = s; // 'name' gets set to parameter 's' + rollNo = r; // 'rollNo' gets set to parameter 'r' } + // Instance method for the display void methodForDisplay() { System.out.println(name+"'s Roll No: "+rollNo); } - + public static void main(String[] args) { + // Creates the Student object with the name "Rambo" and the roll number 21 Student obj1=new Student("Rambo",21); + // Calls the method to display obj1.methodForDisplay(); } } @@ -35,8 +45,9 @@ class Student{ ## Part 2 - Public VS Static * Read the W3Schools page on class methods: [W3Schools Java Class Methods](https://www.w3schools.com/java/java_class_methods.asp) -* In your own words, write a few sentences below explaining the difference between static and public methods in relation to a class. - +* In your own words, write a few sentences below explaining the difference between static and public methods in relation to a class. +You must create a new object to access a public method. With a static method you do not, you can just call it at any time. +What you can't do however, is use static methods outside of it's own class, but you can with a public one. (Hence it's name.) ## Part 3 - Dogs * View the image below, and from the image, construct a Java file **Dog** that mirrors the diagrammed class and the 3 dog objects. diff --git a/src/src.iml b/src/src.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/src/src.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/src/Dog.java b/src/src/Dog.java new file mode 100644 index 0000000..671b433 --- /dev/null +++ b/src/src/Dog.java @@ -0,0 +1,87 @@ +class Food { + static int foodAmount; + + Food(int foodAmount) { + Food.foodAmount = foodAmount; + } +} + +class Energy { + int energyAmount; + + Energy(int energyAmount) { + this.energyAmount = energyAmount; + } +} + +public class Dog { + + private String breed; + private String size; + private String color; + private int age; + + private Food food = new Food(5); + private Energy energy; + private String name; + + public Dog(String breed, String size, String color, int age, Energy energy) { + this.breed = breed; + this.size = size; + this.color = color; + this.age = age; + this.energy = energy; + // I included energy here so that each dog can use energy without affecting the other dogs energy values, but if one dog eats it reduces the amount of food they all can eat. + } + + void name(String name) { + this.name = name; + System.out.println("It's name is " + name); + } + + void eat() { + if (Food.foodAmount > 0) { + Food.foodAmount--; + energy.energyAmount++; + System.out.println(name + " is eating."); + } else { + System.out.println("Ran out of food! Time to go to the store!"); + } + } + + void run() { + if (energy.energyAmount > 0) { + energy.energyAmount--; + System.out.println(name + " is running."); + } else { + System.out.println(name + " is too tuckered out."); + } + } + + void sleep() { + energy.energyAmount++; + System.out.println(name + " is sleeping."); + } + + public String toString() { + return "This dog is a " + this.breed + ", it's " + this.size + " in size, it's " + this.color + ", and it's " + this.age + " years old."; + } + + public static void main(String[] args) { + Dog Dog1 = new Dog("Bulldog", "Medium","Light Gray",5,new Energy(5)); + Dog Dog2 = new Dog("Beagle", "Small","Orange",6,new Energy(5)); + Dog Dog3 = new Dog("German Shepherd", "Large","White & Orange",6,new Energy(5)); + + System.out.println(Dog1); + Dog1.name("Rufus"); + Dog1.eat(); + + System.out.println(Dog2); + Dog2.name("Snoopy"); + Dog2.run(); + + System.out.println(Dog3); + Dog3.name("Odin"); + Dog3.sleep(); + } +}