|  | 
				
					
	
		
			
 			Posted on 2008-08-18 09:15 ∪∩BUG  阅读(3416) 评论(5)  编辑  收藏   所属分类: Java学习笔记      1 第二章 2
  课堂习题: 3
  public class Main 4
    { 5
   public static void main(String[] args)  { 6
  // initialization phase初始段 7
  float f1 = 2.5f; 8
  float f2; 9
  int a = 7; 10
  int b = 2; 11
  int c; 12
   13
  // processing phase 处理段 14
  f2 = f1 + (a/b); 15
  c = (a/b); 16
   17
  // termination phase终止段 18
  System.out.println("f2的值为: " + f2); 19
  System.out.println("c的值为: " + c); 20
  } 21
  22
  } 23
  课后习题: 24
  2-4 25
  (小程序部分) 26
  import java.applet.*; 27
  import java.awt.*; 28
  29
  public class Gess extends Applet 30
    { 31
  32
  33
  public void paint(Graphics g) 34
     { 35
  // 3~10000中的任意数 36
  int m = 20; 37
  int n = 9993; 38
  39
   40
  g.drawString("本程序仅对3~10000中任意取的整数: ",80,50); 41
  g.drawString("20" ,80,65); 42
  g.drawString("9993" ,80,80); 43
  g.drawString("进行测试! " ,80,95); 44
   45
  while (m !=1)                    // 假设最后得到的数一定是1,则如果不为1就换算 46
     { 47
  if (m %2 == 0)                 // 如果被2整除则为偶数,除以2 48
  m  = m /2; 49
  else                        // 如果不被2整除则为奇数,乘以3再加1 50
     { 51
  m  = m *3 + 1; 52
  } 53
   54
  } 55
   56
  while (n !=1)                    // 假设最后得到的数一定是1,则如果不为1就换算 57
     { 58
  if (n %2 == 0)                 // 如果被2整除则为偶数,除以2 59
  n  = n /2; 60
  else                            // 如果不被2整除则为奇数,乘以3再加1 61
     { 62
  n  = n *3 + 1; 63
  } 64
   65
  } 66
  g.drawString("20换算后的值为:        " + m,80,110); 67
  g.drawString("9993换算后的值为:   " + m,80,125); 68
   69
   70
  } 71
  72
  } 73
  74
  (程序部分) 75
  import java.io.*;   // 引入java.io中的所有类(含输入输出类),因为程序中要用到输入类的read()方法 76
  public class Main 77
    { 78
  public static void main(String[] args) 79
     { 80
  81
  byte []buf =new byte[50];       // 50个字节的数组用于存字符串 82
  String str; 83
  long m = 0; 84
  System.out.println("请输入3~10000中的任意整数(回车确认):"); 85
   86
  try         // try 语气 87
     { 88
  System.in.read(buf);                  // 从键盘读入一数字串保存于buf中 89
  str = new String(buf);                // buf转换成字符串对象str 90
  m = Long.parseLong(str.trim());       // 数字转换成整数 91
   92
   }catch (Exception e)  {}       // catch语气,Exception为异常类 93
   94
  while (m !=1)                // 假设最后得到的数一定是1,则如果不为1就换算 95
     { 96
  if (m %2 == 0)           // 如果被2整除则为偶数,除以2 97
  m  = m /2; 98
  else                    // 如果不被2整除则为奇数,乘以3再加1 99
     { 100
  m  = m *3 + 1; 101
  } 102
   103
  } 104
  System.out.println("换算后的值为:\t" + m); 105
  106
  } 107
  108
  } 109
  110
  2-6 111
  public class Main 112
    { 113
  public static void main(String[] args) 114
     { 115
  // initialization phase //初始段 116
  int j = 1; 117
  int []a;               // 用于存放第一个骰子的点数的整形数组 118
  int []b;               // 用于存放第二个骰子的点数的整形数组 119
  int two = 0;           // 相加和为2的计数器 120
  int sev = 0;         // 相加和为7的计数器 121
  int twe = 0;        // 相加和为12的计数器 122
  int oth = 0;         // 相加和为其他数的计数器 123
  a = new int [6];      // 分配数组空间 124
  b = a;   // 骰子的点数相同,这里将数组a赋给数组b(这时b和a同用一块内存空间) 125
   126
  // processing phase 处理段 127
  for(int i = 0; i < 6; i++)          // 通过循环赋值方式初始化数组 128
     { 129
  a[i] = j++; 130
  } 131
  132
  for(int i = 0; i < 3600; i++)    // 测试3600次 133
     { 134
  // 生成0~5随机数 135
  int m = (int)(((Math.random())*6 )); 136
  int n = (int)(((Math.random())*6 )); 137
   138
  if ((a[m] + b[n]) == 2)                // 当相加和为2时对应计数器加1 139
  two++; 140
  else if ((a[m] + b[n]) == 7)           // 当相加和为7时对应计数器加1 141
  sev++; 142
  else if((a[m] + b[n]) == 12)            // 当相加和为12时对应计数器加1 143
  twe++; 144
  else                                            // 当相加和为其他数时对应计数器加1 145
  oth++; 146
  147
  } 148
   149
  // termination phase 终止段 150
  // 输出结果 151
  // ((float)two/3600)为将two/3600的结果强制转换成浮点数输出,否则输出为0 152
  System.out.println("相加的和为   2 的次数是:\t" + two 153
  + ";\t可能性为:\t" + ((float)two/3600)); 154
  System.out.println("相加的和为  12 的次数是:\t" + twe 155
  + ";\t可能性为:\t" + ((float)twe/3600)); 156
  System.out.println("相加的和为   7 的次数是:\t"+ sev 157
  + ";\t可能性为:\t" + ((float)sev/3600)); 158
  System.out.println("相加的和为其他数的次数是:\t" 159
  + oth + ";\t可能性为:\t" + ((float)oth/3600) ); 160
  } 161
  162
  } 163
   164
  165
  第三章 166
  课堂习题 167
  随机产生26个英文字母a-z,直到大于u为止,用while或for语气实现 168
   public class Main  { 169
  170
  public static void main(String[] args) 171
     { 172
  int letter = 0; 173
   174
  System.out.println("用 while 语句实现: "); 175
  while(letter < 'u') 176
     { 177
  letter = (int)(Math.random()*26) + 'a'; 178
  System.out.println((char)letter); 179
  } 180
   181
  System.out.println("用 for 语句实现: "); 182
  for(letter = 97; letter < 'u';letter++) 183
     { 184
  letter = (int)(Math.random()*26) + 'a'; 185
  System.out.println((char)letter); 186
  } 187
  } 188
  189
  } 190
  课后习题 191
  3-1 192
  import java.applet.Applet; 193
  import java.awt.*; 194
  195
  public class Display extends Applet 196
    { 197
  Label lab; 198
  TextField input; 199
   200
  long num; 201
  long m = 0; 202
  long n = 0; 203
  int i = 0;  // 数字个数的计数器 204
   205
  public void init() 206
     { 207
   208
  lab = new Label("请输入任意整数:");   // 提示标签 209
  input = new TextField(20);            // 输入文本框 210
   211
  add(lab);       // 在网页中显示标签lab 212
  add(input);     // 在网页中显示文本框 213
   214
  } 215
   216
  public boolean action (Event e, Object o) 217
     { 218
  if (e.target == input) 219
     { 220
  num = Long.parseLong( input.getText() ); // 字符串转换成整形 221
   222
  // 把得到的数倒置 223
  do 224
     { 225
  m = num%10;     // 求余得最后一个数字 226
  num = num/10;   // 取整去掉最后一个数字 227
  n = n*10 + m;     // 把倒置后的数存到n中 228
  i++;             // 同时计数器自加 229
   230
  }while(num != 0); 231
   232
  System.out.println("下面是得到的结果:" + i); 233
   234
  // 间隔加空格后输出 235
  do 236
     { 237
  System.out.print(" " + n%10); 238
  n = n/10; 239
  }while((i--) > 1); 240
   241
  } 242
  return true; 243
  } 244
  245
  } 246
  247
  3-2 248
   public class Main  { 249
  250
  251
  public static void main(String[] args) 252
     { 253
   254
  System.out.println("整数\t" + "平方\t" + "立方"); 255
   256
  // 范围0~10 257
  for(int i = 0; i <= 10; i++) 258
     { 259
  // 依次打印输出整数,平方,立方 260
  System.out.println(i + "\t" + i*i + "\t" + i*i*i); 261
  } 262
  } 263
  264
  } 265
  266
  3-3 267
  public class Main 268
    { 269
  public static void main(String[] args) 270
     { 271
  // 把图分成三部分左中右三部分打印 272
  for(int i = 0; i < 5;i++ ) 273
     { 274
  // 循环打印出左边部分 275
  for(int j = 0; j <= i;j++) 276
     { 277
  System.out.print("*"); 278
  } 279
   280
  // 循环打印出中间部分 281
  for(int k = 2*i; k < 18; k++)   // 空格要考虑前后各少一个所以从2*i开始 282
     { 283
  System.out.print(" "); 284
  } 285
   286
  // 循环打印出右边部分 287
  for(int m = 0; m <= i;m++) 288
     { 289
  System.out.print("*"); 290
  } 291
  System.out.println(); 292
  } 293
  System.out.println(); 294
  } 295
  296
  } 297
  298
  3-4 299
  import java.applet.Applet; 300
  import java.awt.*; 301
  302
  public class Rectangle extends Applet 303
    { 304
  305
  Label lab; 306
  TextField input; 307
   308
  int width;  // 矩形的长 309
   310
  311
  public void init() 312
     { 313
   314
  lab = new Label("请输入矩形的长(整数):");    // 请示标签 315
  input = new TextField(20);                  // 输入文本框 316
   317
  add(lab);         // 在网页中显示标签lab 318
  add(input);       // 在网页中显示文本框 319
   320
  } 321
  322
  public boolean action(Event e, Object o) 323
     { 324
  // 当用户按下Enter时把输入的字符串转换成整形 325
  if (e.target == input) 326
  width = Integer.parseInt( input.getText() ); 327
   328
  System.out.println("下面是得到的矩形:"); 329
  // 矩形的上底 330
  for(int i = 0; i < width; i++) 331
     { 332
  System.out.print("*"); 333
  } 334
   335
  // 矩形的边 336
  for (int k = 0; k < width-2; k++) 337
     { 338
  System.out.println(); 339
  System.out.print("*"); 340
   341
  // 矩形的中间为空 342
  for (int m = 0; m < width-2; m++) 343
     { 344
  System.out.print(" "); 345
  } 346
   347
  System.out.print("*"); 348
  } 349
   350
  System.out.println(); 351
   352
  // 矩形的下底 353
  for(int j = 0; j < width; j++) 354
     { 355
  System.out.print("*"); 356
  } 357
  return true; 358
   359
  } 360
  } 361
   362
  363
  第四章 364
  4-1 365
  import java.io.*; 366
   367
  public class ReverOrder 368
    { 369
  370
  static int x; 371
  int num; 372
  int m; 373
  int n; 374
   375
  // 从键盘读入字符串 376
  public static String readString() 377
     { 378
  BufferedReader br = new BufferedReader( new InputStreamReader(System.in),1 ); 379
  String string = ""; 380
   381
  try 382
     { 383
  string = br.readLine(); 384
  } 385
  catch( IOException ex ) 386
     { 387
  System.out.println(ex); 388
  } 389
   390
  return string; 391
  } 392
   393
  // 将读入的字符串转换为整形 394
  public static int input() 395
     { 396
  System.out.print("请输入一串数字:\t"); 397
  return Integer.parseInt(readString()); 398
  } 399
   400
  // 倒序输入的字符串 401
  int Rorder(int b) 402
     { 403
  num = b; 404
   405
  do 406
     { 407
  m = num%10;     // 求余得最后一个数字 408
  num = num/10;       // 取整去掉最后一个数字 409
  n = n*10 + m;     // 把倒置后的数存到n中 410
  }while( num != 0); 411
  412
  return n; 413
  } 414
   415
  // 输出结果 416
  void show(int a) 417
     { 418
  System.out.println("倒序输出为:\t" + a); 419
  } 420
   421
  // 主方法 422
  public static void main(String[] args) 423
     { 424
  int value; 425
  ReverOrder order = new ReverOrder(); 426
  x = ReverOrder.input();             // 将输入的数传递给x 427
  value = order.Rorder(x);           // 将x倒置 428
  order.show(value);              // 输出 429
   430
  } 431
  432
  } 433
  434
  4-3 435
  package homework4_3; 436
  import java.io.*; 437
  438
  public class Circle 439
    { 440
  static double radius; 441
  double length; 442
  double area; 443
  static boolean checkr; 444
   445
  // 构造函数初始化半径 446
  public Circle(double r) 447
     { 448
  if(r < 0) 449
     { 450
  checkr = false; 451
  } 452
  else 453
     { 454
  radius = r; 455
  checkr = true; 456
  } 457
  } 458
   459
  // 从键盘读入字符串 460
  public static String readString() 461
     { 462
  BufferedReader br = new BufferedReader( new InputStreamReader(System.in),1 ); 463
  String string = ""; 464
   465
  try 466
     { 467
  string = br.readLine(); 468
  } 469
  catch( IOException ex ) 470
     { 471
  System.out.println(ex); 472
  } 473
   474
  return string; 475
  } 476
   477
  // 将读入的字符串转换为整形 478
  public static double input() 479
     { 480
  System.out.print("请输入圆的半径:\t"); 481
  return Double.parseDouble(readString()); 482
  } 483
   484
  // 计算圆的周长的方法 485
  void Length(double r) 486
     { 487
  System.out.println("圆的周长为:\t" + Math.PI*r*2); 488
  } 489
   490
  // 计算圆的面积 491
  void  Area(double r) 492
     { 493
  System.out.println("圆的面积为:\t" + Math.PI*r*r); 494
  495
  } 496
   497
  // 主方法 498
  public static void main(String[] args) 499
     { 500
  double valueinput; 501
  double valuer; 502
  Circle r = new Circle(input()); 503
   504
  // 如果半径为正则求周长和面积 505
  if(checkr) 506
     { 507
  r.Area(radius); 508
  r.Length(radius); 509
  } 510
  else 511
  System.out.println("输入圆的半径的值不能为负数!"); 512
   513
   514
   515
  } 516
  517
  } 518
  4-4 519
  public class Date 520
    { 521
  static int year; 522
  static int month; 523
  static int day; 524
   525
  public Date() 526
     { 527
  // 无参数的构造函数 528
  } 529
   530
  // 有三个参数的构造函数 531
  public Date(int x, int y, int z) 532
     { 533
  day = x; 534
  month = y; 535
  year = z; 536
  } 537
  538
  // 主方法 539
  public static void main(String[] args) 540
     { 541
  year = 2008; 542
  month = 06; 543
  day = 21; 544
   545
   546
  // (1)三种格式输出今天的日期 547
  System.out.print("今天是:" ); 548
  System.out.println("\t" + year + "  " + month + "  " + day ); 549
  System.out.println("\t" + month + "  " + day + "  " + year ); 550
  System.out.println("\t" + day + "  " + month + "  " + year ); 551
   552
  // (2)调用重载构造函数创建对象date 553
  Date date = new Date(22,06,2008); 554
  System.out.print("\n明天是:" ); 555
  System.out.println("\t" + year + "  " + month + "  " + day ); 556
  System.out.println("\t" + month + "  " + day + "  " + year ); 557
  System.out.println("\t" + day + "  " + month + "  " + year ); 558
  } 559
  560
  } 561
  562
  4-6 563
  // 基类 564
  class base 565
    { 566
  // 方法一 567
  void First() 568
     { 569
  System.out.println("这是基类base的第一个方法!"); 570
  Second(); 571
  } 572
   573
  // 方法二 574
  void Second() 575
     { 576
  System.out.println("这是基类base的第二个方法!"); 577
  } 578
  } 579
  580
  // 派生类 581
  class derived extends base 582
    { 583
  void Second() 584
     { 585
  System.out.println("这是派生类derived的方法!"); 586
  } 587
  } 588
  public class UpCasting 589
    { 590
  // 主方法 591
  public static void main(String[] args) 592
    { 593
  // 派生类derived对象test,test对象里有覆盖了基类的Second()方法的Second()方法 594
  derived test = new derived(); 595
  base up = test; // 向上类型转换, 基类对象up里有有First()方法和被覆盖的Second() 596
  // base up = new base(); //基类对象up里仅有First()方法和未被覆盖的Second() 597
  up.First();                // 调用基类的第一个方法 598
  } 599
  600
  } 601
  实验题目 602
  实验一(略) 603
  实验二(略) 604
  实验三 605
  import javax.swing.JOptionPane; 606
  607
  public class GuessNumber 608
    { 609
  610
  public static void main(String[] args) 611
     { 612
  System.out.println("给你一个1到100的整数,请猜测这个数"); 613
  int realNumber =(int)(Math.random()*100)+1;     // 随机生成1~100的整数 614
  int yourGuess =0;      // 初始化输入的值 615
   616
  String str =JOptionPane.showInputDialog("输入你的猜测: "); 617
  yourGuess = Integer.parseInt(str);  // 将输入的字符串轮换成整形 618
   619
  // 当输入的数字与随机生成的数不相等时 620
  while(yourGuess != realNumber) 621
     { 622
  // 输入的数大于随机生成的数 623
  if(yourGuess > realNumber) 624
     { 625
  str = JOptionPane.showInputDialog("猜大了,再输入你的猜测: "); 626
  yourGuess = Integer.parseInt(str);  // 重置当前输入的值 627
  } 628
   629
  // 输入的数小于随机生成的数 630
  else if (yourGuess < realNumber) 631
     { 632
  str = JOptionPane.showInputDialog("猜小了,再输入你的猜测: "); 633
  yourGuess = Integer.parseInt(str);  // 重置当前输入的值 634
  } 635
  } 636
   637
  System.out.print("猜对了!"); 638
  } 639
  } 640
   641
  642
  补充题: 643
   public class Main  { 644
  645
  public static void main(String[] args) 646
     { 647
  float i = 0; 648
  float sum = 1;          // 总和 649
   650
  int classno = 209;          // 学号 651
   652
  for (i = 2;i <= classno;i++) 653
     { 654
  sum += 1/i; 655
   656
  } 657
   658
  // 输入每一项 659
  for (int j = 2; j <= classno; j++) 660
     { 661
  // 每十项换行 662
  if(j%10 == 0) 663
  System.out.println(); 664
  System.out.print("1/" + j + " + "); 665
  } 666
   667
  // 输出总和 668
  System.out.println("和为: " + sum); 669
  670
  } 671
  672
  } 673
  实验四 674
  // 三角形类 675
  class Triangle 676
    { 677
  double sideA, sideB, sideC, area, length; 678
  boolean boo;    // 能构成三角形的标记 679
  public Triangle(double a,double b,double c) 680
     { 681
  sideA=a; 682
  sideB=b; 683
  sideC=c; 684
   685
  // 如果任意两边之和不大于第三边 686
  if(((a+b)>c) &&((b+c)>a) && (a+c)>b) 687
     { 688
  boo = true;       // 标记为真 689
  } 690
  else 691
     { 692
  boo = false;    // 否则标记为假 693
  } 694
  } 695
   696
  // 三角形的周长 697
  double getLength() 698
     { 699
   700
  // 如果标记为真,即能构成三角形 701
  if(boo) 702
     { 703
  length=sideA+sideB+sideC;   // 求周长的公式 704
  return length;              // 返回周长 705
  } 706
   707
  // 否则返回0 708
  else 709
     { 710
  System.out.println("不是三角形,不能计算周长"); 711
  return 0; 712
  } 713
  } 714
  // 三角形边1 715
  double getsideA() 716
     { 717
  if(boo) 718
     { 719
  return sideA; 720
  } 721
  else 722
     { 723
  System.out.println("边不符合要求,不能计算周长"); 724
  return 0; 725
  } 726
   727
  } 728
  // 三角形边2 729
  double getsideB() 730
     { 731
  if(boo) 732
     { 733
  return sideB; 734
  } 735
  else 736
     { 737
  System.out.println("边不符合要求,不能计算周长"); 738
  return 0; 739
  } 740
  } 741
  // 三角形边3 742
  double getsideC() 743
     { 744
  if(boo) 745
     { 746
  return sideC; 747
  } 748
  else 749
     { 750
  System.out.println("边不符合要求,不能计算周长"); 751
  return 0; 752
  } 753
  } 754
  755
  // 三角形的面积 756
  public double getArea() 757
     { 758
  // 如果标记为真,即能构成三角形 759
  if(boo) 760
     { 761
  double p=(sideA+sideB+sideC)/2.0; 762
  area =Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));   // 求面积公式 763
  return area;                                  // 返回面积 764
  } 765
   766
  // 否则返回0 767
  else 768
     { 769
  System.out.println("不是三角形,不能计算面积"); 770
  return 0; 771
  } 772
  } 773
  public void setABC(double a,double b,double c) 774
     { 775
  sideA = a; 776
  sideB = b; 777
  sideC = c; 778
  if(((a+b)>c)&&((a-b)<c)) 779
     { 780
  boo=true; 781
  } 782
  else 783
     { 784
  boo=false; 785
  } 786
  } 787
  } 788
  789
  // 圆类 790
  class Circle 791
    { 792
  double r, area, length; 793
  public Circle(double a) 794
     { 795
  r = a; 796
  } 797
   798
  // 圆的周长 799
  public double getLength() 800
     { 801
  length = Math.PI*r*2; 802
  return length; 803
  804
  } 805
   806
  // 圆的面积 807
  public double getArea() 808
     { 809
  area = Math.PI*r*r; 810
  return area; 811
  } 812
  } 813
  814
  // 梯形类 815
  class Lader 816
    { 817
  double sideup, sidesb, sidel, sider,area, length; 818
  public Lader(double a,double b,double c,double e) 819
     { 820
  sideup = a; 821
  sidesb = b; 822
  sidel = c; 823
  sider = e; 824
   825
  } 826
  double getLength() 827
     { 828
  length=sideup + sidesb + sidel + sider; 829
  return length; 830
  831
  } 832
  public double getArea() 833
     { 834
  area = ((sideup + sidesb)*sidel)/2; 835
  return area; 836
  } 837
  } 838
  public class AreaAndLength 839
    { 840
  public static void main(String[] args) 841
     { 842
  double length, area; 843
  double sideA,sideB,sideC; 844
  Circle circle = null; 845
  Triangle triangle; 846
  Lader lader; 847
  848
  // 调用Circle 方法返回周长并赋值给length 849
  circle = new Circle(19);      // 修改圆的半径为19 850
  length = circle.getLength(); 851
  System.out.println("圆的周长:"+ length); 852
   853
  // 调用Circle 方法返回面积并赋值给area 854
  area = circle.getArea(); 855
  System.out.println("圆的面积:"+ area); 856
  // 给边值 857
  triangle = new Triangle(3,4,5); 858
  // 求边 859
  sideA = triangle.getsideA(); 860
  sideB = triangle.getsideB(); 861
  sideC = triangle.getsideC(); 862
   863
  // 输出边 864
  System.out.println("三角形的第一条边是:"+ sideA); 865
  System.out.println("三角形的第二条边是:"+ sideB); 866
  System.out.println("三角形的第三条边是:"+ sideC); 867
   868
  // 调用Triangle 方法返回周长并赋值给length 869
  length = triangle.getLength(); 870
  System.out.println("三角形的周长:"+ length); 871
   872
  // 调用Triangle 方法返回面积并赋值给area 873
  area = triangle.getArea(); 874
  System.out.println("三角形的面积:"+ area); 875
   876
  // 调用Lader 方法返回周长并赋值给length 877
  lader = new Lader(4,5,6,7); 878
  length = lader.getLength(); 879
  area = lader.getArea(); 880
  System.out.println("梯形的周长:"+ length); 881
   882
  // 调用Lader 方法返回面积并赋值给area 883
  System.out.println("梯形的面积:"+ area); 884
   885
   886
  } 887
  888
  } 889
  实验五 890
  // 圆类 891
  class Circle 892
    { 893
  static double pi = 3.14;    // 静态变量 894
  // double pi = 3.14; //非静态变量不能被静态方法调用 895
  static int objectNo = 0;    // 静态变量,对象的计数器 896
  int radius; 897
   898
  // 带有一个整形参数的构造函数 899
  Circle(int r) 900
     { 901
  radius = r; 902
  objectNo++;     // 对象的计数器加1 903
  } 904
   905
  // 无参构造函数 906
  Circle() 907
     { 908
  radius = 2; 909
  objectNo++;     // 对象的计数器加1 910
  } 911
   912
  // 求圆的面积的方法 913
  double getArea() 914
     { 915
  return pi*radius*radius; 916
  } 917
   918
  // 静态方法(类方法) 919
  static void setPI(double p) 920
     { 921
  // 重置pi 922
  pi = p; 923
  } 924
   925
  void setRadius(int r) 926
     { 927
  radius = r; 928
  } 929
   930
  // 静态方法 931
  static void displayNO() 932
     { 933
  // 输出对象个数 934
  System.out.println("当前圆的对象个数是:\t" + objectNo); 935
  } 936
  } 937
  938
  public class StaticTest 939
    { 940
  public static void main(String[] args) 941
     { 942
  Circle cir1 = new Circle(5);    // 圆的对象cir1用带有一个参数的构造函数初始化 943
   944
  System.out.println("ciir1圆周率是:\t" + cir1.pi); 945
  System.out.println("ciir1半径是:\t" + cir1.radius); 946
  System.out.println("ciir1面积是:\t" + cir1.getArea()); 947
   948
  cir1.displayNO();          // 通过对象调用静态方法 949
  cir1.setRadius(10);     // 通过对象调用非静态方法 950
  // Circle.setRadius(10); //只有静态方法才能通过类来调用 951
   952
  Circle cir2 = new Circle(10);   // 圆的对象cir2用带有一个参数的构造函数初始化 953
  cir2.setPI(3.1415); // 通过对象调用静态方法 954
   955
  System.out.println("ciir2圆周率是:\t" + cir2.pi); 956
  System.out.println("ciir2半径是:\t" + cir2.radius); 957
  System.out.println("ciir2面积是:\t" + cir2.getArea()); 958
   959
  } 960
  961
  } 962
  实验六 963
  class ScopeTest 964
    { 965
  966
  // 类的实例变量x,m 967
  int x; 968
  static int m = 0 ;   // 静态变量(类变量)m,为类的所有对象共享 969
   970
  void show() 971
     { 972
  // 类的局部x变量屏蔽掉类的实例x 973
  int x = 8,y;// 类的局部变量x 974
   975
  for(y = 0; y < 2; y++)  // 循环两次 976
     { 977
  // z是局部变量,属方法域中的变量,因此作用范围只在此for()语句中. 978
  // int z = 5; 979
   980
  // 局部变量不能重名 981
  int z = 5; 982
  System.out.println("z = " + z); 983
   984
  z = 10;     // 重置z的值为10 985
  System.out.println("z = " + z); 986
   987
  System.out.println("方法域中的X = " + z + "在块内显示"); 988
  } 989
   990
  // 去掉注释后错误是z中此方法中未声明,因为z的作用范围只在for()循环语句块中. 991
  // z = 20; 992
   993
  System.out.println("方法域中的X = " + x + " 在块外显示"); 994
  System.out.println("类域中的x = " + this.x + " 在块外显示"); 995
  System.out.println("类域中的m = " + this.m + " 在块外显示"); 996
  } 997
   998
  void setx(int x) 999
     { 1000
  this.x = x;     // 赋值实例变量的值为2 1001
  } 1002
   1003
  // 静态方法(类方法)只能访问静态变量(类变量) 1004
  static void setm(int m) 1005
  // void setm(int m) //此时是非静态方法,this指代的是对象. 1006
     { 1007
  // 在静态方法中通过类名调用静态变量 1008
  ScopeTest.m = m;    // 赋值静态变量m的值为3 1009
  // this.m = m; //在静态方法中不能有this,因为静态方法属于整个类,而不是某个对象 1010
  } 1011
   1012
  public static void main(String[] args) 1013
     { 1014
  ScopeTest application = new ScopeTest();    // 声明ScopeTest类的对象application 1015
   1016
  application.setx(2);     // 调用方法setx(int x) 1017
  application.setm(3);    // 调用方法setm(int x) 1018
  application.show();     // 调用方法show() 1019
  } 1020
  1021
  } 1022
  1023
  1024
  
	    
    
评论
				
					
						# re: 《JAVA2简明教程》课后习题答案及实验详解  回复  更多评论
						  
					
					2008-08-18 09:19 by 
				 这本书对想快速入门JAVA的初学都来说还是可以的,不用几天就能看完.
这些都是我个人写的代码,有不当之处请大家自行更正.
 
				
					
						# re: 《JAVA2简明教程》课后习题答案及实验源码详解  回复  更多评论
						  
					
					2008-09-16 21:02 by 
				 你好,有所有答案吗? 
				
					
						# re: 《JAVA2简明教程》课后习题答案及实验源码详解  回复  更多评论
						  
					
					2008-09-17 11:07 by 
				 @yhc
不好意思,我只做过这些题的,我的那本书被其他专业的老师拿去了,如果你还有什么题想要解题过程,可以把题目发上来我尽量帮你解答,不过现在很忙,可能有时没有时间,也原谅.
 
				
					
						# re: 《JAVA2简明教程》课后习题答案及实验源码详解[未登录]  回复  更多评论
						  
					
					2009-10-20 10:32 by 
				 有后面的答案吗
好急的啊
 
				
					
						# re: 《JAVA2简明教程》课后习题答案及实验源码详解[未登录]  回复  更多评论
						  
					
					2009-12-19 12:59 by 
				 用户在文本框输入圆的半径,然后随机产生一个坐标位置,在屏幕上画圆,通过其他组件(如:JTextArea)输出圆的直径、周长和面积。另外提供一个按钮,可以清除屏幕,重新画圆
做完后帮忙发到我的邮箱:249332006@qq.com
 谢谢……
 |