Skip to content

Practical Exam Review

  • The practical exam will be on Friday, June 12 and is open book. You may use your previous assignments and the Internet.
  • You may NOT communicate with another person in any way or use a generative AI.
  • You will have one hour to complete the questions. Those with IEPs allowing for extra time will be provided with this on a second day if needed.
  • The scenario you need to implement will be similar to this review. Complete the review in whatever files you would like.

Complete a program according to the following specifications. You can start with the following index.html and index.js:

index.html
<!DOCTYPE html>
<html>
<head>
    <title>Practical Exam Review</title>


</head>
<body>

    <div id="app"></div>
    <script src="index.js"></script>
    <script>
        let output = "";

        function print(text, end="<br>") {
            output += text + end
        }

        main()
        document.getElementById("app").innerHTML = output
    </script>
</body>
</html>
index.js
class Parcel {


}

function main() {
    print("It works! (Delete me)")
}

Step 1

  • Create a class called Parcel
  • Add a constructor that accepts three arguments: length, width and height
  • The constructor should copy the values of these arguments into attributes of the same names.
  • Add a method called printVolume that doesn't accept any arguments. It should calculate the volume of the parcel (by multiplying together the three dimensions) and print the result.
  • Below your class, create an object called myParcel of type Parcel with dimensions (4 x 5 x 6).
  • Call the printVolume method on your myParcel object
  • After this, set the width attribute of your myParcel object to 2, then call its printVolume method again

Step 2

This is an extension of Step 1 - complete in the same file.

  • Create a class called Shipment that inherits from the Parcel class.
  • Add a constructor that accepts four arguments: length, width, height and cost.
  • Call the constructor of the superclass, providing the relevant arguments.
  • Also within the constructor, copy the value of the cost argument into an attribute of the same name.
  • Add a method to the Shipment class called printStats. This method should print two things (on two separate lines):

    1. The shipping cost
    2. The volume of the package. Do this by calling the printVolume method.
  • Below your class, create an object called myShipment of type Shipment. You may choose the dimensions and the cost.

  • Call the printStats method on your myShipment object

Sample Solution

class Parcel {
    constructor(length, width, height) {
        this.length = length
        this.width = width
        this.height = height
    }

    printVolume() {
        print(`Volume = ${this.length * this.width * this.height}`)
    }

}

class Shipment extends Parcel {
    constructor(length, width, height, cost) {
        super(length, width, height)
        this.cost = cost
    }

    printStats() {
        print(`Cost = ${this.cost}`)
        this.printVolume()
    }
}

function main() {
    myParcel = new Parcel(4, 5, 6)
    myParcel.printVolume()
    myParcel.width = 2
    myParcel.printVolume()
    myShipment = new Shipment(10, 5, 7, 15.99)
    myShipment.printStats()
}