Skip to content

Instantly share code, notes, and snippets.

@OlgaKulikova
Last active August 29, 2015 14:07
Show Gist options
  • Save OlgaKulikova/b70760e7584c8684b496 to your computer and use it in GitHub Desktop.
Save OlgaKulikova/b70760e7584c8684b496 to your computer and use it in GitHub Desktop.
Нарисовать прямоугольник в консоли
package Figure;
public abstract class Figure {
public abstract double square();
}
package Figure;
public class Rectangle extends Figure {
public int height, width;
public Rectangle(int height, int width) {
this.height = height;
this.width = width;
}
@Override
public double square() {
return height * width;
}
public void drawRectangle(char dash, char pipe, char space) {
char[][] rectangle = new char[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (i == 0 || i == (height - 1)) {
rectangle[i][j]= dash;
} else if (j == 0 || j == (width - 1)) {
rectangle[i][j] = pipe;
} else {
rectangle[i][j]= space;
}
}
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print(rectangle[i][j]);
}
System.out.println();
}
}
}
package com.Module2.Lesson1.Lesson3;
// Написать программу, которая будет рисовать на консоли прямоугольник с заданными длинами сторон.
import Figure.Rectangle;
public class Task4 {
public static void main(String[] args) {
Rectangle rec = new Rectangle(5, 15);
char dash = '-', pipe = '|', space = ' ';
rec.drawRectangle(dash, pipe, space);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment