Java Module 2: Object Oriented Programming
This week you will not need to submit your solutions. But you should still solve the problems for practice!
- Log onto CodeAnywhere using the updated instructions available here
- In an SSH terminal run
git clone https://github.com/cs2113f18/lec-oop.git
- Do all your work inside the
lec-oop
directory- The in-class worksheet is available here
- You might need the Violet UML editor
Deadline: Wednesday 10/24, before class. You must complete sections 1, 2, and 3 below (up to and including Exercise 2.5). You should solve all the blue exercise slides, including answering the first part of the worksheet. You do not need to submit any of this work… but you should take it just as seriously as normal!
Objectives
- To learn more about objects and inheritance
- To practice object oriented design thinking
The slides are broken up into sections below, or you can view the entire PDF.
1. Procedural vs Object Oriented Design
Exercise 2.1: Look at the code in
proc/SimpleTrack.java
and try to understand how it works. Compile the code withjavac SimpleTrack.java
and run it withjava SimpleTrack
Try to modify the code as indicated in the slide.
2. Working with Objects
Exercise 2.2: Modularize the code in the
oop/
directory. TheRaceTrack.java
file starts out identical to the proceduralSimpleTrack.java
file from the prior exercise. You must turn this into an OOP program by:
- Moving the constants related to printing into the
Terminal
class.- Defining a
Car
class that represents the location of a car.- Modify the
RaceTrack
class so that it is only responsible for instantiating a car object and then printing it to the screen.
- Use the provided pseudocode to guide your for loop.
Exercise 2.3: Modify your program so it can create 3 cars at random positions and show them driving around. Once you do this, try some of the listed extensions.
3. Inheritance and Visibility
Be sure you have read Head First Java Chapter 7 to learn more about Inheritance and Visibility.
Exercise 2.4: Extend your program to define multiple types of vehicles or objects with inheritance.
The book defers discussion of protected
and no keyword
(also called “default”) permission level, but they are simple enough from these slides that you should memorize all of them. Refer to the prior set of slides for a reminder of static variables and how they are accessible.
Exercise 2.5: Answer Question 1 on the worksheet. Data and functions can be:
public
- callable, readable/writable by anyoneprivate
- only accessible within the same classprotected
- accessible in the class, subclass, and package- (no keyword) - accessible within the package