In: Computer Science
Make a class whose objects each represent a box of bricks. Also, the class has to include the following services for its objects:
show what's in the box
get the weight of the box
determine if two boxes are equal
Code--
import java.util.*;
//make a class
class BrickBox
{
private int noOfBricks;
private double wtOfOneBrick;
public BrickBox(int noOfBricks,double
wtOfOneBrick)
{
this.noOfBricks=noOfBricks;
this.wtOfOneBrick=wtOfOneBrick;
}
/*this method will return the
total weight of a box*/
public double getBoxWt()
{
return
noOfBricks*wtOfOneBrick;
}
public boolean isEqual(BrickBox bb)
{
if(this.noOfBricks==bb.noOfBricks)
{
if(this.wtOfOneBrick==bb.wtOfOneBrick)
{
return true;
}
}
return false;
}
}
public class BrickBoxTest
{
public static void main(String[] args)
{
BrickBox box1=new
BrickBox(20,(double)1.5);
BrickBox box2=new
BrickBox(20,(double)1.5);
BrickBox box3=new
BrickBox(30,(double)1.5);
System.out.println("Weight of box1
= "+box1.getBoxWt());
System.out.println("Weight of box2
= "+box2.getBoxWt());
System.out.println("Weight of box3
= "+box3.getBoxWt());
System.out.println();
if(box1.isEqual(box2))
{
System.out.println("Box1 and Box2 is equal");
}
else
{
System.out.println("Box1 and Box2 is not equal");
}
if(box2.isEqual(box3))
{
System.out.println("Box2 and Box3 is equal");
}
else
{
System.out.println("Box2 and Box3 is not equal");
}
if(box1.isEqual(box3))
{
System.out.println("Box1 and Box3 is equal");
}
else
{
System.out.println("Box1 and Box3 is not equal");
}
}
}
Output Screenshot--

Note--
Please upvote if you like the effort.