From b6dc40945f8f2813dc29a28a9d285753cb0fca18 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 14 Apr 2024 22:29:17 -0700 Subject: [PATCH] Commit --- .idea/misc.xml | 2 +- README.md | 29 ++++++++++++++++++----------- images/Dog.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 images/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/README.md b/README.md index 833cd8b..ef1fe1f 100644 --- a/README.md +++ b/README.md @@ -10,24 +10,27 @@ 6. All the instance methods for the class **Student** ```java -class Student{ - private String name; - private int rollNo; +class Student{ //class name = Student + //instance variables/fields + private String name; //sting variable that stores students name + private int rollNo; //int variable that stores students roll number - Student(String s, int r) + //Constructor and the Constructor Parameters + Student(String s, int r) //constructor with parameters { - name = s; - rollNo = r; + name = s; //setter name + rollNo = r; //setter roll number } - - void methodForDisplay() + //Instance method for the class Student + void methodForDisplay() //method that displays students details { System.out.println(name+"'s Roll No: "+rollNo); } - + // student object is created public static void main(String[] args) { - Student obj1=new Student("Rambo",21); - obj1.methodForDisplay(); + Student obj1=new Student("Rambo",21); // creating student object + //the instance variables get set and where its values are + obj1.methodForDisplay(); //values assigned with the object while being created. Method that gets called to display students details. } } ``` @@ -37,6 +40,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. +A static method is a method that can be accessed without creating an object of the class. + +Public method can only be accessed by objects. + ## 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/images/Dog.java b/images/Dog.java new file mode 100644 index 0000000..809e2c3 --- /dev/null +++ b/images/Dog.java @@ -0,0 +1,44 @@ + +public class Dog { + + private String name; + private String breed; + private in age; + private String size; + private boolean isHungry; + + Dog(String name, String breed, int age, String size) { + this.name = name; + this.breed = breed; + this.age = age; + this.size = size; + this.isHungry = true; + } + + void eat(String food) { + System.out.println(name + " is eating " + food + "."); + isHungry = false; + } + + void run() { + System.out.println(name + " is running."); + } + + void sleep() { + System.out.println(name + " is sleeping."); + } + String getName() { + return name; + } + + public static void main(String[] args) { + Dog dog1 = new Dog("Ripjaw", "Bulldog", 5, "Large"); + Dog dog2 = new Dog("Updog", "Beagle", 6, "Large"); + Dog dog3 = new Dog("Billy", "German Shepherd", 6, "Large"); + + dog1.eat("Dog Food"); + dog2.run(); + dog3.sleep(); + } + +}