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.

39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,23 @@
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 Student{ // Class name: Student
private String name; // Instance variable: name (String)
private int rollNo; // Instance variable: rollNo (int)

Student(String s, int r) { // Constructor: Student
name = s; // Constructor parameter for name
rollNo = r; // Constructor parameter for rollNo
}

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

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

Expand All @@ -37,6 +35,9 @@ 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 associated with the class itself and can be called using the class name, while public methods are accessible from outside the class and are typically used to define the behavior of objects and access their state.

## 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
108 changes: 108 additions & 0 deletions src/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
class Dog {
private String name;
private int age;
private String breed;
private String size;
private double weight;
private String colour;

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

public void dogBio() {
System.out.println(name + " is a " + size + " sized " + colour + " " + breed + ", who is currently " + age + " years old");
}

// update the size of the dog based on weight. 1-9 small, 10-19 medium, 20+ large
public void updateSize() {
String lastSize = size;
if (weight < 10.0) {
size = "small";
} else if (weight < 20.0) {
size = "medium";
} else {
size = "large";
}
// print update if size changes
if (!(size.equals(lastSize))) {
System.out.println(name + " is now a " + size + " dog");
}
}

// apply given number as a change in weight, negative numbers are subtractive.
// we only change weight if dog is over 5 pounds and under 50 to prevent unrealistic circumstances.
public void changeWeight(double caloricPounds) {
if (weight >= 50.0) {
System.out.println(name + " is overweight!");
}
else if (weight >= 5.0) {
weight += caloricPounds;
updateSize();
} else {
System.out.println(name + " is underweight!");
}
}

public void eat(Food food) {
System.out.println(name + " is eating " + food.name);
if (food.toxicityToDogs) {
System.out.println(food.name + " was toxic, " + name + " has died.");
die();
} else {
changeWeight(food.caloricPounds);
}
}

// I don't know how to delete an object in java
public void die() {
// this = null; does not work, I would have to pass the object reference through eat() and to die() and set that to null.
// this was just an extra bit for fun, I am going to end the assignment here.
}

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

// we run changeWeight to update weight loss from running. Here they lose 1 pound.
// we could take in distance and use that to decide how much weight they lose.
changeWeight(-1);
}


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

public void setName(String newName) {
System.out.print(name);
this.name = newName;
System.out.println("'s name has been changed to: " + newName);
}

public static void main(String[] args) {
Dog dog1 = new Dog("Buddy", 5, "Bulldog", "medium", 19.5, "light gray");
Dog dog2 = new Dog("Max", 6, "Beagle", "medium", 10.0, "orange");
Dog dog3 = new Dog("Bella", 6, "German Shepherd", "large", 25.0, "white & orange");

Food chocolate = new Food("Chocolate", 0.1, true); // Toxic to dogs
Food beef = new Food("Beef", 0.5, false);
Food dogFood = new Food("Dog Food", 0.4, false);

dog1.dogBio();
dog2.dogBio();
dog3.dogBio();

dog1.eat(beef);
dog2.run();
dog3.eat(dogFood);
dog3.sleep();

dog1.setName("Charlie");
dog1.eat(chocolate);
}
}
29 changes: 29 additions & 0 deletions src/Food.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class Food {
public String name;
public double caloricPounds;
public boolean toxicityToDogs;

public Food(String name, double caloricPounds, boolean toxicityToDogs) {
this.name = name;
this.caloricPounds = caloricPounds;
this.toxicityToDogs = toxicityToDogs;
}

public static void main(String[] args) {
Food chocolate = new Food("Chocolate", 0.1, true); // Toxic to dogs
Food beef = new Food("Beef", 0.5, false);
Food dogFood = new Food("Dog Food", 0.4, false);

// Display information about the foods
System.out.println("Foods:");
System.out.println(chocolate.toString());
System.out.println(beef.toString());
System.out.println(dogFood.toString());
}

@Override
public String toString() {
return "Name: " + name + ", Caloric Pounds: " + caloricPounds +
", Toxicity to Dogs: " + (toxicityToDogs ? "Yes" : "No");
}
}