Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package question4;
- public class Question4 {
- public static void main(String[] args) {
- Rectangle spot = new Rectangle();
- Rectangle spot2 = new Rectangle();
- spot.setLength(3);
- spot.lengthwidth();
- spot2.lengthwidth(4, 7);
- spot.area();
- System.out.println(spot);
- }
- }
- Rectangle Class
- package question4;
- public class Rectangle {
- private double length;
- private double width;
- private double area;
- public Rectangle() {
- length = 1;
- width = 1;
- }
- public Rectangle(double l, double w) {
- length = l;
- width = w;
- }
- public double getLength(){
- return(length);
- }
- public double getWidth() {
- return(width);
- }
- public void setLength(double newLength) {
- length = newLength;
- }
- public void setWidth(double newWidth) {
- width = newWidth;
- }
- public void lengthwidth() {
- System.out.println("The length is "+length+" and the width is "+width);
- }
- public void lengthwidth(double l, double w) {
- System.out.println("The length is "+l+"and the width is "+w);
- }
- public void area() {
- area = (length * width);
- System.out.println("The area of the rectange is "+area);
- }
- public void area(double l,double w) {
- area = (l*w);
- System.out.println("The area of the rectange is "+ area);
- }
- public boolean equals(Object r) {
- Rectangle testObj = (Rectangle)r;
- if (testObj.getLength() == length )
- return(true);
- else
- return(false);
- }
- public String toString() {
- String rectangleString;
- rectangleString = "Rectangle has length " + length+" width "+width;
- return(rectangleString);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement