0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JAVA引数と戻り値の様々な問題 オーバーロードetc

0
Last updated at Posted at 2015-10-14

▪️問題1 底辺の長さが base、高さが height で表される三角形の面積を返しなさい
メソッド名: getTriangleArea
1 つめの引数: height
1 つめの引数の型:double
2 つめの引数: base
2 つめの引数の型:double
戻り値の型: double

▪️問題1ソースコード

public class test32 {
	public static double getTriangle(double height, double base) {
		double ans = (base * height) / 2;
		return ans;
	}

	public static void main(String[] args) {
		double ans = getTriangle(4, 20);
		System.out.println(ans);
	}
}

▪️問題1実行結果
40.0

▪️問題2
処理内容:「メールの宛先アドレス(引数)」に、以下のメールを送信しました
件名:「メールのタイトル(引数)」
本文:「メールの本文」
引数リスト:メールのタイトル(String title)
                   メールの宛先アドレス(String address)
                   メールの本文(String text)
▪️戻り値の型:なし

▪️問題2方法①ソースコード

public class test31 {
	public static void main(String[] args) {
		String title = "こんにちは";
		String address = "nnsky@docomo.ne.jp";
		String text = "元気ですか?";
		email(title, address, text);

	}

	public static void email(String title, String address, String text) {
		System.out.println(address + "に、以下のメールを送信しました");
		System.out.println("件名:「" + title + "」");
		System.out.println("本文:「" + text + "」");
	}
}

▪️問題2方法②ソースコード

public class test31 {
	public static void main(String[] args) {
		email("こんにちは", "i.k@docomo.ne.jp", "元気ですか?");
	}

	public static void email(String title, String address, String text) {
		System.out.println(address + "に、以下のメールを送信しました");
		System.out.println("件名:「" + title + "」");
		System.out.println("本文:「" + text + "」");
	}
}

▪️問題2実行結果
i.k@docomo.ne.jpに、以下のメールを送信しました
件名:「こんにちは」
本文:「元気ですか?」

▪️問題2方法②補足

public static void email(String title, String address, String text)

public static void email(String text, String address, String title)

とすると、結果が以下になる
nnsky@docomo.ne.jpに、以下のメールを送信しました
件名:「元気ですか?」
本文:「こんにちは」

▪️例①

public class test31 {
	public static void main(String[] args) {
		email("こんにちは", "nnsky@docomo.ne.jp", "元気ですか?");
		email("お誘い", "xxsky@docomo.ne.jp", "今度、飲みに行きませんか?");
		email("ご臨席賜りますよう", "yyysky@docomo.ne.jp", "ご多用中誠に恐縮でございますが何とぞご臨席賜りますようご案内申し上げます");

	}

	public static void email(String title, String address, String text) {
		System.out.println(address + "に、以下のメールを送信しました");
		System.out.println("件名:「" + title + "」");
		System.out.println("本文:「" + text + "」");
		System.out.println("");
	}
}

▪️例①実行結果
nnsky@docomo.ne.jpに、以下のメールを送信しました
件名:「こんにちは」
本文:「元気ですか?」

xxsky@docomo.ne.jpに、以下のメールを送信しました
件名:「お誘い」
本文:「今度、飲みに行きませんか?」

yyysky@docomo.ne.jpに、以下のメールを送信しました
件名:「ご臨席賜りますよう」
本文:「ご多用中誠に恐縮でございますが何とぞご臨席賜りますようご案内申し上げます」

▪️オーバーロード例②

package test34;

public class test34 {
	public static void main(String[] args) {
		email("こんにちは", "nnsky@docomo.ne.jp", "元気ですか?");
		email("xxsky@docomo.ne.jp", "今度、飲みに行きませんか?");
	}

	public static void email(String address, String text) {
		System.out.println(address + "に、以下のメールを送信しました");
		System.out.println("件名:無題");
		System.out.println("本文:「" + text + "」");
		System.out.println("");
	}

	public static void email(String title, String address, String text) {
		System.out.println(address + "に、以下のメールを送信しました");
		System.out.println("件名:「" + title + "」");
		System.out.println("本文:「" + text + "」");
		System.out.println("");
	}
}

▪️例②実行結果
i.k@docomo.ne.jpに、以下のメールを送信しました
件名:「こんにちは」
本文:「元気ですか?」

xxsky@docomo.ne.jpに、以下のメールを送信しました
件名:無題
本文:「今度、飲みに行きませんか?」

▪️オーバーロード
同じ名前のメソッドを定義することをオーバーロード(overload)(or多重定義)という。

問題3 

public class sample1 {
	// クラス変数
	public static int i = 0;

	public static void main(String[] args) {
		// isFlgは変数
		i = 10;
		callMethod(i);
	}

	// boolReturn→メソッドの名前 int→戻り値の型 intReturn→メソッドの名前
	// intは戻り値の型。戻り値はi。戻り値と同じ型を指定している
	public static int intReturn(int i) {
		i = i + 10;
		// ▪️課題1 returnの意味を調べる
		// return文→値を戻すだけでなく、メソッドの終了も行う
		return i;
	}

	// ▪️課題2 voidの意味を調べる void→何もない。何も戻さない場合は「void」を指定する
	public static void callMethod(int i) {
		System.out.println(i);
		// メソッドを呼び出し、戻り値を受け取る
		int total = intReturn(i);
		System.out.println(total);
	}
}

▪️問題3 実行結果
10
20

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?