From 18a22c0d6af54196ae0fb9481f28c5c8918530be Mon Sep 17 00:00:00 2001 From: jcheong641 Date: Sun, 14 Apr 2024 15:17:22 -0700 Subject: [PATCH] Completed Assignment 10 --- .idea/misc.xml | 2 +- Dog.java | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 21 ++++++++++++--------- 3 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 Dog.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..47478b9 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..b9599bb --- /dev/null +++ b/Dog.java @@ -0,0 +1,48 @@ +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 dog is eating."); +} +public void run() { + System.out.println("The dog is running."); +} + +public void sleep() { + System.out.println("The dog is sleeping."); +} + public void name() + System.out.println("The dog's name is: "); +} +public String toString() { + return "Breed: " + breed + "\nSize: " + size + "\nColor: " + color + "\nAge: " + age; +} +public static void main(String[] args) { + Dog dog1 = new Dog("Bulldog", "large", "light gray", 5); + Dog dog2 = new Dog("Beagle", "large", "orange", 6); + Dog dog3 = new Dog("German Shepherd", "large", "white & orange", 6); + + System.out.println("Dog 1:"); + System.out.println(dog1.toString()); + + System.out.println("\nDog 2:"); + System.out.println(dog2.toString()); + + System.out.println("\nDog 3:"); + System.out.println(dog3.toString()); + + dog1.run(); + dog3.eat(); + } +} \ No newline at end of file diff --git a/README.md b/README.md index 833cd8b..7f13748 100644 --- a/README.md +++ b/README.md @@ -10,24 +10,25 @@ 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; //name of student + private int rollNo; //roll number of student + //constructor Student(String s, int r) { - name = s; - rollNo = r; + name = s; //constructor parameters: String s (name) + rollNo = r; //constructor parameters: int r (roll number) } - 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); - obj1.methodForDisplay(); + public static void main(String[] args) { //Student object gets created + Student obj1=new Student("Rambo",21); // object with name Rambo with roll number 21 + obj1.methodForDisplay(); //displays student's name and roll number } } ``` @@ -37,6 +38,8 @@ 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. +The difference between static and public methods is that with a static method it is not necessary to make an object in the class in order to be accessed. With a public method, objects are necessary in order to be accessed. Static refers to the method that is defined for the class itself. Public refers to instances of a 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.