From 06f9d3df5c340db4749781e1175990f9729d2604 Mon Sep 17 00:00:00 2001 From: quinnmckay Date: Fri, 12 Apr 2024 13:47:41 -0700 Subject: [PATCH] Completed Java Assignment 10 --- .idea/misc.xml | 2 +- .idea/modules.xml | 1 + README.md | 11 ++++++ source code/source code.iml | 11 ++++++ src/src/Dog.java | 76 +++++++++++++++++++++++++++++++++++++ src/src/Food.java | 27 +++++++++++++ src/src/dogOwner.java | 18 +++++++++ src/untitled.iml | 11 ++++++ 8 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 source code/source code.iml create mode 100644 src/src/Dog.java create mode 100644 src/src/Food.java create mode 100644 src/src/dogOwner.java create mode 100644 src/untitled.iml 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/.idea/modules.xml b/.idea/modules.xml index bdef184..3438287 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/README.md b/README.md index 833cd8b..55e36eb 100644 --- a/README.md +++ b/README.md @@ -10,23 +10,30 @@ 6. All the instance methods for the class **Student** ```java +//class name is Student class Student{ + //Two instance variables, String name and int rollNo private String name; private int rollNo; + //Constructor Student, parameters are String s and int r Student(String s, int r) { + //Instance variable values are set here for both name = s, and rollNo = r name = s; rollNo = r; } + //Instance method here for displaying the Student obj1 void methodForDisplay() { System.out.println(name+"'s Roll No: "+rollNo); } public static void main(String[] args) { + //Student Object is created here Student obj1=new Student("Rambo",21); + //Instance method methodForDisplay is called in order to display name and roll number obj1.methodForDisplay(); } } @@ -37,6 +44,10 @@ 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 main difference between a static and a public method are that static methods can be called without created objects while public classes must be called by creating objects. +// Not calling a method correctly would result in a compile error. +// It is important to use the correct format for calling the certain method in order to have the code function properly and give the desired output. + ## 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/source code/source code.iml b/source code/source code.iml new file mode 100644 index 0000000..cbbcb79 --- /dev/null +++ b/source code/source code.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/src/Dog.java b/src/src/Dog.java new file mode 100644 index 0000000..e1bc5b4 --- /dev/null +++ b/src/src/Dog.java @@ -0,0 +1,76 @@ +public class Dog { + + private String name; + private String breed; + private String size; + private double weight; + private String color; + private int age; + private dogOwner owner; + + Dog (String n, String b, String s, double w, String c, int a, dogOwner o){ + name = n; + breed = b; + size = s; + weight = w; + color = c; + age = a; + owner = o; + } + + void displayDog(){ + System.out.println("Name: " + name); + System.out.println("Breed: " + breed); + System.out.println("Size: " + size); + System.out.println("Weight: " + weight + " lbs"); + System.out.println("Color: " + color); + System.out.println("Age: " + age + " years"); + } + + public void eat(Food food){ + System.out.println(name + " is eating " + food.getWeight() + " grams of " + food.getName()); + food.consume(50); + } + + public void run(){ + System.out.println(name + " is running."); + } + + public void sleep(){ + System.out.println(name + " is sleeping"); + } + + public String theName(){ + return name; + } + + public dogOwner getOwner() { + return owner; + } + + public static void main(String[] args){ + dogOwner owner1 = new dogOwner ("Lebron James", "123-456-7890"); + dogOwner owner2 = new dogOwner ("Shaquille O'neil", "111-111-1111"); + dogOwner owner3 = new dogOwner ("Stephen Curry", "098-765-4321"); + + Dog dog1 = new Dog ("Max","Bulldog", "Large", 45.5, "Light Gray", 5, owner1); + Dog dog2 = new Dog ("Charlie", "Beagle", "Large", 35.0, "Orange", 6, owner2); + Dog dog3 = new Dog ("Ace", "German Shepherd", "Large", 85.0, "White and Orange", 6, owner3); + + Food kibble = new Food ("Dog Kibble", 200); + + + + dog1.displayDog(); + dog1.eat(kibble); + System.out.println("Owner: " + dog1.getOwner().getName() + ", Phone Number: " + dog1.getOwner().getContactNumber()); + System.out.println(); + dog2.displayDog(); + dog2.eat(kibble); + System.out.println("Owner: " + dog2.getOwner().getName() + ", Phone Number: " + dog2.getOwner().getContactNumber()); + System.out.println(); + dog3.displayDog(); + dog3.eat(kibble); + System.out.println("Owner: " + dog3.getOwner().getName() + ", Phone Number: " + dog3.getOwner().getContactNumber()); + } +} diff --git a/src/src/Food.java b/src/src/Food.java new file mode 100644 index 0000000..ace7c10 --- /dev/null +++ b/src/src/Food.java @@ -0,0 +1,27 @@ +public class Food { + private String name; + private double weight; + + public Food(String name, double weight) { + this.name = name; + this.weight = weight; + } + + public String getName(){ + return name; + } + + public double getWeight() { + return weight; + } + + public void consume(double amount) { + if (amount > 0 && amount <= weight) { + weight -= amount; + System.out.println(name + " consumed. Remaining weight: " + weight + " grams."); + } else { + System.out.println("Invalid amount to consume."); + } + } +} + diff --git a/src/src/dogOwner.java b/src/src/dogOwner.java new file mode 100644 index 0000000..f0bcc87 --- /dev/null +++ b/src/src/dogOwner.java @@ -0,0 +1,18 @@ +public class dogOwner { + + private String name; + private String contactNumber; + + public dogOwner(String ownerName, String ownerNumber){ + name = ownerName; + contactNumber = ownerNumber; + } + + public String getName() { + return name; + } + + public String getContactNumber() { + return contactNumber; + } +} diff --git a/src/untitled.iml b/src/untitled.iml new file mode 100644 index 0000000..da0bce3 --- /dev/null +++ b/src/untitled.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file