Question

In: Computer Science

Make a class whose objects each represent a box of bricks. Also, the class has to...

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

Solutions

Expert Solution

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.


Related Solutions

Make a class whose objects each represent a battery. The only attribute an object will have...
Make a class whose objects each represent a battery. The only attribute an object will have is the battery type (one letter). Also, the class has to include the following services for its objects: -determine if two batteries are equal -change battery type -get battery type display battery information the program must include: 1.All / Part 2.Overloading of operators 3.Builder 4.Copy constructor 5.Destroyer 6.Dynamic memory C++
Consider a linked list whose nodes are objects of the class Node: class Node {    ...
Consider a linked list whose nodes are objects of the class Node: class Node {     public int data;     public Node next; } prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list. Which of the following statements is used to insert a new node, referenced by newNodebetween prev and curr? Group of answer choices newNode.next = curr; prev.next = newNode; newNode.next = head; head = newNode;...
Python Please Define a class that will represent soccer players as objects. A soccer player will...
Python Please Define a class that will represent soccer players as objects. A soccer player will have as attributes, name, age, gender, team name, play position on the field, total career goals scored. The class should have the following methods: 1. initializer method that will values of data attributes arguments. Use 0 as default for career goals scored. 2. str method to return all data attributes as combined string object. 3. addToGoals that will accept an argument of int and...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
Create the following class to represent the sprite objects in the game. Sprite Name: String x:...
Create the following class to represent the sprite objects in the game. Sprite Name: String x: x location y: y location       +   Sprite (String);       +   Sprite (String, int, int);            +   setName (String): void       +   setX(int): void       +   setY(int): void       +   getName (): String       +   getX (): int       +   getY (): int       +   collide (Sprite): boolean       +   toString (): String Create ten (10) game objects as Sprite type and stored in...
python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them by your own. A constructor that creates a table with the following: a list of data. ip address a integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the ip address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them on your own. A constructor that creates a table with the following: a list of data. IP address an integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the IP address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them on your own. A constructor that creates a table with the following: a list of data. IP address an integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the IP address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
Hello, I need this is C++. Thank you! (1B) Write a Fraction class whose objects will...
Hello, I need this is C++. Thank you! (1B) Write a Fraction class whose objects will represent Fractions. Note: this is the first part of a multi-part assignment. For this week you should not simplify (reduce) fractions, you should not use "const," and all of your code should be in a single file. In this single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. You must provide...
Create a sub class of the class Box and call it SBox. The SBox class has...
Create a sub class of the class Box and call it SBox. The SBox class has inherited the list of variables and methods from Box & has its own members too. a. It has a variable color having a String data type. b. This class also has a method called SetColor( ) which read the color from the user (Use JOptionPane), set the color to the value given by the user and display it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT