diff --git a/README.md b/README.md index 833cd8b..ba67de7 100644 --- a/README.md +++ b/README.md @@ -10,25 +10,29 @@ 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(); - } +public class Student{ + // Class name: Student + + // Instance variables/fields + private String name; // String variable to store the name of the student + private int rollNo; // Integer variable to store the roll number of the student + + // Constructor + Student(String s, int r) { + name = s; // Setting the name of the student + rollNo = r; // Setting the roll number of the student + } + + // Method for displaying student information + void methodForDisplay() { + System.out.println(name + "'s Roll No: " + rollNo); // Displaying the student's name and roll number + } + + public static void main(String[] args) { + // Creating a Student object + Student obj1 = new Student("Rambo", 21); + obj1.methodForDisplay(); // Calling the method to display student information + } } ``` @@ -36,6 +40,7 @@ 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 like class-wide functions that can be used without making an object, while public methods are actions that objects can do. Public methods can only be used with objects. ## Part 3 - Dogs diff --git a/src/Dog.java b/src/Dog.java new file mode 100644 index 0000000..57e4df3 --- /dev/null +++ b/src/Dog.java @@ -0,0 +1,52 @@ +/** + * @author Alexei Iachkov + * @date 4-14-24 + * @version 1.0 + */ +public class Dog { + private String breed; + private String size; + private String color; + private int age; + + public Dog(String breed, String size, String color, int age) { + this.breed = breed; + this.size = size; + this.color = color; + this.age = age; + } + + public void eat(Food food) { + System.out.println("The " + breed + " is eating."); + food.reduceWeight(); + } + + public void run() { + System.out.println("The " + breed + " is running."); + } + + public void sleep() { + System.out.println("The " + breed + " is sleeping."); + } + + public void name() { + System.out.println("The " + breed + "'s name is Doober."); + } + + public static void main(String[] args) { + Dog dog1 = new Dog("Bulldog", "large", "light gray", 5); + Dog dog2 = new Dog("Beagle", "medium", "orange", 7); + Dog dog3 = new Dog("German Shepherd", "large", "white and orange", 4); + + dog1.eat(new Food()); // Passing a Food object to the eat method + dog2.run(); + dog3.sleep(); + dog3.name(); + } +} + +class Food { + public void reduceWeight() { + System.out.println("Food weight reduced."); + } +}