From 7200bfe76ea4a01f07a38c589ad699f50475dbbb Mon Sep 17 00:00:00 2001 From: Brody Date: Sun, 14 Apr 2024 23:52:16 -0700 Subject: [PATCH] assignment 010 --- Dogs.java | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 54 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 88 insertions(+), 19 deletions(-) create mode 100644 Dogs.java diff --git a/Dogs.java b/Dogs.java new file mode 100644 index 0000000..a1744a8 --- /dev/null +++ b/Dogs.java @@ -0,0 +1,53 @@ +class Dogs { + private String name; + private String breed; + private String size; + private String color; + private int age; + private int energy; + + Dogs(String name, String breed, String size, String color, int age, int energy) { + this.name = name; + this.breed = breed; + this.size = size; + this.color = color; + this.age = age; + this.energy = energy; + } + void Run() { + energy -= 10; + System.out.println(name + " is running and losing energy"); + } + void Sleep() { + energy += 20; + System.out.println(name + " is sleeping and gaining energy"); + } + void Eat(Food food) { + energy += food.getWeight(); + System.out.println(name + " is eating and gaining energy"); + } + String Name() { + return name; + } + public static void main(String[] args) { + Dogs dog1 = new Dogs("Red", "Bulldog", "large", "light gray", 5, 50); + Dogs dog2 = new Dogs("Wood", "Beagle", "large", "orange", 6, 60); + Dogs dog3 = new Dogs("Tater", "German Shepherd", "large", "white and orange", 6, 70); + dog1.Run(); + dog2.Sleep(); + dog3.Eat(new Food(15)); + System.out.println("Dog 1's name is: " + dog1.Name()); + System.out.println("Dog 2's name is: " + dog2.Name()); + System.out.println("Dog 3's name is: " + dog3.Name()); + } +} +class Food { + private int weight; + + public Food(int weight) { + this.weight = weight; + } + public int getWeight() { + return weight; + } +} \ No newline at end of file diff --git a/README.md b/README.md index 833cd8b..803d7fc 100644 --- a/README.md +++ b/README.md @@ -10,25 +10,36 @@ 6. All the instance methods for the class **Student** ```java -class Student{ - private String name; - private int rollNo; - - Student(String s, int r) - { - name = s; - rollNo = r; - } - - void methodForDisplay() - { - System.out.println(name+"'s Roll No: "+rollNo); - } - - public static void main(String[] args) { - Student obj1=new Student("Rambo",21); - obj1.methodForDisplay(); - } +// Class name +class Student { + // Instance variables/datatype name/String and rollno/No + private String name; + private int rollNo; + + // Constructor + // Constructor Parameters: + // - s: student's name data type: String + // - r: student's roll number data type: int + Student(String s, int r) { + // Setting instance variable name to the provided parameter s + name = s; + // Setting instance variable rollNo to the provided parameter r + rollNo = r; + } + + // Instance method for displaying the student's details + void methodForDisplay() { + // Print the student's name and roll number + System.out.println(name + "'s Roll No: " + rollNo); + } + + // Main method + public static void main(String[] args) { + // Creating a Student object with name "Rambo" and roll number 21 + Student obj1 = new Student("Rambo", 21); + // Calling methodForDisplay() + obj1.methodForDisplay(); + } } ``` @@ -37,6 +48,11 @@ 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. + +In a class, static methods are called on the class itself and are often used for tasks that involve the whole class or +use data that belongs to the class. Public methods are called on individual objects of the class and work with the object's +own data. Static methods are for class-wide operations, while public methods are for working with specific 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.