Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rectangle;
- public class Rectangle {
- private int length, width;
- public Rectangle() {
- length = 2;
- width = 1;
- }
- public Rectangle(int l, int w) {
- length = l;
- width = w;
- }
- public int perimeter() {
- int perimeter;
- perimeter = 2 * length + 2 * width;
- return(perimeter);
- }
- public int area() {
- int area;
- area = length * width;
- return(area);
- }
- public static void formula(){
- System.out.println("Area formula: a = l * w");
- }
- public int getLength(){
- return(length);
- }
- public int getWidth(){
- return(width);
- }
- public boolean equals(Object c){
- Rectangle testObj = (Rectangle)c;
- if(testObj.getLength() == length){
- if(testObj.getWidth() == width){
- return(true);
- } else {
- return(false);
- }
- } else {
- return(false);
- }
- }
- public String toString(){
- String rectString;
- rectString = "Rectangle has length " + length + " and width " + width;
- return(rectString);
- }
- public static void main(String[] args) {
- Rectangle box1 = new Rectangle(2, 3);
- Rectangle box2 = new Rectangle(4, 5);
- System.out.println("Perimeter: " + box.perimeter());
- System.out.println("Area: " + box.area());
- Rectangle3.formula();
- if (box1.equals(box2)){
- System.out.println("Objects are equal.");
- } else {
- System.out.println("Objects are not equal.");
- }
- System.out.println(box1);
- System.out.println(box2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment