講義用スライド

Tips

基礎クラス用演習問題

解答は

ホスト名: earth.mlab.im.dendai.ac.jp

ディレクトリ: /home/submit/JavaBeginners/[今日の日付]/[学籍番号]

に提出しなさい。ソースファイル (〜.java) のみを提出。 提出は gFTP 等の ftp ソフトを用いて行うこと。

提出先が木曜日と異なることに注意

問題1(Beginners')

10月17日に作成したVector2Dクラスに,自身をマイナスベクトルとするNegativeメソッドを追加しなさい.

プログラム名はVectorNegativeとする.

class Vector2D
{
    double x;
    double y;
    
    double GetX()
    {
        return x;
    }
    
    double GetY()
    {
        return y;
    }
    
    void SetValue(double newx, double newy)
    {
        x = newx;
        y = newy;
    }
    
    void Print()
    {
        System.out.print("(" + x + ", " + y + ")");
    }
    
    String ToString()
    {
        return "(" + x + ", " + y + ")";
    }

   double GetLength()
   {
       return Math.sqrt(x * x + y * y);
   }
   
   void Normalize()
   {
       double l = GetLength();
       x /= l;
       y /= l;
   }
    
    //TO DO:
    //自身をマイナスベクトルとするメソッドNegativeを作成する
}

public class VectorNegative
{  
	static public void main(String[] args)
	{
		if (args.length != 2)
		{
			System.out.println("引数を2つ指定してください");
			return;
		}
		
		//TO DO:
 		//Vector2D型の変数を作る
                 //インスタンスを作る
                 //コマンドライン引数1番目をx,2番目をyとしてベクトルに値をセットする
                 //Negativeメソッドを使用してベクトルをマイナスベクトルとする
                 //結果を画面に表示する
	 }
}

実行例(表示方法は各自変更して良い):

>java VectorNegative 1.0 2.0
v=(-1.0, -2.0)

問題2(Beginners')

10月17日に作成したVector2Dクラスに,double型の引数sを受け取り,自身をs倍(スケーリング)するScalingメソッドを追加しなさい.

プログラム名はVectorScalingとする.

class Vector2D
{
    double x;
    double y;
    
    double GetX()
    {
        return x;
    }
    
    double GetY()
    {
        return y;
    }
    
    void SetValue(double newx, double newy)
    {
        x = newx;
        y = newy;
    }
    
    void Print()
    {
        System.out.print("(" + x + ", " + y + ")");
    }
    
    String ToString()
    {
        return "(" + x + ", " + y + ")";
    }
    
   double GetLength()
   {
       return Math.sqrt(x * x + y * y);
   }
   
   void Normalize()
   {
       double l = GetLength();
       x /= l;
       y /= l;
   }

   //Negativeメソッド

   //TO DO:
   //double型の引数sを受け取り,自身をs倍(スケーリング)する
   //Scalingメソッドを作る
}  

public class VectorScaling
{  
	static public void main(String[] args)
	{
		if (args.length != 3)
		{
			System.out.println("引数を3つ指定してください");
			return;
		}
		
		//TO DO:
		//Vector2D型の変数を作る
                 //インスタンスを作る
                 //コマンドライン引数1番目をx,2番目をyとしてベクトルに値をセットする
                 //コマンドライン引数3番目の値をsとして,Scalingメソッドを使用しベクトルをs倍する
                 //結果を表示する         
	 }
} 

実行例(表示方法は各自変更して良い):

>java VectorScaling 1.0 1.0 5.0
v=(5.0, 5.0)

添付ファイル: file第6_5回.pdf 3586件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2011-10-24 (月) 11:43:45