Advertisement
Guest User

Untitled

a guest
Mar 28th, 2021
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. public class TestAll {
  2.  
  3.     public static void main(String args[]) {
  4.        
  5.          Circle c1 = new Circle(); {
  6.              radius = 5;
  7.          }
  8.            
  9.        
  10.        
  11.         Point p1 = new Point(); {
  12.             width = 5;
  13.         }
  14.          Point p2 = new Point(); {
  15.             width = 5;
  16.             height = 7;
  17.          }
  18.          Square s1 = new Square(p1);
  19.          
  20.          
  21.          Rectangle r1 = new Rectangle(p2);
  22.    
  23.          
  24.         System.out.println("Details of c1:");
  25.         System.out.println("radius: " + c1.radius());
  26.         System.out.println("area: " + c1.area());
  27.         System.out.println("perimeter: " + c1.perimeter());
  28.         System.out.println("Details of s1: ");
  29.         System.out.println("side length: " + s1.width());
  30.         System.out.println("area: " + s1.area());
  31.         System.out.println("perimeter: " + s1.perimeter());
  32.         System.out.println("Details of r1: ");
  33.         System.out.println("width: " + r1.width());
  34.         System.out.println("height: " + r1.height());
  35.         System.out.println("area: " + r1.area());
  36.         System.out.println("perimeter: " + r1.perimeter());
  37.    
  38.     }  
  39.  
  40.     }}
  41. 1./////////////////////////////////////////////////////////////////////
  42.  
  43. public class Point {
  44.     private int width;
  45.     private int height;
  46.     public Point(int theWidth, int theHeight) {
  47.         width = theWidth;
  48.         height = theHeight;
  49.     }
  50.     public int getWidth() {
  51.     return width;
  52.     }
  53.     public int getHeight() {
  54.         return height;
  55.         }
  56.     public void setHeight(int theHeight) {
  57.          height = theHeight;
  58.         }
  59.     public void setWidth(int theWidth) {
  60.          width = theWidth;
  61.         }
  62. }
  63. 2.///////////////////////////////////////
  64. public interface FigureGeometry
  65. {
  66.  
  67.    
  68.     final float PI = 3.14f;
  69.  
  70.  
  71.    
  72.     float getArea ();    
  73.     float getPerimeter ();
  74.  
  75. }
  76. 3.//////////////////////////////////////////////
  77. public class Circle implements FigureGeometry {
  78.  
  79.    
  80.     private float radius;
  81.  
  82. public Circle(float theRadius) {
  83.     radius = theRadius;
  84. }
  85.    
  86.     public float getRadius ()
  87.     {
  88.         return radius;
  89.     }
  90.  
  91.  
  92.    
  93.     public float getArea ()
  94.     {
  95.         return getRadius() * getRadius() * PI;
  96.     }
  97.  
  98.  
  99.    
  100.     public float getPerimeter ()
  101.     {
  102.         return getRadius() * 2 * PI;
  103.     }
  104.  
  105.  
  106.    
  107.     public void setRadius ( float theRadius )
  108.     {
  109.         radius = theRadius;
  110.     }
  111.  
  112. }
  113. 4.////////////////////////////////////////////////////////
  114. public class Square implements FigureGeometry {
  115. private Point point;
  116.  
  117. public Square(Point p) {
  118.     point = p;
  119. }
  120.  
  121. public int getSideLength() {
  122.     return point.getWidth();
  123. }
  124.  
  125. public float getArea() {
  126.     return getSideLength() * getSideLength();
  127. }
  128.  
  129. public float getPerimeter() {
  130.     return getSideLength() * 4;
  131. }
  132. public void setPoint(Point p) {
  133.     point = p;
  134. }
  135. }
  136.  
  137.  
  138.  
  139. 5.////////////////////////////////////////////////
  140. public class Rectangle {
  141. private Point point;
  142.  
  143. public Rectangle (Point p) {
  144.     point = p;
  145. }
  146.  
  147. public int getWidth() {
  148.     return point.getWidth();
  149. }
  150. public int getHeight() {
  151.     return point.getHeight();
  152. }
  153. public float getArea() {
  154.     return getWidth() * getHeight();
  155. }
  156.  
  157. public float getPerimeter() {
  158.     return ( getWidth() + getHeight() ) * 2;
  159. }
  160. public void setPoint(Point p) {
  161.     point = p;
  162. }
  163. }
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement