Using javascript to 1- Create a new “aircraft” object (via object constructor) t
ID: 3668117 • Letter: U
Question
Using javascript to
1- Create a new “aircraft” object (via object constructor) that includes (among others) a speed property. Include a changeSpeed() method that will increase or decrease the aircraft speed based on the argument sent in the function call. For instance, changeSpeed(5) will increase speed by 5mph and changeSpeed(-5) decreases speed by 5mph. User enters the number and clicks a button to call the method.
2- Use the prototype property to add a new property (range) and a new method (addFuel) to the aircraft prototype. As always, display results.
Explanation / Answer
<!-- save the file as aircraft.html and open the file in any browser -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script language="javascript" type="text/javascript">
// Creating a constructor for Aircraft
function Aircraft(name, type, speed, capacity, fuel) {
this.name = name;
this.type = type;
this.speed = speed;
this.capacity = capacity;
this.fuel = fuel;
// change speed method
this.changeSpeed = function(speedToModify) {
this.speed = parseInt(this.speed, 10) + parseInt(speedToModify, 10);
// calling method to show updated result
this.displayUpdatedDetails();
}
// this method is used to simply creating a cocatenated value of Aircraft object
this.toString = function() {
var str =
"Name - " + this.name + "<br/>" +
"Type - " + this.type + "<br/>" +
"Speed - " + this.speed + "<br/>" +
"Capacity - " + this.capacity + "<br/>" +
"Fuel - " + this.fuel + "<br/>";
return str;
}
}
// creating a prototype proerty for Aircraft object
Aircraft.prototype.range = "500";
// a prototype method "addFuel" for Aircraft object
Aircraft.prototype.addFuel = function(fuelToModify) {
this.fuel = parseInt(this.fuel, 10) + parseInt(fuelToModify, 10);
this.displayUpdatedDetails();
}
// method used for displaying default aircraft object details
Aircraft.prototype.displayDetails = function() {
document.getElementById("displayDetails").innerHTML = this.toString();
document.getElementById("defaultDetails").style.display = 'block';
}
// method used for displaying modfied aircraft object details
Aircraft.prototype.displayUpdatedDetails = function() {
document.getElementById("displayUpdatedDetails").innerHTML = this.toString();
document.getElementById("updatedDetails").style.display = 'block';
}
// creating default Aircraft object
var aircraft = new Aircraft("British Airways", "aeroplane", 55, 500, 250);
</script>
</head>
<body>
<br />
<br />
<br />
<input type="text" value="" id="speed" name="speed" />
<!-- calling changespeed() method on change of textbox to change the speed of aircraft -->
<input type="button" value="change speed" />
<br />
<br />
<br />
<input type="text" value="" id="fuel" name="fuel" />
<!-- calling addFuel() method on change of textbox to change the fule size of aircraft -->
<input type="button" value="add fuel" />
<br />
<br />
<br />
<input type="button" value="Display Default Details" />
<input type="button" value="Display Updated Details" />
<br />
<br />
<div id="defaultDetails">
<B><U>Default Details</U></B><br/>
<p id="displayDetails"></p>
<br />
<br />
</div>
<div id="updatedDetails">
<B><U>Updated Details</U></B><br/>
<p id="displayUpdatedDetails"></p>
<br />
<br />
</div>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.