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.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,33 @@
6. All the instance methods for the class **Student**

```java
// Class: Student
class Student{
// Instance Variables/Fields
// String: 'name'
// int: 'rollNo'
private String name;
private int rollNo;


// Constructor
// Constructor Parameters: String: 's', int: 'r'
Student(String s, int r)
{
name = s;
rollNo = r;
// Where instance variables get set
name = s; // 'name' gets set to parameter 's'
rollNo = r; // 'rollNo' gets set to parameter 'r'
}

// Instance method for the display
void methodForDisplay()
{
System.out.println(name+"'s Roll No: "+rollNo);
}

public static void main(String[] args) {
// Creates the Student object with the name "Rambo" and the roll number 21
Student obj1=new Student("Rambo",21);
// Calls the method to display
obj1.methodForDisplay();
}
}
Expand All @@ -35,8 +45,9 @@ class Student{
## 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.

* In your own words, write a few sentences below explaining the difference between static and public methods in relation to a class.
You must create a new object to access a public method. With a static method you do not, you can just call it at any time.
What you can't do however, is use static methods outside of it's own class, but you can with a public one. (Hence it's name.)
## 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
11 changes: 11 additions & 0 deletions src/src.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
87 changes: 87 additions & 0 deletions src/src/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
class Food {
static int foodAmount;

Food(int foodAmount) {
Food.foodAmount = foodAmount;
}
}

class Energy {
int energyAmount;

Energy(int energyAmount) {
this.energyAmount = energyAmount;
}
}

public class Dog {

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

private Food food = new Food(5);
private Energy energy;
private String name;

public Dog(String breed, String size, String color, int age, Energy energy) {
this.breed = breed;
this.size = size;
this.color = color;
this.age = age;
this.energy = energy;
// I included energy here so that each dog can use energy without affecting the other dogs energy values, but if one dog eats it reduces the amount of food they all can eat.
}

void name(String name) {
this.name = name;
System.out.println("It's name is " + name);
}

void eat() {
if (Food.foodAmount > 0) {
Food.foodAmount--;
energy.energyAmount++;
System.out.println(name + " is eating.");
} else {
System.out.println("Ran out of food! Time to go to the store!");
}
}

void run() {
if (energy.energyAmount > 0) {
energy.energyAmount--;
System.out.println(name + " is running.");
} else {
System.out.println(name + " is too tuckered out.");
}
}

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

public String toString() {
return "This dog is a " + this.breed + ", it's " + this.size + " in size, it's " + this.color + ", and it's " + this.age + " years old.";
}

public static void main(String[] args) {
Dog Dog1 = new Dog("Bulldog", "Medium","Light Gray",5,new Energy(5));
Dog Dog2 = new Dog("Beagle", "Small","Orange",6,new Energy(5));
Dog Dog3 = new Dog("German Shepherd", "Large","White & Orange",6,new Energy(5));

System.out.println(Dog1);
Dog1.name("Rufus");
Dog1.eat();

System.out.println(Dog2);
Dog2.name("Snoopy");
Dog2.run();

System.out.println(Dog3);
Dog3.name("Odin");
Dog3.sleep();
}
}