Skip to content

Commit fdfac98

Browse files
committed
Add classes exercises
1 parent d76db05 commit fdfac98

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

exercises/classesobjects.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
###Exercises
1313

14-
Complete these exercises in `ClassesAndObjects.java`. Please use the `Cat.java` class to complete these exercises.
14+
Complete these exercises in `ClassesAndObjects.java`. Please use the `Cat.java` and `Person.java` classes to complete these exercises.
1515

1616
####1. Cool cats
1717

@@ -42,3 +42,26 @@ Write a method called `makeKitten` that takes 2 Cats as parameters and returns a
4242
ex: makeKitten(garfield, catwoman) returns a Cat named "GarfieldCatwoman" with age 0
4343
```
4444

45+
####5. Adoption
46+
47+
Write a method called `adoption` that takes 1 Cat and 1 Person as a paramater and sets the Cat's owner to be the Person.
48+
49+
```
50+
ex: Person jon = Person('Jon');
51+
adoption(garfield, jon); // Jon is now Garfield's owner!
52+
```
53+
54+
*Hint: this method does not return anything!*
55+
56+
**Bonus Challenge**
57+
58+
If the Cat's name is `Catwoman`, don't set the owner, but instead print: "Catwoman will never be anyone's pet!"
59+
60+
####6. isFree
61+
62+
Write a method called `isFree` that checks whether or not a Cat has an owner.
63+
64+
```
65+
ex: isFree(garfield) returns false
66+
```
67+

src/Cat.java

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class Cat {
66
private String name;
77
private String favoriteFood;
88
private int age;
9+
private Person owner;
910

1011
public Cat() {
1112
}
@@ -37,4 +38,12 @@ public String getName() {
3738
public void setName(String name) {
3839
this.name = name;
3940
}
41+
42+
public void setOwner(Person owner) {
43+
this.owner = owner;
44+
}
45+
46+
public Person getOwner() {
47+
return owner;
48+
}
4049
}

0 commit comments

Comments
 (0)