26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 Object-Oriented <strong>Program</strong>ming 497<br />

examples) can process different types of GUI events, as we will see in Chapter 12 and<br />

Chapter 13.<br />

1 // Fig. 9.29: Circle.java<br />

2 // Definition of class Circle<br />

3<br />

4 public class Circle extends Point { // inherits from Point<br />

5 protected double radius;<br />

6<br />

7 // no-argument construc<strong>to</strong>r<br />

8 public Circle()<br />

9 {<br />

10 // implicit call <strong>to</strong> superclass construc<strong>to</strong>r here<br />

11 setRadius( 0 );<br />

12 }<br />

13<br />

14 // construc<strong>to</strong>r<br />

15 public Circle( double circleRadius, int xCoordinate,<br />

16 int yCoordinate )<br />

17 {<br />

18 // call superclass construc<strong>to</strong>r<br />

19 super( xCoordinate, yCoordinate );<br />

20<br />

21 setRadius( circleRadius );<br />

22 }<br />

23<br />

24 // set radius of Circle<br />

25 public void setRadius( double circleRadius )<br />

26 {<br />

27 radius = ( circleRadius >= 0 ? circleRadius : 0 );<br />

28 }<br />

29<br />

30 // get radius of Circle<br />

31 public double getRadius()<br />

32 {<br />

33 return radius;<br />

34 }<br />

35<br />

36 // calculate area of Circle<br />

37 public double area()<br />

38 {<br />

39 return Math.PI * radius * radius;<br />

40 }<br />

41<br />

42 // convert Circle <strong>to</strong> a String represention<br />

43 public String <strong>to</strong>String()<br />

44 {<br />

45 return "Center = " + super.<strong>to</strong>String() +<br />

46 "; Radius = " + radius;<br />

47 }<br />

Fig. Fig. 9.29 9.29 Circle subclass of Point—indirect implementation of interface Shape<br />

(part 1 of 2).<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!