Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package myApp;
- public class Rectangle {
- /* Properties */
- private double width, height;
- /* Get, Set Methods */
- public double getWidth() {
- return this.width;
- }
- public void setWidth(double width) {
- this.width = width<0?(width*(-1)):width;
- }
- public double getHeight() {
- return this.height;
- }
- public void setHeight(double height) {
- this.height = height<0?(height*(-1)):height;
- }
- /* Constructors */
- public Rectangle() {
- this(10, 10); //default properties
- }
- public Rectangle(double width, double height) {
- this.setWidth(width);
- this.setHeight(height);
- }
- public Rectangle(Rectangle rectObj) {
- this.width = rectObj.width;
- this.height = rectObj.height;
- }
- /* Methods */
- public double getPerimeter() {
- return 2*(width+height);
- }
- public double getArea() {
- return width*height;
- }
- public void details(String prefix) {
- System.out.printf("(%s): %s Width=%.2f, Height=%.2f, Perimeter=%.2f, Area=%.2f //Rectangle object%n",
- this, prefix, width, height, this.getPerimeter(), this.getArea());
- }
- public void details() { //default case
- details(" ");
- }
- public void print(char chr) {
- for (int line=0; line<(int)height; line+=1) {
- for (int index=0; index<(int)width; index+=1) {
- System.out.print(chr);
- }
- System.out.print("\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment