// TSRJ 2008 - Advanced // Lecture 3: Data Definitions - Part 2 // 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; } } // 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; } } // 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; } } // 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; } } // We can use our new data definition for Time inside Show as follows // // To represent a recording of a show using Tivo. (Attempt 2) // // +-----------------+ // | Show2 | // |-----------------| // |String title | // |int channel | +-----------------+ // |Time time |------->| Time | // |int duration | |-----------------| // +-----------------+ |int hours | // |int minutes | // +-----------------+ class Show2 { String title; int channel; Time time; int duration; //Constructor Show2(String title, int channel, Time time, int duration){ this.title = title; this.channel = channel; this.time = time; this.duration = duration; } } class Examples { Examples() {} Flight ltojfk = new Flight("Logan", "JFK", 80, 21); Song pib = new Song("Paint it Black", "The Rolling Stones", "The Aftermath", "Rock"); Show mwc = new Show("Married With Children", 25, 2100, 1); Time t1 = new Time(22,15); Show2 mwc2 = new Show2("Married With Children", 25, new Time(21,0), 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. // 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 ;; class definition. */