Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Square.java 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. |<--- this code is formatted to fit into 80 columns --->|
  10. |<--- this code is formatted to fit into 80 columns --->|
  11. |<--- this code is formatted to fit into 80 columns --->|
  12. */
  13. package tracing;
  14. /**
  15. *
  16. * Square is a 2D shape. It extends the TwoDShape class with the side
  17. * variable, and it implements TwoDShape's abstract methods for
  18. * correctly computing a square's area and distance.
  19. *
  20. */
  21. public class Square extends TwoDShape {
  22. protected double s; // side
  23. /*
  24. * All sorts of constructors
  25. */
  26. public Square(double x, double y, double s) {
  27. super(x, y); this.s = s;
  28. }
  29. public Square(double x, double y) {
  30. this(x, y, 1.0);
  31. }
  32. public Square(double s) {
  33. this(0.0, 0.0, s);
  34. }
  35. public Square() {
  36. this(0.0, 0.0, 1.0);
  37. }
  38. /**
  39. * Returns the perimeter of this square
  40. */
  41. public double perimeter() {
  42. return 4 * s;
  43. }
  44. /**
  45. * Returns the area of this square
  46. */
  47. public double area() {
  48. return s*s;
  49. }
  50. /**
  51. * This method overrides the one in the superclass. It adds some
  52. * circle-specific information.
  53. */
  54. public String toString() {
  55. return ("Square side = " + String.valueOf(s) + super.toString());
  56. }
  57. }