Java程序设计经典例子_第1页
Java程序设计经典例子_第2页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1 / 221Java 2程序设计1 / 221例子1:第一个Java程序public class Hello public static void main (String args ) System.out.println(你好,很高兴学习Java);例子2:第二个java程序class People int height;String ear;void speak(String s) System.out.println(s);public class A public static void main(String args) People zhubajie; zhubajie=new

2、 People(); zhubajie.height=170; zhubajie.ear=两只大耳朵; System.out.println(身高:+zhubajie.height);System.out.println(zhubajie.ear);zhubajie.speak(师傅,咱们别去西天了,改去月宫吧);例子3:Applet程序(由AppletTest.class和AppletTest.htmlimport java.applet.*;import java.awt.*;public class AppletTest extends Applet public void paint(

3、Graphics g) g.setColor(Color.red);g.drawString(我一边喝着咖啡,一边学Java呢,5,30);g.setColor(Color.blue);g.drawString(我学得很认真,10,50);/AppletTest.htmlApplet test example第一章Java程序设计概述组成)1 / 221This is a applet exampleThe source.例子4:Java图形用户界面例子/JavaGUI.javaimport java.awt.FlowLayout; import java.awt.event.*;import

4、 javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;/* author同步计算输入的各个数的总和与平均值*/ public class JavaGUI extends JFrame private static final long serialVersionUID =6515574844960224544L; J TextField input = new JTextField(30);J TextField output = new JT

5、extField(30);J Button close = new JButton(关闭);J Button reset = new JButton(清空);public JavaGUI() setupGUI();public void setupGUI() this.setTitle(计算总和与平均值); this.setLayout(newFlowLayout(); this.add(new JLabel(数据);se +-HCTe宀三一Enu)lu-(Dsed.(D6(Dc- H+tuns(+6u(DLunu V -OM二u_)0J00H6e(D-qnopoHtunslu-=)一ds4=

6、s u-Enu6u匸CD(Dsaj宀-clxe匕es.lndlno二一一 nuHH上s=r)s_enb(D4=s二一OX(D匕(D64ndu一Mls6U_C50lu(D(Dl)p(Dse(D-(DQrA(Dl po 0一-qnd()(Dldepl M(Du)(Du(DlsnA(Dlppe4ndu-SCOLOO寸ONSOJSisesaoppe.szl-(Dsoo)ppe.szl0ndlno)ppe.s_二=erM(DU(S6e=6u_co)uroLU po 0遍 s0一-qnd宀X山SO_IOINOI-L_X山as-(宀宀-CXCD匕(Ds.lndlnor-)lxej.as4ndu一(06elu

7、(D5(Dsnol/l)p(Dloo(DsnoLU po 0一-qnd()(Dldep(Dsnol/l M(Du)(Du(Dlsn(Dsnol/lppe4(Ds(D宀XOMXPLUPSAS()(Dldepb) t=a; a=b; b=t;if(ac) t=a; a=c; c=t;if(bc) t=b; b=c; c=t;System.out.println(a=+a+,b=+b+,c=+c);北洛44强冒畐直二6 / 221例子6:用if语句判断给定的成绩是否及格public class Score public static void main(String args)例子7:switch语

8、句的使用,当主程序执行时,如果第一个命令行参数的首字 符分别是数字、小写字母及大写字母时,系统会显示这个首字符。如果输入的 是非数字或字母,则显示不是数字或字母。class Ex2_07 public static void main(String args) char ch = args0.charAt(0);switch (ch) case 0: case 1: case 2: case 3: case 4:case 5: case 6: case 7: case 8: case 9:System.out.println(The character is digit + ch);break

9、;case a: case b: case c: case d: case e: case f: case g:case h: case i: case j: case k: case l: case m: case n:case o: case p: case q: case r: case s: case t: case u:case v: case w: case x: case y: case z: System.out.println(The character is lowercase letter +ch); break;case A: case B: case C: case

10、D: case E: case F: case G:case H: case I: case J: case K: case L: case M: case N:case O: case P: case Q: case R: case S: case T: case U:int math=65 ,english=85;if(math=60) System.out.println(else System.out.println( if(english90) System.out.println( else System.out.println( System.out.println(数学及格了)

11、;数学不及格);英语是优);英语不是优);我在学习控制语句);7 / 221case V: case W: case X: case Y: case Z:System.out.println(The character is uppercase letter + ch); break;default:System.out.println(The character + ch+ is neither a digit nor a letter.);例子8:使用for循环,计算5+ 55 + 555 +。 的前10项的和public class Example3_6public static voi

12、d main(String args)long sum=0,a=5,item=a,n=10,i=1;for(i=1;i=n;i+) sum=sum+item;item=item*10+a;System.out.println(sum);例子9:使用while循环,计算1 + 1/2! + 1/3! + 1/4! + + 1/20!class Example3_7 public static void main(String args) double sum=0,a=1;int i=1; while(i + sum);的值8 / 221例子11:break和continue使用举例,分别计算10

13、以内的奇数的和,计算50以内的素数class Example3_8 public static void main(String args)int sum=0,i,j;for( i=1;i=10;i+)if(i%2=0) /计算1+3+5+7+9continue;sum=sum+i;System.out.println(sum=+sum);for(j=2;j=50;j+) /求50以内的素数for( i=2;ij/2) System.out.println(+j+是素数);例子11:一维数组举例,输出一维整型数组中的值最小的那个元素及其下标)public class ArrayTest pub

14、lic static void main(String args) int a = 52, 78, 90, 34, 16, 34, 67 ;int indexOfMinElement = 0;for (int i = 1; i ai) indexOfMinElement = i;System.out.println(a + indexOfMinElement + = + aindexOfMinElement);例子12:计算二维数组中各行元素之和并查找其值最大的那个行public class TableTester 9 / 221public static void main(String a

15、rgs) int myTable = 23, 45, 65, 34, 21, 67, 78,46, 14, 18, 46, 98, 63, 88,98, 81, 64, 90, 21, 14, 23,54, 43, 55, 76, 22, 43, 33; int sum, max, maxRow=0;max = 0; /Assume all numbers are positive for (int row=0; row4; row+) sum = 0;for (int col=0; col max) max = sum; maxRow = row;System.out.println(Row

16、 + maxRow + has the highest sum of + max);10 / 221例子1:声明对象,为对象分配内存class XiyoujiRenwu float height,weight;String head, ear,hand,foot, mouth;void speak(String s) System.out.println(s);class A public static void main(Stringargs) XiyoujiRenwu zhubajie; /zhubajie=new XiyoujiRenwu(); / 例子2:声明对象,为对象分配内存cla

17、ss Point int x,y;Point(int a,int b) x=a;y=b;public class A public static void main(String args)Point p1,p2; /p1=new Point(10,10);/p2=new Point(23,35);/例子3:对象调用类中的方法(使用类中的功能)class XiyoujiRenwu float height,weight;String head, ear,hand,foot,mouth;void speak(String s) head=歪着头;System.out.println(s);第三章

18、Java的面向对象程序设计声明对象为对象分配内存,使用new运算符和默认的构造方法声明对象p1和p2为对象分配内存,使用new和类中的构造方法为对象分配内存,使用new和类中的构造方法11 / 221zhubajie.speak(俺老猪我想娶媳妇); /对象调用方法System.out.println(zhubajie现在的头:+zhubajie.head); sunwukong.speak(老孙我重1000斤,我想骗八戒背我); /对象调用方法System.out.println(sunwukong现在的头:+sunwukong.head);例子4:用梯形类创建对象,该对象有计算自身面积的功

19、能class梯形float上底,下底,高,面积;梯形(float x,float y,float h) 上底=x;下底=y;高=h;float计算面积() 面积=(上底+下底)*高/2.0f; return面积;void修改高(float height) 高=height;float获取高()return高;class Example3_3 public static void main(String args)XiyoujiRenwu zhubajie,sunwukong;/zhubajie=new XiyoujiRenwu(); /sunwukong=new XiyoujiRenwu();

20、zhubajie.height=1.80f; /zhubajie.head=大头;zhubajie.ear=一双大耳朵;sunwukong.height=1.62f; /sunwukong.weight=1000f;sunwukong.head=绣发飘飘;System.out.println(zhubajieSystem.out.println(zhubajieSystem.out.println(sunwukongSystem.out.println(sunwukong声明对象为对象分配内存对象给自己的变量赋值对象给自己的变量赋值的身高:+zhubajie.height);的头:+zhuba

21、jie.head);的重量:+sunwukong.weight);的头:+sunwukong.head);12 / 221public class Example3_4 public static void main(String args)梯形laderOne=new梯形(12.0f,3.5f,50),laderTwo=new梯形(2.67f,3.0f,10)例子5:引用类型参数和基本类型参数举例class People String face;void setFace(String s) face=s;class A void f(int x,double y,People p) x=x+

22、1;y=y+1;p.setFace(笑脸);System.out.println(参数x和y的值分别是:+x+,+y);System.out.println(参数对象p的face是:+p.face);public class Example3_5 public static void main(String args)int x=100;double y=100.88;People zhang=new People(); zhang.setFace(很严肃的样子); A a=new A();方法中x和y的值仍然分别是:+x+,+y);方法中对象zhang的face是:+zhang.face);

23、System.out.println(laderOneSystem.out.println(laderTwoSystem.out.println(laderOneSystem.out.println(laderTwoladerOne.修改高(10); floath=laderOne.获取高(); laderTwo.修改高(h*2);System.out.println(laderOneSystem.out.println(laderTwoSystem.out.println(laderOneSystem.out.println(laderTwo的高是:+laderOne.获取高();的高是:+

24、laderTwo.获取高();的面积是:+laderOne.计算面积();的面积是:+laderTwo.计算面积();现在的高是:+laderOne.获取高();现在的高是:+laderTwo.获取高();现在的面积是:+laderOne.计算面积();现在的面积是:+laderTwo.计算面积();a.f(x,y,zhang);System.out.println(mainSystem.out.println(main13 / 221例子6:“圆锥”类在创建对象时,将一个“圆”类的对象的引用传递给圆锥对 象的底圆class圆double半径;圆(double r) 半径=r;double计算

25、面积()return 3.14*半径*半径;void修改半径(double新半径) 半径=新半径;double获取半径()return半径;class圆锥圆 底圆;double高;圆锥(圆circle,double h) this.底圆=circle;this.高=h;double计算体积()double volume;volume=底圆.计算面积()*高/3.0;return volume;void修改底圆半径(double r) 底圆.修改半径(r);double获取底圆半径()return底圆.获取半径();class Example3_6 public static void main

26、(String args)圆circle=new圆(10);圆锥circular=new圆锥(circle,20);14 / 221System.out.println(System.out.println(圆锥底圆半径:+circular.获取底圆半径();圆锥的体积:+circular.计算体积();circular.修改底圆半径(100);System.out.println(System.out.println(圆锥底圆半径:+circular.获取底圆半径();圆锥的体积:+circular.计算体积();例子7.1:static变量举例,两个梯形共享一个底class梯形float上

27、底,高;static float下底;梯形(float x,float y,float h) 上底=x;下底=y;高=h;float获取下底()return下底;void修改下底(float b) 下底=b;class Example3_7 public static void main(String args)梯形laderOne=new梯形(3.0f,10.0f,20),laderTwo=new梯形(2.0f,3.0f,10);例子7.2:static方法举例,计算Fibinaciiclass Fibipublic static long fibinacii(int n) long c=0

28、;if(n=1|n=2)c=1;elsec=fibinacii(n-1)+fibinacii(n-2);return c;梯形.下底=200;/System.out.println(laderOneSystem.out.println(laderTwoladerTwo.修改下底(60);System.out.println(laderOneSystem.out.println(laderTwo通过类名操作类变量的下底:+laderOne.获取下底();的下底:+laderTwo.获取下底();/通过对象操作类变量的下底:+laderOne.获取下底();的下底:+laderTwo.获取下底()

29、;第n项的值15 / 221public class Example3_8 public static void main(Stringargs) System.out.println(Fibi.fibinacii(7);例子8:this关键字举例:计算两个数的和,默认计算class Plus int a, b;Plus() this(10, 20);Plus(int a, int b) this.a = a;this.b = b;int sum() return a + b;public class TestThis public static void main(String args)

30、Plus add = new Plus(); System.out.println(add.sum(); add =new Plus(100, 200); System.out.println(add.sum();例子9:package举例,输出n以内的所有素数package cn.zzuli.cs;public class PrimNumber public void getPrimnumber(int n) int i, j;boolean isPrim=true;for (i = 2; i = n; i+) isPrim=true;for (j = 2; j c&a+cb&c+ba)17

31、 / 221 System.out.println(我是一个三角形); boo=true;else System.out.println(我不是一个三角形); boo=false;public void计算面积() if(boo) double p=(sideA+sideB+sideC)/2.0;double area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC);System.out.println(面积是:+area);else System.out.println(不是一个三角形,不能计算面积); public void修改三边(double a,d

32、ouble b,double c) sideA=a;sideB=b;sideC=c; if(a+bc&a+cb&c+ba) boo=true;else boo=false;例子13:将类打为jar包后引入import cn.zzuli.cs.Trangle;import cn.zzuli.cs.Fibi;class PackageTest public static void main(String args) Trangle trangle = new Trangle(12, 3, 1);trangle.计算面积();trangle.修改三边(3, 4, 5);trangle.计算面积();

33、int k = 3;System.out.println(Fibi第 + k + 项的值是: + Fibi.fibinacii(k);例子14:私有变量和私有方法的访问class Example3_14 private int money;18 / 221Example3_14()money=2000;private int getMoney() return money;public static void main(String args)Example3_14 exa=new Example3_14();exa.money=3000;int m=exa.getMoney();System

34、.out.println(money=+m);19 / 221例子15:子类和父类在同一个包中的继承性class Father private int money; float weight,height; String head; void speak(String s) System.out.println(s);class Son extends Father String hand,foot;public class Example3_15 public static void main(String args) Son boy;boy=new Son(); boy.weight=1.

35、80f; boy.height=120f; boy.head=一个头;boy.hand=两只手;boy.foot=两只脚;boy.speak(我是儿子); System.out.println(boy.hand+boy.foot+boy.head+boy.weight+boy.height); 例子16:子类重写父类的方法fclass Multiply float f(float x,float y) return x*y;class Add extends Multiply float f(float x,float y) return x+y ;public class Example3_

36、16 public static void main(String args) Add sum;sum=new Add();float c=sum.f(4,6);System.out.println(c);例子17:子类重写(f)和未重写(g)父类的方法及调用class Area float f(float r ) return 3.14159f*r*r;float g(float x,float y) return x+y;class Circle extends Area float f(float r) return 3.14159f*2.0f*r;public class Exampl

37、e3_17 20 / 221public static void main(String args)Circle yuan; yuan=new Circle();float length=yuan.f(5.0f);float sum=yuan.g(232.645f,418.567f);System.out.println(length);System.out.println(sum);例子18:final变量和final方法class A final double PI=3.1415926;public double getArea(final double r) return PI*r*r;

38、public class Example3_18 public static void main(String args) A a=new A();System.out.println(面积:+a.getArea(100);例子19:对象的上转型对象(多态性)class类人猿private int n=100;void crySpeak(String s) System.out.println(s);class People extends类人猿void computer(int a,int b) int c=a*b;System.out.println(c);void crySpeak(St

39、ring s) System.out.println(*+s+*);class Example3_19 public static void main(String args) 类人猿monkey=new People(); monkey.crySpeak(I love this game);People people=(People)monkey;puter(10,10);21 / 221例子20:多态性举例,动物的叫声class动物void cry() class狗extends动物void cry()System.out.println(汪汪 .);class猫extends动物void

40、 cry()System.out.println(喵喵 .);class Example3_20 public static void main(String args)动物dongwu;dongwu=new狗();dongwu.cry();dongwu=new猫();dongwu.cry();例子21:抽象类举例, 不能用new运算符创建抽象类的对象, 必须通过其不是 抽象类的子类来创建对象abstract class A abstract int min(int x,int y);int max(int x,int y) return xy?x:y;class B extends A in

41、t min(int x,int y) return xy?x:y;public class Example3_21 public static void main(String args) A a;B b=new B();int max=b.max(12,34);int min=b.min(12,34);System.out.println(max=+max+ min=+min);a=b;22 / 221max=a.max(12,34);System.out.println(max=+max);例子22:抽象类实例,使用抽象类计算一个梯形堆和圆形堆得体积。abstract class图形pub

42、lic abstract double求面积();class梯形extends图形double a,b,h;梯形(double a,double b,double h) this.a=a;this.b=b;this.h=h;public double求面积() return(1/2.0)*(a+b)*h);class圆形extends图形double r;圆形(double r) this.r=r;public double求面积() return(3.14*r*r);class堆图形 底;double高;堆(图形 底double高)this.底=底;this.高=高;void换底(图形 底)

43、 this.底=底;public double求体积()return (底.求面积()*高)/3.0;public class Example3_22 public static void main(String args)堆zui;23 / 221图形tuxing;tuxing=new梯形(2.0,7.0,10.7);System.out.println(梯形的面积+tuxing.求面积();zui=new堆(tuxing,30);System.out.println(梯形底的堆的体积+zui.求体积();tuxing=new圆形(10);System.out.println(半径是10的圆

44、的面积+tuxing.求面积();zui.换底(tuxing);System.out.println(圆形底的堆的体积+zui.求体积();例子23:super举例,使用关键字super调用父类的构造方法class Student int number;String name;Student()Student(int number,String name) this.number=number;=name;System.out.println(I am +name+ my number is +number);class Univer_Student extends Stud

45、ent boolean婚否;Univer_Student(int number,String name,boolean b) super(number,name);婚否=b;System.out.println(婚否=+婚否); public class Example3_23 public static void main(String args) Univer_Studentzhang=new Univer_Student(9901,和晓林,false);例子24:super举例,使用关键字super操作被隐藏的成员变量和方法。class Sumint n;float f()float s

46、um=0;for(int i=1;i=n;i+)sum=sum+i;24 / 221return sum;class Average extends Sum int n;float f()float c;super.n=n;c=super.f();return c/n;float g()float c;c=super.f();return c/2;public class Example3_24 public static void main(String args) Average aver=new Average();aver.n=100;float result_1=aver.f();f

47、loat result_2=aver.g();System.out.println(result_1=+result_1);System.out.println(result_2=+result_2);例子25:接口的使用interface Computable int MAX=100;int f(int x);class China implements Computable int number;public int f(int x) /不要忘记public关键字int sum=0;for(int i=1;i=x;i+)sum=sum+i;return sum;class Japan im

48、plements Computable int number;public int f(int x) return 44+x;public class Example3_25 public static void main(String args)25 / 221China zhang;Japan henlu;zhang=new China();henlu=new Japan();zhang.number=991898+Computable.MAX; henlu.number=941448+Computable.MAX;System.out.println(number:+zhang.numb

49、er+求和+zhang.f(100);System.out.println(number:+henlu.number+求和+henlu.f(100);例子26:接口,一个类实现多个借口interface收费public void收取费用();interface调节温度public void controlTemperature();class公共汽车implements收费 public void收取费用()System.out.println(公共汽车:一元/张,不计算公里数);class出租车implements收费,调节温度public void收取费用()System.out.prin

50、tln(出租车:1.60元/公里,起价3公里); public void controlTemperature() System.out.println(安装了Hair空调);class电影院implements收费,调节温度 publicvoid收取费用()System.out.println(电影院:门票,十元/张);public void controlTemperature() System.out.println(安装了中央空调);class Example3_26public static void main(String args)公共汽车 七路=new公共汽车();出租车 天宇

51、=new出租车();电影院 红星=new电影院();七路.收取费用();天宇.收取费用();红星.收取费用();天宇.controlTemperature();红星.controlTemperature();例子27:接口的回调1interface ShowMessage void26 / 221显示商标(String s);class TV implements ShowMessage publicvoid显示商标(String s) System.out.println(s);class PC implements ShowMessage publicvoid显示商标(String s)

52、System.out.println(s);public class Example3_27 public static void main(String args)ShowMessage sm;sm=new TV();sm.显示商标(长城牌电视机); sm=new PC();sm.显示商标(联想奔月5008PC机); 例子28:接口的回调2interface Computerable public double求面积();class梯形implements Computerable double a,b,h;梯形(double a,double b,double h) this.a=a;th

53、is.b=b;this.h=h;public double求面积() return(1/2.0)*(a+b)*h);class圆形implements Computerable double r;圆形(double r) this.r=r;public double求面积() return(3.14*r*r);class堆 Computerable底;double高;堆(Computerable底double高)this.底=底;27 / 221this.高=高;void换底(Computerable底)this.底=底;public double求体积()return (底.求面积()*高)

54、/3.0;public class Example3_28 public static void main(String args)堆zui;Computerable bottom;bottom=new梯形(2.0,7.0,10.7);System.out.println(梯形的面积+bottom.求面积();zui=new堆(bottom,30);System.out.println(梯形底的堆的体积+zui.求体积();bottom=new圆形(10);System.out.println(半径是10的圆的面积+bottom.求面积(); zui.换底(bottom);System.out

55、.println(圆形底的堆的体积+zui.求体积();例子29:接口作为参数interface SpeakHello void speakHello();class Chinese implements SpeakHello public void speakHello()System.out.println(中国人习惯问候语:你好,吃饭了吗?);class English implements SpeakHello public void speakHello()System.out.println(英国人习惯问候语:你好,天气不错); class KindHellopublic void

56、 lookHello(SpeakHello hello) hello.speakHello();public class Example3_29 public static void main(String args)KindHello kindHello=new KindHello(); kindHello.lookHello(new Chinese(); kindHello.lookHello(newEnglish();28 / 221例子30:内部类class China final String nationalAnthem=义勇军进行曲;Beijing beijing;China()

57、beijing=new Beijing();String getSong()return nationalAnthem;class BeijingString name=北京;void speak()System.out.println(我们是+name+我们的国歌是:+getSong(); public class Example3_30 29 / 221public static void main(String args)China china=new China(); china.beijing.speak();例子31:匿名类class Cubic double getCubic(int n) return 0;abstract class Sqrtpublic abstract double getSqrt(int x);class A void f(Cubic cubic) double result=cubic.getCubic(3); System.ou

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论