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
53 changes: 53 additions & 0 deletions Dogs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Dogs {
private String name;
private String breed;
private String size;
private String color;
private int age;
private int energy;

Dogs(String name, String breed, String size, String color, int age, int energy) {
this.name = name;
this.breed = breed;
this.size = size;
this.color = color;
this.age = age;
this.energy = energy;
}
void Run() {
energy -= 10;
System.out.println(name + " is running and losing energy");
}
void Sleep() {
energy += 20;
System.out.println(name + " is sleeping and gaining energy");
}
void Eat(Food food) {
energy += food.getWeight();
System.out.println(name + " is eating and gaining energy");
}
String Name() {
return name;
}
public static void main(String[] args) {
Dogs dog1 = new Dogs("Red", "Bulldog", "large", "light gray", 5, 50);
Dogs dog2 = new Dogs("Wood", "Beagle", "large", "orange", 6, 60);
Dogs dog3 = new Dogs("Tater", "German Shepherd", "large", "white and orange", 6, 70);
dog1.Run();
dog2.Sleep();
dog3.Eat(new Food(15));
System.out.println("Dog 1's name is: " + dog1.Name());
System.out.println("Dog 2's name is: " + dog2.Name());
System.out.println("Dog 3's name is: " + dog3.Name());
}
}
class Food {
private int weight;

public Food(int weight) {
this.weight = weight;
}
public int getWeight() {
return weight;
}
}
54 changes: 35 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,36 @@
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();
}
// Class name
class Student {
// Instance variables/datatype name/String and rollno/No
private String name;
private int rollNo;

// Constructor
// Constructor Parameters:
// - s: student's name data type: String
// - r: student's roll number data type: int
Student(String s, int r) {
// Setting instance variable name to the provided parameter s
name = s;
// Setting instance variable rollNo to the provided parameter r
rollNo = r;
}

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

// Main method
public static void main(String[] args) {
// Creating a Student object with name "Rambo" and roll number 21
Student obj1 = new Student("Rambo", 21);
// Calling methodForDisplay()
obj1.methodForDisplay();
}
}
```

Expand All @@ -37,6 +48,11 @@ 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.


In a class, static methods are called on the class itself and are often used for tasks that involve the whole class or
use data that belongs to the class. Public methods are called on individual objects of the class and work with the object's
own data. Static methods are for class-wide operations, while public methods are for working with specific objects.

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