From 3d4e3f8181db52705c0ccd61af5cf07a20447036 Mon Sep 17 00:00:00 2001 From: diego Date: Sun, 12 May 2024 23:54:42 -0700 Subject: [PATCH] I finished assignment-10, I had trouble compiling the code when changing SDK version. --- .idea/Java-Assignment-010.iml | 1 + .idea/kotlinc.xml | 6 +++++ .idea/misc.xml | 2 +- README.md | 18 ++++++++++---- src/Dog.java | 44 +++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 .idea/kotlinc.xml create mode 100644 src/Dog.java diff --git a/.idea/Java-Assignment-010.iml b/.idea/Java-Assignment-010.iml index c90834f..245d342 100644 --- a/.idea/Java-Assignment-010.iml +++ b/.idea/Java-Assignment-010.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..0dd4b35 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..2928d94 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..f97869c 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{ + //instance variables named 'name' and it is a String data type private String name; + //instance variable named 'rollNo' with an integer data type private int rollNo; - + //Constructor named 'Student' with parameter's 's' a String type variable and 'r' an integer Student(String s, int r) { + //variable name gets set to parameter of s name = s; + //variable rollNo gets set to parameter of r rollNo = r; } - + //Instant method named 'methodForDisplay' void methodForDisplay() { System.out.println(name+"'s Roll No: "+rollNo); } - public static void main(String[] args) { + public static void main(String[] args) {// + // Create object Student Student obj1=new Student("Rambo",21); + // This initiate the settings of the variables obj1.methodForDisplay(); } } @@ -36,7 +43,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. - +``` +static methods can be accesed by using the class name and method +, while public instance methods require you to acces them via an object of the 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..92509e1 --- /dev/null +++ b/src/Dog.java @@ -0,0 +1,44 @@ +public class Dog { + private final String breed; + private final String size; + private final String color; + private final int age; + private final String name; + + public Dog(String breed, String size, String color, int age, String name) { + this.breed = breed; + this.size = size; + this.color = color; + this.age = age; + this.name = name; + } + + public void eat() { + System.out.println(name + ": is eating."); + } + + public void run() { + System.out.println(name + ": is running."); + } + + public void sleep() { + System.out.println(name + ": is now sleeping, ZZZZZzzzz"); + for (int i = 1; i <= 3; i++) { + System.out.println(i); + } + System.out.println(name + ": is awake now!"); + } + + public String getName() { + return name; + } + + public static void main(String[] args) { + Dog dog1 = new Dog("Bulldog", "large", "light gray", 5, "Buddy"); + Dog dog2 = new Dog("Beagle", "large", "orange", 6, "Max"); + Dog dog3 = new Dog("German Shepherd", "large", "white & orange", 6, "Rocky"); + dog1.eat(); + dog2.run(); + dog3.sleep(); + } +} \ No newline at end of file