#8_Methods and Union of Classes_16_v3.ppt

103 425 2
#8_Methods and Union of Classes_16_v3.ppt

Đ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

Methods and Unions of Classes Week 7, 8 HOW TO DESIGN CLASS HIERARCHIES Basic Java Programming Course material developed by Mai Anh Tho (tho@hcmuaf.edu.vn) Course material developed by Mai Anh Tho (tho@hcmuaf.edu.vn) Recall the Drawing Program Dot Square - int size Circle - int radius AShape # CartesianPoint location CartesianPoint - int x - int y 11 Requirements 1. Compute the area of a shape 2. Compute the distance of a shape to the origin 3. Determine whether some point is inside the shape 4. Compute the bounding box of a shape • All of these methods clearly work with shapes in general but may have to compute different results depending on the concrete shape on which they are invoked • In an object-oriented language, we can indicate that all AShape's have such method with ABSTRACT methods Abstract Methods • An abstract method isn't a method; it is just a method signature preceded with the keyword abstract • An abstract method in an abstract class announces that the concrete subclasses have CONCRETE METHODS with the same signature and a proper method body 1. Computing Area of A Shape Augmenting AShape public abstract class AShape { protected CartesianPoint location; protected AShape(CartesianPoint location) { this.location = location; } public abstract double area(); } • Q: Create a method template for each of the subclasses of AShape Template for Dot public class Dot extends AShape { public Dot(CartesianPoint location) { super(location); } public double area() { this.location } } Tips: a class extends other class, it inherits all fields of this class such as Circle, Square,Dot extends AShape so that it inherit field “location” of AShape class. Template for Square public class Square extends AShape { private int side; public Square(CartesianPoint location, int side) { super(location); this.side = side; } public double area() { this.location this.side } } Template for Circle public class Circle extends AShape { private int radius; public Circle(CartesianPoint location, int radius) { super(location); this.radius = radius; } public double area() { this.location this.radius } } • Q: Do the Example step Examples new Dot(new CartesianPoint(4, 3)).area() // should be 0.0 new Square(new CartesianPoint(4, 3), 30).area() // should be 900.0 new Circle(new CartesianPoint(5, 5), 20).area() // should be 1256.6 • Q: Implement the body of all area() methods [...]... AShape, because shapes have more information than the point and we don’t need to identify which kind of shapes?  A given point is inside a DOT when it equals this DOT  a given point is inside a CIRCLE if its distance to the center of the CIRCLE is less than or equal the radius  A given point is inside a SQUARE when it is between two pairs of lines Augmenting AShape • Q: Add contains() to AShape public... } public abstract double area(); public abstract double distanceToO(); } • Q: Create a method template for each of the subclasses of AShape, do the Example and Implementation steps Extracting Common Methods to Subclasses • Q: Is there any difference in the implementations of distanceToO() in all the subclasses • A: No, there is no difference at all • To remove similarities, we could pull distanceToO()... customer-oriented software development Augmenting CartesianPoint public class CartesianPoint { private int x; private int y; public CartesianPoint(int x, int y) { this.x = x; this.y = y; } public double distanceToO() { return Math.sqrt(this.x * this.x + this.y * this.y); } public boolean isInside(AShape s){ …this.x… …this.y… …s… } } • Q: How to implement isInside() method? • A: based on the relation of shape and. .. distance of a shape to the origin  What is the distance between a shape and the origin? 0 X Y Augmenting AShape • Q: Add distanceToO() to AShape public abstract class AShape { protected CartesianPoint location; protected AShape(CartesianPoint location) { this.location = location; } public abstract double area(); public abstract double distanceToO(); } • Q: Create a method template for each of the subclasses... double distanceToO() { return this.location.distanceToO(); } public abstract double area(); public abstract boolean contains(CartesianPoint point); } • Q: Create a method template for each of the subclasses of AShape and do the Example step Examples new Dot(new CartesianPoint(100, 200)).contains(new CartesianPoint(100, 200)) // should be true new Dot(new CartesianPoint(100, 200)).contains(new CartesianPoint(80,... (!(obj instanceof CartesianPoint)) return false; else { CartesianPoint that = (CartesianPoint) obj; return (this.x == that.x) && (this.y == that.y); } }  A given point is inside a DOT when it equals this DOT  a given point is inside a CIRCLE if its distance to the center of the CIRCLE is less than or equal the radius  A given point is inside a SQUARE when it is between two pairs of lines distanceTo()... 0), 10).contains(new CartesianPoint(12, 5)) // should be false Domain Knowledge • How to determine whether some point is inside the shape is a kind of knowledge called DOMAIN KNOWLEDGE • To comprehend the domain knowledge, we sometimes look up in a book and in other situations we gather from experts isInside() in CartesianPoint public boolean isInside(AShape shape) { return shape.contains(this); } contains()... this.location.distanceTo(point) . template for each of the subclasses of AShape, do the Example and Implementation steps Extracting Common Methods to Subclasses • Q: Is there any difference in the implementations of distanceToO(). Compute the area of a shape 2. Compute the distance of a shape to the origin 3. Determine whether some point is inside the shape 4. Compute the bounding box of a shape • All of these methods clearly. Methods and Unions of Classes Week 7, 8 HOW TO DESIGN CLASS HIERARCHIES Basic Java Programming Course material

Ngày đăng: 16/07/2014, 17:00

Mục lục

  • Methods and Unions of Classes

  • Recall the Drawing Program

  • 1. Computing Area of A Shape

  • 2. Computing the distance of a shape to the origin

  • Extracting Common Methods to Subclasses

  • 3. Determining whether some point is inside the shape

  • 4. Computing the bounding box of a shape

  • What is a Bounding Box?

  • A Summary of Important Points

  • Relax & …Do Exercises …

  • The Fuel Consumption Problem

  • A gallery Class Diagram

Tài liệu cùng người dùng

Tài liệu liên quan