In: Computer Science
Overloaded Constructors
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
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