diff --git a/exec1.html b/exec1.html new file mode 100644 index 0000000000000000000000000000000000000000..0b2857a7eec4c3b607264f70d10c4f04aaf15c4d --- /dev/null +++ b/exec1.html @@ -0,0 +1,36 @@ +<!DOCTYPE html> +<html> + <body> + <script src = "exec1.js"></script> + <script> + "use strict"; + + let p1 = new Person("180699-1018"); + let p2 = new Person("130908A406T"); + + // Below is the "old code" that should work once you have modified the Person class. + // Note that month numbering starts from zero in date.getMonth() + let success = true; + console.log(p2.PICend); + if (p1.birthDate.getFullYear() != 1999 || p1.birthDate.getMonth() != 5 || p1.birthDate.getDate() != 18) { + alert(`Wrong birth date for person with PIC ${p1.PIC}: ${p1.birthDate}`); + success = false; + } + if(p1.PICend != "1018") { + alert(`Wrong PIC end for person with PIC ${p1.PIC}: ${p1.PICend}`); + success = false; + } + if (p2.birthDate.getFullYear() != 2008 || p2.birthDate.getMonth() != 8 || p2.birthDate.getDate() != 13) { + alert(`Wrong birth date for person with PIC ${p2.PIC}: ${p2.birthDate}`); + success = false; + } + if(p2.PICend.toUpperCase() != "406T") { + alert(`Wrong PIC end for person with PIC ${p2.PIC}: ${p2.PICend}`); + success = false; + } + if (success) { + alert("All tests passed successfully!"); + } + </script> + </body> +</html> \ No newline at end of file diff --git a/exec1.js b/exec1.js new file mode 100644 index 0000000000000000000000000000000000000000..84933655727eb5527f7eaef3ac633cd31cba5da1 --- /dev/null +++ b/exec1.js @@ -0,0 +1,47 @@ +"use strict" + +class Person { + + constructor(PIC) { + this._PIC = PIC; + this.birthDate = this.getDate(); + this.PICend = this.getPICend(); + } + + get PIC() { return this._PIC; } + // We usually should not need to change the SSN of a person. + //However, it is possible that it has been mistyped + set PIC(PIC) { this._PIC = PIC;} + + getDate=()=>{ + let date + if(this._PIC.split("-")[0].length===6){ + date = this._PIC.split("-")[0]; + }else if (this._PIC.split("A")[0].length===6) + date = this._PIC.split("A")[0]; + + let day = date.slice(0,2) + let mnt = Number(date.slice(2,4))-1 + let yr = date.slice(4,6) + if(yr<20) + yr = "20"+yr + date = new Date(yr,mnt,day) + // console.log(date.getFullYear()); + return date + } + + getPICend=()=>{ + console.log(this._PIC.split("-")); + let pic + if(this._PIC.split("-")[1]){ + pic = this._PIC.split("-")[1]; + } + else if (this._PIC.split("A")[1]) + pic = this._PIC.split("A")[1]; + + return pic + } + // TODO: implement getters for birthDate and PICend + + +} \ No newline at end of file diff --git a/exec2.html b/exec2.html new file mode 100644 index 0000000000000000000000000000000000000000..e4940ed9d13c64e35d03770773fd1c4c6a052be0 --- /dev/null +++ b/exec2.html @@ -0,0 +1,31 @@ +<!DOCTYPE html> +<html> + <body> + <script src = "exec2.js"></script> + <script> + "use strict"; + + let grid = new Grid3D(10, 5, 20); + grid.setValue(1, 2, 3, 100); + let d = new Date(); + grid.setValue(9, 4, 19, d); + + let success = true; + if (grid.getValue(0, 0, 0) != null) { + alert(`Wrong value at (0, 0, 0). Expected null, got ${grid.getValue(0, 0, 0)}`); + success = false; + } + if (grid.getValue(1, 2, 3) != 100) { + alert(`Wrong value at (1, 2, 3). Expected 100, got ${grid.getValue(1, 2, 3)}`); + success = false; + } + if (grid.getValue(9, 4, 19) != d) { + alert(`Wrong value at (9, 4, 19). Expected ${d}, got ${grid.getValue(9, 4, 19)}`); + success = false; + } + if (success) { + alert("All tests performed successfully"); + } + </script> + </body> +</html> \ No newline at end of file diff --git a/exec2.js b/exec2.js new file mode 100644 index 0000000000000000000000000000000000000000..0da1e4092e2e1c614a30c59370edee034c78736d --- /dev/null +++ b/exec2.js @@ -0,0 +1,47 @@ +"use strict"; + +/** + * Grid3D is a 3-dimensional array. Initially it contains null values. + * + * @param {number} slices The size of the Grid in the first dimension + * @param {number} rows The size of the Grid in the second dimension + * @param {number} columns The size of the Grid in the third dimension + */ +class Grid3D{ + + constructor(slices, rows, columns){ + let arr = new Array(slices); + for(let slice_num = 0; slice_num < arr.length; slice_num++) { + arr[slice_num] = new Array(rows); + for(let row_num = 0; row_num < arr[slice_num].length; row_num++) { + arr[slice_num][row_num] = new Array(columns); + arr[slice_num][row_num].fill(null); + } + } + + this._arr = arr; +} + /** + * Get the value stored in the given location of the array + * + * @param {number} slice_num The location in the first dimension + * @param {number} row_num The location in the second dimension + * @param {number} col_num The location in the third dimension + */ + getValue (slice_num, row_num, col_num) { + return this._arr[slice_num][row_num][col_num]; + } + + /** + * Set the value of the given location + * + * @param {number} slice_num The location in the first dimension + * @param {number} row_num The location in the second dimension + * @param {number} col_num The location in the third dimension + * @param{*} value The value to store in the specified location + */ + setValue (slice_num, row_num, col_num, value) { + this._arr[slice_num][row_num][col_num] = value; + } + + } diff --git a/exec3.html b/exec3.html new file mode 100644 index 0000000000000000000000000000000000000000..effbbc6444e58216fe072ac09ca59370437a3d31 --- /dev/null +++ b/exec3.html @@ -0,0 +1,35 @@ +<!DOCTYPE html> +<html> + <body> + <script src="exec3_solution.js"></script> + + <script> + "use strict"; + let o = new Organism(10, 50, 50); + let [age, mass, locX, locY] = [o.getAge(), o.getMass(), o.getXLocation(), o.getYLocation()]; + let success = checkValues([0, 10, 50, 50], [age, mass, locX, locY]); + // Try to make some changes + o.setLocation(74, 22); + o.setMass(-5); + for(let i = 0; i < 5; i++) { + o.increaseAge(); + } + [age, mass, locX, locY] = [o.getAge(), o.getMass(), o.getXLocation(), o.getYLocation()]; + success = success && checkValues([5, 10, 74, 22], [age, mass, locX, locY]); + + if (success) { + alert("All tests passed."); + } + + function checkValues(expected, actual) { // We use this function to display error messages + for (let i = 0; i < expected.length; i++) { + if (expected[i] != actual[i]) { + alert(`Wrong values: expected [${expected}], actual [${actual}]`); + return false; + } + } + return true; + } + </script> + </body> +</html> \ No newline at end of file diff --git a/exec3_solution.js b/exec3_solution.js new file mode 100644 index 0000000000000000000000000000000000000000..1c6d2834af750eb7007a40844ebec75d54525c5d --- /dev/null +++ b/exec3_solution.js @@ -0,0 +1,40 @@ +class Organism { + constructor(mass, locX, locY) { + this.age = 0; + this.mass = mass; + this.locX = locX; + this.locY = locY; + } + + getAge() { + return this.age; + //console.log(this.age); + } + increaseAge() { + return (this.age = this.age + 1); + } + + setAge() { + this.age = 0; + increaseAge(); + } + getMass() { + return this.mass; + } + setMass() { + if (this.mass > 0) { + return this.mass; + } + } + setLocation(x, y) { + this.locX = x; + this.locY = y; + } + + getXLocation() { + return this.locX; + } + getYLocation() { + return this.locY; + } +} diff --git a/exec4.html b/exec4.html new file mode 100644 index 0000000000000000000000000000000000000000..89504d815b9d67948babbd6538c6ea81fcd34489 --- /dev/null +++ b/exec4.html @@ -0,0 +1,84 @@ +<!DOCTYPE html> +<html> + <body> + <script src="exec4_solution.js"></script> + + <script> + "use strict"; + + let plant = new Plant(10, 50, 50); + plant.performTimeStep(); + + let [age, mass, locX, locY] = [ + plant.getAge(), + plant.getMass(), + plant.getXLocation(), + plant.getYLocation(), + ]; + + let success = checkValues([0, 10.01, 50, 50], [age, mass, locX, locY]); + // Try to make some changes + plant.setLocation(74, 22); + plant.setMass(-5); + + for (let i = 0; i < 5; i++) { + plant.increaseAge(); + } + + [age, mass, locX, locY] = [ + plant.getAge(), + plant.getMass(), + plant.getXLocation(), + plant.getYLocation(), + ]; + success = + success && checkValues([5, 10.01, 74, 22], [age, mass, locX, locY]); + if (success) { + alert("All tests passed for Plant class."); + } + //ANIMAL START + + let animal = new Animal(10, 50, 50); + animal.performTimeStep(); + animal.performTimeStep(); + animal.performTimeStep(); + + [age, mass, locX, locY] = [ + animal.getAge(), + animal.getMass(), + animal.getXLocation(), + animal.getYLocation(), + ]; + let success1 = checkValues([0, 10, 53, 50], [age, mass, locX, locY]); + animal.setLocation(74, 22); + animal.setMass(-5); + + for (let i = 0; i < 5; i++) { + animal.increaseAge(); + } + [age, mass, locX, locY] = [ + animal.getAge(), + animal.getMass(), + animal.getXLocation(), + animal.getYLocation(), + ]; + success1 = + success1 && checkValues([5, 10, 74, 22], [age, mass, locX, locY]); + + if (success1) { + alert("All tests passed for Animal class."); + } + + function checkValues(expected, actual) { + // We use this function to display error messages + for (let i = 0; i < expected.length; i++) { + if (expected[i] != actual[i]) { + alert(`Wrong values: expected [${expected}], actual [${actual}]`); + return false; + } + } + return true; + } + </script> + </body> +</html> diff --git a/exec4_solution.js b/exec4_solution.js new file mode 100644 index 0000000000000000000000000000000000000000..38177a7f434ebad9029a742065ddc610f9125580 --- /dev/null +++ b/exec4_solution.js @@ -0,0 +1,60 @@ +class Organism { + constructor(mass, locX, locY) { + this.age = 0; + this.mass = mass; + this.locX = locX; + this.locY = locY; + } + + maxAge = 2; + isAlive; + performTimeStep() { + this.age += 1; + if (this.age > this.maxAge) { + this.isAlive = false; + } + } + + getAge() { + return this.age; + //console.log(this.age); + } + increaseAge() { + return (this.age = this.age + 1); + } + + setAge() { + this.age = 0; + increaseAge(); + } + getMass() { + return this.mass; + } + setMass() { + if (this.mass > 0) { + return this.mass; + } + } + setLocation(x, y) { + this.locX = x; + this.locY = y; + } + + getXLocation() { + return this.locX; + } + getYLocation() { + return this.locY; + } +} + +class Plant extends Organism { + performTimeStep() { + this.mass += 0.01; + } +} +class Animal extends Organism { + performTimeStep() { + this.locX += 1; + } +} diff --git a/exec5.html b/exec5.html new file mode 100644 index 0000000000000000000000000000000000000000..9a96a1170a948541bb9cdfd9da80b1c18e65eab8 --- /dev/null +++ b/exec5.html @@ -0,0 +1,54 @@ +<!DOCTYPE html> +<html> + <body> + <script src="exec5_solution.js"></script> + <script> + let arr = new MyArray(); + let success = true; + + try { + arr.set(0, "First"); + } catch (err) { + alert(`Test of adding an element to an empty array failed: ${err}`); + success = false; + } + + let value; + try { + value = arr.get(0); + if (value != "First") throw new Error("Wrong content in array."); + } catch (err) { + alert( + `Test of getting an element from a nonempty array failed: ${err}` + ); + success = false; + } + + try { + value = arr.get(1); // Should throw an exception so the next lines are not executed + success = false; + alert("Test of getting a non-existing element from an array failed."); + } catch (err) {} + + try { + arr.set(2, "Third"); // Should throw an exception + success = false; + alert( + "Test of setting element at index 2 for one-element array failed - did not throw an exception." + ); + } catch (err) {} + + try { + arr.set("stringIndex", "Second"); + success = false; + alert( + "Test of setting element at index 'stringIndex' failed - did not throw an exception." + ); + } catch (err) {} + + if (success) { + alert("All tests passed successfully."); + } + </script> + </body> +</html> diff --git a/exec5_solution.js b/exec5_solution.js new file mode 100644 index 0000000000000000000000000000000000000000..b42c8aaef5ea3f26f418b25830c3a85717432450 --- /dev/null +++ b/exec5_solution.js @@ -0,0 +1,25 @@ +class MyArray { + constructor() { + this.arr = []; + } + + get(index) { + if (Number.isInteger(index)) { + if (index === 0 || index <= this.arr.length - 1) { + return this.arr[index]; + } else throw "Parameter is not a number!"; + } + } + + set(index, value) { + if (Number.isInteger(index)) { + if (index == this.arr.length) { + this.arr.push(value); + } else { + var i = this.arr[index]; + if (i) this.arr[index] = value; + else throw "error"; + } + } else throw "error"; + } +}