From 217e0999c186098d2784077856016a6cbd52a918 Mon Sep 17 00:00:00 2001 From: Michael Hilt Date: Thu, 28 Jul 2022 14:21:18 -0400 Subject: [PATCH 1/2] ch28Exercises --- exercises/SpaceLocation.js | 12 ++++++++ exercises/SpaceLocation.ts | 9 ++++++ exercises/parts1-5.js | 61 ++++++++++++++++++++++++++++++++++++++ exercises/parts1-5.ts | 54 +++++++++++++++++++++++++++++++-- 4 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 exercises/SpaceLocation.js create mode 100644 exercises/parts1-5.js diff --git a/exercises/SpaceLocation.js b/exercises/SpaceLocation.js new file mode 100644 index 00000000..ec5eefae --- /dev/null +++ b/exercises/SpaceLocation.js @@ -0,0 +1,12 @@ +"use strict"; +exports.__esModule = true; +exports.SpaceLocation = void 0; +// Paste in the provided code here: +var SpaceLocation = /** @class */ (function () { + function SpaceLocation(name, kilometersAway) { + this.name = name; + this.kilometersAway = kilometersAway; + } + return SpaceLocation; +}()); +exports.SpaceLocation = SpaceLocation; diff --git a/exercises/SpaceLocation.ts b/exercises/SpaceLocation.ts index c74620cb..14646a2a 100644 --- a/exercises/SpaceLocation.ts +++ b/exercises/SpaceLocation.ts @@ -1 +1,10 @@ // Paste in the provided code here: +export class SpaceLocation { + kilometersAway: number; + name: string; + + constructor(name: string, kilometersAway: number) { + this.name = name; + this.kilometersAway = kilometersAway; + } +} \ No newline at end of file diff --git a/exercises/parts1-5.js b/exercises/parts1-5.js new file mode 100644 index 00000000..489e31f7 --- /dev/null +++ b/exercises/parts1-5.js @@ -0,0 +1,61 @@ +"use strict"; +// URL for the instructions: +// https://education.launchcode.org/intro-to-professional-web-dev/chapters/typescript/exercises.html +exports.__esModule = true; +// Part 1: Declare (5) Variables With Type +// let spacecraftName: string = "Determination"; +// let speedMph: number = 17500; +var kilometersToMars = 225000000; +var kilometersToTheMoon = 384400; +// let milesPerKilometer: number = 0.621; +var kilometersToJupiter = 588000000; +// Part 2: Print Days to Mars +// let milesToMars: number = kilometersToMars * milesPerKilometer; +// let hoursToMars: number = milesToMars / speedMph; +// let daysToMars: number = hoursToMars / 24; +// Code an output statement here (use a template literal): +// console.log(`It will take ${spacecraftName} ${daysToMars} days to get to Mars.`); +// Part 3: Create a Function ("getDaysToLocation") +// function getDaysToLocation(kilometersAway:number):number { +// let milesToLocation: number = kilometersAway * milesPerKilometer +// let hoursToLocation: number = milesToLocation / speedMph; +// let daysToLocation: number = hoursToLocation / 24; +// return daysToLocation; +// }; +// Move your output statement from part 2 here. Update the template literal to call +// the function and print the outputs for a Mars trip and a moon trip. +//console.log(`It will take ${spacecraftName} ${getDaysToLocation(kilometersToMars)} days to get to Mars.`); +//console.log(`It will take ${spacecraftName} ${getDaysToLocation(kilometersToJupiter)} days to get to Jupiter.`); +// Part 4: Create a Spacecraft Class +var Spacecraft = /** @class */ (function () { + function Spacecraft(name, speedMph) { + this.milesPerKilometer = 0.621; + this.name = name; + this.speedMph = speedMph; + } + ; + Spacecraft.prototype.getDaysToLocation = function (kilometersAway) { + var milesToLocation = kilometersAway * this.milesPerKilometer; + var hoursToLocation = milesToLocation / this.speedMph; + var daysToLocation = hoursToLocation / 24; + return daysToLocation; + }; + Spacecraft.prototype.printDaysToLocation = function (location) { + console.log("".concat(this.name, " would take ").concat(this.getDaysToLocation(location.kilometersAway), " days to get to ").concat(location.name, ".")); + }; + ; + return Spacecraft; +}()); +// Create an instance of the class here: +var spaceShuttle = new Spacecraft('Determination', 17500); +// Move your output statements from part 3 here. Update the template literals use the +// instance of the class. +// console.log(`It will take ${spaceShuttle.name} ${spaceShuttle.getDaysToLocation(kilometersToMars)} days to get to Mars.`); +// console.log(`It will take ${spaceShuttle.name} ${spaceShuttle.getDaysToLocation(kilometersToTheMoon)} days to get to the moon.`); +// Part 5: Export and Import the SpaceLocation Class +// Add the required import statement BEFORE the part 1 concent. +var SpaceLocation_1 = require("./SpaceLocation"); +// Add the printDaysToLocation function to the Spacecraft class. +// Paste in the code from step 6 here: +spaceShuttle.printDaysToLocation(new SpaceLocation_1.SpaceLocation('Mars', kilometersToMars)); +spaceShuttle.printDaysToLocation(new SpaceLocation_1.SpaceLocation('the Moon', kilometersToTheMoon)); diff --git a/exercises/parts1-5.ts b/exercises/parts1-5.ts index dc943d7d..56842ba5 100644 --- a/exercises/parts1-5.ts +++ b/exercises/parts1-5.ts @@ -3,45 +3,95 @@ // Part 1: Declare (5) Variables With Type - - +// let spacecraftName: string = "Determination"; +// let speedMph: number = 17500; +let kilometersToMars: number = 225000000; +let kilometersToTheMoon: number = 384400; +// let milesPerKilometer: number = 0.621; +let kilometersToJupiter: number = 588000000; // Part 2: Print Days to Mars +// let milesToMars: number = kilometersToMars * milesPerKilometer; +// let hoursToMars: number = milesToMars / speedMph; +// let daysToMars: number = hoursToMars / 24; // Code an output statement here (use a template literal): +// console.log(`It will take ${spacecraftName} ${daysToMars} days to get to Mars.`); // Part 3: Create a Function ("getDaysToLocation") +// function getDaysToLocation(kilometersAway:number):number { + +// let milesToLocation: number = kilometersAway * milesPerKilometer +// let hoursToLocation: number = milesToLocation / speedMph; +// let daysToLocation: number = hoursToLocation / 24; + +// return daysToLocation; +// }; // Move your output statement from part 2 here. Update the template literal to call // the function and print the outputs for a Mars trip and a moon trip. +//console.log(`It will take ${spacecraftName} ${getDaysToLocation(kilometersToMars)} days to get to Mars.`); +//console.log(`It will take ${spacecraftName} ${getDaysToLocation(kilometersToJupiter)} days to get to Jupiter.`); // Part 4: Create a Spacecraft Class +class Spacecraft{ + milesPerKilometer: number = 0.621; + name: string; + speedMph: number + + constructor(name: string, speedMph: number){ + this.name = name; + this.speedMph = speedMph; + }; + getDaysToLocation(kilometersAway:number):number { + let milesToLocation: number = kilometersAway * this.milesPerKilometer + let hoursToLocation: number = milesToLocation / this.speedMph; + let daysToLocation: number = hoursToLocation / 24; + + return daysToLocation; + } + + printDaysToLocation(location: SpaceLocation) { + console.log(`${this.name} would take ${this.getDaysToLocation(location.kilometersAway)} days to get to ${location.name}.`); + }; + + +} // Create an instance of the class here: +let spaceShuttle = new Spacecraft('Determination', 17500); // Move your output statements from part 3 here. Update the template literals use the // instance of the class. +// console.log(`It will take ${spaceShuttle.name} ${spaceShuttle.getDaysToLocation(kilometersToMars)} days to get to Mars.`); +// console.log(`It will take ${spaceShuttle.name} ${spaceShuttle.getDaysToLocation(kilometersToTheMoon)} days to get to the moon.`); // Part 5: Export and Import the SpaceLocation Class // Add the required import statement BEFORE the part 1 concent. +import {SpaceLocation} from './SpaceLocation'; + // Add the printDaysToLocation function to the Spacecraft class. + // Paste in the code from step 6 here: + +spaceShuttle.printDaysToLocation(new SpaceLocation('Mars', kilometersToMars)); +spaceShuttle.printDaysToLocation(new SpaceLocation('the Moon', kilometersToTheMoon)); From 0209e0a589fb58aa1210cbb2705e0cd26ee99274 Mon Sep 17 00:00:00 2001 From: Michael Hilt Date: Fri, 29 Jul 2022 12:14:34 -0400 Subject: [PATCH 2/2] finished ch28 studio --- studio/Astronaut.js | 11 +++++++++ studio/Astronaut.ts | 14 ++++++++++++ studio/Cargo.js | 11 +++++++++ studio/Cargo.ts | 13 +++++++++++ studio/Payload.js | 2 ++ studio/Rocket.js | 40 ++++++++++++++++++++++++++++++++ studio/Rocket.ts | 56 +++++++++++++++++++++++++++++++++++++++++++++ studio/index.js | 46 +++++++++++++++++++++++++++++++++++++ studio/index.ts | 49 ++++++++++++++++++++++++++++++++++++++- 9 files changed, 241 insertions(+), 1 deletion(-) create mode 100644 studio/Astronaut.js create mode 100644 studio/Astronaut.ts create mode 100644 studio/Cargo.js create mode 100644 studio/Cargo.ts create mode 100644 studio/Payload.js create mode 100644 studio/Rocket.js create mode 100644 studio/Rocket.ts create mode 100644 studio/index.js diff --git a/studio/Astronaut.js b/studio/Astronaut.js new file mode 100644 index 00000000..11479ee2 --- /dev/null +++ b/studio/Astronaut.js @@ -0,0 +1,11 @@ +"use strict"; +exports.__esModule = true; +exports.Astronaut = void 0; +var Astronaut = /** @class */ (function () { + function Astronaut(massKg, name) { + this.massKg = massKg; + this.name = name; + } + return Astronaut; +}()); +exports.Astronaut = Astronaut; diff --git a/studio/Astronaut.ts b/studio/Astronaut.ts new file mode 100644 index 00000000..821547cf --- /dev/null +++ b/studio/Astronaut.ts @@ -0,0 +1,14 @@ +import { Payload } from "./Payload"; + +export class Astronaut implements Payload { + + name: string; + massKg: number; + + constructor(massKg: number, name: string) { + this.massKg = massKg; + this.name = name; + } + + +} \ No newline at end of file diff --git a/studio/Cargo.js b/studio/Cargo.js new file mode 100644 index 00000000..d07013f3 --- /dev/null +++ b/studio/Cargo.js @@ -0,0 +1,11 @@ +"use strict"; +exports.__esModule = true; +exports.Cargo = void 0; +var Cargo = /** @class */ (function () { + function Cargo(massKg, material) { + this.massKg = massKg; + this.material = material; + } + return Cargo; +}()); +exports.Cargo = Cargo; diff --git a/studio/Cargo.ts b/studio/Cargo.ts new file mode 100644 index 00000000..d3bca4f7 --- /dev/null +++ b/studio/Cargo.ts @@ -0,0 +1,13 @@ +import { Payload } from './Payload'; + +export class Cargo implements Payload { + + massKg: number; + material: string; + + constructor (massKg: number, material: string) { + this.massKg = massKg; + this.material = material; + } + +} \ No newline at end of file diff --git a/studio/Payload.js b/studio/Payload.js new file mode 100644 index 00000000..0e345787 --- /dev/null +++ b/studio/Payload.js @@ -0,0 +1,2 @@ +"use strict"; +exports.__esModule = true; diff --git a/studio/Rocket.js b/studio/Rocket.js new file mode 100644 index 00000000..d93ab15f --- /dev/null +++ b/studio/Rocket.js @@ -0,0 +1,40 @@ +"use strict"; +exports.__esModule = true; +exports.Rocket = void 0; +var Rocket = /** @class */ (function () { + function Rocket(name, totalCapacityKg) { + this.cargoItems = []; + this.astronauts = []; + this.name = name; + this.totalCapacityKg = totalCapacityKg; + } + Rocket.prototype.sumMass = function (items) { + var totalMass = 0; + for (var i = 0; i < items.length; i++) { + totalMass += items[i].massKg; + } + return totalMass; + }; + Rocket.prototype.currentMassKg = function () { + return this.sumMass(this.astronauts) + this.sumMass(this.cargoItems); + }; + Rocket.prototype.canAdd = function (item) { + return this.currentMassKg() + item.massKg <= this.totalCapacityKg; + }; + Rocket.prototype.addCargo = function (cargo) { + if (!this.canAdd(cargo)) { + return false; + } + this.cargoItems.push(cargo); + return true; + }; + Rocket.prototype.addAstronaut = function (astronaut) { + if (!this.canAdd(astronaut)) { + return false; + } + this.astronauts.push(astronaut); + return true; + }; + return Rocket; +}()); +exports.Rocket = Rocket; diff --git a/studio/Rocket.ts b/studio/Rocket.ts new file mode 100644 index 00000000..5d8562ee --- /dev/null +++ b/studio/Rocket.ts @@ -0,0 +1,56 @@ +import { Cargo } from './Cargo'; +import { Astronaut } from './Astronaut'; +import { Payload } from './Payload'; + +export class Rocket { + + name: string; + totalCapacityKg: number; + cargoItems: Cargo[] = []; + astronauts: Astronaut[] = []; + + constructor (name: string, totalCapacityKg: number) { + this.name = name; + this.totalCapacityKg = totalCapacityKg; + } + + sumMass( items: Payload[] ): number { + + let totalMass = 0; + + for (let i: number = 0; i < items.length; i++){ + totalMass += items[i].massKg; + } + + return totalMass; + } + + currentMassKg(): number { + return this.sumMass(this.astronauts) + this.sumMass(this.cargoItems); + } + + canAdd(item: Payload): boolean { + return this.currentMassKg() + item.massKg <= this.totalCapacityKg; + } + + addCargo(cargo: Cargo): boolean { + if (!this.canAdd(cargo)) { + return false; + } + + this.cargoItems.push(cargo); + return true; + } + + addAstronaut(astronaut: Astronaut): boolean{ + + if (!this.canAdd(astronaut)) { + return false; + } + + this.astronauts.push(astronaut); + return true; + + } + +} \ No newline at end of file diff --git a/studio/index.js b/studio/index.js new file mode 100644 index 00000000..78047d9b --- /dev/null +++ b/studio/index.js @@ -0,0 +1,46 @@ +"use strict"; +// Instructions are published in the online book. The URL is: +// https://education.launchcode.org/intro-to-professional-web-dev/chapters/typescript/studio.html +exports.__esModule = true; +// TODO: +// * Code the Astronaut, Cargo, and Rocket classes in new files. +// * Import the three classes into this file. +var Astronaut_1 = require("./Astronaut"); +var Cargo_1 = require("./Cargo"); +var Rocket_1 = require("./Rocket"); +var falcon9 = new Rocket_1.Rocket('Falcon 9', 7500); +var astronauts = [ + new Astronaut_1.Astronaut(75, 'Mae'), + new Astronaut_1.Astronaut(81, 'Sally'), + new Astronaut_1.Astronaut(99, 'Charles') +]; +for (var i = 0; i < astronauts.length; i++) { + var astronaut = astronauts[i]; + var status_1 = ''; + if (falcon9.addAstronaut(astronaut)) { + status_1 = "On board"; + } + else { + status_1 = "Not on board"; + } + console.log("".concat(astronaut.name, ": ").concat(status_1)); +} +var cargo = [ + new Cargo_1.Cargo(3107.39, "Satellite"), + new Cargo_1.Cargo(1000.39, "Space Probe"), + new Cargo_1.Cargo(753, "Water"), + new Cargo_1.Cargo(541, "Food"), + new Cargo_1.Cargo(2107.39, "Tesla Roadster"), +]; +for (var i = 0; i < cargo.length; i++) { + var c = cargo[i]; + var loaded = ''; + if (falcon9.addCargo(c)) { + loaded = "Loaded"; + } + else { + loaded = "Not loaded"; + } + console.log("".concat(c.material, ": ").concat(loaded)); +} +console.log("Final cargo and astronaut mass: ".concat(falcon9.currentMassKg(), " kg.")); diff --git a/studio/index.ts b/studio/index.ts index bef1e6b1..642dce46 100644 --- a/studio/index.ts +++ b/studio/index.ts @@ -3,4 +3,51 @@ // TODO: // * Code the Astronaut, Cargo, and Rocket classes in new files. -// * Import the three classes into this file. \ No newline at end of file +// * Import the three classes into this file. + + + +import { Astronaut } from './Astronaut'; +import { Cargo } from './Cargo'; +import { Rocket } from './Rocket'; + +let falcon9: Rocket = new Rocket('Falcon 9', 7500); + +let astronauts: Astronaut[] = [ + new Astronaut(75, 'Mae'), + new Astronaut(81, 'Sally'), + new Astronaut(99, 'Charles') +]; + +for (let i = 0; i < astronauts.length; i++) { + let astronaut = astronauts[i]; + let status = ''; + if (falcon9.addAstronaut(astronaut)) { + status = "On board"; + } else { + status = "Not on board"; + } + console.log(`${astronaut.name}: ${status}`); +} + +let cargo: Cargo[] = [ + new Cargo(3107.39, "Satellite"), + new Cargo(1000.39, "Space Probe"), + new Cargo(753, "Water"), + new Cargo(541, "Food"), + new Cargo(2107.39, "Tesla Roadster"), +]; + +for (let i = 0; i < cargo.length; i++) { + let c = cargo[i]; + let loaded = ''; + if (falcon9.addCargo(c)) { + loaded = "Loaded" + } else { + loaded = "Not loaded" + } + console.log(`${c.material}: ${loaded}`); +} + +console.log(`Final cargo and astronaut mass: ${falcon9.currentMassKg()} kg.`); +