HELP!!! JAVA (class methods)

0 francis smith · July 27, 2015
have written a class rectangle to represent arectangle;  but i want to insert two double data fields like width &height and i wana use a no args constructor to create a default rectangle (width &height should be 1) and finally should have a GetArea method and Getparameter method..help pleaase

Post a Reply

Replies

Oldest  Newest  Rating
+1 Rashad Kadado · July 28, 2015

public class Rectangle {

  private double width;
  private double height;
  
  public Rectangle() {
    this.width = 20;
    this.height = 20;
  }
  
  public Rectangle(double width, double height) {
    this.width = width;
    this.height = height;
  }
  
  public double getArea() {
    return width*height;
  }

public double getWidth() {
return width;
}
  
public double getHeight() {
return height;
}

}

Here is the rectangle class. The rectangle class has the width and height fields. It has a constructor that takes in no parameters so when you call the constructor with no parameters it will set it to whatever you want to default to be. Also there is a rectangle constructor that takes in the width and height and sets the fields equal to that. Then there is the simple getArea method that returns the area of the rectangle. As for get parameters I think for ease you would want to make a getWidth and getHeight methods.
  • 1

Java / Android Development

107,286 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator