From 6b8702ea2bbf9800e31d18a4ef6d5d7c1f14162d Mon Sep 17 00:00:00 2001 From: tpedersen42 Date: Fri, 12 Apr 2024 17:27:39 -0700 Subject: [PATCH] Done with Assignment 10 --- .idea/Dog.java | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ .idea/misc.xml | 2 +- README.md | 27 ++++++++++++------- 3 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 .idea/Dog.java diff --git a/.idea/Dog.java b/.idea/Dog.java new file mode 100644 index 0000000..97293be --- /dev/null +++ b/.idea/Dog.java @@ -0,0 +1,73 @@ +public class Dog { + //private instance variables/fields for all data members + private String breed; + private String size; + private String color; + private int age; + + //constructor to set initial state of data members + 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("dog is eating."); + + } + + //instance method for running + public void run() { + System.out.println("dog is running."); + } + + //instacne methof for sleeping + public void sleep() { + System.out.println("dog is sleeping."); + } + + //instacne method for getting name of dog + public String getName() { + return breed; //assuming breed is the name + } + + //main to creat 3 dog objects + public static void main(String[] args) { + Dog dog1 = new Dog("Bulldog", "large", "light gray", 5); + System.out.println("Dog 1:"); + System.out.println("name: " + dog1.getName()); + System.out.println("size: " + dog1.size); + System.out.println("color: " + dog1.color); + System.out.println("age: " + dog1.age); + dog1.eat(); + dog1.run(); + dog1.sleep(); + + //dog2object + Dog dog2 = new Dog("Beagle", "large", "orange", 6); + System.out.println("Dog 2:"); + System.out.println("name: " + dog2.getName()); + System.out.println("size: " + dog2.size); + System.out.println("color: " + dog2.color); + System.out.println("age: " + dog2.age); + dog2.eat(); + dog2.run(); + dog2.sleep(); + + //dog3object + Dog dog3 = new Dog("german sheoherd", "large", "white and orange", 6); + System.out.println("Dog 3:"); + System.out.println("name: " + dog3.getName()); + System.out.println("size: " + dog3.size); + System.out.println("color: " + dog3.color); + System.out.println("age: " + dog3.age); + dog3.eat(); + dog3.run(); + dog3.sleep(); + + } +} 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..17f6986 100644 --- a/README.md +++ b/README.md @@ -10,23 +10,28 @@ 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 and data types + private String name;// String type variable to store students name + private int rollNo;// integer type variable to store student roll number + + //constructor and constructor parameters + //constructor with parameters to initialize name and rollNo Student(String s, int r) { - name = s; - rollNo = r; + name = s; //set name to vale of paremeter s + rollNo = r;// set rollNo to value of parameter r } - + //instance method for displaying students name and roll number void methodForDisplay() - { + { //display student name and roll number System.out.println(name+"'s Roll No: "+rollNo); } - + //main method where student object is created public static void main(String[] args) { + //create new student object named obj1 with name rambo and roll number 21 Student obj1=new Student("Rambo",21); + //call the methodForDisplay to display students name and roll number obj1.methodForDisplay(); } } @@ -36,7 +41,9 @@ 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. - + - Simply put, Static methods can be accessed without creating an object of the class where public methods can only be accessed by objects. + - Public methods define the behavior of the objects created from the class and can access instance variables and other instance methods of the class. + - Static methods belong to the class itself rather than a specific instance. ## 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.