Docsity

Java Program to Create and Order a Catalog of Plays - Prof. Douglas J. Wokoun, Assignments of Computer Science

Partial preview of the text

Download Java Program to Create and Order a Catalog of Plays - Prof. Douglas J. Wokoun and more Assignments Computer Science in PDF only on Docsity!

CS251 Intermediate Programming

Lecture 24

Data Structures

  • Final Monday, December 15.• Homework 6 due next Wednesday.• Last Quiz next Monday.

keySet()

Set

values()

Collection

iterator()

compareTo()

compare()

{ public class Play Here is the basic data model class:

public Play(String title,private int year;private String genre;private String title; String genre, int year)

{ this.year = year;this.genre = genre;this.title = title;

{ public String getTitle()} return this.title;

{ public String getGenre()} return this.genre;

}

{ public int getYear() return this.year;

{ public String toString()} return this.title + ", " + ')';this.genre + " (" + this.year +

}

}

{ public class TestPlays And this is the start of the test harness:

{ public static void main(String[] args) Play[] plays = new Play[] {// Set up the data new Play("The Merchant of Venice", "Comedy", 1597)new Play("Macbeth", "Tragedy", 1606),new Play("As You Like It", "Comedy", 1599),new Play("Henry VI", "History", 1592),new Play("A Midsummer Night's Dream", "Comedy", 1595),new Play("Othello", "Tragedy", 1603),

};

}

}

public class Play implementsAdding a natural ordering to the model class:

Comparable

{ public Play(String title,private int year;private String genre;private String title; String genre, int year)

{ this.year = year;this.genre = genre;this.title = title;

{ public String getTitle()} return this.title;

{ public String getGenre()} return this.genre;

}

{ public int getYear() return this.year;

{ public String toString()} return this.title + ", " + ')';this.genre + " (" + this.year +

{ public int compareTo(Play p)} return this.title.compareTo( p.title);

}

}

public class Play implements Adding a natural ordering to the model class:

Comparable

{ public Play(String title,private int year;private String genre;private String title; String genre, int year)

{ this.year = year;this.genre = genre;this.title = title;

{ public String getTitle()} return this.title;

{ public String getGenre()} return this.genre;

}

{ public int getYear() return this.year;

{ public String toString()} return this.title + ", " + ')';this.genre + " (" + this.year +

{ public int compareTo(Play p)} return this.title.compareTo( p.title);

}

}

Creating a catalog of
Play
s ordered alphabetically by title:

{ public class TestPlays import java.util.TreeSet; { public static void main(String[] args) Play[] plays = new Play[] {// Set up the data new Play("The Merchant of Venice", "Comedy", 1597)new Play("Macbeth", "Tragedy", 1606),new Play("As You Like It", "Comedy", 1599),new Play("Henry VI", "History", 1592),new Play("A Midsummer Night's Dream", "Comedy", 1595),new Play("Othello", "Tragedy", 1603),

for (Play p : plays)TreeSet titleSet = new TreeSet();}; titleSet.add(p);

for (Play p : titleSet) System.out.println(p);

}

}

Creating a catalog of
Play
s ordered alphabetically by title:

{ public class TestPlays import java.util.TreeSet; { public static void main(String[] args) Play[] plays = new Play[] {// Set up the data new Play("The Merchant of Venice", "Comedy", 1597)new Play("Macbeth", "Tragedy", 1606),new Play("As You Like It", "Comedy", 1599),new Play("Henry VI", "History", 1592),new Play("A Midsummer Night's Dream", "Comedy", 1595),new Play("Othello", "Tragedy", 1603),

for (Play p : plays)TreeSet titleSet = new TreeSet();}; titleSet.add(p);

for (Play p : titleSet) System.out.println(p);

}

}

Creating a catalog of
Play
s ordered alphabetically by title:

{ public class TestPlays import java.util.TreeSet; { public static void main(String[] args) Play[] plays = new Play[] {// Set up the data new Play("The Merchant of Venice", "Comedy", 1597)new Play("Macbeth", "Tragedy", 1606),new Play("As You Like It", "Comedy", 1599),new Play("Henry VI", "History", 1592),new Play("A Midsummer Night's Dream", "Comedy", 1595),new Play("Othello", "Tragedy", 1603),

for (Play p : plays)TreeSet titleSet = new TreeSet();}; titleSet.add(p);

for (Play p : titleSet) System.out.println(p);

}

}

Creating a catalog of
Play
s ordered alphabetically by title:

{ public class TestPlays import java.util.TreeSet; { public static void main(String[] args) Play[] plays = new Play[] {// Set up the data new Play("The Merchant of Venice", "Comedy", 1597)new Play("Macbeth", "Tragedy", 1606),new Play("As You Like It", "Comedy", 1599),new Play("Henry VI", "History", 1592),new Play("A Midsummer Night's Dream", "Comedy", 1595),new Play("Othello", "Tragedy", 1603),

for (Play p : plays)TreeSet titleSet = new TreeSet();}; titleSet.add(p);

for (Play p : titleSet) System.out.println(p);

}

}

What we have in memory at this point looks something like this:

genre = Tragedyname = Macbeth date = 1606

genre = Tragedyname = Othello date = 1603

date = 1592^ genre = Historyname = Henry VI

genre = Comedyname = As You Like It date = 1599

genre = Comedy

name = The Merchant of Venice

date = 1597

genre = Comedy

name = A Midsummer Night's Dream

date = 1595

Play[] plays

TreeSet titleSet

We'll use anothercatalog.To allow a listing of the plays in date order, we'll create a date

TreeSet

to do this, but we've already "used

up" the

Comparable

interface, so now we'll need to create an

external

Comparator

to do this ordering.

We create a new class that knows how to order

Play

s by date.

Adding a catalog of
Play
s ordered by date to the test harness:

{ public class TestPlays import java.util.TreeSet; { public static void main(String[] args) Play[]// Set up the data

(^) plays

(^) = (^) new

(^) Play[]

(^) { (^) stuff

(^) };

for (Play p : plays)TreeSet titleSet = new TreeSet(); titleSet.add(p);

for (Play p : titleSet) System.out.println(p);

for (Play p : plays)TreeSet dateSet = new TreeSet(new PlayYearComparator()); dateSet.add(p);

for (Play p : dateSet) System.out.println(p);

}

}

Adding a catalog of
Play
s ordered by date to the test harness:

{ public class TestPlays import java.util.TreeSet; { public static void main(String[] args) Play[]// Set up the data

(^) plays

(^) = (^) new

(^) Play[]

(^) { (^) stuff

(^) };

for (Play p : plays)TreeSet titleSet = new TreeSet(); titleSet.add(p);

for (Play p : titleSet) System.out.println(p);

for (Play p : plays)TreeSet dateSet = new TreeSet(new PlayYearComparator()); dateSet.add(p);

for (Play p : dateSet) System.out.println(p);

}

}