java大作业附运行截图及代码Word下载.docx
《java大作业附运行截图及代码Word下载.docx》由会员分享,可在线阅读,更多相关《java大作业附运行截图及代码Word下载.docx(47页珍藏版)》请在冰豆网上搜索。
品牌:
"
+name+"
颜色:
+color+"
自重:
"
+weight+"
公斤搭载的人数:
+passenger;
题目2:
课本P5315;
题目2的运行结果截图:
题目2的源程序:
publicclassReverse{
System.out.println("
逆序输出"
);
for(inti=args[0].length()-1;
i>
=0;
i--)
{
System.out.println(args[0].charAt(i));
}
选作题
题目3:
参考下列要求,修改Snowman.java:
(1)在图片的右上角添加文本;
(2)给雪人增加更多的装饰;
(3)给画面添加更多内容,比如云朵,圣诞树,房屋等。
题目3的运行结果截图:
题目3的源程序:
importjava.awt.*;
importjava.applet.Applet;
publicclassSnowmanextendsApplet
{
publicvoidinit()
{setBackground(Color.cyan);
//设置背景色
//绘制雪人
publicvoidpaint(Graphicsg)
{
finalintMID=150;
finalintTOP=50;
g.setColor(Color.blue);
//设置前景色
g.fillRect(0,175,300,50);
//ground
g.setColor(Color.yellow);
g.fillOval(-40,-40,80,80);
//sun
g.setColor(Color.white);
g.fillOval(MID-20,TOP,40,40);
//head
g.fillOval(MID-35,TOP+35,70,50);
//uppertorso
g.fillOval(MID-50,TOP+80,100,60);
//lowertorso
g.fillOval(MID-40,TOP+10,5,5);
//雪花
g.fillOval(MID-80,TOP+20,5,5);
g.fillOval(MID+35,TOP+15,5,5);
g.fillOval(MID-120,TOP+50,5,5);
g.fillOval(MID-100,TOP+90,5,5);
g.setColor(Color.black);
g.fillOval(MID-10,TOP+10,5,5);
//lefteye
g.fillOval(MID+5,TOP+10,5,5);
//righteye
g.fillOval(MID-2,TOP+45,8,10);
g.fillOval(MID-2,TOP+60,8,10);
g.drawArc(MID-10,TOP+20,20,10,190,160);
//smile
g.drawLine(MID-25,TOP+60,MID-50,TOP+40);
//leftarm
g.drawLine(MID+25,TOP+60,MID+55,TOP+60);
//rightarm
g.drawLine(MID-20,TOP+5,MID+20,TOP+5);
//brimofheat
g.fillRect(MID-15,TOP-20,30,25);
//topofhat
g.setColor(Color.red);
g.fillOval(MID-2,TOP+18,5,5);
//鼻子
g.drawString("
有点冷啊!
MID+10,TOP-30);
g.setColor(Color.gray);
g.fillRect(MID-120,TOP+35,20,90);
g.setColor(Color.green);
g.fillOval(MID-150,TOP+10,75,60);
题目4:
编写一个Applet程序,在上面展示自己设计的名片,包括文字和图形。
(注:
下图名片中的背景图案仅为参考样式。
)
题目4的运行结果截图:
题目4的源程序:
publicclassDesignextendsApplet{
{setBackground(Color.lightGray);
西安电子科技大学"
MID+10,TOP+130);
XidianUniversity"
MID+10,TOP+150);
g.setColor(Color.magenta);
g.drawOval(MID-90,TOP-40,60,60);
g.drawOval(MID-100,TOP+80,40,40);
g.drawOval(MID+10,TOP-20,50,50);
g.drawOval(MID-130,TOP+50,40,40);
g.setColor(Color.cyan);
g.drawOval(MID-20,TOP-50,30,30);
g.drawOval(MID-40,TOP+20,65,65);
第二次上机报告
课本P9212;
packagech2;
importjava.io.IOException;
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
publicclassAPP1{
publicstaticvoidmain(Stringargs[])throwsIOException{
BufferedReaderbuf;
buf=newBufferedReader(newInputStreamReader(System.in));
Stringstr;
intx;
输入一个整数"
输入quit,回车后退出"
while(true){
str=buf.readLine();
if(str.equals("
quit"
))
break;
x=Integer.parseInt(str);
System.out.println(x+"
项:
+factorial(x));
}
staticlongfactorial(intk){
long[]fib=newlong[k];
fib[0]=1;
fib[1]=1;
if(k>
1){
for(inti=2;
i<
=k-1;
i++)
fib[i]=fib[i-1]+fib[i-2];
returnfib[k-1];
课本P9221;
importjava.io.*;
publicclassAPP2{
publicstaticvoidmain(String[]args)throwsIOException{
inta,b,c;
doubler,h;
System.out.print("
请输入立方体边长:
str=buf.readLine();
a=Integer.parseInt(str);
立方体的体积:
+volume(a));
System.out.println();
请输入长方体的长:
请输入长方体的宽:
b=Integer.parseInt(str);
请输入长方体的高:
c=Integer.parseInt(str);
长方体的体积:
+volume(a,b,c));
请输入圆柱体的底面半径:
r=Double.parseDouble(str);
请输入圆柱体的高:
h=Double.parseDouble(str);
圆柱体的体积:
+volume(r,h));
staticintvolume(inta){
intvolume;
volume=a*a*a;
returnvolume;
staticintvolume(inta,intb,intc){
volume=a*b*c;
staticdoublevolume(doubler,doubleh){
doublevolume;
volume=Math.PI*r*r*h;
课本P1451;
publicclassAPP3{
publicstaticvoidmain(Stringargs[])
{MyRectangleobj=newMyRectangle(1,5,6,2);
System.out.println(obj.getW());
System.out.println(obj.getH());
System.out.println(obj.area());
System.out.println(obj.toString());
classMyRectangle{
intxUp;
intyUp;
intxDown;
intyDown;
MyRectangle(intxUp,intyUp,intxDown,intyDown){
this.xUp=xUp;
this.yUp=yUp;
this.xDown=xDown;
this.yDown=yDown;
publicStringgetW(){
return"
宽"
+(yUp-yDown);
publicStringgetH(){
长"
+(xDown-xUp);
publicStringarea(){
面积"
+((yUp-yDown)*(xDown-xUp));
publicStringtoString(){
+(yUp-yDown)+"
+(xDown-xUp)+"
课本P1454
publicclassUser{
privateStringcommand;
publicstaticintnum=0;
publicUser(Stringname){
this();
this.name=name;
publicUser(Stringname,Stringcommand){
this(name);
mand=command;
return"
姓名:
指令:
+command;
publicUser(){
num++;
publicStringgetcommand(){
returncommand;
publicvoidsetcommand(Stringcommand){
publicclassAPP4{
publicstaticvoidmain(Stringargs[]){
Userobj1=newUser("
张三"
gohome"
System.out.println(obj1.toString());
obj1.setcommand("
cometoschool"
指令为:
+obj1.getcommand());
用户个数:
+obj1.num);
设计Circle类,在小程序窗口中绘制多个圆。
每个圆的大小、颜色利用构造方法的参数指定。
publicclassAPP5extendsApplet
{
Circlec1,c2,c3,c4,c5,c6,c7;
publicvoidinit(){
c1=newCircle(10,Color.red,100,100);
c2=newCircle(20,Color.yellow,35,35);
c3=newCircle(30,Color.green,70,70);
c4=newCircle(40,Color.blue,0,0);
setBackground(Color.black);
publicvoiddrawcircle(Graphicsg){
c1.draw(g);
c2.draw(g);
c3.draw(g);
c4.draw(g);
}publicclassCircle{
intdiameter;
intx;
inty;
Colorcolor;
publicCircle(intsize,Colorshade,intx1,inty1){
diameter=size;
color=shade;
x=x1;
y=y1;
publicvoiddraw(Graphicsg){
g.setColor(color);
g.fillOval(x,y,diameter,diameter);
计Building类,在小程序窗口中绘制若干幢大楼。
大楼的宽度与高度利用构造方法的参数指定。
每幢大楼的颜色均为黑色,上面有一些黄色窗户,窗户的个数为1~10之间的随机数,窗户的位置在整栋大楼上随机分布。
publicclassRectangle{
doublem;
doublen;
doublex;
doubley;
Rectangle(doubleDownx,doubleDowny,Colorshade,doubleUpx,doubleUpy){
m=Downx;
n=Downy;
x=Upx;
y=Upy;
g.setColor(color);
g.fillRect((int)m,(int)n,(int)x,(int)y);
java.awt.*;
importjava.math.*;
importjava.util.Random;
publicclassBuildingextendsApplet{
Rectangler1,r2,r3,r4,r5,r6,r7,r8;
Randomrd1=newRandom();
inti;
r1=newRectangle(10,80,Color.black,20,80);
r2=newRectangle(40,60,Color.black,35,100);
r3=newRectangle(80,90,Color.black,30,70);
r4=newRectangle(120,110,Color.black,10,50);
publicvoidpaint(Graphicsg){
r1.draw(g);
r2.draw(g);
r3.draw(g);
r4.draw(g);
intnum1=rd1.nextInt(10);
for(i=1;
=num1;
i++){
r5=newRectangle(Math.floor(Math.random()*(30-20+1)+20),Math.floor(Math.random()*(160-80+1)+80),Color.yellow,2,2);
r5.draw(g);
}
intnum2=rd1.nextInt(10);
=num2;
r6=newRectangle(Math.floor(Math.random()*(75-40+1)+40),Math.floor(Math.random()*(160-60+1)+60),Color.yellow,2,2);
r6.draw(g);
intnum3=rd1.nextInt(10);
=num3;
r7=newRectangle(Math.floor(Math.random()*(110-80+1)+80),Math.floor(Math.random()*(160-90+1)+90),Color.yellow,2,2);
r7.draw(g);
intnum4=rd1.nextInt(10);
=num4;
r8=newRectangle(Math.floor(Math.random()*(130-120+1)+120),Math.floor(Math.random()*(160-110+1)+110),Color.yellow,2,2);
r8.draw(g);
}
第三次上机报告
课本P1465
packagemylib.myclasses;
//MyCube继承MyRectangle构造一个立方体
publicclassMyCubeextendsMyRecta