Advertisement
deyanmalinov

1. Shapes Drawing

Jul 8th, 2020
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. package DPM;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         Drawable circle = new Circle(4);
  7.         Drawable rectangle = new Rectangle(9,9);
  8.         circle.draw();
  9.         rectangle.draw();
  10.     }
  11. }-----------------------------
  12. package DPM;
  13. public interface Drawable {
  14.     void draw();
  15. }-----------------------------
  16. package DPM;
  17. public class Circle implements Drawable{
  18.     private Integer radius;
  19.     public Circle(Integer radius){
  20.         this.radius = radius;
  21.     }
  22.     public Integer getRadius() {
  23.         return radius;
  24.     }
  25.     @Override
  26.     public void draw() {
  27.         double r_in = this.getRadius() - 0.4;
  28.         double r_out = this.getRadius() + 0.4;
  29.         for (double y = this.getRadius(); y >= -this.getRadius(); --y) {
  30.             for (double x = -this.getRadius(); x < r_out; x += 0.5) {
  31.                 double value = x * x + y * y;
  32.                 if (value >= r_in * r_in && value <= r_out * r_out) {
  33.                     System.out.print("*");
  34.                 } else
  35.                     System.out.print(" "); }
  36.             System.out.println(); }
  37.     }
  38. }---------------------------------------
  39. package DPM;
  40. public class Rectangle implements Drawable{
  41.     private Integer width;
  42.     private Integer height;
  43.     public Rectangle(Integer width, Integer height){
  44.         this.width = width;
  45.         this.height= height;
  46.     }
  47.     public Integer getWidth(){
  48.         return width;
  49.     }
  50.     public Integer getHeight(){
  51.         return height;
  52.     }
  53.     @Override
  54.     public void draw() {
  55.         for (int i = 0; i < this.getHeight(); i++) {
  56.             System.out.print("*");
  57.             for (int j = 1; j < this.getWidth() - 1; j++) {
  58.                 System.out.print(" ");
  59.                 if (i == 0 || i == (this.getHeight() - 1)){
  60.                     System.out.print("*");
  61.                 } else { System.out.print(" "); }}
  62.             System.out.print(" ");
  63.             System.out.print("*");
  64.             System.out.println();
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement