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.

32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,34 @@
6. All the instance methods for the class **Student**

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

Student(String s, int r)
//2. All instance variables/fields and their data-types
private String name; // Instance variable name of type String
private int rollNo; // Instance variable rollNo of type int
//3. The Constructor and the Constructor Parameters
Student(String s, int r) //3. Constructor with parameters s and r
{
name = s;
rollNo = r;
name = s; // Assigning value of s to name
rollNo = r; // Assigning value of r to rollNo
}
{ //5. Where the instance variables value gets set and what its values are.
name = s; // Setting the value of name to s
rollNo = r; // Setting the value of rollNo to r
}
//6. All the instance methods for the class Student
void methodForDisplay() // Instance method methodForDisplay
{
System.out.println(name+"'s Roll No: "+rollNo); // Printing name and rollNo
}

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

public static void main(String[] args) {
Student obj1=new Student("Rambo",21);
obj1.methodForDisplay();
//4. Where a Student object gets created.
Student obj1=new Student("Rambo",21); // Creating object obj1 of class Student
obj1.methodForDisplay(); // Calling method methodForDisplay
}
}
```
Expand All @@ -36,7 +46,7 @@ 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 methods that belong to the class itself, and not to the object of the class. They can be called without creating an instance of the class. Public methods are methods that can be accessed by any other class. They are accessible from any other class, and can be called from any other class.
## 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
68 changes: 68 additions & 0 deletions src/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Written By: Austin Barnett
// Date: 4/10/2024
// Version 1.0
public class Dog {
// Private instance variables/fields
private String name;
private int age;
private double weight;

public Dog(String name, int age, double weight) {
this.name = name;
this.age = age;
this.weight = weight;
}

public void eat(Food food) {
System.out.println(name + " is eating.");
weight += food.getWeight();
food.setWeight(0);
}


public void run() {
System.out.println(name + " is running.");

weight -= 0.5;
}


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


public String getName() {
return name;
}


public static void main(String[] args) {
Dog dog1 = new Dog("Buddy", 3, 20.0);
Dog dog2 = new Dog("Charlie", 1, 10.0);
Dog dog3 = new Dog("Max", 5, 25.0);


Food dogFood = new Food(5.0);
dog1.eat(dogFood);
dog2.run();
dog3.sleep();
}
}


class Food {
private double weight;

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

public double getWeight() {
return weight;
}

public void setWeight(double weight) {
this.weight = weight;
}
}