From b36f6b77dc10361275ba82c1f391bdbf46d8f618 Mon Sep 17 00:00:00 2001 From: nstine77 Date: Sun, 14 Apr 2024 17:26:43 -0700 Subject: [PATCH] minor warnings and I'm pretty sure redundant strings, but I'm not seeing the right solutions yet. --- .idea/.name | 1 + .idea/Java-Assignment-010.iml | 2 +- .idea/misc.xml | 2 +- README.md | 20 ++++---- src/Dog.java | 94 +++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 .idea/.name create mode 100644 src/Dog.java diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..129655d --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Dog \ No newline at end of file diff --git a/.idea/Java-Assignment-010.iml b/.idea/Java-Assignment-010.iml index c90834f..82236ea 100644 --- a/.idea/Java-Assignment-010.iml +++ b/.idea/Java-Assignment-010.iml @@ -5,7 +5,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..d11ee85 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..3ef0ee0 100644 --- a/README.md +++ b/README.md @@ -10,23 +10,23 @@ 6. All the instance methods for the class **Student** ```java -class Student{ - private String name; - private int rollNo; +class Student{ // Class name is Student + private String name; // String type instance variable + private int rollNo; // Int data type instance variable - Student(String s, int r) + Student(String s, int r) // Constructor, where the instance variables get set { - name = s; - rollNo = r; + name = s; // Constructor instance variable String for student name + rollNo = r; // Constructor instance variable int for student roll call ID } - void methodForDisplay() + void methodForDisplay() // The only instance method, shows student name and number { System.out.println(name+"'s Roll No: "+rollNo); } public static void main(String[] args) { - Student obj1=new Student("Rambo",21); + Student obj1=new Student("Rambo",21); // Here's the student object obj1.methodForDisplay(); } } @@ -36,7 +36,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. - + +* Answer - A static method can be accessed without creating an object of the class. I'm unsure, but I think a public method can ONLY be accessed by 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. diff --git a/src/Dog.java b/src/Dog.java new file mode 100644 index 0000000..0fd1506 --- /dev/null +++ b/src/Dog.java @@ -0,0 +1,94 @@ +import java.util.Scanner; + +public class Dog { + private String name; + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Collecting dog names + System.out.println("Enter name for Dog 1:"); + String nameOfDog1 = scanner.nextLine(); + System.out.println("Enter name for Dog 2:"); + String nameOfDog2 = scanner.nextLine(); + System.out.println("Enter name for Dog 3:"); + String nameOfDog3 = scanner.nextLine(); + + // Dog objects with details from the image + String breed1 = "Bulldog"; + String size1 = "Large"; + String color1 = "Light grey"; + int age1 = 5; + + String breed2 = "Beagle"; + String size2 = "Large"; + String color2 = "Orange"; + int age2 = 6; + + String breed3 = "German Shepherd"; + String size3 = "Large"; + String color3 = "White & Orange"; + int age3 = 6; + + Dog dog1 = new Dog(nameOfDog1, breed1, size1, color1, age1); + Dog dog2 = new Dog(nameOfDog2, breed2, size2, color2, age2); + Dog dog3 = new Dog(nameOfDog3, breed3, size3, color3, age3); + + // The dogs have to eat, right? + Food food = new Food(40); + + // These strings seem like the easy way to work with the methods required. + dog1.eat(food); + dog1.run(); + dog1.sleep(); + + dog2.eat(food); + dog2.run(); + dog2.sleep(); + + dog3.eat(food); + dog3.run(); + dog3.sleep(); + } + + public Dog(String name, String breed, String size, String color, int age) { + this.name = name; + } + + public void eat(Food food) { + food.setWeight(food.getWeight() - 1); + String message = this.getName() + " is eating."; + System.out.println(message); + } + + public void run() { + System.out.println(this.getName() + " is running!"); + } + + public void sleep() { + System.out.println(this.getName() + " is sleeping soundly."); + } + + public String getName() { + return this.name; + } + + public static class Food { + private int weight; + + public Food(int weight) { + this.weight = weight; + } + + // Setter method to modify weight + public void setWeight(int newWeight) { + this.weight = newWeight; + } + + // Getter method for biscuits remaining + public int getWeight() { + return this.weight; + + } + } +} \ No newline at end of file