diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..69ace3f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/README.md b/README.md index 833cd8b..91be808 100644 --- a/README.md +++ b/README.md @@ -10,25 +10,23 @@ 6. All the instance methods for the class **Student** ```java -class Student{ - private String name; - private int rollNo; - - Student(String s, int r) - { - name = s; - rollNo = r; - } - - void methodForDisplay() - { - System.out.println(name+"'s Roll No: "+rollNo); - } - - public static void main(String[] args) { - Student obj1=new Student("Rambo",21); - obj1.methodForDisplay(); - } +class Student{ // Class name: Student + private String name; // Instance variable: name (String) + private int rollNo; // Instance variable: rollNo (int) + + Student(String s, int r) { // Constructor: Student + name = s; // Constructor parameter for name + rollNo = r; // Constructor parameter for rollNo + } + + void methodForDisplay() { // Instance method: methodForDisplay + System.out.println(name+"'s Roll No: "+rollNo); // Displaying name and rollNo + } + + public static void main(String[] args) { + Student obj1=new Student("Rambo",21); // Creating a Student object (obj1) with name "Rambo" and rollNo 21 + obj1.methodForDisplay(); // Calling methodForDisplay for obj1 + } } ``` @@ -37,6 +35,9 @@ class Student{ * 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. + +Static methods are associated with the class itself and can be called using the class name, while public methods are accessible from outside the class and are typically used to define the behavior of objects and access their state. + ## 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/Dog.java b/src/Dog.java new file mode 100644 index 0000000..9c626c5 --- /dev/null +++ b/src/Dog.java @@ -0,0 +1,108 @@ +class Dog { + private String name; + private int age; + private String breed; + private String size; + private double weight; + private String colour; + + public Dog(String name, int age, String breed, String size, double weight, String colour) { + this.name = name; + this.age = age; + this.breed = breed; + this.size = size; + this.weight = weight; + this.colour = colour; + } + + public void dogBio() { + System.out.println(name + " is a " + size + " sized " + colour + " " + breed + ", who is currently " + age + " years old"); + } + + // update the size of the dog based on weight. 1-9 small, 10-19 medium, 20+ large + public void updateSize() { + String lastSize = size; + if (weight < 10.0) { + size = "small"; + } else if (weight < 20.0) { + size = "medium"; + } else { + size = "large"; + } + // print update if size changes + if (!(size.equals(lastSize))) { + System.out.println(name + " is now a " + size + " dog"); + } + } + + // apply given number as a change in weight, negative numbers are subtractive. + // we only change weight if dog is over 5 pounds and under 50 to prevent unrealistic circumstances. + public void changeWeight(double caloricPounds) { + if (weight >= 50.0) { + System.out.println(name + " is overweight!"); + } + else if (weight >= 5.0) { + weight += caloricPounds; + updateSize(); + } else { + System.out.println(name + " is underweight!"); + } + } + + public void eat(Food food) { + System.out.println(name + " is eating " + food.name); + if (food.toxicityToDogs) { + System.out.println(food.name + " was toxic, " + name + " has died."); + die(); + } else { + changeWeight(food.caloricPounds); + } + } + + // I don't know how to delete an object in java + public void die() { + // this = null; does not work, I would have to pass the object reference through eat() and to die() and set that to null. + // this was just an extra bit for fun, I am going to end the assignment here. + } + + public void run() { + System.out.println(name + " is running"); + + // we run changeWeight to update weight loss from running. Here they lose 1 pound. + // we could take in distance and use that to decide how much weight they lose. + changeWeight(-1); + } + + + public void sleep() { + System.out.println(name + " is sleeping"); + } + + public void setName(String newName) { + System.out.print(name); + this.name = newName; + System.out.println("'s name has been changed to: " + newName); + } + + public static void main(String[] args) { + Dog dog1 = new Dog("Buddy", 5, "Bulldog", "medium", 19.5, "light gray"); + Dog dog2 = new Dog("Max", 6, "Beagle", "medium", 10.0, "orange"); + Dog dog3 = new Dog("Bella", 6, "German Shepherd", "large", 25.0, "white & orange"); + + Food chocolate = new Food("Chocolate", 0.1, true); // Toxic to dogs + Food beef = new Food("Beef", 0.5, false); + Food dogFood = new Food("Dog Food", 0.4, false); + + dog1.dogBio(); + dog2.dogBio(); + dog3.dogBio(); + + dog1.eat(beef); + dog2.run(); + dog3.eat(dogFood); + dog3.sleep(); + + dog1.setName("Charlie"); + dog1.eat(chocolate); + } +} diff --git a/src/Food.java b/src/Food.java new file mode 100644 index 0000000..1e16287 --- /dev/null +++ b/src/Food.java @@ -0,0 +1,29 @@ +public class Food { + public String name; + public double caloricPounds; + public boolean toxicityToDogs; + + public Food(String name, double caloricPounds, boolean toxicityToDogs) { + this.name = name; + this.caloricPounds = caloricPounds; + this.toxicityToDogs = toxicityToDogs; + } + + public static void main(String[] args) { + Food chocolate = new Food("Chocolate", 0.1, true); // Toxic to dogs + Food beef = new Food("Beef", 0.5, false); + Food dogFood = new Food("Dog Food", 0.4, false); + + // Display information about the foods + System.out.println("Foods:"); + System.out.println(chocolate.toString()); + System.out.println(beef.toString()); + System.out.println(dogFood.toString()); + } + + @Override + public String toString() { + return "Name: " + name + ", Caloric Pounds: " + caloricPounds + + ", Toxicity to Dogs: " + (toxicityToDogs ? "Yes" : "No"); + } +}