From b3e1939b26ef6f7309a84521e7358744222e67ee Mon Sep 17 00:00:00 2001 From: Tyler-FS Date: Sun, 14 Apr 2024 13:53:55 -0700 Subject: [PATCH] Completed Java Assignment 010 --- .idea/misc.xml | 2 +- README.md | 27 ++++++++++++++++------- src/Dog.java | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 src/Dog.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..8633114 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..0a0878c 100644 --- a/README.md +++ b/README.md @@ -10,23 +10,26 @@ 6. All the instance methods for the class **Student** ```java -class Student{ - private String name; - private int rollNo; +class Student{ //Class name + private String name; // instance variable - type: String + private int rollNo; // instance variable - type: int - Student(String s, int r) + //Constructor + Student(String s, int r) // Constructor Parameters: String: s, int: r { - name = s; - rollNo = r; + name = s; // sets instance variable 'name' to value of parameter 's' + rollNo = r; // sets instance variable 'rollNo' to value of parameter 'r' } - void methodForDisplay() + void methodForDisplay() // instance method { System.out.println(name+"'s Roll No: "+rollNo); } public static void main(String[] args) { - Student obj1=new Student("Rambo",21); + Student obj1=new Student("Rambo",21); /* Student object gets created, + instance variable values set by parameters passed to Student constructor + */ obj1.methodForDisplay(); } } @@ -37,6 +40,14 @@ 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. + ```text + As it relates to a class, a static method can be called independently from + an object and can be called without an object being created. A public + method, however, can only be accessed by an object of that class. In order + to access a public method, an object must first be created and then used + to access the method. + ``` + ## 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/src/Dog.java b/src/Dog.java new file mode 100644 index 0000000..4a24686 --- /dev/null +++ b/src/Dog.java @@ -0,0 +1,58 @@ +import java.util.*; +public class Dog { + private String breed; + private String size; + private String color; + private int age; + private String name; + + 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() { + if(Objects.equals(this.size, "large")) { + this.size = "extra large"; + } else if (Objects.equals(this.size, "medium")) { + this.size = "large"; + } else if (Objects.equals(this.size, "small")) { + this.size = "medium"; + } + } + + public void run() { + System.out.println(this.name + " is running around. They are now tired and could use some sleep."); + } + + public void sleep() { + if (this.name == null) { + System.out.println(this + " is sleeping...zzz...zzz...zzz..."); + } else { + System.out.println(this.name + " is sleeping...zzz...zzz...zzz..."); + } + } + + public void name(String name) { + this.name = name; + } + + + public static void main(String[] args) { + Dog dog1 = new Dog("Bulldog", "large", "light grey", 5); + Dog dog2 = new Dog("Beagle", "large", "orange", 6); + Dog dog3 = new Dog("German Shepherd", "large", "white & orange", 6); + + System.out.println(dog1.toString()); + dog1.name("Bully"); + System.out.println(dog1.name.toString()); + dog2.sleep(); + dog1.sleep(); + dog1.run(); + dog1.eat(); + System.out.println(dog1.size.toString()); + + } +}