Question

In: Computer Science

JAVA, trying to better learn and use JAVA //Please note throughout if possible looking to better...

JAVA, trying to better learn and use JAVA

//Please note throughout if possible looking to better understand the process.

Write the following class: Box

Define Class Box

Write the class header

Class Variables

Class Box is to have the following private data members:

  • height of type double

  • width of type double

  • length of type double

Constructors

Class Box is to have two constructors with the following specifications:

  • a no-arg constructor that initializes each double data member to zero

  • a constructor that takes three parameters, each representing one of the class data members. the arguments are to be listed in the order of (height, width, length)

Get and Get Methods

Class Box is to have standard get/set methods for each data member

Auxiliary Methods

  • an auxiliary method getVolume( ). The method is to have an access of public and return a value of type double. The method is to do the following:

    • calculate and return the volume of the box

      • volume is calculated by multiplying the length of the box by the width of the box by the height of the box

Method Overrides

Class Box is to have an overridden equals() method that does the following:

  • tests to see if the parameter represents an object (null test)

  • tests to see if the parameter object is of the same class type as the calling object (class test)

  • determines if the calling object and the parameter object store identical data values for the corresponding data members (variable to variable test)

Class Box is to have an overridden toString() method that does the following:

  • displays the following information in the format presented:

Height: display height of the object

Width: display the width of the object

Length: display the length of the object

Solutions

Expert Solution

//-----CODE-----
// Main class. Box class is tested in this class
class Main {
  // main function
  public static void main(String[] args) {
    // creating a Box Object using parameterized constructor
    Box b = new Box(10,15,20);
    System.out.println("Box b created");
    // function call: toString
    System.out.println(b.toString());
    // function call: getVolume
    System.out.println(b.getVolume());
    
    // creating another Box Object using parameterized constructor
    Box b1 = new Box(10,10,20);
    System.out.println("Box b1 created");
    // function call: toString
    System.out.println(b1.toString());
    // function call: getVolume
    System.out.println(b1.getVolume());

    System.out.println("Checking if b and b1 are equal");
    // function call: equals
    System.out.println(b.equals(b1));
  }
}
// class Box
class Box{
  // private members
  private double height;
  private double width;
  private double length;
  
  // constructor without parameter
  public Box(){

    this.height=0;
    this.width=0;
    this.length=0;
  }
  // constructor with parameter
  public Box(double h,double w,double l){
    this.height=h;
    this.width=w;
    this.length=l;
  }

  // getters and setters
  public double getHeight(){
    return this.height;
  }

  public void setHeight(double H){
    this.height=H;
  }

    public double getWidth(){
    return this.width;
  }

  public void setWidth(double W){
    this.width=W;
  }

    public double getLength(){
    return this.length;
  }

  public void setLength(double L){
    this.length=L;
  }

  public double getVolume(){
    return this.height*this.width*this.length;
  }
  // method equals
  public Boolean equals(Box b){

    Boolean ans= false;
    if(this.height==b.getHeight()&&this.width==b.getWidth()&&this.length==b.getLength()){
      ans=true;
    }

    return ans;
  }
  // method toString
  public String toString(){
    return "Height: "+this.height+"\nWidth: "+this.width+"\nLength:"+this.length;
  }

}

Output

Summary

The program is implemented in Java.

Box class is created with private members, default constructor, parametrized constructor, getters and setters.

Method getVolume and overriding methods equals and toString are also implemented.

Comments and included in code for better understanding.

Code and output are provided.


Related Solutions

JAVA, this is practice to better understand java. //Please note throughout if possible looking to better...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better understand the process. Write the following class: Person. Define Class Person Write the class header Class Variables Class Person is to have the following data members: firstName of type String, lastName of type String representing a person’s first and last names and ssn of type int representing a social security number. Each data member has an access specifier of type private. Constructors Class Person...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better understand the process. Write the following class: TV. Define Class TV Write the class header Class Variables Class TV is to have the following instance variables: 1.channel of type int 2. volumeLevel of type int 3. isOn of type boolean Each data member is to have an access specifier of private. Constructor Class TV is to have a single, no-arg constructor that initializes the...
I need this code translated from C++ to Java. Im personally still trying to learn Java,...
I need this code translated from C++ to Java. Im personally still trying to learn Java, so if you can include screenshots of your IDE/output that would be helpful. Much appreciated! #include <iostream> #include <string> using namespace std; class pizza { public:    string ingrediants, address;    pizza *next;    pizza(string ingrediants, string address)    {        this->address = address;        this->ingrediants = ingrediants;        next = NULL;    } }; void enqueue(pizza **head, pizza **tail, pizza...
This is an entry to Java Question. Please answer with Pseudocode and Java code for better...
This is an entry to Java Question. Please answer with Pseudocode and Java code for better understanding. We are mainly using basic in and out declarations and are focusing on API for method invocations. Any help would be very appreciated :) Problem 7: Distance (10 points) Use API (Data Science) Data Science is an emergent field from Computer Science with applications in almost every domain including finances, medical research, entertainment, retail, advertising, and insurance. The role of a data analyst...
Hello! If possible, could you provide how and in excel? I'm stuck and trying to learn...
Hello! If possible, could you provide how and in excel? I'm stuck and trying to learn so I can do well on the exam! A 20-year annuity pays $2,350 per month at the end of each month. If the discount rate is 13 percent compounded monthly for the first eight years and 10 percent compounded monthly thereafter, what is the present value of the annuity? (Do not round intermediate calculations and round your answer to 2 decimal places, e.g., 32.16.)...
PLEASE USE FACTORS IF POSSIBLE An electric switch manufacturing company is trying to decide between three...
PLEASE USE FACTORS IF POSSIBLE An electric switch manufacturing company is trying to decide between three different assembly methods. Method A has an estimated first cost of $40,000, an annual operating cost (AOC) of $9000, and a service life of 2 years. Method B will cost $80,000 to buy and will have an AOC of $6000 over its 4-year service life. Method C costs $130,000 initially with an AOC of $4000 over its 8-year life. Methods A and B will...
Because I'm trying to learn how to solve this kind of problem in the future please...
Because I'm trying to learn how to solve this kind of problem in the future please show all intermediate steps so I can follow Suppose F= <xy+y, x-y^2> Let R be the triangular regiorn bounded by the curves y=x, y=4-x, and y=0, Let C be the boundary of this region, oriented counterclock wise. a) Can you use Fundamental theorem for Line integrals to find ∮ F•dr? explain why or why not? What are the other two possible methods of finding...
Please show the step I'm trying to learn not cheat The chief accountant for Grandview Corporation...
Please show the step I'm trying to learn not cheat The chief accountant for Grandview Corporation provides you with the company’s 2021 statement of cash flows and income statement. The accountant has asked for your help with some missing figures in the company’s comparative balance sheets. These financial statements are shown next ($ in millions). GRANDVIEW CORPORATION Statement of Cash Flows For the Year Ended December 31, 2021 Cash Flows from Operating Activities: Collections from customers $ 96 Payment to...
No execel please! I am trying to learn the math for the exam! Thank you!! Shanna...
No execel please! I am trying to learn the math for the exam! Thank you!! Shanna wants to buy a house costing $325,000 and has obtained a loan from TD Bank. A minimum down payment of 15% would be required and the bank will provide the difference. Her grandparent have told her that they will cover her down payment. a. TD Bank has quoted her mortgage interest rate is 4.5%; this rate would be compounded semiannually, while her payments would...
No Excel Please! I am trying to learn the math for the exam! Thank you!! You...
No Excel Please! I am trying to learn the math for the exam! Thank you!! You are 35 years old today and are considering your retirement needs. You expect to retire at age 65 (in 30 years) and you plan to live to age 99. You want to buy a house costing $300,000 on your 65th birthday and your living expenses will be $30,000 a year after that (starting at the end of year 65 and continuing through the end...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT