メインコンテンツまでスキップ

クラス

課題01
 

2次元の座標を示すxyをメンバーに持つクラスPoint2を定義してください。 コンストラクタの引数はxyです。

class Point2 {
// ここを実装してください
}

課題02
 

クラスPoint2のインスタンスp1を生成してください。x1y2とします。

{
const p1 = /* ここを実装してください */
console.assert(p1.x === 1);
console.assert(p1.y === 2);
}

課題03
 

クラスPoint2のインスタンスp2を生成してください。x3y4とします。

{
const p2 = /* ここを実装してください */
console.assert(p2.x === 3);
console.assert(p2.y === 4);
}

課題04
 

p1p2xの和とyの和を持つPoint2のインスタンスp3を生成してください。

{
const p3 = new Point2(/* ここを実装してください */);
console.assert(p3.x === 4);
console.assert(p3.y === 6);
}

課題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
 

任意の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
 

任意の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
 

クラスPoint2を継承してクラスPoint3を定義してください。 メンバにz座標が追加されます。

class Point3 {
...
constructor(x, y, z) {
// ここを実装してください
}
...
}

課題09
 

クラスPoint3でメソッドequalsaddsubdistanceをオーバーライドしてください。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
 

関数isPoint2を定義してください。 この関数は引数に与えられたpPoint2のインスタンスであれば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
 

関数isPoint3を定義してください。 この関数は引数に与えられたpPoint3のインスタンスであれば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
 

クラスTreeを定義してください。 メンバーとして数値を保持するleafTreeの配列を保持するbranchesを持ちます。 コンストラクタの引数はleafbranchesです。

class Tree {
/**
* Treeのコンストラクタ
* @param leaf 数値型
* @param branches Tree型の配列
*/
constructor(leaf, branches) {
// ここを実装してください
}
}

課題13
 

leafの値が1branchesが空のTreeのインスタンスを生成してください。

使用例
{
const tree = new Tree(/* ここを実装してください */);

console.assert(tree.leaf === 1);
console.assert(tree.branches.length === 0);
}

課題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
 

t1に含まれるTreeのうち、leafの値が3であるTreeが保持しているbranchesの要素数(Treeインスタンスの数)を取得する関数を定義してください。

function getLengthOfBranches() {
// ここを実装してください
}
使用例
{
console.assert(getLengthOfBranches() === 3);
}

課題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
 

任意のTreeのインスタンスに含まれる全てのleafの値の和を求めるメソッドsumLeafsを定義してください。

class Tree {
...
sumLeafs() {
// ここを実装してください
}
}
使用例
{
const a = new Tree(1, [new Tree(2, [new Tree(3, [])])]);
console.assert(a.sumLeafs() === 6);
}

課題18
 

sumLeafs(t1)の結果を確かめてください。

{
console.assert(t1.sumLeafs() === 55)
}

課題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
 

任意の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
 

doubleLeafs(t1)の結果を確かめてください。