Question

In: Computer Science

Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided...

Overloaded Constructors

  1. Add a pair of constructors to the Height class that implement the initializations provided by the two setHeight operations in Figure 7.11. Minimize the total number of statements by having the one parameter constructor call the one-parameter setHeight method and having the two-parameter constructor call the two-parameter setHeight method.

  1. Provide a complete rewritten main method for the HeightDriver class such that the new method uses one of the new constructors from part a) to generate this output:

6.0 ft

/***********************************************************

* Height.java

* Dean & Dean

*

* This class stores and prints height values.

***********************************************************/

class Height

{

double height; // a person's height

String units; // like cm for centimeters

//********************************************************

public void setHeight(double height)

{

    this.height = height;

    this.units = "cm";

}

//********************************************************

public void setHeight(double height, String units)

{

    this.height = height;

    this.units = units;

  }

//********************************************************

public void print()

{

    System.out.println(this.height + " " + this.units);

}

} // end class Height

/*******************************************************************

* HeightDriver.java

* Dean & Dean

*

* This class is a demonstration driver for the Height class.

*******************************************************************/

public class HeightDriver

{

public static void main(String[] args)

{

    Height myHeight = new Height();

    myHeight.setHeight(72.0, "in");

    myHeight.print();

    myHeight.setHeight(180.0);

    myHeight.print();

} // end main

} // end class HeightDriver

Solutions

Expert Solution

Below is your code:

HeightDriver.java

/***********************************************************
 * 
 * Height.java
 * 
 * Dean & Dean
 * 
 *
 * 
 * This class stores and prints height values.
 ***********************************************************/

class Height

{

        double height; // a person's height

        String units; // like cm for centimeters

        // ******************************************************

        // 1-Parameter Constructor
        public Height(double height) {
                this.setHeight(height);
        }

        // ******************************************************

        // ******************************************************

        // 2-Parameter Constructor
        public Height(double height, String units) {
                this.setHeight(height, units);
        }

        // ******************************************************

        // ********************************************************

        public void setHeight(double height)

        {

                this.height = height;

                this.units = "cm";

        }

        // ********************************************************

        public void setHeight(double height, String units)

        {

                this.height = height;

                this.units = units;

        }

        // ********************************************************

        public void print()

        {

                System.out.println(this.height + " " + this.units);

        }

} // end class Height

/*******************************************************************
 * 
 * HeightDriver.java
 * 
 * Dean & Dean
 * 
 *
 * 
 * This class is a demonstration driver for the Height class.
 *******************************************************************/

public class HeightDriver

{

        public static void main(String[] args)

        {

                Height myHeight = new Height(6, "ft");

                // myHeight.setHeight(72.0, "in");
                //
                // myHeight.print();
                //
                // myHeight.setHeight(180.0);

                myHeight.print();

        } // end main

} // end class HeightDriver

Below are the screenshots of the code changed. Heighlighted part is the changed code

Output

6.0 ft


Related Solutions

Implement a class named stack pair that provides a pair of stacks. Make the class a...
Implement a class named stack pair that provides a pair of stacks. Make the class a template class. So, you will have two files: stack pair.h and stack pair.template, following the style of the text. The basic idea is that two stacks can share a single static array. This may be advantageous if only one of the stacks will be in heavy use at any one time. • The class should have various methods to manipulate the stack: T pop...
Working with arrays in our class, reinforce constructors and setters and getters and implement dynamic variables....
Working with arrays in our class, reinforce constructors and setters and getters and implement dynamic variables. Create a class called Movie that contains information about a movie. The class has the following attributes (you choose the data type for the member variables): ■ The movie name ■ The MPAA rating (for example, G, PG, PG-13, R) ■ Array of size 5 called Ratings, each index will hold the following. [0] The number of people that have rated this movie as...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for the class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Methods should be provided that allow a user of the class to find out the length, width, area and perimeter of the shape plus a toString()method to print the values of the basic dimensions. Now implement...
Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
Use the Heap class provided to implement a sort routine in a Main class where the...
Use the Heap class provided to implement a sort routine in a Main class where the user enters a series of values, each value is then pushed onto a heap, then the values are printed out in ascending order. public class Heap { public static final int SIZE = 1025; public Heap() { elt = new Element[SIZE]; lastLoc = 0; } public void push(String k, Object o) { if (!fullCheck()) { lastLoc++; elt[lastLoc] = new Element(k,o); int loc = lastLoc;...
Make one program in c++ Implement the Point class composed of an ordered pair (?, ?)....
Make one program in c++ Implement the Point class composed of an ordered pair (?, ?). Redefine the operators in your Point class Calculate the coefficients of a Linear Regression ? = ?? + ? with next data for x and y. x y 46 4.7 72 12.3 59 6.5 56 5.9 54 6.6 70 10.3 68 8.4 48 4
Start with the provided code for the class linkedListType. Be sure to implement search, insert, and...
Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Now, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. Note: the link pointer of the last element of the list is NULL. Test your new function in main. Submit a .zip of your entire project....
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a member function named      int distance2origin() that calculates the distance from the (x, y, z) point to the origin (0, 0, 0) the ‘prototype’ in the class definition will be      int distance2origin() outside the class definition             int Coord :: distance2origin() {                         // your code } _______________________________________________________________________________________________________ /************************************************** * * program name: Coord02 * Author: * date due: 10/19/20 * remarks:...
Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided...
Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided MPQ class. The functions from MPQ that are virtual function (remove min(), is empty(), min(), and insert()) must be implemented in the child classes. The functions remove min() and min() should throw an exception if the minimum priority queue is empty. For the UnsortedMPQ class, you will use a vector to implement the minimum priority queue functions. The insert() function should be O(1) and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT