From 5320803b34d2e1b255da5f12e04458b64d42f8b9 Mon Sep 17 00:00:00 2001 From: crucial707 Date: Wed, 10 Apr 2024 13:33:03 -0700 Subject: [PATCH] Java-Assignment-010 Austin Barnett --- .idea/misc.xml | 2 +- README.md | 32 ++++++++++++++++-------- src/Dog.java | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 src/Dog.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..1b2d693 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..d80d5a6 100644 --- a/README.md +++ b/README.md @@ -10,24 +10,34 @@ 6. All the instance methods for the class **Student** ```java +//1. Class name class Student{ - private String name; - private int rollNo; - - Student(String s, int r) + //2. All instance variables/fields and their data-types + private String name; // Instance variable name of type String + private int rollNo; // Instance variable rollNo of type int + //3. The Constructor and the Constructor Parameters + Student(String s, int r) //3. Constructor with parameters s and r { - name = s; - rollNo = r; + name = s; // Assigning value of s to name + rollNo = r; // Assigning value of r to rollNo + } + { //5. Where the instance variables value gets set and what its values are. + name = s; // Setting the value of name to s + rollNo = r; // Setting the value of rollNo to r + } + //6. All the instance methods for the class Student + void methodForDisplay() // Instance method methodForDisplay + { + System.out.println(name+"'s Roll No: "+rollNo); // Printing name and rollNo } - - void methodForDisplay() { System.out.println(name+"'s Roll No: "+rollNo); } public static void main(String[] args) { - Student obj1=new Student("Rambo",21); - obj1.methodForDisplay(); + //4. Where a Student object gets created. + Student obj1=new Student("Rambo",21); // Creating object obj1 of class Student + obj1.methodForDisplay(); // Calling method methodForDisplay } } ``` @@ -36,7 +46,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 are methods that belong to the class itself, and not to the object of the class. They can be called without creating an instance of the class. Public methods are methods that can be accessed by any other class. They are accessible from any other class, and can be called from any other 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. diff --git a/src/Dog.java b/src/Dog.java new file mode 100644 index 0000000..c6cbf33 --- /dev/null +++ b/src/Dog.java @@ -0,0 +1,68 @@ +// Written By: Austin Barnett +// Date: 4/10/2024 +// Version 1.0 +public class Dog { + // Private instance variables/fields + private String name; + private int age; + private double weight; + + public Dog(String name, int age, double weight) { + this.name = name; + this.age = age; + this.weight = weight; + } + + public void eat(Food food) { + System.out.println(name + " is eating."); + weight += food.getWeight(); + food.setWeight(0); + } + + + public void run() { + System.out.println(name + " is running."); + + weight -= 0.5; + } + + + public void sleep() { + System.out.println(name + " is sleeping."); + } + + + public String getName() { + return name; + } + + + public static void main(String[] args) { + Dog dog1 = new Dog("Buddy", 3, 20.0); + Dog dog2 = new Dog("Charlie", 1, 10.0); + Dog dog3 = new Dog("Max", 5, 25.0); + + + Food dogFood = new Food(5.0); + dog1.eat(dogFood); + dog2.run(); + dog3.sleep(); + } +} + + +class Food { + private double weight; + + public Food(double weight) { + this.weight = weight; + } + + public double getWeight() { + return weight; + } + + public void setWeight(double weight) { + this.weight = weight; + } +}