• 當前位置:首頁 > IT技術 > 微信平臺 > 正文

    一些入門的java小程序
    2021-08-04 10:13:10

    一個簡單的Java應用程序
    public class Hello
    {?
    ????public static void main (String args[ ])
    ????{
    ?????? System.out.println("這是一個簡單的應用程序");
    ????}
    }


    源程序
    public class People
    {
    ????float hight,weight;?
    ????String head,ear,mouth;
    ????void speak(String s)
    ????{?
    ?????? System.out.println(s);
    ????}
    }
    class A
    {?
    ????public static void main(String args[])
    ????{
    ??????People zhubajie;
    ??????zhubajie=new People();
    ??????zhubajie.weight=200f;????
    ??????zhubajie.hight=1.70F;
    ??????zhubajie.head="大頭";
    ??????zhubajie.ear="兩只大耳朵";
    ??????zhubajie.mouth="一只大嘴";
    ??????System.out.println("重量"+zhubajie.weight+"身高" +zhubajie.hight);
    ??????System.out.println(zhubajie.head+zhubajie.mouth+zhubajie.ear);
    ??????zhubajie.speak("師傅,咱們別去西天了,改去月宮吧");
    ????}
    }


    一個簡單的Java小應用程序(Java Applet)
    import java.applet.*;?
    import java.awt.*;
    public class boy extends Applet
    {??
    ????public void paint(Graphics g)
    ????{
    ???? g.setColor(Color.red);???
    ???? g.drawString("我一邊喝著咖啡,一邊學Java呢",2,30);
    ???? g.setColor(Color.blue);
    ???? g.drawString("我學得很認真",10,50);
    ????}
    }????


    基本數據類型和數組
    例子1
    public class??Example2_1?
    {??
    ????public static void main (String args[ ])
    ????{
    ???? char chinaWord='你',japanWord='ぁ';
    ???? int??p1=20328,p2=12358;
    ???? System.out.println("漢字'你'字在unicode表中的順序位置:"+(int)chinaWord);
    ???? System.out.println("日語'ぁ'字在unicode表中的順序位置:"+(int)japanWord);?
    ???? System.out.println("unicode表中第20328位置上的字符是:"+(char)p1);
    ???? System.out.println("unicode表中第12358位置上的字符是:"+(char)p2);?
    ????}
    }

    例子2?
    public class Example2_2
    {?
    ????public static void main (String args[ ])?
    ????{?
    ??????byte??a=120;
    ??????short b=255;
    ??????int c=2200;?
    ??????long d=8000;
    ??????float f;
    ??????double g=123456789.123456789;
    ??????b=a;
    ??????c=(int)d;
    ??????f=(float)g;?? //導致精度的損失.
    ??????System.out.print("a=??"+a);???
    ??????System.out.println(" b=??"+b);?
    ??????System.out.print("??c=??"+c);????
    ??????System.out.println("??d=??"+d);?
    ??????System.out.println("f=??"+f);??
    ??????System.out.println("g=??"+g);?
    ????}
    }

    例子3?
    public class Example2_3
    {?
    ????public static void main(String args[])
    ????{
    ???? int a[]={1,2,3,4};
    ???? int b[];
    ???? System.out.println(a[3]);
    ???? b=a;
    ???? b[3]=100;
    ???? System.out.println(a[3]);
    ???? System.out.println(b[3]);
    ????}
    }??
    運行結果:
    4
    100
    100


    運算符、表達式和語句
    例子1
    class Example3_1
    {??
    ????public static void main(String args[])
    ????{
    ??????char a1='十',a2='點',a3='進',a4='攻';
    ??????char secret='8';
    ??????a1=(char)(a1^secret);???
    ??????a2=(char)(a2^secret);
    ??????a3=(char)(a3^secret);???
    ??????a4=(char)(a4^secret);
    ??????System.out.println("密文:"+a1+a2+a3+a4);
    ??????a1=(char)(a1^secret);???
    ??????a2=(char)(a2^secret);
    ??????a3=(char)(a3^secret);??
    ??????a4=(char)(a4^secret);
    ??????System.out.println("原文:"+a1+a2+a3+a4);
    ????}
    }

    PS:^為按位異或,按位異或兩次即變為原來。

    例子2
    class Example3_2
    {
    ????public static void main(String args[])
    ????{
    ??????float x=12.56f,y;
    ??????if(x<=0)
    ??????{
    ????????y=x+1;
    ??????}
    ??????else if(x>0&&x<=16)
    ??????{
    ????????y=2*x+1;
    ??????}
    ??????else?
    ??????{
    ???????? y=3*x+3;
    ??????}
    ??????System.out.println(y);
    ????}
    }

    例子3
    import java.applet.*;
    import java.awt.*;
    public class Example3_3 extends Applet
    {?
    ????public void paint(Graphics g)
    ????{?
    ??????int x=2,y=1;
    ??????switch(x+y)
    ????????{
    ???????? case 1 :
    ???????????? g.setColor(Color.red);g.drawString("i am 1",5,10);
    ???????????? break;????
    ???????? case 2:
    ???????????? g.setColor(Color.blue); g.drawString("i am 2",5,10);
    ???????????? break;???
    ???????? case 3:???
    ????????????g.setColor(Color.green); g.drawString("i am 3",5,10);
    ????????????break;?????
    ???????? default:??g.drawString("沒有般配的",5,10);
    ????????}
    ????}
    }??

    例子4
    import java.applet.*;
    import java.awt.*;
    public class Example3_4??extends Applet
    {??
    ????public void paint(Graphics g)
    ????{??
    ??????int sum=0;?
    ??????for(int i=1;i<=100;i++)
    ????????{?
    ?????????? sum=sum+i;
    ????????}
    ??????g.drawString("sum= "+sum,10,20);
    ????}
    }

    例子5
    class Example3_5?
    {???
    ????public static void main(String args[])
    ????{ double sum=0,a=1;int i=1;
    ??????while(i<=20)
    ????????{
    ??????????a=a*(1.0/i);
    ??????????sum=sum+a;
    ??????????i=i+1;??????????
    ????????}
    ??????System.out.println("sum="+sum);
    ????}
    }

    例子 6
    class Example3_6
    {??
    ????public static void main(String args[])?
    ????{?
    ?????? int sum=0,i,j;
    ?????? for( i=1;i<=10;i++)??????????????????//計算1+3+5+7+9。
    ?????? {??if(i%2==0)?
    ??????????{
    ???????????? continue;??
    ??????????}?
    ??????????else
    ???????????? {}?
    ??????????sum=sum+i;
    ?????? }
    ?????? System.out.println("sum="+sum);
    ????}
    }

    例子 7
    class Example3_7?
    {???
    ????public static void main(String args[])
    ????{?
    ??????int n=23,start,end,middle;
    ??????int a[]={-2,1,4,5,8,12,17,23,45,56,90,100};
    ??????start=0;
    ??????end=a.length;
    ??????middle=(start+end)/2;
    ??????int count=0;
    ??????while(n!=a[middle])
    ????????{
    ??????????if(n>a[middle])
    ????????????{
    ??????????????start=middle;
    ????????????}
    ??????????else if(n<a[middle])
    ????????????{
    ??????????????end=middle;
    ????????????}
    ??????????middle=(start+end)/2;
    ??????????count++;
    ??????????if(count>a.length/2)
    ???????????? break;
    ???????? }
    ??????if(count>a.length/2)
    ???????? System.out.println(":"+n+"不在數組中");
    ??????else
    ???????? System.out.println(":"+n+"是數組中的第"+middle+"個元素");?
    ????}
    }


    類、對象、和接口
    例子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(String args[])
    ????{?
    ?????? XiyoujiRenwu??zhubajie;?????? //聲明對象。
    ?????? zhubajie=new??XiyoujiRenwu(); //為對象分配內存,使用new 運算符和默認的構造方法。
    ????}
    }

    例子2
    class 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和p2。
    ??????p1=new Point(10,10);???????? //為對象分配內存,使用 new 和類中的構造方法。
    ??????p2=new Point(23,35);????????//為對象分配內存,使用 new 和類中的構造方法。
    ????}
    }

    例子3
    class XiyoujiRenwu
    {?
    ????float height,weight;
    ????String head, ear,hand,foot,mouth;
    ????void speak(String s)
    ????{?
    ?????? head="歪著頭";
    ?????? System.out.println(s);
    ????}
    }
    public class Example
    {??
    ????public static void main(String args[])?
    ????{?
    ???????? XiyoujiRenwu??zhubajie,sunwukong;//聲明對象。
    ???????? zhubajie=new??XiyoujiRenwu();??//為對象分配內存,使用new 運算符和默認的構造方法。
    ???????? sunwukong=new??XiyoujiRenwu();
    ???????? zhubajie.height=1.80f;????????????????????????????????//對象給自己的變量賦值。
    ???????? zhubajie.weight=160f;??????
    ???????? zhubajie.hand="兩只黑手";
    ???????? zhubajie.foot="兩只大腳";?
    ???????? zhubajie.head="大頭";?
    ???????? zhubajie.ear="一雙大耳朵";?
    ???????? zhubajie.mouth="一只大嘴";?
    ???????? sunwukong.height=1.62f;????????????????????????????????//對象給自己的變量賦值。
    ???????? sunwukong.weight=1000f;????
    ???????? sunwukong.hand="白嫩小手";
    ???????? sunwukong.foot="兩只繡腳";?
    ???????? sunwukong.head="繡發飄飄";?
    ???????? sunwukong.ear="一對小耳";??
    ???????? sunwukong.mouth="櫻桃小嘴";
    ???????? System.out.println("zhubajie的身高:"+zhubajie.height);
    ???????? System.out.println("zhubajie的頭:"+zhubajie.head);
    ???????? System.out.println("sunwukong的重量:"+sunwukong.weight);
    ???????? System.out.println("sunwukong的頭:"+sunwukong.head);
    ???????? zhubajie.speak("俺老豬我想娶媳婦");?????????????????????? //對象調用方法。
    ???????? System.out.println("zhubajie現在的頭:"+zhubajie.head);
    ???????? sunwukong.speak("老孫我重1000斤,我想騙八戒背我");???????? //對象調用方法。
    ???????? System.out.println("sunwukong現在的頭:"+sunwukong.head);
    ????}
    }


    例子4
    class 圓?
    {??
    ????double 半徑;
    ????圓(double r)
    ????{?
    ?????? 半徑=r;
    ???? }
    ????double 計算面積()?
    ????{??
    ?????? 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 Example?
    {??
    ????public static void main(String args[])
    ????{?
    ?????? 圓 circle=new 圓(10);
    ?????? 圓錐 circular=new 圓錐(circle,20);
    ?????? System.out.println("圓錐底圓半徑:"+circular.獲取底圓半徑());?
    ?????? System.out.println("圓錐的體積:"+circular.計算體積());
    ?????? circular.修改底圓半徑(100);
    ?????? System.out.println("圓錐底圓半徑:"+circular.獲取底圓半徑());
    ?????? System.out.println("圓錐的體積:"+circular.計算體積());??
    ????}
    }


    例子5
    class 梯形?
    {???
    ????float 上底,高;
    ????static float 下底;????????????????//類變量。
    ????梯形(float 上底,float 高)?
    ????{?
    ?????? this.上底=上底;
    ?????? this.高=高;
    ????}
    ????float 獲取上底()
    ????{??
    ?????? return 上底;
    ????}
    ????float 獲取下底()
    ????{??
    ?????? return 下底;
    ????}
    }

    class Example4_5?
    {???
    ????public static void main(String args[])?
    ????{?
    ?????? 梯形 laderOne,laderTwo;????????????????????//梯形的字節碼被加載到內存。
    ?????? 梯形.下底=60;??????????????????????????????//通過類名操作類變量。
    ?????? laderOne=new 梯形(18.0f,20);
    ?????? laderTwo=new 梯形(9.0f,10);
    ?????? System.out.println("laderOne的上底:"+laderOne.獲取上底());
    ?????? System.out.println("laderOne的下底:"+laderOne.獲取下底());
    ?????? System.out.println("laderTwo的上底:"+laderTwo.獲取上底());
    ?????? System.out.println("laderTwo的下底:"+laderTwo.獲取下底());
    ????}?
    }

    例子6
    package tom.jiafei;
    public class??Example4_6?
    {??
    ????public static void main(String args[])
    ????{
    ?????? System.out.println("我有包名");??
    ????}
    }


    例子7
    import java.applet.Applet;
    import java.awt.*;
    public class Example extends Applet?
    {??
    ????Button redbutton;
    ????public void init()
    ????{???
    ???????? redbutton=new Button("我是一個紅色的按鈕");?
    ???????? redbutton.setBackground(Color.red);
    ???????? add(redbutton);
    ????}
    ????public void??paint(Graphics g)?
    ????{??
    ???????? g.drawString("it is a button",30,50);
    ????}
    }


    例子8
    import tom.jiafei.*;
    class Example4_8?
    {??
    ????public static void main(String args[])
    ????{??
    ????????Trangle trangle=new Trangle(12,3,1);
    ??????????????trangle.計算面積();
    ??????????????trangle.修改三邊(3,4,5);
    ??????????????trangle.計算面積();
    ????}
    }


    例子9
    class Example4_9?
    {???
    ????private int money;
    ????Example4_9()?
    ????{??
    ????????money=2000;
    ????}?
    ????private int getMoney()?
    ????{?
    ?????? return money;
    ????}
    ????public static void main(String args[])?
    ????{?
    ?????? Example??exa=new Example();
    ?????? exa.money=3000;
    ?????? int m=exa.getMoney();
    ?????? System.out.println("money="+m);
    ????}
    }


    例子10
    class Father?
    {??
    ????private int money;
    ????int weight=100;
    ????String speak(String s)?
    ????{?
    ??????return s ;
    ????}
    }
    class Son extends Father?
    {???
    ????String hand ;
    ????void f()
    ????{
    ??????weight=200;
    ??????System.out.println(weight);
    ????}
    }?
    class Suizi extends Son
    {
    ????String foot ;
    }
    public class Example4_10?
    {??
    ????public static void main(String args[])?
    ????{?
    ?????? Son son=new Son();
    ?????? Suizi sunzi=new Suizi();
    ?????? son.hand="兩只手 ";
    ?????? sunzi.hand="兩小只手 ";
    ?????? sunzi.foot="兩只腳 ";
    ?????? System.out.println(son.hand);
    ?????? son.f();
    ?????? System.out.println(sunzi.weight+":"+sunzi.hand+":"+sunzi.foot);
    ?????? System.out.println(sunzi.speak("我是孫子"));
    ????}
    }????


    例子11
    Father.java:
    package tom.jiafei;
    public class??Father?
    {?
    ????int??height;
    ????protected int money=120;
    ????public?? int weight;
    ????protected int getMoney()?
    ????{??
    ??????return money;
    ????}
    ????void setMoney(int newMoney)?
    ????{??
    ??????money=newMoney;
    ????}?
    }


    Jerry.java:
    package sun.com;
    import tom.jiafei.Father;
    public class Jerry extends Father??????????//Jerry和Father在不同的包中.
    {??
    ????void f()
    ????{
    ??????money=1000;??????????????????????????//合法,
    ??????//height=1.89f;????????????????????????//非法,因為Jerry沒有繼承友好的height
    ??????System.out.println(money);??????????????//輸出結果是1000。
    ??????//setMoney(300);??????????????????????//非法,因為Jerry沒有繼承友好的方法setMoney。
    ???? int number=getMoney();????????????????//合法.
    ??????System.out.println(number);????????????//輸出結果是1000。
    ????}
    ????public static void main(String args[])?
    ????{??
    ?????? Jerry??jerry=new Jerry();
    ?????? jerry.f();
    ????}
    }


    例子
    protected的進一步說明
    A.java:
    package tom.jiafei;
    public class??A
    {?
    ????protected int x=120;
    ????protected void fA()?
    ????{??
    ??????System.out.println("我是A類中的protected方法");
    ??????System.out.println("x="+x);
    ????}
    }
    B.java:
    package sun.com;
    import tom.jiafei.A;
    public class B extends A
    {
    ????protected void fB()?
    ????{??
    ??????System.out.println("我是B類中自己定義的方法");
    ????}
    ????public static void main(String args[])
    ????{
    ??????B b=new B(); //對象b在B類中.
    ??????b.x=1000;??//合法.?
    ??????b.fA();????//合法.
    ??????b.fB();????//合法.
    ????}
    }


    DL.java:
    package sun.com;
    import sun.com.B;
    public class DL
    {
    ????public static void main(String args[])
    ????{
    ?????? B b=new B(); //對象b在DL類中.
    ?????? b.x=1000;??//非法,因為對象b的成員變量x是從A類繼承的,但DL和A不在同一包中.?
    ?????? b.fA();????//非法.因為方法fA是B類從A類繼承的protected方法,但DL和A不在同一包中.
    ?????? b.fB();????//合法,因為方法fB是B類中自己定義的protected方法, DL類和B類在同一包中.
    ????}
    }


    Example.java
    package tom.jiafei;
    import sun.com.B;
    public class Example
    {
    ????public static void main(String args[])
    ????{
    ?????? B b=new B(); //對象b在Example類中.
    ?????? b.x=1000;??//合法,因為對象b的成員變量x是從A類繼承的,而Example和A在 同一包中.
    ?????? b.fA(); //合法.因為方法fA是B類從A類繼承的protected方法,而Example和A在同一包中.
    ?????? b.fB(); //非法,因為方法fB是B類中自己定義的protected方法,但 Example類和B類不在同一?
    ???????????? // 包中.
    ????}
    }


    例子12
    import java.applet.*;
    import java.awt.*;
    class A
    {??
    ????private int number=100;
    ????float f(int x,int y)?
    ????{??
    ?????? return x+y;
    ????}
    ????float g(float x,float y)?
    ????{?
    ?????? return x+y+number;
    ????}
    }
    class B extends A
    {?
    ????float f(int x,int y)?
    ????{?
    ?????? return x*y;
    ????}??
    }
    public class Example?
    {??
    ????public static void main(String args[])?
    ????{??
    ??????B b=new B();
    ??????System.out.println(b.f(2,5));?? //調用重寫的方法。
    ??????System.out.println(b.g(2,5));?? //b調用繼承的父類的方法。
    ????}?
    }?


    例子13
    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(String s)?
    ????{?
    ?????? System.out.println("**"+s+"**");?
    ????}??
    }
    class Example
    {??public static void main(String args[])?
    ????{??
    ?????? 類人猿 monkey=new People();?? //monkey是People對象的上轉型對象。
    ?????? monkey.crySpeak("I love this game");
    ?????? //monkey.n=23;????????????????//非法,因為子類未繼承n.
    ?????? //monkey.computer(12,19);???? //非法,computer是子類新增的功能.
    ?????? People people=(People)monkey; //把上轉型對象強制轉化為子類的對象。
    ?????? people.computer(10,10);
    ????}
    }


    例子14
    class??動物?
    {??void cry()?
    ????{
    ????}
    }
    class 狗 extends 動物 {?
    {??void cry()?
    ????{??System.out.println("汪汪.....");?
    ????}??
    }
    class 貓 extends 動物?
    {??void cry()?
    ????{??System.out.println("喵喵.....");?
    ????}??
    }
    class Example4_14?
    {??public static void main(String args[])?
    ????{??動物 dongwu;
    ?????? if(Math.random()>=0.5)?????
    ??????????{
    ????????????dongwu=new 狗();
    ????????????dongwu.cry();?
    ??????????}
    ?????? else
    ???????? {
    ?????????? dongwu=new 貓();
    ????????????ongwu.cry();
    ??????????}
    ????}
    }


    例子15
    abstract class 圖形?
    {??
    ????public 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 換底(圖形 底)?
    ????{
    ????????this.底=底;
    ????}
    ????public double 求體積()?
    ????{??
    ?????? return (底.求面積()*高)/3.0;
    ????}
    }
    public class Example4_15
    {?
    ????public static void main(String args[])?
    ????{?
    ?????? 堆 zui;
    ?????? 圖形 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的圓的面積"+tuxing.求面積());
    ?????? zui.換底(tuxing);
    ?????? System.out.println("圓形底的堆的體積"+zui.求體積());
    ????}
    }


    例子16
    class Student?
    {??
    ????int number;String name;
    ????Student(int number,String name)?
    ????{??
    ????????this.number=number;
    ????????this.name=name;
    ????????System.out.println("I am "+name+ "my number is "+number);
    ????}
    }
    class Univer_Student extends Student?
    {??
    ????boolean 婚否;
    ????Univer_Student(int number,String name,boolean b)?
    ????{
    ?????? super(number,name);
    ?????? 婚否=b;
    ?????? System.out.println("婚否="+婚否);
    ????}
    }
    public class Example4_16?
    {?
    ????public static void main(String args[])?
    ????{??
    ??????Univer_Student zhang=new Univer_Student(9901,"和曉林",false);
    ????}
    }


    例子17
    class Sum?
    {??
    ????int n;
    ????float f()?
    ????{??
    ?????? float sum=0;
    ?????? for(int i=1;i<=n;i++)
    ?????????? sum=sum+i;
    ?????????? return 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 Example4_17
    {??
    ????public static void main(String args[])?
    ????{??
    ?????? Average aver=new Average();
    ?????? aver.n=100;
    ?????? float result_1=aver.f();
    ?????? float result_2=aver.g();
    ?????? System.out.println("result_1="+result_1);
    ?????? System.out.println("result_2="+result_2);
    ????}
    }


    例子18
    import java.applet.*;
    import java.awt.*;
    interface Computable?
    {??
    ????final int MAX=100;
    ????void speak(String s);
    ????int f(int x);
    ????float g(float x,float y);
    }
    class China implements Computable?
    {?
    ????int xuehao;
    ????public??int f(int x)?? //不要忘記public關鍵字。
    ????{?
    ?????? int sum=0;
    ?????? for(int i=1;i<=x;i++)
    ??????????{?
    ????????????sum=sum+i;
    ??????????}
    ?????? return sum;
    ????}
    ????public float g(float x,float y)
    ????{??
    ???????? return 6;?????????????????? //至少有return語句。??
    ????}
    ????public void speak(String s)?
    ????{?
    ????}
    }
    class Japan implements Computable?
    {?
    ????int xuehao;
    ????public int f(int x)?
    ????{??
    ????????return 68;?
    ????}
    ????public??float g(float x,float y)?
    ????{?
    ????????return x+y;
    ????}
    public void speak(String s)?
    ????{????????????????????????????//必須有方法體,但體內可以沒有任何語句。
    ????}
    }
    public class Example4_18??extends Applet?
    {?
    ????China Li;
    ????Japan Henlu;
    ????public void init()?
    ????{??
    ?????? Li=new China();???
    ?????? Henlu=new Japan();??
    ?????? Li.xuehao=991898;
    ?????? Henlu.xuehao=941448;
    ????}
    ????public void paint(Graphics g)?
    ????{?
    ?????? g.drawString("xuehao:"+Li.MAX+Li.xuehao+"從1到100求和"+Li.f(100),10,20);
    ?????? g.drawString("xuehao:"+Henlu.MAX+Henlu.xuehao+"加法"+Henlu.g(2.0f,3.0f),10,40);
    ????}
    }???


    例子19
    interface??收費?
    {?
    ????public void??收取費用();
    }
    interface??調節溫度?
    {?
    ????public void??controlTemperature();
    }
    class 公共汽車 implements 收費?
    {??
    ????public??void 收取費用()?
    ????{??
    ?????? System.out.println("公共汽車:一元/張,不計算公里數");
    ????}
    }?
    class 出租車 implements 收費, 調節溫度
    {??
    ????public void 收取費用()?
    ????{??
    ?????? System.out.println("出租車:1.60元/公里,起價3公里");
    ????}
    ????public void??controlTemperature()
    ????{??
    ?????? System.out.println("安裝了Hair空調");
    ????}
    }
    class 電影院 implements 收費,調節溫度
    {??
    ????public void 收取費用()
    ????{?
    ????????System.out.println("電影院:門票,十元/張");
    ????}
    ????public void??controlTemperature()
    ????{??
    ????????System.out.println("安裝了中央空調");
    ????}
    }
    class Example4_19
    {?
    ????public static void main(String args[])
    ????{??
    ?????? 公共汽車 七路=new 公共汽車();
    ?????? 出租車?? 天宇=new 出租車();
    ?????? 電影院?? 紅星=new 電影院();
    ?????? 七路.收取費用();
    ?????? 天宇.收取費用();
    ?????? 紅星.收取費用();
    ?????? 天宇.controlTemperature();
    ?????? 紅星.controlTemperature();
    ????}
    }


    例子20
    interface??ShowMessage?
    {??
    ????void 顯示商標(String s);
    }
    class TV implements ShowMessage?
    {??
    ????public void 顯示商標(String s)?
    ????{?
    ?????? System.out.println(s);
    ????}
    }
    class PC implements ShowMessage?
    {?
    ????public void 顯示商標(String s)?
    ????{??
    ????????System.out.println(s);
    ????}
    }
    public class Example4_20?
    {??
    ????public static void main(String args[])
    ????{?
    ?????? ShowMessage sm;??????????????????//聲明接口變量。
    ?????? sm=new TV();???????????????????? //接口變量中存放對象的引用。
    ?????? sm.顯示商標("長城牌電視機");??????//接口回調。
    ?????? sm=new PC();???????????????????? //接口變量中存放對象的引用。
    ?????? sm.顯示商標("聯想奔月5008PC機"); //接口回調。
    ????}?
    }


    例子21
    interface??Computerable
    {?
    ????public??double 求面積();
    }
    class 梯形 implements Computerable?
    {?
    ????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 圓形 implements Computerable?
    {??
    ????double r;
    ????圓形(double r)?
    ????{??
    ?????? this.r=r;
    ????}
    ????public double 求面積()?
    ????{
    ?????? return(3.14*r*r);
    ????}
    }
    class 堆?
    {?
    ????Computerable 底;?????????? //聲明一個接口變量,可以回調"求面積"方法。
    ????double 高;
    ????堆(Computerable 底,double 高)?
    ????{??
    ?????? this.底=底;
    ?????? this.高=高;
    ????}
    ????void 換底(Computerable 底)?
    ????{??
    ????????this.底=底;
    ????}
    ???? public double 求體積()
    ????{??
    ??????return (底.求面積()*高)/3.0;
    ????}
    }
    public class Example4_21
    {??
    ????public static void main(String args[])
    ????{??
    ?????? 堆 zui;
    ?????? Computerable bottom;
    ?????? bottom=new 梯形(2.0,7.0,10.7); //接口變量中存放對象的引用。
    ?????? System.out.println("梯形的面積"+bottom.求面積()); //bottom接口回調,求面積。
    ?????? zui=new??堆(bottom,30);
    ?????? System.out.println("梯形底的堆的體積"+zui.求體積());
    ?????? bottom=new 圓形(10);??//接口變量中存放對象的引用。
    ?????? System.out.println("半徑是10的圓的面積"+bottom.求面積());
    ?????? zui.換底(bottom);
    ?????? System.out.println("圓形底的堆的體積"+zui.求體積());
    ????}
    }


    例子22
    public class Example4_22
    {??
    ????public static void main(String args[])?
    ????{??
    ??????int n=0,m=0,t=0;
    ??????try
    ???????? {??
    ????????????t=9999;
    ????????????m=Integer.parseInt("8888");
    ????????????n=Integer.parseInt("12s3a");????//發生異常,轉向catch。
    ????????????System.out.println("我沒有機會輸出");
    ???????? }
    ??????catch(Exception e)
    ???????? {
    ?????????? System.out.println("發生異常");
    ?????????? n=123;
    ???????? }
    ??????System.out.println("n="+n+",m="+m+",t="+t);
    ????}
    }



    例子23
    class MyException extends Exception
    {
    ????String message;
    ????MyException()
    ????{
    ????message="數字不是正數";
    ????}
    ???? public String toString()
    ????{
    ??????return message;
    ????}
    }
    class YourException extends Exception
    {
    ????String message;
    ????YourException()
    ????{
    ????message="數字不是偶數";
    ????}
    public String toString()
    ????{
    ??????return message;
    ????}
    }
    class A
    {
    ????public void f(int n) throws MyException,YourException
    ????{
    ??????if(n<0)
    ????????{
    ??????????throw(new??MyException());???????????? //拋出異常,結束方法的執行。
    ????????}
    ??????if(n%2!=0)
    ????????{
    ??????????throw(new??YourException());?????????? //拋出異常,,結束方法的執行。
    ????????}
    ??????double number=Math.sqrt(n);
    ??????System.out.println(number);
    ????}
    public static void main(String args[])
    ????{
    ??????A a=new A();
    ???? try?
    ???????? {
    ?????????? a.f(9);
    ???????? }
    ??????catch(MyException e)
    ???????? {
    ????????????System.out.println(e.toString());
    ???????? }
    ??????catch(YourException e)
    ???????? {
    ????????????System.out.println(e.toString());
    ???????? }
    ?????? try?
    ???????? {
    ?????????? a.f(-8);
    ???????? }
    ??????catch(MyException e)
    ???????? {
    ????????????System.out.println(e.toString());
    ???????? }
    ??????catch(YourException e)
    ???????? {
    ????????????System.out.println(e.toString());
    ???????? }
    ?????? try?
    ???????? {
    ?????????? a.f(16);
    ???????? }
    ??????catch(MyException e)
    ???????? {
    ????????????System.out.println(e.toString());
    ???????? }
    ?????? catch(YourException e)
    ???????? {
    ????????????System.out.println(e.toString());
    ???????? }
    ????}
    }



    常用實用類
    例子1
    class Example5_1
    {
    ????public static void main(String args[])
    ????{
    ?????? String s1,s2;
    ?????? s1=new String("we are students");
    ?????? s2=new String("we are students");
    ?????? System.out.println(s1.equals(s2));????//輸出結果是:true。
    ?????? System.out.println(s1==s2);???????? //輸出結果是:false
    ?????? String s3,s4;?
    ?????? s3="how are you";
    ?????? s4="how are you";?
    ?????? System.out.println(s3.equals(s4));????//輸出結果是:true。
    ?????? System.out.println(s3==s4);???????? //輸出結果是:true。?????
    ????}
    }


    例子2
    class Example5_2?
    {?? public static void main(String args[])
    ????{??int number=0;?
    ?????? String s="student;entropy;engage,english,client";
    ?????? for(int k=0;k<s.length();k++)
    ????????{??if(s.regionMatches(k,"en",0,2))
    ???????????? {??number++;
    ???????????? }
    ???????? }?
    ?????? System.out.println("number="+number);
    ????}
    }


    例子3
    class Example5_3?
    {??public static void main(String args[])?
    ????{??String a[]={"boy","apple","Applet","girl","Hat"};
    ?????? for(int i=0;i<a.length-1;i++)?
    ??????????{for(int j=i+1;j<a.length;j++)?
    ????????????{??if(a[j].compareTo(a[i])<0)?
    ?????????????? {??String temp=a[i];
    ?????????????????? a[i]=a[j];
    ?????????????????? a[j]=temp;
    ????????????????}
    ????????????}?
    ???????? }
    ??????for(int i=0;i<a.length;i++)?
    ???????? {??System.out.print("??"+a[i]);
    ???????? }
    ????}
    }

    例子4
    public class Example5_4
    {??public static void main(String args[])?
    ????{??double n,sum=0.0 ;
    ?????? for(int i=0;i<args.length;i++)?
    ????????{??sum=sum+Double.parseDouble(args[i]);
    ????????}
    ??????n=sum/args.length;
    ??????System.out.println("平均數:"+n);
    ????}
    }

    例子5
    import java.util.Date;?
    import java.awt.*;
    public class Example5_5
    {??
    ????public static void main(String args[])
    ????{?
    ??????Date date=new Date();
    ??????Button button=new Button("確定");
    ??????System.out.println(date.toString());
    ??????System.out.println(button.toString());??
    ????}
    }

    例子6
    class Example5_6
    {???
    ????public static void main(String args[])
    ????{??
    ?????? char c[],d[];
    ?????? String s=”巴西足球隊擊敗德國足球隊”;
    ?????? c=new char[2];
    ?????? s.getChars(5,7,c,0);
    ?????? System.out.println&copy;;
    ?????? d=new char[s.length()];
    ?????? s.getChars(7,12,d,0);
    ?????? s.getChars(5,7,d,5);
    ?????? s.getChars(0,5,d,7);
    ?????? System.out.println(d);
    ????}
    }

    例子7
    class Example5_7
    {??
    ????public static void main(String args[])?
    ????{?
    ?????? String s="列車時刻表";
    ?????? char a[]=s.toCharArray();
    ?????? for(int i=0;i<a.length;i++)
    ???????? {??a[i]=(char)(a[i]^'t');
    ???????? }
    ?????? String secret=new String(a);?
    ??????System.out.println("密文:"+secret);
    ??????for(int i=0;i<a.length;i++)
    ????????{??
    ????????????a[i]=(char)(a[i]^'t');
    ????????}
    ????String code=new String(a);??
    ????System.out.println("原文:"+code);
    ????}
    }

    例子8
    public class Example5_8
    {?
    ????public static void main(String args[])
    ????{??
    ?????? byte d[]="你我他".getBytes();???????????
    ?????? System.out.println("數組d的長度是(一個漢字占兩個字節):"+d.length);
    ?????? String s=new String(d,0,2);
    ?????? System.out.println(s);
    ????}
    }

    例子9
    class Example5_9
    {
    ????public static void main(String args[])
    ????{
    ??????StringBuffer str=new StringBuffer();
    ?????? str.append("大家好");
    ?????? System.out.println("str:"+str);
    ??????System.out.println("length:"+str.length());
    ?????? System.out.println("capacity:"+str.capacity());?
    ?????? str.append("我們大家都很愿意學習Java語言");
    ?????? System.out.println("str:"+str);
    ?????? System.out.println("length:"+str.length());
    ??????System.out.println("capacity:"+str.capacity());?
    ?????? StringBuffer sb=new StringBuffer("Hello");
    ?????? System.out.println("length:"+sb.length());
    ?????? System.out.println("capacity:"+sb.capacity());?
    ????}
    }

    例子10
    class Example5_10
    {
    ????public static void main(String args[])
    ????{
    ?????? StringBuffer str=new StringBuffer("我們大家都很愿意學習Java語言");
    ?????? str.setCharAt(0 ,'w');?
    ?????? str.setCharAt(1 ,'e');
    ?????? System.out.println(str);?
    ?????? str.insert(2, " all");
    ?????? System.out.println(str);
    ?????? str.delete(6,8);
    ?????? System.out.println(str);
    ?????? int index=str.indexOf("都");
    ?????? str.replace(index,str.length()," love java");
    ?????? System.out.println(str);
    ????}
    }

    例子11
    import java.util.*;
    public class Example5_11?
    {??
    ????public static void main(String args[])
    ????{??
    ?????? String s="we are stud,ents";
    ?????? StringTokenizer fenxi=new StringTokenizer(s," ,"); //空格和逗號做分
    ?????? int number=fenxi.countTokens();
    ?????? while(fenxi.hasMoreTokens())?
    ????????{?
    ?????????? String str=fenxi.nextToken();
    ?????????? System.out.println(str);
    ?????????? System.out.println("還剩"+fenxi.countTokens()+"個單詞");
    ????????}
    ??????System.out.println("s共有單詞:"+number+"個");
    ????}?
    }

    例子12
    import java.util.*;
    public class Example5_12
    {??public static void main(String args[])?
    ?? {??String s=new String("abcABC123");
    ??????System.out.println(s);????
    ??????char a[]=s.toCharArray();
    ??????for(int i=0;i<a.length;i++)
    ?????? { if(Character.isLowerCase(a[i]))?
    ??????????{ a[i]=Character.toUpperCase(a[i]);
    ??????????}
    ?????? else if(Character.isUpperCase(a[i]))?
    ??????????{ a[i]=Character.toLowerCase(a[i]);
    ??????????}
    ?????? }
    ???? s=new String(a);
    ???? System.out.println(s);??????
    ?? }?
    }

    例子13
    import java.util.Date;
    import java.text.SimpleDateFormat;
    class Example5_13
    {??
    ????public static void main(String args[])
    ????{?
    ??????Date nowTime=new Date();
    ??????System.out.println("現在的時間:"+nowTime);
    ??????SimpleDateFormat matter1=new SimpleDateFormat("yyyy年MM月dd日 北京時間");
    ??????System.out.println("現在的時間:"+matter1.format(nowTime));
    ??????SimpleDateFormat matter2=
    ??????new SimpleDateFormat("yyyy年MM月Edd日HH時mm分ss秒 北京時間");
    ??????System.out.println("現在的時間:"+matter2.format(nowTime));
    ??????SimpleDateFormat matter3=
    ??????new SimpleDateFormat("北京時間dd日HH時MMM ss秒mm分EE");
    ??????System.out.println("現在的時間:"+matter3.format(nowTime));
    ??????long time=-1800;
    ??????Date date=new Date(time);
    ??????System.out.println("-1800秒表示的日期時間是:"+date);
    ????}
    }

    例子14
    import java.util.*;
    class Example5_14
    {?
    ????public static void main(String args[])?
    ?? {??
    ??????Calendar calendar=Calendar.getInstance(); //創建一個日歷對象。
    ??????calendar.setTime(new Date());??????????//用當前時間初始化日歷時間。
    ??????String 年=String.valueOf(calendar.get(Calendar.YEAR)),
    ???????????? 月=String.valueOf(calendar.get(Calendar.MONTH)+1),
    ???????????? 日=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)),
    ???????????? 星期=String.valueOf(calendar.get(Calendar.DAY_OF_WEEK)-1);
    ??????int hour=calendar.get(Calendar.HOUR_OF_DAY),
    ??????????minute=calendar.get(Calendar.MINUTE),
    ??????????second=calendar.get(Calendar.SECOND);
    ??????System.out.println("現在的時間是:");
    ??????System.out.println(""+年+"年"+月+"月"+日+"日 "+ "星期"+星期);
    ??????System.out.println(""+hour+"時"+minute+"分"+second+"秒");
    ??????calendar.set(1962,5,29);??//將日歷翻到1962年6月29日,注意5表示六月。
    ??????long time1962=calendar.getTimeInMillis();
    ??????calendar.set(2003,9,5);??//將日歷翻到2003年10月5日。9表示十月。
    ??????long time2003=calendar.getTimeInMillis();
    ??????long 相隔天數=(time2003-time1962)/(1000*60*60*24);
    ??????System.out.println("2003年10月5日和1962年6月29日相隔"+相隔天數+"天");
    ?? }??
    }

    例子 15
    import java.util.*;?
    class Example5_15?
    {?
    ????public static void main(String args[])
    {?
    ?? System.out.println(" 日 一 二 三 四 五 六");
    ?????? Calendar 日歷=Calendar.getInstance();????????
    ?????? 日歷.set(2004,9,1);??//將日歷翻到2004年10月1日,注意0表示一月。
    ?????? //獲取1日是星期幾(get方法返回的值是1表示星期日,星期六返回的值是7):
    ?????? int 星期幾=日歷.get(Calendar.DAY_OF_WEEK)-1;
    ?????? String a[]=new String[星期幾+31];???????????? //存放號碼的一維數組
    ?????? for(int i=0;i<星期幾;i++)
    ???????????? {
    ????????????????a[i]="**";
    ???????????? }
    ?????? for(int i=星期幾,n=1;i<星期幾+31;i++)
    ???????????? {?
    ?????????????? if(n<=9)
    ??????????????????a[i]=String.valueOf(n)+" ";
    ?????????????? else
    ??????????????????a[i]=String.valueOf(n) ;
    ?????????????? n++;
    ???????????? }??
    ??????//打印數組:
    ??????for(int i=0;i<a.length;i++)
    ?????? {?
    ??????????if(i%7==0)
    ??????????{?
    ???????????? System.out.println("");??????//換行。
    ??????????}
    ???????? System.out.print(" "+a[i]);
    ??????}?
    ?? }?
    }

    例子 16
    import java.text.NumberFormat;
    class Example5_16
    {?
    ????public static void main(String args[])
    ????{??
    ??????double a=Math.sqrt(5);
    ??????System.out.println("格式化前:"+a);
    ??????NumberFormat f=NumberFormat.getInstance();
    ??????f.setMaximumFractionDigits(5);
    ??????f.setMinimumIntegerDigits(3);
    ??????String s=f.format(a);
    ??????System.out.println("格式化后:"+s);
    ??????System.out.println("得到的隨機數:");
    ??????int number=8;
    ??????for(int i=1;i<=20;i++)
    ?????? {?
    ???????? int randomNumber=(int)(Math.random()*number)+1;//產生1到8之間的隨機數。
    ???????? System.out.print(" "+randomNumber);
    ???????? if(i%10==0)
    ???????????? System.out.println("");
    ?????? }
    ????}?
    }

    例子17
    import java.util.*;
    class Example5_17
    {
    ????public static void main(String args[])
    ????{?
    ??????Vector vector=new Vector();?
    ??????for(int i=1;i<=18;i++)
    ??????{
    ????????vector.add(new Integer(i));?????? //向量填加18個整數對象.
    ??????}
    ??????int a[]=new int[vector.size()];
    ??????int i=0;
    ??????while(vector.size()>0)??????????????
    ??????{??
    ????????int number=(int)(Math.random()*vector.size());???
    ????????Integer integer=(Integer)vector.elementAt(number);
    ????????a[i]=integer.intValue();????????????????????//得到整數對象中的int數.
    ????????vector.removeElementAt(number);????????????//向量移掉number處的整數對象.
    ????????i++;
    ?????? }
    ??????for(i=0;i<18;i++)
    ??????{
    ???????? System.out.print(" "+a[i]);
    ?????? }
    ????}
    }

    例子18
    import java.util.*;
    public class Example5_18
    {
    ????public??static void main(String args[])
    ????{?
    ??????LinkedList mylist=new LinkedList();
    ??????mylist.add("is");?
    ??????mylist.add("a");
    ??????int number=mylist.size();
    ??????System.out.println("現在鏈表中有"+number+"個節點:");
    ??????for(int i=0;i<number;i++)
    ????????{
    ?????????? String temp=(String)mylist.get(i);
    ?????????? System.out.println("第"+i+"節點中的數據:"+temp);
    ????????}
    ???? mylist.addFirst("It");
    ???? mylist.addLast("door");

    資源:
    [下載文件]

    本文摘自 :https://blog.51cto.com/u

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码