class ALoBookspublic abstract class ALoBooks { public abstract ALoBooks addBook book; }... • The list of stations is: Kenmore, Park, Center, North, Science, Ashmont, Downtown, Charles, S
Trang 1Methods with effects
Week 14,15
A2 Programming Course
Trang 2Cyclical Data
Trang 3Bookstore Manager’s Problem
Trang 4A Variation of the Bookstore Manager’s
Problem
• Develop a program that assists a bookstore
manager The manager's program should keep a record for each book
• The record must include information about the author, the book's title, its price, and its
publication year
• The information about the author includes
author's name, year of birth, and a list of books written by this author
• Note: Remember to implement toString()
Trang 5• The bookstore has the following books written by Jack London, born 1876:
– Call of the Wild (1903), published in 1995, $10
– The Sea-Wolf (1904), published in 1999, $12
– Martin Eden (1913), published in 1998, $12
– White Fang (1906), published in 2001, $10
• The bookstore has the following books written by
Danielle Steel, born in 1955, all in their original edition:
Trang 7Problem in Designing Constructors
• To create a new object in the class Book we need
to refer to the Author But each Author refers to a list of books
• Q: The egg comes first or the hen comes first?
• Solution: A reasonable solution is to define an
Author object with an empty list of books Then,
as the author writes a new book, we create a Book
add this book to the author's list of books
Trang 8public class AuthorTest extends TestCase {
public void test() {
Author jackLondon = new Author("Jack London", 1876);
Book cotw = new Book(jackLondon, "Call of the Wild", 1995, 10);Book tsw = new Book(jackLondon, "The Sea-Wolf", 1999, 12);Book me = new Book(jackLondon, "Martin Eden", 1998, 12);
Book wf = new Book(jackLondon, "White Fang", 2001, 10);
System.out.println(jackLondon);
Author danielleSteel = new Author("Danielle Steel", 1955);
Book d = new Book(danielleSteel, "Daddy", 1989, 20);
Book h = new Book(danielleSteel, "Heartbeat", 1992, 15);
Book j = new Book(danielleSteel, "Jewels", 1993, 22);
Book w = new Book(danielleSteel, "Wings", 1995, 25);
Book tg = new Book(danielleSteel, "The Ghost", 1995, 28);
System.out.println(danielleSteel);
}
}
Trang 9public class Author {
private String name;
private int birthYear;
private ALoBooks books;
public Author(String name, int birthYear) {
public String toString() {
return this.name + ", " + this.birthYear + "\n" + this.books;}
}
Trang 10public class Book {
private Author author;
private String title;
private double cost;
private int publishYear;
public Book(Author author, String title, int publishYear, double cost) {
public String toString() {
return this.author.getName() + ", " + this.title + ", " +
this.cost + ", " + this.publishYear + "\n";
}
}
Trang 11addBook() in Author
// class Author
void addBook(Book book) {
this.books = this.books.add(book); }
Trang 12class ALoBooks
public abstract class ALoBooks {
public abstract ALoBooks add(Book book); }
Trang 13class Empty
public class Empty extends ALoBooks {
public ALoBooks add(Book book) {
Trang 14Class Cons
public class Cons extends ALoBooks {
private Book first;
private ALoBooks rest;
public Cons(Book first, ALoBooks rest) {
this.first = first;
this.rest = rest;
}
public ALoBooks add(Book book) {
return new Cons (book, this);
}
public String toString() {
return this.first + " " + this.rest;
}
}
Trang 15New add(Book book) version
public abstract class ALoBooks {
public ALoBooks add(Book book){
}
}
Trang 16The Transportation Network
Problem
Trang 17Problem Statement
• The Metropolitan Transit Agency has a web
site that allows the user to display information about any of its stations and its train routes
Trang 18• The list of stations is: Kenmore, Park, Center, North, Science, Ashmont, Downtown, Charles, State, Bowdoin, Maverick, Wonderland
Center, North to Science
• Red Line runs from Ashmont through
Downtown and Park to Charles
State, Maverick, to Wonderland
Trang 19Kenmore Park Center North
Ashmont Downtown State Maverick Wonderland
Charles Bowdoin
Science
Trang 20The Problem
• The class Station needs to have fields for its
• The class Route needs to have a field for its name, the origin, the destination, and for a
list of stations
• Again, we cannot build a route without
knowing all the stations, but the station
should know what routes it serves
Trang 21• Then, create the route object, and instruct the
serves this route , as well as the origin and
destination station
Trang 2211 stations
ConsLoStations
- Station first
- ALoStations rest
11 rest
2 11
first
Trang 23A Simpler Class Diagram
Trang 24public class RouteTest extends TestCase {
public void test() {
// create stations for green route
Station kentmore = new Station("Kentmore"); Station park = new Station("Park");
Station center = new Station("Center");Station north = new Station("North");
Station science = new Station("Science");
// create green route
Route green = new Route("Green", kentmore, science);
green.addStation(park); green.addStation(center);
green.addStation(north); System.out.println(green);
// create stations for red route
Station ashmont = new Station("Ashmont"); Station charles = new Station("Charles");
Station downtown = new Station("Downtown");
// create red route
Route red = new Route("Red", ashmont, charles);
red.addStation(downtown); red.addStation(park);
System.out.println(red);
// create stations for blue route
Station bowdoin = new Station("Bowdoin"); Station state = new Station("State");
Station maverick = new Station("Maverick"); Station wonderland = new Station("Wonderland"); // create blue route
Route blue = new Route("Blue", bowdoin, wonderland);
Trang 25public class Station {
private String name;
private ALoRoutes routes;
public Station(String name) {
public String toString() {
return "Station " + this.name + ": " + this.routes.getName();}
}
Trang 26public class Route {
private String name;
private Station origin;
private Station destination;
private ALoStations stations;
public Route(String name, Station origin, Station destination) {
public String toString() {
return "Route " + this.name + ": " + this.stations;
}
}
Trang 27addRoute() in Station
// class Station
void addRoute(Route route) {
this.routes = new ConsLoRoutes(route, this.routes); }
Trang 28addStation() in Route
Q: How can we add a Station to a Route?
//class Route
this.stations = new ConsLoStations(station, this.stations);
}
Trang 29Class ALoRoutes
public abstract class ALoRoutes {
public abstract String getNames(); }
Trang 31class ConsLoRoutes
public class ConsLoRoutes extends ALoRoutes {
private Route first;
private ALoRoutes rest;
public ConsLoRoutes(Route first, ALoRoutes rest) {
this.first = first;
this.rest = rest;
}
public String getNames() {
return this.first.getName() + " " + this.rest.getNames();}
}
Trang 32class ALoStations
public abstract class ALoStations { }
Trang 34class ConsLoStations
public class ConsLoStations extends ALoStations {
private Station first;
private ALoStations rest;
public ConsLoStations(Station first, ALoStations rest) {
this.first = first;
this.rest = rest;
}
public String toString() {
return this.first.getName() + " " + this.rest;
}
}
Trang 35• Compare with another implementation in book “ How to design class hierarchies”
Trang 36The University Registrar Problem
Trang 37Problem Statement
• In the university registrar's database there is a list of course sections offered this term
• For each course section, the registrar records the
students enrolled in the course
• The information about each student includes the
name, an id number, and student's schedule
taking this term
Trang 38• The university offers the following courses this term:
– Math course, 4 credits
– Physics course, 5 credits
– Biology course, 5 credits
– Music course, 2 credits
Trang 40The Problem
• The problem we need to face is how to
include a list of students in the object that
represents one course, and, at the same time, include student's schedule in the object that represents a student
Trang 4111 courses
ConsLoCourses
- Course first
- ALoCourse rest
11 rest
Trang 42public class CourseTest extends TestCase {
public void test() {
// create courses
Course math = new Course("Math", 4); Course physics = new Course("Physics", 5); Course biology = new Course("Biology", 5); Course music = new Course("Music", 2);
// create student Jenna
Student jenna = new Student(1123, "Jenna");
jenna.addCourse(math); jenna.addCourse(physics);
jenna.addCourse(biology); System.out.println(jenna);
// create student John
Student john = new Student(1345, "John");
john.addCourse(biology); john.addCourse(music);
john.addCourse(physics); System.out.println(john);
// create student Ernie
Student ernie = new Student(4323, "Ernie");
ernie.addCourse(biology); ernie.addCourse(music);
System.out.println(ernie);
// create student Ernie
Student rob = new Student(3213, "Rob");
Trang 43public class Course {
private String name;
private int credits;
private ALoStudents students;
public Course(String name, int credits) {
public String toString(){
return this.name+ ", "+this.credits+", cac sinh vien theo hoc:"+
this.students;
}
}
Trang 44public class Student {
private int id;
private String name;
private ALoCourses courses;
public Student(int id, String name ) {
public String toString(){
return this.id+ "-"+ this.name+",cac mon dang ky:"+this.courses.getNames();
}
}
Trang 45public void addStudent(Student student) {
this.students = new ConsLoStudents(student, this.students);
}
Trang 46class ALoStudents
public abstract class ALoStudents { public abstract String getNames(); }
Trang 48class ConsLoStudents
public class ConsLoStudents extends ALoStudents {
private Student first;
private ALoStudents rest;
public ConsLoStudents(Student first, ALoStudents rest) {
this.first = first;
this.rest = rest;
}
public String getNames() {
return this.first.getName() + " " + this.rest.getNames();}
}
Trang 49class ALoCourses
public abstract class ALoCourses { public abstract String getNames(); }
Trang 51class ConsLoCourses
public class ConsLoCourses extends ALoCourses {
private Course first;
private ALoCourses rest;
public ConsLoCourses(Course first, ALoCourses rest) {
this.first = first;
this.rest = rest;
}
public String getNames() {
return this.first.getName() + " " + this.rest.getNames();}
}
Trang 52Exercises
Trang 53Exercise 2.1.1
• Develop the data definition for classes that
represent employees and their project groups Each employee (identified by a name and year
of birth) belongs to one or more project
groups, and may be a leader of one or more groups Each group has a name, one leader,
and a list of employees in the group
Trang 54Exercise 2.1.2
• Develop the data definition for classes that represent the teams in the town youth baseball league League has a list of teams and a list of coaches Each team has a name, a coach, a list of players, and the current record of wins and losses A coach may coach more than one team For each coach we record the name, phone number, and coach's teams For each player
we need to know the name, year of birth, and the
team Player can be on only one team