問題6:クラスとオブジェクト
| この問題を解くためには… |
| → 基本編第6日目参照 |
|---|
prob6-1.(難易度★)
以下のプログラムを改造し、クラスMinMaxのメソッドMax(),Min()の引数の数を3つにし、期待される実行結果にならい、3つの数の最大値・最小値を出せるようにプログラムを改造しなさい。
プロジェクトProblem6_1/Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem6_1
{
class Program
{
static void Main(string[] args)
{
MinMax m = new MinMax();
int a = 4,b = 2;
Console.WriteLine("{0}と{1}のうち、最大のものは{2}",a,b,m.Max(a,b));
Console.WriteLine("{0}と{1}のうち、最小のものは{2}",a,b,m.Min(a, b));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem6_1
{
// 最大値と最小値を求めるクラス
class MinMax
{
// 最大値の取得
public int Max(int n1, int n2)
{
if (n1 > n2)
{
return n1;
}
return n2;
}
// 最大値の取得
public int Min(int n1, int n2)
{
if (n1 < n2)
{
return n1;
}
return n2;
}
}
}
4と2のうち、最大のものは4
4と2のうち、最小のものは2
4と2のうち、最小のものは2
prob6-2.(難易度★)
以下のプログラムは、二つの実数の値の計算をするくらす、Calcを使ったプログラムである。このクラスに、指定したメソッド追加し、期待される実行結果が得られるようにプログラムを変更しなさい。
プロジェクトProblem6_2/Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem6_2
{
class Program
{
static void Main(string[] args)
{
Calc c = new Calc();
double a = 4.1,b = 2.3;
Console.WriteLine(a + " + " + b + " = " + c.Add(a,b));
Console.WriteLine(a + " - " + b + " = " + c.Sub(a, b));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem6_1
{
// 最大値と最小値を求めるクラス
class MinMax
{
// 最大値の取得
public int Max(int n1, int n2)
{
if (n1 > n2)
{
return n1;
}
return n2;
}
// 最大値の取得
public int Min(int n1, int n2)
{
if (n1 < n2)
{
return n1;
}
return n2;
}
}
}
これを実行すると、以下のようになる。
実行結果
4.1 + 2.3 = 6.4
4.1 - 2.3 = 1.8
Calcクラスに追加するメソッド
4.1 - 2.3 = 1.8
| メソッド名 | Mul |
|---|---|
| 処理内容 | 与えられた二つの引数の積を返す。 |
| 引数 | 二つの数値(ともにdouble) |
| 戻り値 | 二つの引数の積 |
| メソッド名 | Div |
|---|---|
| 処理内容 | 与えられた二つの引数の商を返す。 |
| 引数 | 二つの数値(ともにdouble) |
| 戻り値 | 二つの引数の商 |
4.1 + 2.3 = 6.0
4.1 - 2.3 = 2.0
4.1 * 2.3 = 9.43 ← mul()メソッドで実行
4.1 / 2.3 = 1.78260869565217 ← div()メソッドで実行
4.1 - 2.3 = 2.0
4.1 * 2.3 = 9.43 ← mul()メソッドで実行
4.1 / 2.3 = 1.78260869565217 ← div()メソッドで実行
prob6-3.(難易度★)
以下のプログラムは、円の半径から、円周の長さを求めるものである。このクラスに、指定したメソッド追加し、期待される実行結果が得られるようにプログラムを変更しなさい。
プロジェクトProblem6_3/Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem6_3
{
class Program
{
static void Main(string[] args)
{
Circle c = new Circle();
// 円の半径を設定
c.r = 4.0;
Console.WriteLine("半径" + c.r +"の円の円周の長さは" + c.Circumference());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem6_3
{
class Circle
{
// 半径
public double r;
// 円周の長さを求める
public double Circumference()
{
return 2 * 3.14 * r;
}
}
}
これを実行すると、以下のようになる。
実行結果
半径4.0の円の円周の長さは25.12
このプログラムのCalcクラスに、以下のメソッドを追加する。
Calcクラスに追加するメソッド| メソッド名 | Area |
|---|---|
| 処理内容 | メンバ変数rで与えられた円の面積を求める。 |
| 引数 | なし |
| 戻り値 | メンバ変数rで与えられる円の面積 |
これにより、期待される実行結果は以下の通り
期待される実行結果
半径4.0の円の円周の長さは25.12
半径4.0の円の面積の長さは50.24 ← 同一の円の面積を求めた結果
半径4.0の円の面積の長さは50.24 ← 同一の円の面積を求めた結果









