185k views
12 votes
JAVA CODE.

Write a method that takes a Rectangle as a parameter, and changes the width so it becomes a square (i.e. the width is set to the value of the length).

This method must be called makeSquare() and it must take a Rectangle parameter.

User Hmk
by
5.6k points

1 Answer

10 votes

Solution :

public class Rectangle

{

int len,bre;

Rectangle(int l,int b)

{

setLength(l);

setBreadth(b);

}

void setLength(int l)

{

len=l;

}

void setBreadth(int b)

{

bre=b;

}

int getLength()

{

return len;

}

int getBreadth()

{

return bre;

}

void makeSquare(Rectangle r)

{

r.setBreadth(len);

System.out.println("Square Parameter");

System.out.println("Length:" +r.getLength() + " Length: ".getBreadth());

}

public static void main(String[] args){

Rectangle r=new Rectangle(10,5);

System.out.println("Rectangle Parameter");

System.out.println("Length:"+r.getLength()+" Breadth:"+r.getBreadth());

r.makeSquare(r);

}

}

}

User Adam Singer
by
4.8k points