《Java语言程序设计清华大学出版社例题源代码.docx》由会员分享,可在线阅读,更多相关《Java语言程序设计清华大学出版社例题源代码.docx(304页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Java语言程序设计清华大学出版社例题源代码1-11class Examplel_l(public static void main(String args)ISystem, out. println(/zWelcomen to njava !); 1_2import javax. swing. JOptionPane;class Examplel_2(public static void main(String args)(JOptionPane. showMessageDialog(null, z,Welconen to njava !);System, exit (0)J 退出程序)1_
2、3/ 个简单的Java小应用程序/import javax. swing. JApplet;import java. awt. Graphics;public class Example1_3 extends AppletString s;public void init()(s二我对Java很痴迷。;public void paint(Graphics g)ig. drawString(s, 25, 25);【2】计算二个数的和class Example2_l public static void main(String args) int x, y, s;x = 3;y = 5;s =x+
3、y; 求和System, out. printin (数之和为:+ s);2_2计算圆的面积class Example2 2 public static void main(String args) double pi,r,s;r = 10. 8; 圆的半径pi = 3.1416;s = pi * r * r; 计算面积System, out. printin (“圆的面积为:+ s);2_3/* char变量的用法/class Example2_3 public static void main(String args) char chi,ch2;chi = 88; / code for X
4、ch2 =Y;System, out. print (,zchl and ch2:);System, out. printin (chi + ch2);2 4/布尔类型的用法/class Example2_4 public static void main(String args口)boolean b;b = false;System, out. println(/zb is + b);b = true;System, out. printin (/zb is + b);/ outcome of a relational operator is a boolean valueSystem, o
5、ut.println(10 9 is ” + (10 9); 2_5public class Example2_5public static void main(String agrs) 定义几个变量并赋值 int a=41;int b=21;double x=6.4;double y=3. 22;System, out. print In (“变量数值:);System. out. println(a=+a);System, out. printin (b=+b) ; System, out. print In (zzx=zz+x);System, out. println(zzy=+y);
6、 加法System, out. printin (加:);System, out. print In (a+b= +(a+b) ; System, out. println(x+y=*+(x+y); 减法System, out. printin (减:);System, out. printin (a-b-+ (a-b); System, out. println(x-y= + (x-y); 乘法System, out. printin (乘:);System, out. print In Ca*b=*+ (a*b); System, out. println(x*y=+(x*y); 除法Sy
7、stem, out. printin (除:“);System, out. printin (a/b=+ (a/b); System. out. printin (x/y=+ (x/y); 从除法 中求得余数System, out. print In (计算余数:);System. out.println(a%b=+(a%b);System, out. print In (x%y=+(x%y): 混合类型System, out. print In (混合类型:);System, out. print In (b+y=+(b+y);System, out. print In(a*x=+(a*x)
8、; 2_6public class Example2_6public static void main(String args) 定义若干整型数int i=37;int j=42;int k=42;System, out. printin(变量数值);System, out. println(i=+i);System, out. printin (j=+j);System, out. printin (k= +k);大于System, out. printin(大于:);System, out. println(ij=*+(ij);/falseSystem, out. printing ji=
9、+(ji);/trueSystem, out. println(*kj=*+(kj);/false 大于等于System, out. printin(大于等于:);System, out. printinCi=j= +(i=j) ;falseSystem. out. printinC*j,=1= +(j=i) ;trueSystem, out. printin (k=j=+(k=j) ; true 小于System, out. printin(于:);System. out. printin ij=+ (ij);/trueSystem, out. printin(ji=+(ji);/false
10、System. out. println(*kj=,,+ (kj);/false 小于等于System. out. printin (“小于等于:“);System, out. printin(i=j=+(ij);/trueSystem, out. printin(j=i=*+(j=i);/falseSystem, out. printin (k=j=*+(kb)(t=a; a=b; b=t; if(ac)(t=a; a=c; c=t;if(bc)System, out. printin(a=+a+, b=+b+,c=+c); /* if-else-if 结构.*/2_9class Examp
11、le2_9 public static void main(String args) int month = 4; / 4 月份String season;if (month = 12 |month = 1|month = 2) season 二冬天;else if (month=3|month=4 |month = 5) season =春天;else if (month=6|month=7 |month = 8) season =夏天;else if (month9|month10| month 11) season二秋天;)else season =不合法的月份”;System, out
12、. println(4 月是+ season + . ); 2_10/* switch开关语句/import java.applet. *;import java. awt. *;public class Example2_10 extends Appletpublic void paint(Graphics g)int x=l, y=l;switch(x+y)(case 1: :g. setColor(Color, red);g. drawString (zi am 1”, 5, 10);break;case 2:g.setColor(Color, blue);g. drawString(i
13、 am 2”, 5,10);/ break;case 3:g. setColor(Color, green);g. drawString (i am 3”, 5, 10);break;default:g. drawString (没有般配的,5, 10);)/*APPLET C0DE=Example2_10. class WIDTH=200 HEIGHT=300 /* for 循环 /import javax. swing. JOptionPane;public class Example2_11(public static void main(String args) int sum=0;f
14、or(int i=1;i=100;i+)!sum二sum+i;JOptionPane. showMessageDialog(null, zl+2+3+. +100=+sum) ; System, exit (0); /退出程序2_12/* while 循环 /import javax. swing. JOptionPane;public class Example2_12(public static void main(String args)int s=l, i=l;while(i=10)s=s*i;i+;)JOptionPane. showMessageDialog(null, wl*2*
15、3*. . *10= +s) ; System, exit (0): 退 出程序)2,13/* do-while 循环 */import java, applet. *;import java. awt. *;public class Example2_13 extends Applet(public void paint(Graphics g)(int i=l;doIg. draw0val(110-i*10,110-i*10, i*20, i*20);i+;while(i=10); */2_14/使用break语句跳出循环/import javax. swing. JOptionPane;c
16、lass Example2_14ipublic static void main(String args)(for(int i=0; i100; i+)(if (i = 10) break; / i=10 时跳出循环JOptionPane. showMessageDialog(null,/zi=+i); JOptionPane. showMessagcDialog(null, 循环已经结束!”); System, exit (0): 退出 程序2_15/使用break语句跳出内循环/class Example2_15for(int i=l; i6; i+)(for(int j=l;j3; j+
17、)(if (i = 3) break; / i=5 时跳出循环 int sum=i+j;System, out. printin(i+ + +j+ = +sum); System, out. printin (循环已经结束!”);2_16/使用标签化中断的break语句跳出循环/class Example2_16(public static void main(String args)(out: for(int i=l; i6; i+) 设置标号for (int j=l;j3; j+)Iif (i = 3) break out; / i=3 时跳出循环 int sum=i+j;System,
18、out. printin(i+j+= +sum); 2_17/continue语句打印三角形/import javax. swing. JOptionPane;class Example2_17 public static void main(String args) String output=;for(int i=0; i i) continue ;output= output +*+output=output+、n”;JOptionPane. showMessageDialog(null, output);System, exit(0);)2_18/求一组数字的平均值/import ja
19、vax. swing. JOptionPane;class Example2_18 public static void main(String args) double nums = 10. 1, 11.2, 12. 3, 13. 4, 14. 5;double result = 0;for(int i二; i5; i+)result = result + numsi; JOptionPane. showMessageDialog(null,“平均值为:+ result / 5); System, exit(0);2,19/Z二维数组赋值class Example2_19public sta
20、tic void main(String args) int twoD= new int45;int i, j, k = 0;for(i=0; i4; i+)for(j=0; j5; j+) twoDij = k;k+;for(i=0; i4; i+) for(j=0; j5; j+)System, out. print(twoDij + “;System, out. printlnO ;2_20import java. util. *;class Example2 20(public static void main(String口 args)iVector vl =new Vector()
21、;for (int i=l;i10;i+)ivl. addElement1+ i);System, out. println(vl. toStringO);vl. insertElementAt(abc”, 5);System, out. println(n 插入元素:+vl. toStringO); 2,21import java. util. *;public class Example2_21 public static void main(String args) String s=*boy, java, Applet girl, Hat*;StringTokenizer st=new
22、 StringTokenizer (s, * , *): 空格和逗号做分 !nt number=st. countTokensO ;while(st. hasMoreTokens() String str=st. nextTokenO ;System, out. println(str);System, out. printin (*s 共有单词:“+number+个);3,1import javax. swing. *;class 三角形面积(float area(int x, int h)float s;s=(x*h)/2;return s;public class Example3_lp
23、ublic static void main(String args)三角形面积a=new三角形面积();float s=a. area(3,4);JOptionPane. showMessageDialog(null,三角形面积=+$); System, exit (0);)3_2import javax. swing. *;class Dataloading(double distance(double x , double y)(double d=Math. sqrt(x*x+y*y);return d;)double distance(double x , double y ,doub
24、le z )(double d二Math, sqrt(x*x+y*y+z*z);return d;public class Example3_2Dataloading dis=new DataloadingO;double dl=dis. distance(2, 3);double d2=dis. distance(3, 4, 5);JOptionPane. showMessageDialog(null,接受二个参数:平面距离d一+dl+、n +”接受三个参数:平面距离d+d2 ); System, exit(0);3_3/构造长方体/class Box(double width, heigh
25、t, depth;Box() / Box的构造方法.width = 10;height = 10;depth = 10;double volume () /Z计算长方体体积return width * height * depth;public static void main(String args)(Box box = new Box ();double v;v = box. volume ();System, out. printin (长方体体枳为:+ v) ; (3_4)/*构造长方体/class Box(double width, height, depth;double volu
26、me () /Z计算长方体体积width = 10;height = 10;depth = 10;return width * height * depth;public static void main(String args)(Box box = new Box (); /使用缺省构造方法double v;v = box. volume ();System, out. printin (长方体体积为:+ v);3_5/计算长方体体积/import javax. swing. *;class Boxdouble volume(double width,double height,double
27、 depth) return width * height * depth;)public class Example3_5public static void main(String args)Box box = new Box ();double v;v = box. volume (3, 4, 5);JOptionPane. showMessageDialog(null,“长方体体积=+丫); System, exit (0);3_6/对象作为方法的参数/class圆面积(double r;圆面积(double r) this.r=r;)double area()(return 3.14
28、*r*r;class圆柱体积(圆柱体积(圆面积 a, double h) double v=a. area() *h;System. out. printin (圆柱体的体积=+丫); class Example3_6 public static void main(String args口)圆面积A=new圆面积(5);圆柱体积c=new圆柱体积(A, 10) ; )3_7/类的继承/class A 类为父类(void prnt(int x, int y)(int z=x+y;String s二 x+y=;System, out. println(s+z);class B extends A
29、 / B 类是 A 的子类String name;void bb()System, out. printin(name);public static void main(String args)(B bl=new B(); /创建B类的对象blbl. name=zsm;bl. bb();bl. prnt (3, 5);3_8/子类重写了父类的方法/import java. io. *;class A(void callme()(System, out. println(A 类中的 cal lme()方法”);class B extends Avoid callme()System, out.
30、printlnCB 类中的 cal lme()方法”);public class Example3_8(public static void main(String args)(A a=new B ();a.callme();3_9/本例说明静态方法的调用,B是静态方法,所以A可以直接调用,而没有对B进行实例化。*/ class B(static int count;B()(count+;System, out. printin(count=+count);)public static void p()System, out. println(I am B!);class Example3_9
31、public static void main(String args)(B. p();B bl=new B();B b2=new B();B b3=new B();/*如果类B中的p()没有声明为static,则必须在A中对B进行实例化,否则编译不能通过。*/ /*class Bpublic void p() System, out. printin (/ZI am B!); class Apublic static void main(String args) B b=new B();b. p();*/3_10class Member (static int classVar;int in
32、stanceVar;static void setClassVar(int i) (classVar二i;/ instanceVar=i; /Z类方法不能访问实例变量 static int getClassVar()(return classVar;void setlnstanceVar(int i)(classVar=i; 实例方法不但可以访问类变量,也可以实例变量instanceVar=i; )int getInstanceVar()return instanceVar;public static void main(String args)(Member ml=new Member();
33、Member m2=new Member();ml. setClassVar(1);m2. setClassVar(2);System, out. printin(ml. classVar=+ml. getClassVar()+m2. ClassVar=/+m2. getClassVar () ; ml. setlnstanceVar (11);m2. setlnstanceVar(22);System, out. printin(ml. InstanceVar=+ml. getlnstanceVar()+ m2. InstanceVar=+m2. getlnstanceVar(); 3_11
34、/*抽象类/abstract class生物生物类为抽象类public abstract String 特征();class植物extends生物植物是生物的子类String leaf;植物(String leaf)this, leaf=_leaf;public String 特征()return leaf; class动物extends生物动物是生物的子类String mouth;动物(String _mouth)this, mouth二一mouth; public String 特征()return mouth;public class Example3_l1public static v
35、oid main(String args)植物A = new植物(“叶);System. out. printin (植物的特征:+A ,特征();动物B二new动物(嘴巴);System. out. printin (“动物的特征:+B,特征();)3_12/接口的使用设拟编写个超类Prnting,其中定义了一个prnt ()方法,其内容如下: class Prnting (void prnt () (System, out. printin(蔬菜和水果都重要。);) )现要将其改成接口。*/将一个类改成接口,只要把class换成interface,再把其所有方法的内容都抽掉。 interf
36、ace Prntingvoid prnt ();)public class Example3_ 12 implements Prnting实现接口,重写 prnt()方法 void prnt () iSystem, out. printin (蔬菜重要。): )public void收取费用;interface调节温度(public void controlTemperatureO;class公共汽车!mplements收费public void收取费用()(System. out. printin (公共汽车:一元/张,不计算公里数”);class 出租车!mplements收费,调节温度(
37、public void收取费用()(System, out. print In (出租车:1. 60 元/公里,起价 3 公里);public void controlTemperature()System, out. printin (安装了 Hair 空调);class电影院implements收费,调节温度public void收取费用()(System. out. printin(电影院:门票,十元/张;public void controlTemperature() System, out. printin(安装了中央空调”);class Example3_13ipublic sta
38、tic void main(String args) 公共汽车七路二new公共汽车();出租车天宇二new出租车();电影院红星二new电影院();七路.收取费用();天宇,收取费用();红星.收取费用();天宇.controlTemperature ();红星.controlTemperature();)void 显示商标(String s);class TV implements ShowMessagepublic void 显示商标(String s)System, out. printin (s);class PC implements ShowMessagepubl ic void
39、显示商标(String s)System, out. println(s);public class Example3_14public static void main(String args)ShowMessage sm; 声明接口变量。sm二new TV();接口变量中存放对象的引用。sm.显示商标(长城牌电视机”);接 回调。sm二new PC(); 接口变量中存放对象的引用。sm.显示商标(联想奔月5008PC机); 接回调。3,15存放在当前目录下import abc. test. *;public class PackageTest(public static void main
40、(String args)!MyTest mt = new Mytest();mt. prn ();4_1/* TextField类的简单示例/import java, applet. *;import java. awt. *;public class Example4_l extends AppletTextField textl, text2, text3;声明对象public void init()textl=new TextField(输入密码:,10); 对象实例化textl. setEditable(false);设置文本为只读text2=new TextField(lO);text2. setEchoChar (*);设置掩