;; CS U213 Spring 2007 ;; Lecture 2: January 10, 2007 ;; Data Definitions in Scheme ;; --------------------------- ;; ;; Plane Flight ;; ------------ ;; ;; To represent a plane trip ;; ;; A Flight is (make-flight String String Number Number) ;; interpretation: (make-flight o d p s) creates a Flight with ;; o as the origin airport, ;; d as the destination airport, ;; p as the price of the flight and ;; s as the seat number. (define-struct flight (departure arrival cost seat)) ;; Examples: ;; --------- (make-flight "Logan" "JFK" 80 21) ;; Songs in Albums ;; --------------- ;; ;; To represent a song in an album ;; ;; A Song is (make-song String String String String) ;; interpretation: (make-song t a b g) creates a song with ;; t as the song title ;; a as the artist name ;; b as the album name ;; g as the song's genre (define-struct song (title artist album genre)) ;; Examples: ;; -------- (make-song "Paint it Black" "The Rolling Stones" "Aftermath" "Rock") ;; Tivo Programs ;; ------------- ;; ;; To represent a recording of a show using Tivo. ;; ;; A Show is (make-show String Number Number Number) ;; interpretation: (make-show n c t d) creates a recording for a show with ;; n as the name of the show ;; c as the channel's number ;; t as the time the show starts ;; d as the duration of the show (define-struct show (name channel time duration)) ;; Examples: ;; -------- (define mwc (make-show "Married With Children" 25 2100 1)) ;; Scheme functions to work with our data definitions ;; -------------------------------------------------- (show-name mwc) ;; "Married With Children" (show-channel mwc) ;; 25 (show-time mwc) ;; 2100 (show-duration mwc) ;; 1 (show? mwc) ;; true ;; The above should be a refresher on data definitions and scheme. ;; How do we go about doing the same thing in Java? ;; NOTE: To start a one-line comment in Java we use '//' ;; Up till now you can run the given code using the Beginner HtDP language. ;; The rest of these notes run in ProfessorJ Begginer Language. // Class Diagram // ------------- // // A diagram that specifies the name of a Class and its fields. // Think of Class Diagrams as a way of representing the // text (comments) part of the data definition in Scheme. // This representation can be used for Scheme as well, but is very // useful in class based languages like Java, where the actual data definition // is quite verbose. // // To represent a plane trip // // +-----------------+ // | Flight | // |-----------------| // |String departure| // |String arrival | // |int cost | // |int seat | // +-----------------+ class Flight { String departure; String arrival; int cost; int seat; //Constructor Flight(String departure, String arrival, int cost, int seat){ this.departure = departure; this.arrival = arrival; this.cost = cost; this.seat = seat; } } class Examples { Examples() {} Flight ltojfk = new Flight("Logan", "JFK", 80, 21); } // Notice, that you have to define the constructor yourselves - ProfessorJ // Begginer Language does not do it for you. // To create an instance of your class you need to use the keyword // 'new' before you call your class' constructor. Instances of classes // are called 'objects'. To call a constructor you need to provide the // name of the constructor, 'Flight', and the arguments to the constructor // in a comma separated list inside parens, ("Logan", "JFK", 80, 21). // To name an object so that you can refer to it later you need to provide // the class name and then the name you want to use, i.e. 'Flight ltojfk' // followed by an equal sign '='. // You can think of this as saying that the name ltojfk can be used to refer // only to objects created by the class Flight and I want it to refer to the // instance that has "Logan" as the departure airport, "JFK" as the arriving // airport, it costs $80 and is assigned seat number 21. // To represent a song in an album // // +-----------------+ // | Song | // |-----------------| // |String title | // |String artist | // |String album | // |String genre | // +-----------------+ class Song { String title; String artist; String album; String genre; //Constructor Song(String title, String artist, String album, String genre){ this.title = title; this.artist = artist; this.album = album; this.genre = genre; } } class Examples { Examples() {} Song mwc = new Song("Paint it Black", "The Rolling Stones", "The Aftermath", "Rock"); } // To represent a recording of a show using Tivo. // // +-----------------+ // | Show | // |-----------------| // |String title | // |int channel | // |int time | // |int duration | // +-----------------+ class Show { String title; int channel; int time; int duration; //Constructor Show(String title, int channel, int time, int duration){ this.title = title; this.channel = channel; this.time = time; this.duration = duration; } } class Examples { Examples() {} Show mwc = new Show("Married With Children", 25, 2100, 1); } // Look at the data definition for Song in Java. Specifically look at the field // with the name 'time' inside the class Song. We are using an int to represent // time. int is a Java primitive type-of-data for integers, But that is not how // humans think of time. // Examples // -------- // 20:00 (8:00 PM) // 20:30 (8:30 PM) // 21:20 (8:20 PM) // 7:20 (7:20 AM) // // // +-----------------+ // | Time | // |-----------------| // |int hours | // |int minutes | // +-----------------+ // class Time { int hours; int minutes; Time (int hours, int minutes){ this.hours = hours; this.minutes = minutes; } } class Examples { Examples() {} Time t1 = new Time(22,15); } // We can use our new data definition for Time inside Show as follows // // To represent a recording of a show using Tivo. (Attempt 2) // // +-----------------+ // | Show | // |-----------------| // |String title | // |int channel | +-----------------+ // |Time time |------->| Time | // |int duration | |-----------------| // +-----------------+ |int hours | // |int minutes | // +-----------------+ class Show { String title; int channel; Time time; int duration; //Constructor Show(String title, String channel, Time time, String duration){ this.title = title; this.channel = channel; this.time = time; this.duration = duration; } } class Examples { Examples() {} Show mwc = new Show("Married With Children", 25, new Time(21,00), 1); } // Note how class names start with a capital letter (e.g. Show, Time etc.). // Also how the arrows in the our class diagrams correspond to the arrows // that we draw in data definitions in Scheme when one define-struct refers // to another define-struct. // // Notice how the Java code we have been writing to represent our class diagrams // is boilerplate code, i.e. a task that a program could do. In fact inside ProfessorJ // there is a scheme program that consumes a class diagram (more precisely information // from a class diagram) and generates the boilerplate Java code. // So back to scheme ... ;; A ClassDef is (make-cd String [Listof FieldDef]) ;; interpretation: (make-cd c (list f1 f2)) creates a class definition with ;; c as the class name ;; f1 and f2 as the fields inside c ;; ;; A FieldDef is (make-fd String String) ;; interpretation: (make-fd t n) creates a field definition with ;; t as the type of data that the field can hold ;; n as the name for the field (define-struct fd (type name)) (define-struct cd (name fields)) ;; Examples: ;; -------- (define show-cd (make-cd "Show" (list (make-fd "String" "title") (make-fd "int" "channel") (make-fd "Time" "time") (make-fd "int" "duration")))) ;; Challenge: Write a Scheme program that consumes a ClassDef and produces ;; a String that represents a Java constructor for the class defined by this ;; classd definition.