Question

In: Computer Science

The following code is for a class named Box. The class Box includes a constructor method...

The following code is for a class named Box. The class Box includes a constructor method Box, and a method getVolume().

For your assignment you are to develop a java class named MatchBox. Your class MatchBox must extend the class Box and in addition to the attributes width, height, and depth that are defined in the class Box, MatchBox must add a new attribute weight. The getVolume method must both report the values of width, height, depth, and weight, but must also calculate and report the volume by multiplying height by width by depth. The class MatchBox must also add the method calculateWeight() that will calculate weight based upon the volume assuming that the volume is a quantity of water which weighs .03611 pounds per cubic inch. Also method calculateWeight should show the result like this: weight of MatchBox is X.

For example a car class can inherit some properties from a General vehicle class. Here we find that the base class is the vehicle class and the subclass is the more specific car class. A subclass must use the extends clause to derive from a super class which must be written in the header of the subclass definition. The subclass inherits members of the superclass and hence promotes code reuse. The subclass itself can add its own new behavior and properties.

Your new class must include a main method that creates a new MatchBox object, calls the getVolume method and reports the results by printing the following items to the screen (where the X is replaced by the calculated value) For your assignment, assume that the value of width is 5, height is 10, and the depth is 3. The output should look like the following with X replaced with the appropriate calculated value.

width of MatchBox is X
height of MatchBox is X
depth of MatchBox is X
weight of MatchBox is X
Volume is: X

Helpful Hints

When creating this assignment you should assume a java file for each component. First create a Box.java to hold the box class, a MatchBox.java for the MatchBox class and a main.java for the main routine. All three files, when using Netbeans must belong to the same package so make sure you use the package statement at the beginning of each file.

Keep in mind that in the new class MatchBox, you can either use the methods and variables in the Box class, redefine methods or variables for the MatchBox class that exist in Box or create new methods and variables.

======

The class Box is as follows:

class Box {
 
   double width;
   double height;
   double depth;
 
   // This is an empty constructor
   Box() {
          ;
   }
 
   Box(double w, double h, double d) {
          width = w;
          height = h;
          depth = d;
   }
 
   void getVolume() {
          System.out.println("Volume is : " + width * height * depth);
   }

Solutions

Expert Solution

class Box{ }// this the class box
  
double width;
double height;   
double depth;
public Box() // constructor of class box
{
}
public Box(double w,double h,double d) // Constructor of class box with parameter
{
width=w;
height=h;
depth=d;
}
public void getVolume(){ // method getvolume will print the volume
System.out.println("Volume is:"+width*height*depth);
}
}
class MatchBox extends Box { // subclass of box is matchbox
double weight;

public MatchBox(double w, double h, double d) { // constructor of matchbox
super(w, h, d); // it is used to call base class constructor
}

public double calculateWeight() { // method calculateweight
weight = (width * height * depth) * 03611;
return weight;   
}

public void getVolume() { // overloading getvolume method
System.out.println("width of MatchBox is: " + width);
System.out.println("height of MatchBox is: " + height);
System.out.println("depth of MatchBox is: " + depth);
System.out.println("weight of MatchBox is: " + weight);
}
}
public class Main {

public static void main(String[] args) {

MatchBox matchbox = new MatchBox(5, 10, 3); // creating object of matchbox class
matchbox.calculateWeight(); // calling the method calculateweight of matchbox class
matchbox.getVolume(); // calling method getvolume of matchbox class
Box box = new Box(5, 10, 3); // creating object of box class
box.getVolume(); // calling the method getvolume of box class
}
}


Related Solutions

- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new ArrayList, which contains the nonduplicate elements from the original list public static ArrayList removeDuplicates(ArrayList list) b. for a sentinel-controlled loop to input a varying amount of integers into the original array (input ends when user enters 0) c. to output the original array that displays all integers entered d. to output the new array that displays the list with duplicates removed Use this TEST...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
Write a class named TestScores. The class constructor should accept an array of test scores as...
Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program. Use TestScoresDemo.java to test your code public class TestScoresDemo { public static void main(String[] args) { // An array with test scores. //...
Javascipt Assignment: Create a document that includes a constructor function named Company in the document head...
Javascipt Assignment: Create a document that includes a constructor function named Company in the document head area. Include four properties in the Company object definition: name, products, motto, and numEmployees (for number of employees.) Instantiate a new Company object in the document body , then assign values to each of the properties Print the name, products, motto, and the numEmployees properties to the screen using document.write methods. Combine each printed property with a descriptive string. For example, when you print...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
C++ Code Required to Show The constructor of parent class executes before child class
C++ Code Required to Show The constructor of parent class executes before child class
Complete the class code below and include a method named partition which takes an integer array...
Complete the class code below and include a method named partition which takes an integer array and a partition integer as parameters and then partitions the original array into two new arrays where elements of the first array are all less than or equal to the partition integer and the second array are all greater than the partition integer. The method should print the original array, followed by the lower partition and finally the upper partition. Nothing should be returned...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the department (for example, ENG) id (int) - the course number (for example, 101) credits (double) - the credits (for example, 3) price (double) - the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT