Code Practice with Object Oriented Concepts
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods11 exercises27 graded tests
Files
Code Practice with Object Oriented Concepts
Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.
Exercise 1
Write a method that overloads the talk method by taking in a name and printing Hello with that name.
Open the
01-oocodeex1folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 2
Edit this code so the class Beagle is a subclass of the Dog class. When you run the code it should print “woof!” and then “arf arf”.
Open the
02-oocodeex2folder and editDog.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 3
Add an equals method to this class that returns true if the current Dog and passed Dog have the same name. The code should print false twice then true twice.
Open the
03-oocodeex3folder and editDog.java. Press Run to try it; Run Tests checks it (4 tests).
Exercise 4
Override the taste method from the Candy class in the Chocolate class to return tastes chocolately. It should print tastes sweet! and then tastes chocolately.
Open the
04-oocodeex4folder and editCandy.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 5
Overload the greet method to just print Hello if not given any parameters. It should print Hello and then Hello Sansa.
Open the
05-oocodeex5folder and editStudent.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 6
Add a call to Pet’s brag method before printing anything in Dog’s brag method (hint: use super to call an overridden method). It should print I have the best pet! and then I have the best dog.
Open the
06-oocodeex6folder and editPet.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 7
Finish the Teacher constructor. Use super to use the Person constructor to set the fields inherited from Person. It should print Destini 20 followed by Erica 55 Masters in Teaching.
Open the
07-oocodeex7folder and editPerson.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 8
Add public getter and setter methods to the Store class so its variables can be accessed by other classes. It should print the store’s name and address and then change both and print the new values.
Open the
08-oocodeex8folder and editStore.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 9
Correctly finish the Dog subclass for the following Animal class. Override the methods speak() to print woof and eat() to print num num.
Open the
09-oocodeex9folder and editDog.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 10
Override the compareTo method so that it returns a positive number if the current Person is older than the passed other and a negative number if they are younger. If their age is the same then return the compareTo result on the names.
Open the
10-oocodeex10folder and editPerson.java. Press Run to try it; Run Tests checks it (4 tests).
Exercise 11
Override the Person class’s speak method inside the Student class. Make the method print I'm a student.
Open the
11-oocodeex11folder and editPerson.java. Press Run to try it; Run Tests checks it (2 tests).
For teachers
Code Practice with Object Oriented Concepts
Students read CSAwesome2's “Code Practice with Object Oriented Concepts” in CS Room and work through its 11 Java coding exercises.
- Time: 2 class periods
- Tests: 27 in 11 graded exercises, set up for you when you import the lesson
Goals
- Practice for Unit 5: Inheritance (optional).
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/ooCodePractice.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–11, each in its own folder, pressing Run to try their code.
- Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.
Exercises
| Folder | File | Grading | Tests |
|---|---|---|---|
01-oocodeex1 | Test1.java | Autograded | 2 |
02-oocodeex2 | Dog.java | Autograded | 2 |
03-oocodeex3 | Dog.java | Autograded | 4 |
04-oocodeex4 | Candy.java | Autograded | 2 |
05-oocodeex5 | Student.java | Autograded | 2 |
06-oocodeex6 | Pet.java | Autograded | 2 |
07-oocodeex7 | Person.java | Autograded | 2 |
08-oocodeex8 | Store.java | Autograded | 2 |
09-oocodeex9 | Dog.java | Autograded | 3 |
10-oocodeex10 | Person.java | Autograded | 4 |
11-oocodeex11 | Person.java | Autograded | 2 |
Teachers can download a reference solution after signing in.