Cyclical data

43
Cyclical data

description

Cyclical data. Problem 1. 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. - PowerPoint PPT Presentation

Transcript of Cyclical data

Page 1: Cyclical data

Cyclical data

Page 2: Cyclical data

Problem 1

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.

Page 3: Cyclical data

Examples

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.

Books written by Danielle Steel, born in 1955: Daddy (1989), $20; Heartbeat (1992), $15; Jewels (1993), $22; Wings (1995), $25; The Ghost (1995) $28.

Page 4: Cyclical data

Class diagram

Empty

ALoBooks<<abstract>>

Cons

- Book first- ALoBooks rest

11

rest

Book

- Author author- String title- double price- int publicationYear

11first

Author

- String name- int birthYear- ALoBooks books 1..*1..*

books

11author

Page 5: Cyclical data

What’s problem?

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.

Solution???

Page 6: Cyclical data

Solution

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 object, add it to the bookstore list of books, and add this book to the author's list of books.

Page 7: Cyclical data

AuthorTest.java

public 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);

}}

Page 8: Cyclical data

Author.java

class Author {String name;int dob;ALoBooks books;

public Author(String name, int dob) {super();this.name = name;this.dob = dob;this.books = new MTLoBooks();

}

String getName() {return this.name;

}

public String toString() {return this.name + ", " + this.dob + "\n" + this.books;

}

public void addBook(Book book) {this.books = this.books.add(book);

}}

Page 9: Cyclical data

Book.java

class Book {Author author;String title;int year;int price;

public Book(Author author, String title, int year, int price) {super();this.title = title;this.author = author;this.year = year;this.price = price;this.author.addBook(this);

}

public String toString() {return this.author.getName() + ", " + this.title + ", " + this.price + ", " + this.year + "\n";

}}

Page 10: Cyclical data

ALoBooks.java, MTLoBooks.java

public abstract class ALoBooks {public abstract ALoBooks add(Book book);

}

public class MTLoBooks extends ALoBooks {public ALoBooks add(Book book) {

return new Cons(book, new MTLoBooks());}

public String toString() {return "\n";

}}

Page 11: Cyclical data

ConsLoBooks.java

class ConsLoBooks extends ALoBooks {Book fst;ALoBooks rst;

public ConsLoBooks(Book first, ALoBooks rest) {this.fst = first;this.rst = rest;

}

public ALoBooks add(Book book) {return new ConsLoBooks(book, this);

}

public String toString() {return this.fst + " " + this.rst;

}}

Page 12: Cyclical data

Problem 2

The Metropolitan Transit Agency has a web site that allows the user to display information about any of its stations and its train routes.

The user may view the list of all stations along a given route.

The user may also find out what are all the routes that serve a given station.

Page 13: Cyclical data

Examples

The list of station is: Park, Center, North, Science, Ashmont, Downtown, Charles, State, Bowdoin, Maverick, Wonderland. Green Line runs from Kenmore through Park,

Center, North to Science. Red Line runs from Ashmont through Downtown

and Park to Charles. Blue Line runs from Bowdoin through Center,

State, Maverick, to Wonderland

Page 14: Cyclical data

Train routes

Kenmore Park Center North

Ashmont Downtown State Maverick Wonderland

Charles Bowdoin

Science

Page 15: Cyclical data

Class diagram

EmptyLoRoutes

EmptyLoStations

ALoRoutes

ConsLoRoutes

- Route first- ALoRoutes rest

11

rest

ALoStations

Route

- String name- Station origin- Station destination- LoStations stations

11first

11

stations

ConsLoStations

- Station first- ALoStations rest

11

rest

Station

- String name- ALoRoutes routes 11

routes

2

origin, destination

211first

Page 16: Cyclical data

Simple class diagram

Station

- String name- List routes

Route

- String name- Station origin- Station destination- List stations

** **

Page 17: Cyclical data

What’s problem?

The class Station needs to have fields for its name, and a list of Routes.

The class Route needs to have a field for its name the origin, the destination, and for a list of stations.

We can’t build a route without knowing all the stations, but the station should know what routes it serves.

Solution???

Page 18: Cyclical data

Solution

To create a representation of this data we first build all station objects, omitting the route information.

Next, for each route, we first build a list of its stations, then create the route object, and instruct the constructor to notify every station that it now serves this route, as well as the origin and destination station.

Page 19: Cyclical data

RouteTest.javapublic class RouteTest extends TestCase {

public void test() {// create stations for green routeStation 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 routeRoute green = new Route("Green", kentmore, science);green.addStation(park);green.addStation(center);green.addStation(north);System.out.println(green);

// print stations on green routeSystem.out.println(kentmore);System.out.println(park);System.out.println(center);System.out.println(north);System.out.println(science);

// create blue route// create red route

}}

Page 20: Cyclical data

Station.java

public class Station {private String name;private ALoRoutes routes;

public Station(String name) {this.name = name;this.routes = new EmptyLoRoutes();

}

public String getName() {return this.name;

}

public String toString() {return "Station " + this.name + ": " + this.routes.getNames();

}

void addRoute(Route route) {//another solutionthis.routes = new ConsLoRoutes(route, routes);

}}

Page 21: Cyclical data

Route.java

public class Route {private String name;private Station origin;private Station destination;private ALoStations stations;

public Route(String name, Station origin, Station destination) {this.name = name;this.origin = origin;this.destination = destination;this.stations = new ConsLoStations(origin, new ConsLoStations(destination, new EmptyLoStations()));this.origin.addRoute(this);this.destination.addRoute(this);

}public String getName() {

return this.name;}public String toString() {

return "Route " + this.name + ": " + this.stations;}public void addStation(Station station) {

station.addRoute(this);this.stations = new ConsLoStations(station, stations);

}}

Page 22: Cyclical data

ALoRoutes.java, EmptyLoRoutes.java

public abstract class ALoRoutes {public abstract String getNames();

}

public class EmptyLoRoutes extends ALoRoutes {public String getNames() {

return "";}

}

Page 23: Cyclical data

ConsLoRoutes.java

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();

}}

Page 24: Cyclical data

ALoStations.java, EmptyLoStation.java

public abstract class ALoStations {

}

public class EmptyLoStations extends ALoStations {public String toString() {

return "";}

}

Page 25: Cyclical data

ConLoStations.java

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;

}}

Page 26: Cyclical data

Problem 3

In the university registrar's data base there is a list of course sections offered this term.

For each course section, the registrar records the course name the number of credits, and the list of students enrolled in the course.

The information about each student includes the name, an id number, and student's schedule. The schedule is the list of courses student is taking this term.

Page 27: Cyclical data

Example

Courses in this term: Math course, 4 credits Physics course, 5 credits Biology course, 5 credits Music course, 2 credits

Students are enrolled in these courses: Jenna, id 1123, is taking Math, Physics, and Biology John, id 1345, is taking Biology, Music, and Physics Ernie, id 4323, is taking Biology and Music Rob, id 3213, is taking Physics and Biology

Page 28: Cyclical data

Class diagram

EmptyLoStudents

EmptyLoCourses

ConsLoStudents

- Student first- ALoStudents rest

ALoStudents

11

rest

Student

- int id- String name- ALoCourses courses

11first

ALoCourses

11courses

ConsLoCourses

- Course first- ALoCourse rest

11

rest

Course

- String name- int credits- ALoStudents students 11

students

11first

Page 29: Cyclical data

What’s problem?

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.

Solution???

Page 30: Cyclical data

Solution

Initially, the constructor for the class Course defines object with empty roster and the constructor for the class Student defines object with empty schedule.

When a student adds a course to her schedule, both her schedule and the roster for that course need to change.

Page 31: Cyclical data

CourseTest.java

public class CourseTest extends TestCase {public void test() {

// create coursesCourse 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 JennaStudent jenna = new Student(1123, "Jenna");jenna.addCourse(math);jenna.addCourse(physics);jenna.addCourse(biology);System.out.println(jenna);

// create student John// create student Ernie// create student Rob

System.out.println(math);System.out.println(physics);System.out.println(biology);System.out.println(music);

}}

Page 32: Cyclical data

Course.java

public class Course {private String name;private int credits;private ALoStudents students;

public Course(String name, int credits) {this.name = name;this.credits = credits;this.students = new EmptyLoStudents();

}

public void addStudent(Student student) {this.students = new ConsLoStudents(student, this.students);

}

public String getName() {return this.name;

}

public String toString() {return "Course " + this.name + ": " + this.students.getNames();

}}

Page 33: Cyclical data

Student.java

public class Student {private int ID;private String name;private ALoCourses courses;

public Student(int id, String name) {super();this.ID = id;this.name = name;this.courses = new EmptyLoCourses();

}

public String getName() {return this.name;

}

public void addCourse(Course course) {this.courses = new ConsLoCourses(course, this.courses);course.addStudent(this);

}}

Page 34: Cyclical data

ALoCourses.java, EmptyLoCourses.java

public abstract class ALoCourses {public abstract String getNames();

}

public class EmptyLoCourses extends ALoCourses {public String getNames() {

return "";}

}

Page 35: Cyclical data

ConsLoCourses.java

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();

}}

Page 36: Cyclical data

ALoStudents.java, EmptyLoStudents.java

public abstract class ALoStudents {public abstract String getNames();

}

public class EmptyLoStudents extends ALoStudents {public String getNames() {

return "";}

}

Page 37: Cyclical data

ConsLoStudents.java

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();

}}

Page 38: Cyclical data

Summary

Page 39: Cyclical data

Exercises Exercise 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.  

Exercise 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.

Page 40: Cyclical data

Exercises

Bookstore Find out how many books were written by some

author; Create a list of all books in the bookstore that

were written by modern authors -- those born after 1940;

Find out which of two authors wrote more books.

Page 41: Cyclical data

Exercises

Metropolitan Transit Agency web site Count the number of stops on a given route, including the

origin and destination. Count the number of routes served by this station. Find out whether a given route goes through this station. Find out whether this route goes through a given station. The web site user wants to see all transfer stations on

some route. The rider wants to find out whether when travelling on one

route she will get to a station where a transfer to some other desired route is possible. For example, is there a transfer from Red line to Blue line?

Page 42: Cyclical data

Exercises Exercise 5.1.1.  

Return to the data definition for classes that represent employees and their project groups  2.1.1. Develop the methods to solve the following problems:

Determine the size of some group given the list of groups in the company; An employee want to report on the yearly review how many people she supervised; An employee want to know whether he is in the same work group as another

employee; An employee wants to know how many people are in all the groups he is assigned to.

Make sure everyone is counted only once. Exercise 5.1.2.  

Recall the data definition for classes that represent the teams in the town youth baseball league  2.1.2. Develop the methods to solve the following problems:

A player wants to know how many people are on his team; A coach want to know how many players are on all her teams; The league wants a list of all the winning teams; The league wants a list of all coaches of winning teams; The league wants to know the total number of players on all teams; A player wants to know whether her friend's team has a better record than her team; A player want to know whether his friend has the same coach as he.

Page 43: Cyclical data

Exercises Exercise 5.1.3.  

Return to the data definition for classes that represent the registrar system for a university. Develop the methods to solve the following problems:

Find out how many courses is a student taking; Figure out how many credits is a student taking this term; Looking at the course roster, an instructor want to know whether some student is enrolled in the

course; Use the methods from the previous problem to assure that a students cannot register for the same

course twice; Find out how many students are enrolled in a course; (Optional) Add enrollment limit to each course. Use the methods from the previous problem to

enforce enrollment limits for each course; A student wants to make sure she is enrolled in some course; A student want to find out whether she has a class with another student.

Exercise 5.1.4.   Return to the data definition for classes that represent the bookstore manager's list of books.

Develop additional methods to solve the following problems: Produce a list of all books some author published during the past three years; Produce a list of all authors who published books in the bookstore manager's list of books; Produce a list of all authors who published a book this year, if you have a list of all authors.