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

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
  • To translate a word problem into a software architecture

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 with javac SimpleTrack.java and run it with java 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. The RaceTrack.java file starts out identical to the procedural SimpleTrack.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 anyone
  • private - only accessible within the same class
  • protected - accessible in the class, subclass, and package
  • (no keyword) - accessible within the package

4. UML and OOP Design

In this course we will use a simplified form of UML and will focus on Class diagrams.

  • Rectangles are used to define a class, with the name in the top section, a list of instance variables in the middle section, and functions in the bottom section.
  • We will use a arrow with a white triangular head to indicate the “is a” relationship, which corresponds to inheritance in Java. The arrow should point towards the parent class.
  • We will use a simple angled arrow to represent the “associated with” relationship. This means that the first class will be making function calls and possibly instantiating objects of the class it is pointing to.

You can draw UML diagrams with the tool of your choice, or do it on paper. I recommend Violet UML - download the Jar file and double click to run it. On a Mac you may need to right click and select Open to avoid security warnings.

Exercise 2.6: Draw a UML diagram to represent the Mail System.

Exercise 2.7: Draw a UML diagram to represent the Banking System.


Previous - Next

© 2003, Rahul Simha (revised 2017). Further revised by Tim Wood 2018.