クラス
課題01
課題01
2次元の座標を示すx
とy
をメンバーに持つクラスPoint2
を定義してください。
コンストラクタの引数はx
とy
です。
class Point2 {
// ここを実装してください
}
課題02
課題02
クラスPoint2
のインスタンスp1
を生成してください。x
は1
でy
は2
とします。
{
const p1 = /* ここを実装してください */
console.assert(p1.x === 1);
console.assert(p1.y === 2);
}
課題03
課題03
クラスPoint2
のインスタンスp2
を生成してください。x
は3
でy
は4
とします。
{
const p2 = /* ここを実装してください */
console.assert(p2.x === 3);
console.assert(p2.y === 4);
}
課題04
課題04
p1
とp2
のx
の和とy
の和を持つPoint2
のインスタンスp3
を生成してください。
{
const p3 = new Point2(/* ここを実装してください */);
console.assert(p3.x === 4);
console.assert(p3.y === 6);
}
課題05
課題05
任意の2つのPoint2
のインスタンスのx
の和とy
の和を持つPoint2
のインスタンスを生成するメソッドadd
を実装してください。
class Point2 {
...
add(point) {
// ここを実装してください
}
}
{
const p3 = p1.add(p2);
console.assert(p3.x === p1.x + p2.x);
console.assert(p3.y === p1.y + p2.y);
}
課題06
課題06
任意の2つのPoint2
のインスタンスのx
の差とy
の差を持つPoint2
のインスタンスを生成するメソッドsub
を実装してください。
class Point2 {
...
sub(point) {
// ここを実装してください
}
}
{
const p3 = p1.sub(p2);
console.assert(p3.x === p1.x - p2.x);
console.assert(p3.y === p1.y - p2.y);
}
課題07
課題07
任意の2つのPoint2
のインスタンスの距離を返すメソッドdistance
を実装してください。
class Point2 {
...
distance(point) {
// ここを実装してください
}
...
}
{
const d = p1.distance(p2);
console.assert(p3.x === Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
課題08
課題08
クラスPoint2
を継承してクラスPoint3
を定義してください。
メンバにz
座標が追加されます。
class Point3 {
...
constructor(x, y, z) {
// ここを実装してください
}
...
}
課題09
課題09
クラスPoint3
でメソッドequals
、add
、sub
、distance
をオーバーライドしてください。Point3
ではz
が追加されていることに注意してください。
class Point3 {
...
equals(p) {
// ここを実装してください
}
add(p) {
// ここを実装してください
}
sub(p) {
// ここを実装してください
}
distance(p) {
// ここを実装してください
}
...
}
{
const p1 = new Point3(1, 2, 3);
const p2 = new Point3(1, 2, 3);
const p3 = new Point3(4, 5, 6);
console.assert(p1.equals(p2) === true);
console.assert(p1.equals(p3) === false);
console.assert(
p1.add(p2).equals(
new Point3(
p1.x + p2.x,
p1.y + p2.y,
p1.z + p2.z
)
) === true
);
console.assert(
p1.sub(p3).equals(
new Point3(
p1.x - p3.x,
p1.y - p3.y,
p1.z - p3.z
)
) === true
);
console.assert(
p1.distance(p3).equals(
Math.sqrt(
Math.pow(p1.x - p3.x, 2) +
Math.pow(p1.y - p3.y, 2) +
Math.pow(p1.z - p3.z, 2)
)
) === true
);
}
課題10
課題10
関数isPoint2
を定義してください。
この関数は引数に与えられたp
がPoint2
のインスタンスであればtrue
を返します。
function isPoint2(p) {
// ここを実装してください
}
{
const p1 = new Point2(1, 2);
const p2 = new Point3(1, 2, 3);
console.assert(isPoint2(p1) === true);
console.assert(isPoint2(p2) === false);
}
課題11
課題11
関数isPoint3
を定義してください。
この関数は引数に与えられたp
がPoint3
のインスタンスであればtrue
を返します。
function isPoint3(p) {
// ここを実装してください
}
{
const p1 = new Point2(1, 2);
const p2 = new Point3(1, 2, 3);
console.assert(isPoint3(p1) === false);
console.assert(isPoint3(p2) === true);
}
課題12
課題12
クラスTree
を定義してください。
メンバーとして数値を保持するleaf
とTree
の配列を保持するbranches
を持ちます。
コンストラクタの引数はleaf
とbranches
です。
class Tree {
/**
* Treeのコンストラクタ
* @param leaf 数値型
* @param branches Tree型の配列
*/
constructor(leaf, branches) {
// ここを実装してください
}
}
課題13
課題13
leaf
の値が1
でbranches
が空のTree
のインスタンスを生成してください。
{
const tree = new Tree(/* ここを実装してください */);
console.assert(tree.leaf === 1);
console.assert(tree.branches.length === 0);
}
課題14
課題14
以下のオブジェクトと同等の構造を持つTree
のインスタンスを生成し、変数t1
に代入してください。
{
leaf: 1,
branches: [
{ leaf: 2, branches: [] },
{ leaf: 3,
branches: [
{
leaf: 4,
branches [
{ leaf : 5, branches: [] }
{ leaf : 6, branches: [] }
]
},
{ leaf: 7, branches: [] },
{ leaf: 8, branches: [] }
]
},
{
leaf: 9,
branches: [
{ leaf: 10, branches: [] }
]
}
]
}
const t1 = new Tree(/* ここを実装してください */);
課題15
課題15
t1
に含まれるTree
のうち、leaf
の値が3
であるTree
が保持しているbranches
の要素数(Tree
インスタンスの数)を取得する関数を定義してください。
function getLengthOfBranches() {
// ここを実装してください
}
{
console.assert(getLengthOfBranches() === 3);
}
課題16
課題16
任意のTree
のインスタンスに含まれる全てのTree
のうち、指定した値と一致するleaf
を保持する全てのTree
を配列で返すTree
のメソッドを定義してください。そのようなTree
が無い場合には、[]
を返してください。
class Tree {
...
findInnerTree(leaf) {
// ここを実装してください
}
...
}
{
const a = new Tree(1, []);
const xs = a.findInnerTree(1)
console.assert(xs.length === 1);
console.assert(xs[0] === a);
console.assert(a.findInnerTree(2).length === 0);
}
{
const a = new Tree(2, []);
const b = new Tree(2, [a]);
const c = new Tree(1, [b]);
const xs = c.findInnerTree(2)
console.assert(xs.length === 2);
console.assert(xs.some(x => x === a));
console.assert(xs.some(x => x === b));
console.assert(findInnerTree(c, 10).length === 0);
}
課題17
課題17
任意のTree
のインスタンスに含まれる全てのleaf
の値の和を求めるメソッドsumLeafs
を定義してください。
class Tree {
...
sumLeafs() {
// ここを実装してください
}
}
{
const a = new Tree(1, [new Tree(2, [new Tree(3, [])])]);
console.assert(a.sumLeafs() === 6);
}
課題18
課題18
sumLeafs(t1)
の結果を確かめてください。
{
console.assert(t1.sumLeafs() === 55)
}
課題19
課題19
2つのTree
のインスタンスのleft
の値が同じでbranches
の構造も同じであればtrue
を返すメソッドを定義してください。
class Tree {
...
equals(tree) {
// ここを実装してください
}
...
}
{
const a = new Tree(1, [new Tree(2, [])]);
const b = new Tree(1, [new Tree(2, [])]);
const c = new Tree(1, [new Tree(2, [new Tree(3, [])])]);
console.assert(a.equals(b) === true);
console.assert(equals(a, c) === false);
}
課題20
課題20
任意のTree
のインスタンスに含まれる全てのleaf
の値を2倍にした同じ構造の別のTree
を作成するメソッドdoubleLeafs
を定義してください。
class Tree {
...
doubleLeafs() {
// ここを実装してください
}
...
}
{
const a = new Tree(1, [
new Tree(2, [
new Tree(3, []),
]),
new Tree(4, []),
]);
const b = new Tree(1, [
new Tree(4, [
new Tree(6, []),
]),
new Tree(8, []),
]);
console.assert(equals(doubleLeafs(a), b) === true);
console.assert(doubleLeafs(a) !== b);
}
課題21
課題21
doubleLeafs(t1)
の結果を確かめてください。