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

90.2K

Verified Solution

Question

Programming

Overloaded Constructors

  1. Add a pair of constructors to the Height class that implementthe initializations provided by the two setHeight operations inFigure 7.11. Minimize the total number of statements by having theone parameter constructor call the one-parameter setHeight methodand having the two-parameter constructor call the two-parametersetHeight method.

  1. Provide a complete rewritten main method for the HeightDriverclass such that the new method uses one of the new constructorsfrom 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

Answer & Explanation Solved by verified expert
3.8 Ratings (432 Votes)
Below is your codeHeightDriverjava Heightjava Dean Dean This class stores and prints height values class Height double height a persons height String units like cm for centimeters    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students