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>
Step 1
- Create a class called
Parcel - Add a constructor that accepts three arguments:
length,widthandheight - The constructor should copy the values of these arguments into attributes of the same names.
- Add a method called
printVolumethat 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
myParcelof typeParcelwith dimensions (4 x 5 x 6). - Call the
printVolumemethod on yourmyParcelobject - After this, set the width attribute of your
myParcelobject to 2, then call itsprintVolumemethod again
Step 2
This is an extension of Step 1 - complete in the same file.
- Create a class called
Shipmentthat inherits from theParcelclass. - Add a constructor that accepts four arguments:
length,width,heightandcost. - Call the constructor of the superclass, providing the relevant arguments.
- Also within the constructor, copy the value of the
costargument into an attribute of the same name. -
Add a method to the
Shipmentclass calledprintStats. This method should print two things (on two separate lines):- The shipping cost
- The volume of the package. Do this by calling the
printVolumemethod.
-
Below your class, create an object called
myShipmentof typeShipment. You may choose the dimensions and the cost. - Call the
printStatsmethod on yourmyShipmentobject
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()
}