Constructors Part 5 Tutorial

This tutorial will build on the Box class from my This Keyword Part 2 Tutorial. The length, height, and width of a box should allow for fractional dimensions. In this tutorial I will expand on the concept of constructor overloading by creating a constructor with double primitive data type parameters.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ConstructorFive
C:\Java>cd ConstructorFive
C:\Java\ConstructorFive>Notepad ConstructorFive.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class ConstructorFive {
    public static void main(String args[]) {
	
        Box b = new Box(); 
        b.setLength(10);
        b.setHeight(2);		
        b.setWidth(5);
        System.out.println("The volume of box b is: " + b.calculateVolume() + " cubic " + b.getUnitOfMeasurement());
        System.out.println();

        Box c = new Box(4, 8, 3);
        System.out.println("The volume of box c is: " + c.calculateVolume() + " cubic " + c.getUnitOfMeasurement());
        System.out.println();

        Box d = new Box(18, 15, 21, "centimeters");
        System.out.println("The volume of box d is: " + d.calculateVolume() + " cubic " + d.getUnitOfMeasurement());
        System.out.println();

        Box e = new Box(4.5, 5, 5, "inches");
        System.out.println("The volume of box e is: " + e.calculateVolumeDouble() + " cubic " + e.getUnitOfMeasurement());
        System.out.println();
    }
}
class Box {
    private int length = 0; 
    private int height = 0; 
    private int width = 0;
    private String unitOfMeasurement; 

    private double lengthD = 0.0D; 
    private double heightD = 0.0D; 
    private double widthD = 0.0D;

    // Default constructor - Don't forget to always include this.
    Box() {
        //super();  remove this
        this(0, 0, 0, "inches");
    }

    // First Constructor created
    Box(int length, int height, int width) {
        this(length, height, width, "inches");
        //this.length = length; remove these now
        //this.height = height; remove these now
        //this.width = width; remove these now
    } 
    
    // Second Constructor
    Box(int length, int height, int width, String unitOfMeasurement ) {
        this.length = length;
        this.height = height;
        this.width = width;
        this.unitOfMeasurement = unitOfMeasurement;
    }   
    
    // New Constructor
    Box(double lengthD, double heightD, double widthD, String unitOfMeasurement ) {
        this.lengthD = lengthD;
        this.heightD = heightD;
        this.widthD = widthD;
        this.unitOfMeasurement = unitOfMeasurement;
    } 
       
    void setLength (int length) {
        this.length = length;
    }
    int getLength () {
        return length;
    }

    void setHeight (int height) {
        this.height = height;
    }
    int getHeight () {
        return height;
    }

    void setWidth (int width) {
        this.width = width;
    }
    int getWidth () {
        return width;
    }

    void setUnitOfMeasurement (String unitOfMeasurement) {
        this.unitOfMeasurement = unitOfMeasurement;
    }
    String getUnitOfMeasurement () {
        return unitOfMeasurement;
    }

    // create a method to get the volume of the box
    int calculateVolume() {
       return (length * height * width);	
    }

    // create a method to get the volume of the box
    double calculateVolumeDouble() {
       return (lengthD * heightD * widthD);	
    }
}


Now switch back to the command prompt (CMD) and type in javac ConstructorFive.java and press Enter.
Now type in java ConstructorFive and press Enter.


C:\Java\ConstructorFive>javac ConstructorFive.java
C:\Java\ConstructorFive>java ConstructorFive
The volume of box b is: 100 cubic inches

The volume of box c is: 96 cubic inches

The volume of box d is: 210 cubic inches

The volume of box e is: 112.5 cubic inches


Final thoughts

The ability to overload a constructor allows us to seamlessly expand our classes without breaking code that is already in use. You can have two constructors with the same number of parameters as long the data types for the parameters are different.


Tutorials