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..a095f92 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,14 @@ ```java class Student{ +// class name private String name; private int rollNo; - - Student(String s, int r) - { - name = s; - rollNo = r; + //instance variables name (String) and rollNo (int) + Student(String s, int r) //constructor Student + { //parameters + name = s; //instance variable 1 (s) + rollNo = r; //instance variable 2 (r) } void methodForDisplay() @@ -26,16 +27,14 @@ class Student{ } public static void main(String[] args) { - Student obj1=new Student("Rambo",21); + Student obj1=new Student("Rambo",21);// Object 1 created, values set obj1.methodForDisplay(); } } ``` - -## 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. +Static can be called directly using the class name, without creating an object of the class. On the other hand, a public method is a method that can be accessed from anywhere, +regardless of whether it's called from within the class or from outside the class. This can be useful or detrimental to your project, depending on how you use it. +You must also create an object. ## Part 3 - Dogs diff --git a/src/Dog.java b/src/Dog.java new file mode 100644 index 0000000..6306fe3 --- /dev/null +++ b/src/Dog.java @@ -0,0 +1,47 @@ +import java.util.Scanner; + +public class Dog { + + private String name; + private int age; + private String breed; + private String size; + private String color; + + public Dog(String name, int age, String breed, String size, String color) { + this.name = name; + this.age = age; + this.breed = breed; + this.size = size; + this.color = color; + } + + // method to change the dog's name + public void changeName() { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter the new name for the dog: "); + name = scanner.nextLine(); + System.out.println("The dog's name has been changed to " + name); + } + + public void eat(Food food) { + if (food.getWeight() >= 5) { + food.decreaseWeight(5); + System.out.println(name + " ate 5 pounds of food. The remaining weight of the food is " + food.getWeight() + " pounds."); + } else { + System.out.println("There is not enough food for " + name + " to eat."); + } + } + + public static void main(String[] args) { + Dog dog1 = new Dog("Buddy", 3, "Golden Retriever", "Large", "Golden"); + Dog dog2 = new Dog("Max", 5, "German Shepherd", "Medium", "Black and Tan"); + Dog dog3 = new Dog("Charlie", 2, "Labrador", "Small", "Black"); + + Food food = new Food(100); + + dog1.eat(food); + + System.out.println("The remaining weight of the food is " + food.getWeight() + " pounds."); + } +} \ No newline at end of file diff --git a/src/Food.java b/src/Food.java new file mode 100644 index 0000000..fde895d --- /dev/null +++ b/src/Food.java @@ -0,0 +1,15 @@ +public class Food { + + private int weight; + + public Food(int weight) { + this.weight = weight; + } + + public int getWeight() { + return weight; + } + + public void decreaseWeight(int amount) { + weight -= amount; + }} \ No newline at end of file