Algorithms and OOD (CSC 207 2014F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [Learning Outcomes] [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Student-Curated Resources] [Java 8 API] [Java 8 Tutorials] [Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2014S (Rebelsky)] [CSC 207 2014F (Walker)] [CSC 207 2011S (Weinman)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Overview
Polymorphism
(define square (lambda (x) (* x x)))square works for integerssquare works for doublessquare works for complex numbers#define square(x) ((x)*(x))Inheritance
In Java
public class GrinnellStudentEmployee
extends GrinnellStudent
Encapsulation
Suppose we are representing points on the plane
public class Point
{
// +--------+------------------------------------------------------
// | Fields |
// +--------+
double x;
double y;
// +---------+-----------------------------------------------------
// | Methods |
// +---------+
public double distanceFromOrigin()
{
return Math.sqrt(x*x + y*y);
} // distanceFromOrigin
} // class Point
Two approaches for the client
public class GeoLocator
{
public static void main(String[] args)
throws Exception;
{
Point pt1 = GoogleMaps.makePoint("Grinnell, IA");
Point pt2 = GoogleMaps.makePoint("Minneapolis, Minisoda");
// I need to figure out which of point 1 and point 2 is closer
/ to the origin
if (pt1.distanceFromOrigin() < pt2.distanceFromOrigin())
giveDirectionsTo(pt1);
else
giveDirectionsTo(pt2);
}
}
// I need to figure out which of point 1 and point 2 is closer
/ to the origin (Note: For non-negative x and y,
// x < y iff sqrt(x) < sqrt(y).)
if (pt1.x*pt1.x + pt1.y*pt1.y < pt2.x*ptx + pt2.y*pt2.y)
giveDirectionsTo(pt1);
else
giveDirectionsTo(pt2);
Which is better?
What happens if we decide that we should represent points with radius and angle?
public class Point
{
// +--------+------------------------------------------------------
// | Fields |
// +--------+
double radius;
double theta;
// +---------+-----------------------------------------------------
// | Methods |
// +---------+
public double distanceFromOrigin()
{
return radius;
} // distanceFromOrigin
} // class Point