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.

52 changes: 52 additions & 0 deletions Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Dog class
public class Dog {
// Private instance variables/fields
private String breed;
private String size;
private String color;
private int age;

// Constructor to set the initial state of the data members via passed parameters
public Dog(String breed, String size, String color, int age) {
this.breed = breed;
this.size = size;
this.color = color;
this.age = age;
}

// Instance method for eating
public void eat() {
System.out.println("The dog is eating.");
}

// Instance method for running
public void run() {
System.out.println("The dog is running.");
}

// Instance method for sleeping
public void sleep() {
System.out.println("The dog is sleeping.");
}

// Instance method for getting the name of the dog
public void name() {
System.out.println("The dog's name is " + breed + ".");
}

// Main method
public static void main(String[] args) {
// Creating three Dog objects
Dog dog1 = new Dog("Labrador", "Large", "Golden", 5);
Dog dog2 = new Dog("Poodle", "Medium", "White", 3);
Dog dog3 = new Dog("Bulldog", "Small", "Brown", 7);

// Calling instance methods for each dog object
dog1.eat();
dog2.run();
dog3.sleep();
dog1.name();
dog2.name();
dog3.name();
}
}
50 changes: 32 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,35 @@
6. All the instance methods for the class **Student**

```java
// Class name
class Student{
private String name;
private int rollNo;

Student(String s, int r)
{
name = s;
rollNo = r;
}

void methodForDisplay()
{
System.out.println(name+"'s Roll No: "+rollNo);
}

public static void main(String[] args) {
Student obj1=new Student("Rambo",21);
obj1.methodForDisplay();
}
// Instance variables/fields and their data-types
private String name;
private int rollNo;

// Constructor and the Constructor Parameters
Student(String s, int r)
{
name = s; // Setting the value of the name instance variable
rollNo = r; // Setting the value of the rollNo instance variable
}

// Instance method for displaying student's name and roll number
void methodForDisplay()
{
System.out.println(name+"'s Roll No: "+rollNo); // Displaying the name and roll number
}

public static void main(String[] args) {
// Where a Student object gets created
Student obj1=new Student("Rambo",21); // Creating a Student object named obj1

// Where the instance variables value gets set and what its values are
// The values are set in the constructor when the Student object is created
// name = "Rambo" and rollNo = 21

obj1.methodForDisplay(); // Calling the methodForDisplay() method to display the student's details
}
}
```

Expand All @@ -37,6 +47,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 are associated with the class itself rather than with instances of the class. They can be called directly using the class name, without needing to create an object of the class. Static methods are commonly used for utility functions or operations that do not require access to instance variables.

On the other hand, public methods are instance methods that are associated with individual objects or instances of the class. They can access instance variables and are typically called using an object of the class. Public methods define the behavior of objects and can modify the state of an object's instance variables.

## 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