Report#6


学籍番号:055704J
氏 名 :伊志嶺拓人

Wiki に戻る

Q23

●ソースプログラム
public class q23{
    public static void main(String args[]){
    int x, y;
    x = 100;
    x += 1;
    x--;
    y = 200 + x;
    System.out.println(y++);
    }
}
●実行結果
300
●考察
x = 100でxに100が入る。    x==100
x += 1でxに1足される。     x==101
x-- でxから1引く。       x==100
y = 200 + x でyに200+xを入れる y==300
System.out.println(y++)でyを出力した後に1足す。
よって出力されるのは 300

Q24

●ソースプログラム
public class q24{
    public static void main(String args[]){            
        int a =0, x=0;
        a = 5; a +=3; x = ++a;
        System.out.println(x);
    }
}
●実行結果
9
●考察
a = 5 でaに5を代入 a==5
a += 3 でaにプラス3 a==5+3==8
x = ++aでaにプラス1したのをxに代入 
よって出力結果は
9

Q25

●ソースプログラム
public class q25{
    public static void main(String args[]){     
        int a=9, b=3;
        a /= b;
        System.out.println(a %= b);
    }
}
●実行結果
0
●考察
a=9,b=3で値をそれぞれに代入
a /=b でa/bをaに代入 a==3
a %=b でaをbで割ったあまりを出力
よって出力されるのは 0

Q26

●ソースプログラム
public class q26{
    public static void main(String args[]){     
        int i,j;
        for(i=0, j=0; i<3; i++) ++j;
        System.out.println(i * j);
    }
}
●実行結果
9
●考察
for文に{}が付いてない場合は次のセミコロンまでを{}内と考える
なので、このfor文は三回繰り返されi==3,j==3になる
よって出力結果はi*j==3*3なので 9

Q27

●ソースプログラム
public class q27{
    public static void main(String args[]){     
       int i=2;
       while(i-- > 0) System.out.print(i);
●実行結果
10
●考察
while文の場合も{}がない時セミコロンまでを{}内とする。
while(i-- >0)でi--となっているので後から1引く
i(==2)は0より大きいので1引いて出力する 1
i(==1)は0より大きいので1引いて出力する 0
i(==0)は0より大きくないので終了
よって出力結果は 10

Q28

●ソースプログラム
public class q28{
    public static void main(String args[]){     
        int num=10000;
        for(int i=0; i < 4; i++) num >>= i;
        System.out.println(num);
●実行結果
156
●考察
num==10000を二進数で表すと 10011100010000
コレを右シフトさせます。for文より0,1,2,3とシフトさせるので合計6ビットシフトさせる
よって 00000010011100これを十進数で出力するので 156

Q29

●ソースプログラム
public class q29{
    public static void main(String args[]){     
       int num = 0;
        for(int i = 1; i<=10; i++){
            if(++num % i == 0) num++;
        }
        System.out.println(++num);
    }
}
●実行結果
12
●考察
if文は最初i=1の時だけ実行され、後はnumの方が1多くて実行されない
iは最後は10になる事からnumは11になる
出力するまえに++numで1足すので出力結果は 12

Q30

●ソースプログラム
public class q30{
        public static void main(String args[]){    
            int a = 9;
            if(a++ != 10 | a++ == 10) a++;
            System.out.println(a);
    }
}
●実行結果
12
●考察
a++なので条件に当てはめた後に+1する。
a(=9)!=10 | a(==10) ==10となり条件満たすのでa++{a(==11)+1}
よって出力結果は 12

Q31 Helloは何回表示されるか述べよ

●ソースプログラム
public class q33{
    public static void main(String args[]){     
        for(int i =0; i < 5; i++)
        System.out.println( "i == " + i);
        System.out.println("Hello");
    }
}  
●実行結果
i == 0
i == 1
i == 2
I == 3
i == 4
Hello
●考察
for文に{}がない場合は次のセミコロンまでを{}内とするので、Helloは{}外になる
よって一回しか表示されない。

Q32

●ソースプログラム
public class q32{
    public static void main(String args[]){     
        int i;
        for(i = 0; i < 9;i += 3){}
       System.out.println(i)
    }
}
●実行結果
9
●考察
iには3足されていき、iが9以上になった時に終了するので
最終的にiは9になる。よって9が表示される。

Q33

●ソースプログラム
public class q33{
    public static void main(String args[]){     
        for( int i=0; i < 8; i ++){
          System.out.println(i);
          i += 3;
    }
}
●実行結果
0
4
●考察
最初i=0の時出力されるので0が表示される
i += 3でi=3になり、その後i++より1プラスされi=4
そのまま出力されて
0
4
そして3と1足されてi==8となり終了

Q34

●ソースプログラム
class q34{
    public static void main(String args[]){
        int i = 0;
        for( sayHello(); i <= 6; i += 3){
             sayHello();
        }
    }
    static void sayHello(){
        System.out.println("Hello");
    }
}

●実行結果
Hello
Hello
Hello
Hello
●考察
まずfor( sayHello(); i <= 6; i += 3)のsayHello()で一回表示
for文は三回繰り返されるので三回表示
したがって、4回表示される。

Q35

●ソースプログラム
class q35{
    public static void main(String args[]){     
      Player p1 = new Player ();
      Player p2 = new Player ();
      p1.id = 1000;
      p2.id = 2000;
      p1.num += p1.id;
      p2.num += p1.id;
      System.out.println(Player.num);
    }
}
class Player{
  int id =0;
   static int num =0;
}
●実行結果
3000
●考察
p1には1000,p2には2000が代入されている。
p1.numとp2.numはnumがstatic変数なので値を共有
p1.num += p1.idでnum==1000
p2.num += p2.idでnum==1000+2000=3000
よってPlayer.num==3000

Q36

●ソースプログラム
class Test{
   public static void main(String args[]){
     Player p1 =new Player();
     Player p2 =new Player();
     p1.id = 1000;
     p2.id = 2000;
     Player.num += p1.id;
     Player.num += p2.id;
     System.out.println("p1.num == "+p1.num);
     System.out.println("p2.num == "+p2.num);
   }
}
class Player{
  int id = 0;
  static int num = 0;
}
●実行結果
p1.num == 3000
p2.num == 3000
●考察
クラスPlayerのint numはstatic変数で定義されている。
なのでp1.num p2.num Player.numは共有された状態である。
Player.numに1000足して2000足しているので、Player.num==3000である。
p1.numもp2.numもPlayer.numと同じなので出力結果は
p1.num == 3000
p2.num == 3000

Q37

●ソースプログラム
class Test{
  public static void main(String args[]){
    Card c1 = new Card();
    Card c2 = cew Card();
    Card c3 = c1;
    c1.deposit = 1000;
    c2.deposit = 2000;
    c3.deposit = 3000;
    int sum = c1.deposit + c2.deposit + c3.deposit;
    System.out.println(sum);
  }
}
class Card{
  int deposit;
}
●実行結果
8000
●考察
Card c3 = c1;と置いてるのでc1とc3は同じものと考える。
c1に1000を代入した後にc3に3000代入してるのでc1、c3==3000となる。
よってsum=3000+2000+3000=8000

Q38

●ソースプログラム
class Test2{
  public static void main(String args[]){
   Card c1 =new Card();
   Card c2 = c1;
   c1.deposit =1000;
   c2.deposit =2000;
   Bank.useCard( c1 );
   Bank.useCard( c2 );
   /*c1.depositとc2.depositの関係は?*/
   System.out.println("c1.deposit==" +c1.deposit);
   System.out.println("c2.deposit==" +c2.deposit);
  }
}
class Card{ int deposit; }
class Bank{
    public static void useCard( Card c){
      c.deposit -= 500;
    }
}
●実行結果
c1.deposit==1000
c2.deposit==1000
●考察
Card c2 = c1よりc2とc1は同じものと考える。
c1.deposit =1000;でc2.depositも1000になる。
c2.deposit =2000;でc1.depositも2000になる。
Bank.useCard( c1 );
\ Bank.useCard( c2 );
で二回500引かれるのでc1.deposit,c2.depositは1000になる。
よって出力結果は
c1.deposit==1000
c2.deposit==1000
となりc1.deposit==c2.depositという関係になる。

■反省・感想

for文とかで{}がない時はどうなるかとか知らなかったので勉強になりました。
残りレポート二つなので頑張ります。
・参考文献・URL
:独習Java 第3版
上に戻る