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..beadd7d 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/Main/Main.iml b/Main/Main.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Main/Main.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Main/src/Dog.java b/Main/src/Dog.java new file mode 100644 index 0000000..8f1faf8 --- /dev/null +++ b/Main/src/Dog.java @@ -0,0 +1,53 @@ +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() { + System.out.println("The " + breed + " is eating."); + } + + public void run() { + System.out.println("The " + breed + " is running."); + } + + public void sleep() { + System.out.println("The " + breed + " is sleeping."); + } + + public String name() { + return breed; + } + + public static void main(String[] args) { + + Dog Dog1Object = new Dog("Bulldog", "large", "light gray", 5); + Dog Dog2Object = new Dog("Beagle", "large", "orange", 6); + Dog Dog3Object = new Dog("German Shepherd", "large", "white & orange", 6); + + // Testing the methods for Dog1Object + System.out.println("Dog1: " + Dog1Object.name()); + Dog1Object.eat(); + Dog1Object.run(); + Dog1Object.sleep(); + + System.out.println("Dog2: " + Dog2Object.name()); + Dog2Object.eat(); + Dog2Object.run(); + Dog2Object.sleep(); + + System.out.println("Dog3: " + Dog3Object.name()); + Dog3Object.eat(); + Dog3Object.run(); + Dog3Object.sleep(); + } +} diff --git a/README.md b/README.md index 833cd8b..b090dc5 100644 --- a/README.md +++ b/README.md @@ -10,33 +10,47 @@ 6. All the instance methods for the class **Student** ```java +// Class name: Student 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(); - } + // Instance variables/fields: + // - name: String + private String name; + // - rollNo: int + private final int rollNo; + + // Constructor: initializes name and rollNo + // Constructor Parameters: + // - s: String (for name) + // - r: int (for rollNo) + Student(String s, int r) + { + name = s; // Setting name to the value of s + rollNo = r; // Setting rollNo to the value of r + } + + // Instance method for displaying student's name and roll number + void methodForDisplay() + { + System.out.println(name+"'s Roll No: "+rollNo); + } + + public static void main(String[] args) { + // Creating a Student object: + // - name: "Rambo" + // - rollNo: 21 + Student obj1=new Student("Rambo",21); + // Calling the instance method to display student information + 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. - +Public methods can only be accessed my objects, whereas static methods can be accessed without creating an object of the class ## 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.