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
43 changes: 24 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,37 @@
6. All the instance methods for the class **Student**

```java
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();
}
public class Student{
// Class name: Student

// Instance variables/fields
private String name; // String variable to store the name of the student
private int rollNo; // Integer variable to store the roll number of the student

// Constructor
Student(String s, int r) {
name = s; // Setting the name of the student
rollNo = r; // Setting the roll number of the student
}

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

public static void main(String[] args) {
// Creating a Student object
Student obj1 = new Student("Rambo", 21);
obj1.methodForDisplay(); // Calling the method to display student information
}
}
```

## Part 2 - Public VS Static

* 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 like class-wide functions that can be used without making an object, while public methods are actions that objects can do. Public methods can only be used with objects.

## Part 3 - Dogs

Expand Down
52 changes: 52 additions & 0 deletions src/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @author Alexei Iachkov
* @date 4-14-24
* @version 1.0
*/
public class Dog {
private String breed;
private String size;
private String color;
private int age;

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(Food food) {
System.out.println("The " + breed + " is eating.");
food.reduceWeight();
}

public void run() {
System.out.println("The " + breed + " is running.");
}

public void sleep() {
System.out.println("The " + breed + " is sleeping.");
}

public void name() {
System.out.println("The " + breed + "'s name is Doober.");
}

public static void main(String[] args) {
Dog dog1 = new Dog("Bulldog", "large", "light gray", 5);
Dog dog2 = new Dog("Beagle", "medium", "orange", 7);
Dog dog3 = new Dog("German Shepherd", "large", "white and orange", 4);

dog1.eat(new Food()); // Passing a Food object to the eat method
dog2.run();
dog3.sleep();
dog3.name();
}
}

class Food {
public void reduceWeight() {
System.out.println("Food weight reduced.");
}
}