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
- 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!
- 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.javaand try to understand how it works. Compile the code with
javac SimpleTrack.javaand run it with
Try to modify the code as indicated in the slide.
2. Working with Objects
Exercise 2.2: Modularize the code in the
RaceTrack.javafile starts out identical to the procedural
SimpleTrack.javafile from the prior exercise. You must turn this into an OOP program by:
- Moving the constants related to printing into the
- Defining a
Carclass that represents the location of a car.
- Modify the
RaceTrackclass 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
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 anyone
private- only accessible within the same class
protected- accessible in the class, subclass, and package
- (no keyword) - accessible within the package