1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Methods and union of classes (lập TRÌNH cơ bản SLIDE)

125 43 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 125
Dung lượng 451,89 KB

Nội dung

A2 Programming Course Methods and Unions of Classes Week 7,8,9 The Drawing Program Develop a drawing program that deals with at least three kinds of shapes: dots, squares, and circles The shapes are located on a Cartesian grid whose origin is in the northwest A dot is located in the grid and is drawn as a small disk of a fixed size (3 pixels) A square's location is specified via its north-west corner in the grid and its size A circle's essential properties are its center point and its radius Scheme data definitions • In the terminology of Scheme data definitions, a shape is one of three possible kinds of values More specifically, if we were to develop this program in Scheme, we would introduce five data definitions: one for shapes, one for dots, one for squares, one for circles, and one for Cartesian points • ;; A Shape is one of: ;; - a Dot ;; - a Square ;; - a Circle • (define-struct Dot (loc)) ;; A Dot is a structure: (make-Dot cartPt) • (define-struct Square (loc size)) ;; A Square is a structure: (make-Square cartPt Number) • (define-struct Circle (loc radius)) ;;A Circle is a structure: (make-Circle cartPt Number) • (define-struct CartPt (x y)) ;; A CartPt is a structure: (make-CartPt Number Number) New approach AShape # CartesianPoint location Dot Square - int size CartesianPoint - int x - int y Circle - int radius • Dot, Square, and Circle are SUBCLASSES or REFINED CLASSES of AShape • Because all instances of AShape are instances of either Dot, Square, or Circle, we say that AShape is an ABSTRACT CLASS or SUPERCLASS Class diagram • • • When people discuss classes, they often identify the commonalities (tương đồng) and the differences among the subclasses Also, they tend to attribute the commonalities to the superclass, and the differences to the subclasses A look at the diagram suggests that the three subclasses have one thing in common, namely, a location That is, each dot, square, or circle has a location Then again, circles differs from squares in that one has a radius and the other a size attribute; the dot doesn't have any other attributes AShape # CartesianPoint location Dot Square - int size CartesianPoint - int x - int y Circle - int radius Java data definitions //class CartesianPoint public class CartesianPoint { private int x; private int y; public CartesianPoint(int x, int y){ this.x=x; this.y=y; } } // class Shape public abstract class Shape{ protected CartesianPoint location; } //class Dot public class Dot extends AShape { public Dot(CartesianPoint location) { this.location = location; } } //Class Square public class Square extends AShape { private int side; public Square(CartesianPoint location, int side) { this.location = location;; this.side = side; } //class Circle public class Circle extends AShape { private int radius; public Circle(CartesianPoint location, int radius) { this.location = location; this.radius = radius; } Keyword • The keyword abstract in front of a class indicates that there are no instances of this class; it only exists to collect the instances of other classes under a single header • The keyword extends makes the Dot class a refinement or an extension of AShape therefore INHERITS all of AShape's fields The protected or abstract modifiers for attribute and method • protected: the class itself and its subclass can access this attribute / method • abstract: (just for method) a method signature An abstract method in an abstract class announces that the concrete subclasses have CONCRETE METHODS with the same signature and a proper method body Q: Review public and private modifiers for attribute and method Modifiers for class • None modifier: Classes in the same package can see this class • public: Classes in all packages can see this class • abstract: this is abstract class Encapsulation Test constructors public class TestCartesianPoint extends TestCase { public void testConstructor(){ CartesianPoint caPt1= new CartesianPoint (4,3); CartesianPoint caPt2= new CartesianPoint (5,12); CartesianPoint caPt3= new CartesianPoint (6,8); } //test for class CartesianPoint public class TestDot extends TestCase { public void testConstructor(){ CartesianPoint caPt1= new CartesianPoint (4,3); CartesianPoint caPt2= new CartesianPoint (5,12); CartesianPoint caPt3= new CartesianPoint (6,8); Dot d1 = new Dot(caPt1); Dot d2 = new Dot(caPt2); Dot d3 = new Dot(caPt3); } // test for class Dot 10 5.1.2 Solutions public abstract class AnItem { protected String branchName; protected double weight; protected double price; public AnItem(String branchName,double weight,double price){ this.branchName = branchName; this.weight = weight; this.price = price; } public double unitPrice(){ return this.price / this.weight; } public boolean lowerPrice(double amount){ return this.unitPrice() < amount; } public boolean cheaperThan(AnItem that){ return this.unitPrice() < that.unitPrice(); } } 111 Ice Cream public class IceCream extends AnItem{ private String flavor; private String package; public IceCream(String branchName, double weight, double price, String flavor, String package) { super(branchName, weight, price); this.flavor = flavor; this.package = package; } } 112 Coffee public class Coffee extends AnItem{ private String label; public Coffee(String label, String branchName, double weight, double price) { super(branchName,weight,price); this.label = label; } } 113 Juice public class Juice extends AnItem { private String flavor; private String package; public Juice(String branchName, double weight, double price, String flavor, String package) { super(branchName, weight, price); this.flavor = flavor; this.package = package; } } back 114 Exercise 5.1.3 • Recall the class hierarchies concerning taxi vehicles from exercise 4.1.3: ATaxiVehicle # int idNum # int passengers # int pricePerMile Cab Limo - int minRental Van - boolean access 115 5.1.3 Class Diagram ATaxiVehicle # int idNum # int passengers # int pricePerMile double fare(double numberOfMiles) boolean lowerPrice(double numberOfMiles, double amount) boolean cheaperThan(double numberOfMiles, ATaxiVehicle that) Cab + double fare(double numberOfMiles) Limo - int minRental Van - boolean access + double fare(double numberOfMiles) + double fare(double numberOfMiles) 116 Solution 5.1.3 public abstract class ATaxiVehicle{ protected int idNum; protected int passengers; protected int pricePerMile; public ATaxiVehicle(int idNum, int passengers, int pricePerMile){ this.idNum = idNum; this.passengers = passengers; this.pricePerMile = pricePerMile; } public abstract double fare(double numberOfMiles); public boolean lowerPrice(double numberOfMiles,double amount){ return this.fare(numberOfMiles) < amount; } public boolean cheaperThan(double numberOfMiles,ATaxiVehicle that){ return this.fare(numberOfMiles) < that.fare(numberOfMiles); } } 117 Cab public class Cab extends ATaxiVehicle { public Cab(int idNum,int passengers,int pricePerMile) { super(idNum,passengers,pricePerMile); } public double fare(double numberOfMiles) { return this.pricePerMile * numberOfMiles; } } 118 Limo public class Limo extends ATaxiVehicle { private int minRental; public Limo ( int minRental, int idNum, int passengers, int pricePerMile){ super (idNum,passengers, pricePerMile); this.minRental = minRental; } public double fare( double numberOfMiles) { if ( this.pricePerMile * numberOfMiles< minRental) return this.minRental; else return this.pricePerMile * numberOfMiles; } } 119 Van public class Van extends ATaxiVehicle { private boolean access; public Van ( boolean access, int idNum, int passengers, int pricePerMile) { super (idNum,passengers, pricePerMile); this.access = access; } public double fare(double numberOfMiles) { return this.pricePerMile * numberOfMiles + this.passengers; } } Back 120 5.1.4 Class Diagram ABook # String title # String author # double price # int publicationYear + double salePrice() + boolean cheaperThan(ABook that) + boolean sameAuthor(ABook that) Hardcover Sale Paperback + double salePrice() + double salePrice() + double salePrice() 121 5.1.4 Solution public abstract class ABook { protected String title; protected String author; protected double price; protected int publicationYear; public ABook(String title, String author, double price, int publicationYear){ this.title =title; this.author = author; this.price = price; this.publicationYear = publicationYear; } public abstract double salePrice(); public boolean cheaperThan(ABook that){ return this.salePrice() < that.salePrice(); } public boolean sameAuthor(ABook that){ return this.author.equals(that.author); } } 122 Hardcover Book public class Hardcover extends ABook { public Hardcover(String title, String author, double price, int publicationYear) { super (title, author, price, publicationYear); } public double salePrice() { return this.price * 0.8; } } 123 Sale Book public class Sale extends ABook { public Sale(String title, String author, double price, int publicationYear) { super(title, author, price, publicationYear); } public double salePrice() { return this.price * 0.5; } } 124 Paperbacks Book public class Paperback extends ABook{ public Paperback(String title, String author, double price, int publicationYear) { super(title, author, price, publicationYear); } public double salePrice() { return this.price; } } Back 125 ... Add at least two attributes per class of vehicle 19 Prepare for next week How to design class hierarchy • III  Fun Methods       5 ? ?Methods and Unions of Classes 20 Relax & …Do Exercises … GOOD... template for each of the subclasses of AShape, the Example and Implementation steps 38 Extracting Common Methods to Subclasses • Q: Is there any difference in the implementations of distanceToO()... x - int y Circle - int radius • Dot, Square, and Circle are SUBCLASSES or REFINED CLASSES of AShape • Because all instances of AShape are instances of either Dot, Square, or Circle, we say that

Ngày đăng: 29/03/2021, 10:41

TỪ KHÓA LIÊN QUAN