Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@
6. All the instance methods for the class **Student**

```java
class Student{
private String name;
private int rollNo;
class Student{ //Class name
private String name; // instance variable - type: String
private int rollNo; // instance variable - type: int

Student(String s, int r)
//Constructor
Student(String s, int r) // Constructor Parameters: String: s, int: r
{
name = s;
rollNo = r;
name = s; // sets instance variable 'name' to value of parameter 's'
rollNo = r; // sets instance variable 'rollNo' to value of parameter 'r'
}

void methodForDisplay()
void methodForDisplay() // instance method
{
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); /* Student object gets created,
instance variable values set by parameters passed to Student constructor
*/
obj1.methodForDisplay();
}
}
Expand All @@ -37,6 +40,14 @@ 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.

```text
As it relates to a class, a static method can be called independently from
an object and can be called without an object being created. A public
method, however, can only be accessed by an object of that class. In order
to access a public method, an object must first be created and then used
to access the method.
```

## 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.
Expand Down
58 changes: 58 additions & 0 deletions src/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.util.*;
public class Dog {
private String breed;
private String size;
private String color;
private int age;
private String name;

public Dog(String breed, String size, String color, int age) {
this.breed = breed;
this.size = size;
this.color = color;
this.age = age;
}

public void eat() {
if(Objects.equals(this.size, "large")) {
this.size = "extra large";
} else if (Objects.equals(this.size, "medium")) {
this.size = "large";
} else if (Objects.equals(this.size, "small")) {
this.size = "medium";
}
}

public void run() {
System.out.println(this.name + " is running around. They are now tired and could use some sleep.");
}

public void sleep() {
if (this.name == null) {
System.out.println(this + " is sleeping...zzz...zzz...zzz...");
} else {
System.out.println(this.name + " is sleeping...zzz...zzz...zzz...");
}
}

public void name(String name) {
this.name = name;
}


public static void main(String[] args) {
Dog dog1 = new Dog("Bulldog", "large", "light grey", 5);
Dog dog2 = new Dog("Beagle", "large", "orange", 6);
Dog dog3 = new Dog("German Shepherd", "large", "white & orange", 6);

System.out.println(dog1.toString());
dog1.name("Bully");
System.out.println(dog1.name.toString());
dog2.sleep();
dog1.sleep();
dog1.run();
dog1.eat();
System.out.println(dog1.size.toString());

}
}