一些简单得JAVA小程序Word格式.docx
- 文档编号:22327585
- 上传时间:2023-02-03
- 格式:DOCX
- 页数:35
- 大小:20.12KB
一些简单得JAVA小程序Word格式.docx
《一些简单得JAVA小程序Word格式.docx》由会员分享,可在线阅读,更多相关《一些简单得JAVA小程序Word格式.docx(35页珍藏版)》请在冰豆网上搜索。
publicvoidpaint(Graphicsg)
;
("
我一边喝着咖啡,一边学Java呢"
2,30);
我学得很认真"
10,50);
}
基本数据类型和数组
例子1
publicclass
Example2_1
charchinaWord='
你'
japanWord='
ぁ'
int
p1=20328,p2=12358;
汉字\'
你\'
字在unicode表中的顺序位置:
+(int)chinaWord);
日语\'
ぁ\'
+(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
inta[]={1,2,3,4};
intb[];
b=a;
b[3]=100;
运行结果:
4
100
运算符、表达式和语句
classExample3_1
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);
原文:
例子2
classExample3_2
floatx=,y;
if(x<
=0)
y=x+1;
elseif(x>
0&
&
x<
=16)
y=2*x+1;
else
y=3*x+3;
例子3
publicclassExample3_3extendsApplet
intx=2,y=1;
switch(x+y)
case1:
iam1"
5,10);
break;
case2:
iam2"
case3:
iam3"
break;
default:
没有般配的"
例子4
publicclassExample3_4
extendsApplet
intsum=0;
for(inti=1;
i<
=100;
i++)
sum=sum+i;
sum="
+sum,10,20);
例子5
classExample3_5
{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;
=10;
i++)
圆=circle;
this.高=h;
double计算体积()
{
doublevolume;
volume=底圆.计算面积()*高/;
return
volume;
void修改底圆半径(doubler)
底圆.修改半径(r);
double获取底圆半径()
return底圆.获取半径();
classExample
圆circle=new圆(10);
圆锥circular=new圆锥(circle,20);
圆锥底圆半径:
+circular.获取底圆半径());
圆锥的体积:
+circular.计算体积());
circular.修改底圆半径(100);
class梯形
float上底,高;
staticfloat下底;
底=上底;
this.高=高;
float获取上底()
return上底;
float获取下底()
return下底;
classExample4_5
梯形laderOne,laderTwo;
底=60;
取上底());
laderOne的下底:
+laderOne.获取下底());
laderTwo的上底:
+laderTwo.获取上底());
laderTwo的下底:
+laderTwo.获取下底());
}
例子6
package;
Example4_6
我有包名"
例子7
import
publicclassExampleextendsApplet
Buttonredbutton;
publicvoidinit()
redbutton=newButton("
我是一个红色的按钮"
add(redbutton);
publicvoid
paint(Graphicsg)
itisabutton"
30,50);
例子8
classExample4_8
Trangletrangle=newTrangle(12,3,1);
trangle.计算面积();
trangle.修改三边(3,4,5);
例子9
classExample4_9
privateintmoney;
Example4_9()
money=2000;
privateintgetMoney()
returnmoney;
Example
exa=newExample();
=3000;
intm=();
money="
+m);
例子10
classFather
intweight=100;
Stringspeak(Strings)
returns;
classSonextendsFather
Stringhand;
voidf()
weight=200;
classSuiziextendsSon
Stringfoot;
publicclassExample4_10
Sonson=newSon();
Suizisunzi=newSuizi();
="
两只手"
两小只手"
两只脚"
();
:
+;
我是孙子"
));
例子11
Father
int
height;
protectedintmoney=120;
public
intweight;
protectedintgetMoney()
returnmoney;
voidsetMoney(intnewMoney)
money=newMoney;
publicclassJerryextendsFather
money=1000;
=1000;
();
:
publicclassDL
Bb=newB();
=1000;
为方法fA是B类从A类继承的protected方法,但DL和A不在同一包中.
publicclassExample
为方法fA是B类从A类继承的protected方法,而Example和A在同一包中.
例子12
privateintnumber=100;
floatf(intx,inty)
returnx+y;
floatg(floatx,floaty)
returnx+y+number;
classBextendsA
returnx*y;
publicclassExample
Bb=newB();
Peoplepeople=(People)monkey;
..."
class猫extends动物
voidcry()
喵喵....."
classExample4_14
动物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换底(图形底)
publicdouble求体积()
return(底.求面积()*高)/;
publicclassExample4_15
堆zui;
图形tuxing;
tuxing=new梯形,,;
梯形的面积"
+tuxing.求面积());
zui=new
堆(tuxing,30);
梯形底的堆的体积"
+zui.求体积());
tuxing=new圆形(10);
半径是10的圆的面积"
zui.换底(tuxing);
圆形底的堆的体积"
例子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
Univer_Studentzhang=newUniver_Student(9901,"
和晓林"
false);
例子17
classSum
intn;
floatf()
floatsum=0;
for(inti=1;
=n;
returnsum;
classAverageextendsSum
floatc;
=n;
c=();
returnc/n;
floatg()
returnc/2;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 一些 简单 JAVA 程序
![提示](https://static.bdocx.com/images/bang_tan.gif)