一些简单得JAVA小程序.docx

上传人:b****7 文档编号:9191489 上传时间:2023-02-03 格式:DOCX 页数:35 大小:20.12KB
下载 相关 举报
一些简单得JAVA小程序.docx_第1页
第1页 / 共35页
一些简单得JAVA小程序.docx_第2页
第2页 / 共35页
一些简单得JAVA小程序.docx_第3页
第3页 / 共35页
一些简单得JAVA小程序.docx_第4页
第4页 / 共35页
一些简单得JAVA小程序.docx_第5页
第5页 / 共35页
点击查看更多>>
下载资源
资源描述

一些简单得JAVA小程序.docx

《一些简单得JAVA小程序.docx》由会员分享,可在线阅读,更多相关《一些简单得JAVA小程序.docx(35页珍藏版)》请在冰豆网上搜索。

一些简单得JAVA小程序.docx

一些简单得JAVA小程序

一个简单的Java应用程序

publicclassHello

{

    publicstaticvoidmain(Stringargs[])

    {

      "这是一个简单的应用程序");

    }

}

 

源程序

publicclassPeople

{

    floathight,weight;

    Stringhead,ear,mouth;

    voidspeak(Strings)

    {

      

    }

}

classA

{

    publicstaticvoidmain(Stringargs[])

    {

      Peoplezhubajie;

      zhubajie=newPeople();

      =200f;    

      =;

      ="大头";

      ="两只大耳朵";

      ="一只大嘴";

      "重量"++"身高"+;

      

      ("师傅,咱们别去西天了,改去月宫吧");

    }

}

 

一个简单的Java小应用程序(JavaApplet)

import.*;

import.*;

publicclassboyextendsApplet

{  

    publicvoidpaint(Graphicsg)

    {

    ;  

    ("我一边喝着咖啡,一边学Java呢",2,30);

    ;

    ("我学得很认真",10,50);

    }

}    

 

基本数据类型和数组

例子1

publicclass  Example2_1

{  

    publicstaticvoidmain(Stringargs[])

    {

    charchinaWord='你',japanWord='ぁ';

    int  p1=20328,p2=12358;

    "汉字\'你\'字在unicode表中的顺序位置:

"+(int)chinaWord);

    "日语\'ぁ\'字在unicode表中的顺序位置:

"+(int)japanWord);

    "unicode表中第20328位置上的字符是:

"+(char)p1);

    "unicode表中第12358位置上的字符是:

"+(char)p2);

    }

}

例子2

publicclassExample2_2

{

    publicstaticvoidmain(Stringargs[])

    {

      byte  a=120;

      shortb=255;

      intc=2200;

      longd=8000;

      floatf;

      doubleg=;

      b=a;

      c=(int)d;

      f=(float)g;        "a=  "+a);  

      "b=  "+b);

      "  c=  "+c);    

      "  d=  "+d);

      "f=  "+f);  

      "g=  "+g);

    }

}

例子3

publicclassExample2_3

{

    publicstaticvoidmain(Stringargs[])

    {

    inta[]={1,2,3,4};

    intb[];

    

    b=a;

    b[3]=100;

    

    

    }

}  

运行结果:

4

100

100

 

运算符、表达式和语句

例子1

classExample3_1

{  

    publicstaticvoidmain(Stringargs[])

    {

      chara1='十',a2='点',a3='进',a4='攻';

      charsecret='8';

      a1=(char)(a1^secret);  

      a2=(char)(a2^secret);

      a3=(char)(a3^secret);  

      a4=(char)(a4^secret);

      "密文:

"+a1+a2+a3+a4);

      a1=(char)(a1^secret);  

      a2=(char)(a2^secret);

      a3=(char)(a3^secret);  

      a4=(char)(a4^secret);

      "原文:

"+a1+a2+a3+a4);

    }

}

例子2

classExample3_2

{

    publicstaticvoidmain(Stringargs[])

    {

      floatx=,y;

      if(x<=0)

      {

        y=x+1;

      }

      elseif(x>0&&x<=16)

      {

        y=2*x+1;

      }

      else

      {

        y=3*x+3;

      }

      

    }

}

例子3

import.*;

import.*;

publicclassExample3_3extendsApplet

{

    publicvoidpaint(Graphicsg)

    {

      intx=2,y=1;

      switch(x+y)

        {

        case1:

            ;("iam1",5,10);

            break;    

        case2:

            ;("iam2",5,10);

            break;  

        case3:

  

            ;("iam3",5,10);

            break;    

        default:

  ("没有般配的",5,10);

        }

    }

}  

例子4

import.*;

import.*;

publicclassExample3_4  extendsApplet

{  

    publicvoidpaint(Graphicsg)

    {  

      intsum=0;

      for(inti=1;i<=100;i++)

        {

          sum=sum+i;

        }

      ("sum="+sum,10,20);

    }

}

例子5

classExample3_5

{  

    publicstaticvoidmain(Stringargs[])

    {doublesum=0,a=1;inti=1;

      while(i<=20)

        {

          a=a*i);

          sum=sum+a;

          i=i+1;          

        }

      "sum="+sum);

    }

}

例子6

classExample3_6

{  

    publicstaticvoidmain(Stringargs[])

    {

      intsum=0,i,j;

      for(i=1;i<=10;i++)                  圆=circle;

      this.高=h;

    }

    double计算体积()

  {  

      doublevolume;

      volume=底圆.计算面积()*高/;

      return  volume;

    }

    void修改底圆半径(doubler)

    {  

        底圆.修改半径(r);

    }

    double获取底圆半径()

    {  

        return底圆.获取半径();

    }

}

 

classExample

{  

    publicstaticvoidmain(Stringargs[])

    {

      圆circle=new圆(10);

      圆锥circular=new圆锥(circle,20);

      "圆锥底圆半径:

"+circular.获取底圆半径());

      "圆锥的体积:

"+circular.计算体积());

      circular.修改底圆半径(100);

      "圆锥底圆半径:

"+circular.获取底圆半径());

      "圆锥的体积:

"+circular.计算体积());  

    }

}

 

例子5

class梯形

{  

    float上底,高;

    staticfloat下底;                底=上底;

      this.高=高;

    }

    float获取上底()

    {  

      return上底;

    }

    float获取下底()

    {  

      return下底;

    }

}

classExample4_5

{  

    publicstaticvoidmain(Stringargs[])

    {

      梯形laderOne,laderTwo;                    底=60;                              取上底());

      "laderOne的下底:

"+laderOne.获取下底());

      "laderTwo的上底:

"+laderTwo.获取上底());

      "laderTwo的下底:

"+laderTwo.获取下底());

    }

}

例子6

package;

publicclass  Example4_6

{  

    publicstaticvoidmain(Stringargs[])

    {

      "我有包名");  

    }

}

 

例子7

import

import.*;

publicclassExampleextendsApplet

{  

    Buttonredbutton;

    publicvoidinit()

    {  

        redbutton=newButton("我是一个红色的按钮");

        ;

        add(redbutton);

    }

    publicvoid  paint(Graphicsg)

    {  

        ("itisabutton",30,50);

    }

}

 

例子8

import.*;

classExample4_8

{  

    publicstaticvoidmain(Stringargs[])

    {  

        Trangletrangle=newTrangle(12,3,1);

              trangle.计算面积();

              trangle.修改三边(3,4,5);

              trangle.计算面积();

    }

}

 

例子9

classExample4_9

{  

    privateintmoney;

    Example4_9()

    {  

        money=2000;

    }

    privateintgetMoney()

    {

      returnmoney;

    }

    publicstaticvoidmain(Stringargs[])

    {

      Example  exa=newExample();

      =3000;

      intm=();

      "money="+m);

    }

}

 

例子10

classFather

{  

    privateintmoney;

    intweight=100;

    Stringspeak(Strings)

    {

      returns;

    }

}

classSonextendsFather

{  

    Stringhand;

    voidf()

    {

      weight=200;

      

    }

}

classSuiziextendsSon

{

    Stringfoot;

}

publicclassExample4_10

{  

    publicstaticvoidmain(Stringargs[])

    {

      Sonson=newSon();

      Suizisunzi=newSuizi();

      ="两只手";

      ="两小只手";

      ="两只脚";

      

      ();

      ":

"++":

"+;

      "我是孙子"));

    }

}    

 

例子11

:

package;

publicclass  Father

{

    int  height;

    protectedintmoney=120;

    public  intweight;

    protectedintgetMoney()

    {  

      returnmoney;

    }

    voidsetMoney(intnewMoney)

    {  

      money=newMoney;

    }

}

 

:

package;

import

publicclassJerryextendsFather          {  

    voidf()

    {

      money=1000;                                                  =1000;  

      ();          ();        }

}

 

package;

import

publicclassDL

{

    publicstaticvoidmain(Stringargs[])

    {

      Bb=newB();      =1000;  

      ();    为方法fA是B类从A类继承的protected方法,但DL和A不在同一包中.

      ();        }

}

 

package;

import

publicclassExample

{

    publicstaticvoidmain(Stringargs[])

    {

      Bb=newB();      =1000;        ();为方法fA是B类从A类继承的protected方法,而Example和A在同一包中.

      ();    }

}

 

例子12

import.*;

import.*;

classA

{  

    privateintnumber=100;

    floatf(intx,inty)

    {  

      returnx+y;

    }

    floatg(floatx,floaty)

    {

      returnx+y+number;

    }

}

classBextendsA

{

    floatf(intx,inty)

    {

      returnx*y;

    }  

}

publicclassExample

{  

    publicstaticvoidmain(Stringargs[])

    {  

      Bb=newB();

                    Peoplepeople=(People)monkey;...");

    }  

}

class猫extends动物

{  voidcry()

    {  "喵喵.....");

    }  

}

classExample4_14

{  publicstaticvoidmain(Stringargs[])

    {  动物dongwu;

      if()>=    

          {

            dongwu=new狗();

            ();

          }

      else

        {

          dongwu=new猫();

            ();

          }

    }

}

 

例子15

abstractclass图形

{  

    publicabstractdouble求面积();

}

class梯形extends图形

{

    doublea,b,h;

    梯形(doublea,doubleb,doubleh)

    {  

      =a;=b;=h;

    }

    publicdouble求面积()

    {  

        return((1/*(a+b)*h);

    }

}

class圆形extends图形

{  

    doubler;

    圆形(doubler)

    {  

      =r;

    }

    publicdouble求面积()

    {  

      return*r*r);

    }

}

class堆

{  

    图形底;

    double高;

    堆(图形底,double高)

    {

        this.底=底;

        this.高=高;

    }

    void换底(图形底)

    {

        this.底=底;

    }

    publicdouble求体积()

    {  

      return(底.求面积()*高)/;

    }

}

publicclassExample4_15

{

    publicstaticvoidmain(Stringargs[])

    {

      堆zui;

      图形tuxing;

      tuxing=new梯形,,;

      "梯形的面积"+tuxing.求面积());

      zui=new  堆(tuxing,30);

      "梯形底的堆的体积"+zui.求体积());

      tuxing=new圆形(10);

      "半径是10的圆的面积"+tuxing.求面积());

      zui.换底(tuxing);

      "圆形底的堆的体积"+zui.求体积());

    }

}

 

例子16

classStudent

{  

    intnumber;Stringname;

    Student(intnumber,Stringname)

    {  

        =number;

        =name;

        "Iam"+name+"mynumberis"+number);

    }

}

classUniver_StudentextendsStudent

{  

    boolean婚否;

    Univer_Student(intnumber,Stringname,booleanb)

    {

      super(number,name);

      婚否=b;

      "婚否="+婚否);

    }

}

publicclassExample4_16

{

    publicstaticvoidmain(Stringargs[])

    {  

      Univer_Studentzhang=newUniver_Student(9901,"和晓林",false);

    }

}

 

例子17

classSum

{  

    intn;

    floatf()

    {  

      floatsum=0;

      for(inti=1;i<=n;i++)

          sum=sum+i;

          returnsum;  

    }

}

classAverageextendsSum

{  

    intn;  

    floatf()

    {  

      floatc;

      =n;

      c=();

      returnc/n;

    }

    floatg()

    {

      floatc;

      c=();

      returnc/2;

 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 财务管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1