From 691af26605d9bdf110c017b8363718cfc3e28516 Mon Sep 17 00:00:00 2001 From: Juanc Date: Sun, 14 Apr 2024 23:21:25 -0700 Subject: [PATCH] Completed Assignment 010 --- Dog.java | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 18 ++++++++-------- 2 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 Dog.java diff --git a/Dog.java b/Dog.java new file mode 100644 index 0000000..fe1e2cd --- /dev/null +++ b/Dog.java @@ -0,0 +1,61 @@ +public class Dog { + + private String breed; + private String size; + private String color; + private int age; + private String name; + + private boolean isHungry; + + Dog(String breed, String size, String color, int age) { + this.breed = breed; + this.size = size; + this.color = color; + this.age = age; + this.isHungry = true; + } + + void setName(String name) { + this.name = name; + } + + String getName() { + return name; + } + + void eat() { + if (isHungry) { + System.out.println(getName() + " is eating."); + isHungry = false; + } else { + System.out.println(getName() + " is not hungry right now."); + } + } + + void run() { + System.out.println(getName() + " is running."); + isHungry = true; + } + + + void sleep() { + System.out.println(getName() + " is sleeping."); + } + + public static void main(String[] args) { + + Dog bulldog = new Dog("Bulldog", "Large", "Light Grey", 5); + bulldog.setName("Buddy"); + + Dog beagle = new Dog("Beagle", "Large", "Orange", 6); + beagle.setName("Max"); + + Dog germanShepherd = new Dog("German Shepherd", "Large", "White and Orange", 6); + germanShepherd.setName("Rocky"); + + bulldog.eat(); + beagle.run(); + germanShepherd.sleep(); + } +} diff --git a/README.md b/README.md index 833cd8b..e5efdf0 100644 --- a/README.md +++ b/README.md @@ -10,22 +10,24 @@ 6. All the instance methods for the class **Student** ```java -class Student{ - private String name; +class Student{ //class name (student) + private String name; private int rollNo; - + //Instance variables with their data types Student(String s, int r) - { - name = s; - rollNo = r; + { + //Constructor and Constructor Parameters: S: string for students name and r: int for rollNo + name = s; //The name instance variable + rollNo = r;//The rollNo instance variable } - + //All the instance methods for the class student void methodForDisplay() { System.out.println(name+"'s Roll No: "+rollNo); } public static void main(String[] args) { + //Where Student gets created Student obj1=new Student("Rambo",21); obj1.methodForDisplay(); } @@ -36,7 +38,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 belong to the class and can be called using the class name without creating an object, while public methods belong to objects and require an instance of the class to be called. ## 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.