From 6151389cab63888b2fae95933bb91424a3298840 Mon Sep 17 00:00:00 2001 From: CreeperKelten Date: Sun, 14 Apr 2024 23:40:54 -0700 Subject: [PATCH] Kelten Stowe --- .idea/misc.xml | 2 +- Dog.java | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 50 +++++++++++++++++++++++++++++++----------------- 3 files changed, 85 insertions(+), 19 deletions(-) create mode 100644 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/Dog.java b/Dog.java new file mode 100644 index 0000000..14a657b --- /dev/null +++ b/Dog.java @@ -0,0 +1,52 @@ +// Dog class +public class Dog { + // Private instance variables/fields + private String breed; + private String size; + private String color; + private int age; + + // Constructor to set the initial state of the data members via passed parameters + public Dog(String breed, String size, String color, int age) { + this.breed = breed; + this.size = size; + this.color = color; + this.age = age; + } + + // Instance method for eating + public void eat() { + System.out.println("The dog is eating."); + } + + // Instance method for running + public void run() { + System.out.println("The dog is running."); + } + + // Instance method for sleeping + public void sleep() { + System.out.println("The dog is sleeping."); + } + + // Instance method for getting the name of the dog + public void name() { + System.out.println("The dog's name is " + breed + "."); + } + + // Main method + public static void main(String[] args) { + // Creating three Dog objects + Dog dog1 = new Dog("Labrador", "Large", "Golden", 5); + Dog dog2 = new Dog("Poodle", "Medium", "White", 3); + Dog dog3 = new Dog("Bulldog", "Small", "Brown", 7); + + // Calling instance methods for each dog object + dog1.eat(); + dog2.run(); + dog3.sleep(); + dog1.name(); + dog2.name(); + dog3.name(); + } +} diff --git a/README.md b/README.md index 833cd8b..9afc5d5 100644 --- a/README.md +++ b/README.md @@ -10,25 +10,35 @@ 6. All the instance methods for the class **Student** ```java +// Class name 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 and their data-types + private String name; + private int rollNo; + + // Constructor and the Constructor Parameters + Student(String s, int r) + { + name = s; // Setting the value of the name instance variable + rollNo = r; // Setting the value of the rollNo instance variable + } + + // Instance method for displaying student's name and roll number + void methodForDisplay() + { + System.out.println(name+"'s Roll No: "+rollNo); // Displaying the name and roll number + } + + public static void main(String[] args) { + // Where a Student object gets created + Student obj1=new Student("Rambo",21); // Creating a Student object named obj1 + + // Where the instance variables value gets set and what its values are + // The values are set in the constructor when the Student object is created + // name = "Rambo" and rollNo = 21 + + obj1.methodForDisplay(); // Calling the methodForDisplay() method to display the student's details + } } ``` @@ -37,6 +47,10 @@ 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 rather than with instances of the class. They can be called directly using the class name, without needing to create an object of the class. Static methods are commonly used for utility functions or operations that do not require access to instance variables. + +On the other hand, public methods are instance methods that are associated with individual objects or instances of the class. They can access instance variables and are typically called using an object of the class. Public methods define the behavior of objects and can modify the state of an object's instance variables. + ## 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.