Advertisement
yko0

Figures-oriented-task

Dec 6th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. public abstract class Figure {
  2.     double r1;
  3.     double dx0 = 0.0;   //x_center
  4.     double dy0 = 0.0;   //y_center
  5.    
  6.     Figure (double a) { r1 = a; }
  7.    
  8.     abstract double area();
  9.     abstract String name();
  10.    
  11.     void move (double dx, double dy) {
  12.         dx0 += dx;
  13.         dy0 += dy;
  14.     }
  15. }
  16.  
  17. class Square extends Figure {
  18.    
  19.     Square (double a) { super(a); }
  20.    
  21.     double area() { return Math.pow(r1,2); }
  22.    
  23. //  public String toString() does the same
  24.     String name() { return "Это квадрат со стороной " + r1; }  
  25. }
  26.  
  27. class Rectangle extends Square {
  28.    
  29.     double r2;
  30.     Rectangle (double a, double b) {
  31.         super(a);
  32.         r2 = b;
  33.     }
  34.        
  35.     double area() { return r1*r2; }
  36.    
  37.     String name() { return "Это четырехугольник со сторонами " + r1 + "," + r2; }
  38. }
  39.  
  40. class Circle extends Figure {
  41.     Circle (double a) { super(a); } //radius
  42.    
  43.     double area() { return Math.PI*r1*r1; }
  44.    
  45.     String name() { return "Это круг радиусом " + r1;    }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement