440 likes | 570 Views
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.
E N D
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.
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.
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???
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.
AuthorTest.java • publicclass AuthorTest extends TestCase { • publicvoid 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); • } • }
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() { • returnthis.name; • } • public String toString() { • returnthis.name + ", " + this.dob + "\n" + this.books; • } • publicvoid addBook(Book book) { • this.books = this.books.add(book); • } • }
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() { • returnthis.author.getName() + ", " + this.title + ", " + this.price + ", " + this.year + "\n"; • } • }
ALoBooks.java, MTLoBooks.java publicabstractclass ALoBooks { publicabstract ALoBooks add(Book book); } • publicclass MTLoBooks extends ALoBooks { • public ALoBooks add(Book book) { • returnnew Cons(book, new MTLoBooks()); • } • public String toString() { • return "\n"; • } • }
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) { • returnnew ConsLoBooks(book, this); • } • public String toString() { • returnthis.fst + " " + this.rst; • } • }
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.
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
Ashmont Downtown State Maverick Wonderland Kenmore Park Center North Science Charles Bowdoin Train routes
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???
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.
RouteTest.java • publicclass RouteTest extends TestCase { • publicvoid 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); • // print stations on green route • System.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 • } • }
Station.java • publicclass Station { • private String name; • private ALoRoutes routes; • public Station(String name) { • this.name = name; • this.routes = new EmptyLoRoutes(); • } • public String getName() { • returnthis.name; • } • public String toString() { • return "Station " + this.name + ": " + this.routes.getNames(); • } • void addRoute(Route route) { • //another solution • this.routes = new ConsLoRoutes(route, routes); • } • }
Route.java • publicclass 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() { • returnthis.name; • } • public String toString() { • return "Route " + this.name + ": " + this.stations; • } • publicvoid addStation(Station station) { • station.addRoute(this); • this.stations = new ConsLoStations(station, stations); • } • }
ALoRoutes.java, EmptyLoRoutes.java publicabstractclass ALoRoutes { publicabstract String getNames(); } • publicclass EmptyLoRoutes extends ALoRoutes { • public String getNames() { • return ""; • } • }
ConsLoRoutes.java • publicclass ConsLoRoutes extends ALoRoutes { • private Route first; • private ALoRoutes rest; • public ConsLoRoutes(Route first, ALoRoutes rest) { • this.first = first; • this.rest = rest; • } • public String getNames() { • returnthis.first.getName() + " " + this.rest.getNames(); • } • }
ALoStations.java, EmptyLoStation.java publicabstractclass ALoStations { } • publicclass EmptyLoStations extends ALoStations { • public String toString() { • return ""; • } • }
ConLoStations.java • publicclass ConsLoStations extends ALoStations { • private Station first; • private ALoStations rest; • public ConsLoStations(Station first, ALoStations rest) { • this.first = first; • this.rest = rest; • } • public String toString() { • returnthis.first.getName() + " " + this.rest; • } • }
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.
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
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???
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.
CourseTest.java • publicclass CourseTest extends TestCase { • publicvoid 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 • // create student Ernie • // create student Rob • System.out.println(math); • System.out.println(physics); • System.out.println(biology); • System.out.println(music); • } • }
Course.java • publicclass Course { • private String name; • privateint credits; • private ALoStudents students; • public Course(String name, int credits) { • this.name = name; • this.credits = credits; • this.students = new EmptyLoStudents(); • } • publicvoid addStudent(Student student) { • this.students = new ConsLoStudents(student, this.students); • } • public String getName() { • returnthis.name; • } • public String toString() { • return "Course " + this.name + ": " + this.students.getNames(); • } • }
Student.java • publicclass Student { • privateint 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() { • returnthis.name; • } • publicvoid addCourse(Course course) { • this.courses = new ConsLoCourses(course, this.courses); • course.addStudent(this); • } • }
ALoCourses.java, EmptyLoCourses.java publicabstractclass ALoCourses { publicabstract String getNames(); } • publicclass EmptyLoCourses extends ALoCourses { • public String getNames() { • return ""; • } • }
ConsLoCourses.java • publicclass ConsLoCourses extends ALoCourses { • private Course first; • private ALoCourses rest; • public ConsLoCourses(Course first, ALoCourses rest) { • this.first = first; • this.rest = rest; • } • public String getNames() { • returnthis.first.getName() + " " + this.rest.getNames(); • } • }
ALoStudents.java, EmptyLoStudents.java publicabstractclass ALoStudents { publicabstract String getNames(); } • publicclass EmptyLoStudents extends ALoStudents { • public String getNames() { • return ""; • } • }
ConsLoStudents.java • publicclass ConsLoStudents extends ALoStudents { • private Student first; • private ALoStudents rest; • public ConsLoStudents(Student first, ALoStudents rest) { • this.first = first; • this.rest = rest; • } • public String getNames() { • returnthis.first.getName() + " " + this.rest.getNames(); • } • }
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.
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.
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?
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.
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.