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.

21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

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

Student(String s, int r)
{
name = s;
rollNo = r;
//instance variables name (String) and rollNo (int)
Student(String s, int r) //constructor Student
{ //parameters
name = s; //instance variable 1 (s)
rollNo = r; //instance variable 2 (r)
}

void methodForDisplay()
Expand All @@ -26,16 +27,14 @@ class Student{
}

public static void main(String[] args) {
Student obj1=new Student("Rambo",21);
Student obj1=new Student("Rambo",21);// Object 1 created, values set
obj1.methodForDisplay();
}
}
```

## 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 can be called directly using the class name, without creating an object of the class. On the other hand, a public method is a method that can be accessed from anywhere,
regardless of whether it's called from within the class or from outside the class. This can be useful or detrimental to your project, depending on how you use it.
You must also create an object.

## Part 3 - Dogs

Expand Down
47 changes: 47 additions & 0 deletions src/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.Scanner;

public class Dog {

private String name;
private int age;
private String breed;
private String size;
private String color;

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

// method to change the dog's name
public void changeName() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the new name for the dog: ");
name = scanner.nextLine();
System.out.println("The dog's name has been changed to " + name);
}

public void eat(Food food) {
if (food.getWeight() >= 5) {
food.decreaseWeight(5);
System.out.println(name + " ate 5 pounds of food. The remaining weight of the food is " + food.getWeight() + " pounds.");
} else {
System.out.println("There is not enough food for " + name + " to eat.");
}
}

public static void main(String[] args) {
Dog dog1 = new Dog("Buddy", 3, "Golden Retriever", "Large", "Golden");
Dog dog2 = new Dog("Max", 5, "German Shepherd", "Medium", "Black and Tan");
Dog dog3 = new Dog("Charlie", 2, "Labrador", "Small", "Black");

Food food = new Food(100);

dog1.eat(food);

System.out.println("The remaining weight of the food is " + food.getWeight() + " pounds.");
}
}
15 changes: 15 additions & 0 deletions src/Food.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Food {

private int weight;

public Food(int weight) {
this.weight = weight;
}

public int getWeight() {
return weight;
}

public void decreaseWeight(int amount) {
weight -= amount;
}}