/* --- CSU213 Spring 2006 Lecture Notes --------- Copyright 2006 Viera K. Proulx Lecture 1: All I Ever Needed to Know I Learned in Kindergarden */ /* Goals: - recall data definitions from HtDP - translate data definitions into class diagrams and Java code - understand that the concepts are the same Consider the library catalog that contains information about books, CDs, and magazines. What information does it contain? Books: title, author, catalog number, ... and more CDs: title, artist, number of tracks, catalog number, ... and more Magazines: title, volume or year, issue, catalog number, ... and more Let us make examples of this kind of data: book: Cat in a Hat by Dr Seuss, number 77 book: HtDP, by Matthias, number 42 cd: Thriller, with Michael Jackson, 10 tracks, number 99 cd: Pearl, by Janis Joplin, 12 tracks, number 88 magazine: Time, issue 52 in 2004, number 33 magazine: Wired, issue 3 in 2004, number 44 The data definition in HtDP would then be: ;;A Library item is one of (define-struct book (title author no)) ;; where title and author are Strings and no is a Number (define-struct cd (title artist tracks no)) ;; where title and artist are Strings and tracks and no are Numbers (define-struct mag (title year issue no)) ;; where title is a String and year, issue and no are Numbers and we could make examples of the information shown above represented as data: (define ch (make-book "Cat in a Hat" "Dr Seuss" 77)) (define htdp (make-book "HtDP" "Matthias F" 42)) (define thriller (make-cd "Thriller" "Michael Jackson" 10 99)) (define pearl (make-cd "Pearl" "Janis Joplin" 12 88)) (define time (make-mag "Time" 2004 52 33)) (define wired (make-mag "Wired" 2004 3 44)) ;; DrScheme then gives us the following functions to deal with this data: (book? ch) ;; --> produces true (book? pearl) ;; --> produces false (book-title htdp) ;; --> produces "HtDP" (book-author ch) ;; --> produces "Dr Seuss" (book-no ch) ;; --> produces 77 We have similar functions for the other two structures: cd and mag. We can represent all the information about books in the following diagram: +---------------+ | Book | +---------------+ | String title | | String author | | int no | +---------------+ In Java, this represents a class of data, the component data parts are called fields, or member data, and the description of the type of data for each field has to be explicitly stated as a part of the Java class definition. Each field can hold only one type of data - we cannot say that author is either a String or boolean. We also have to write the constructor explicitly ourselves, though the code always looks the same. The numbers come in two flavors: whole numbers, known as 'int' and inexact numbers that typically represent fractions, known as 'double'. Later, we will se variations of these. The complete Java definition of the class Book is shown below: */ // to represent a book class Book { String title; String author; int no; Book(String title, String author, int no) { this.title = title; this.author = author; this.no = no; } } /* The examples of books then are defined as follows: Book ch = new Book("Cat in a Hat", "Dr Seuss", 77); Book htdp = new Book("HtDP", "Matthias F", 42); These definitions can be written in the Interactions Window, or become data definitions is a class that holds our examples and tests. (See the lab for details.) For practice, make class diagrams and define Java classes to represent Cds and magazines. */ /* Part 2: Containment Imagine that we would like more information about the author, such as the year of birth. It is clear, that the information about the author should not be defined together with the book object, as there may be several books written by the same author. We can design the class to represent authors, and have the class Book contain an instance of the class Author. All books written by the same author should contain the same instance of Author. Here are the revised examples: book: HtDP by Matthias, (born in ??1920 - he is not that old, but what do we know), catalog number 42 book: ALJaFP by Matthias, (same one), catalog number 29 book Cat in a Hat, by Dr Seuss, born in 1915, catalog number 77 The two class definition then become: */ /* +-------------+ | Author | +-------------+ | String name | | int year | +-------------+ */ // to represent an author class Author { String name; int year; Author(String name, int year) { this.name = name; this.year = year; } } /* +---------------+ | Book2 | +---------------+ | String title | | Author author | | int no | +---------------+ */ // to represent a book class Book2 { String title; Author author; int no; Book2(String title, Author author, int no) { this.title = title; this.author = author; this.no = no; } } /* Additionally, we would like the class diagram to represent the containment relationship between the two classes. we use 'containment arrow' as follows: +---------------+ | Book2 | +---------------+ | String title | | Author author |---------+ | int no | | +---------------+ v +-------------+ | Author | +-------------+ | String name | | int year | +-------------+ We now need to make examples of our information represented as instances of these classes: Author matthias = new Author("Matthias", 1920); Author drseuss = new Author("Dr Seuss", 1915); Book htdp = new Book("HtDP", matthias, 42); Book aljafp = new Book("ALJaFP", matthias, 29); Book ch = new Book("Cat in a Hat", drseuss, 77); */