Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////// Rectangle Class ///////////////////////
- public class Rectangle {
- // Rectangle properties
- private int length;
- private int width;
- public Rectangle() {
- this(10, 10);
- }
- public Rectangle(int length, int width) {
- setLength(length);
- setWidth(width);
- }
- public int getLength() {
- return length;
- }
- public void setLength(int length) {
- if(length < 0) {
- System.out.println("Length of rectangle can not get a negative value");
- this.length = 0;
- } else {
- this.length = length;
- }
- }
- public int getWidth() {
- return width;
- }
- public void setWidth(int width) {
- if(width < 0) {
- System.out.println("Width of rectangle can not get a negative value");
- this.width = 0;
- } else {
- this.width = width;
- }
- }
- public int getPerimeter() {
- return 2 * (length + width);
- }
- public int getArea() {
- return length * width;
- }
- public void print(char c) {
- for(int i = 0; i < width; i++) {
- System.out.print(c);
- }
- System.out.println();
- for(int i = 0; i < length-2; i++) {
- System.out.print(c);
- for(int j = 0; j < width-2; j++) {
- System.out.print(" ");
- }
- System.out.println(c);
- }
- for(int i = 0; i < width; i++) {
- System.out.print(c);
- }
- System.out.println();
- }
- public void print() {
- print('*');
- }
- }
- //////////////////////////// Mani Class ///////////////////////////
- public class RectangleView {
- public static void main(String[] args) {
- Rectangle r1 = new Rectangle(7,5);
- Rectangle r2 = new Rectangle();
- System.out.println("Perimeter of rectangle 1 is: " + r1.getPerimeter() + " and the area is: " + r1.getArea());
- r2.print();
- r1.print('$');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement