/************************************ * CS2510 Spring 2011 * Lecture #2 * Data Definitons using FunJava ************************************/ /* Class Diagram: Books and Authors +---------------+ +-----------------+ | Author |<--+ | Book | +---------------+ | +-----------------+ | String name | | | String title | | int yob | +----+ Author author | +---------------+ | double price | +-----------------+ */ // Respresents the Author of a Book class Author{ String name; int yob; Author(String name, int yob){ this.name = name; this.yob = yob; } } // Respresents a Book that someone might buy class Book{ String title; Author author; double price; Book(String title, Author author, double price){ this.title = title; this.author = author; this.price = price; } } // Examples for Books and Authors class BookExamples{ BookExamples(){} Author kspenc = new Author("K. Spencer", 2007); Book book1 = new Book("My Life", kspenc, 0.99); Book book2 = new Book("Your Life", kspenc, 1.55); Author bchad = new Author("B. Chadwick", 1980); Book book3 = new Book("LOL, OMG", bchad, 100.25); } /* Class Diagram: (I)Shapes +---------------+ | IShape | +---------------+ +-------+-------+ / \ +-+-+ | +------------------+-------------------+ | | | +------+-------+ +------+--------+ +------+-------+ | Circle | | Rect | | Overlay | +--------------+ +---------------+ +--------------+ | int rad | | int w | | IShape top | | String color | | int h | | IShape bot | +--------------+ | String color | +--------------+ +---------------+ */ // An Interface Representing different types of Shapes... interface IShape{ } // Represents a Circle with a radious and color class Circle implements IShape{ int radius; String color; Circle(int radius, String color){ this.radius = radius; this.color = color; } } // Represents a Ractangle with a width/height and color class Rect implements IShape{ int w; int h; String color; Rect(int w, int h, String color){ this.w = w; this.h = h; this.color = color; } } // Represents the Overlay-ing of two IShapes class Overlay implements IShape{ IShape top; IShape bot; Overlay(IShape top, IShape bot){ this.top = top; this.bot = bot; } } // Examples of IShapes class IShapeExamples{ IShapeExamples(){} IShape c1 = new Circle(20, "red"); IShape r1 = new Rect(10, 20, "blue"); IShape c2 = new Circle(50, "goldenrod"); IShape ov1 = new Overlay(this.c1, this.r1); IShape ov2 = new Overlay(new Overlay(this.c1, this.c2), this.r1); } // Examples class for all the examples class AllExamples{ AllExamples(){} BookExamples books = new BookExamples(); IShapeExamples shapes = new IShapeExamples(); }