1、java大作业附运行截图及代码Java程序设计上机报告 学 院 通信工程学院 专 业 通信工程 学生姓名 梁芷馨 学生学号 150* 第一次上机报告必做题 题目1:课本P53 12;题目1的运行结果截图:题目1的源程序:package ch1;public class Car public static void main(String args) Carinf obj1=new Carinf(本田,黑色,1500,5); System.out.println(obj1.show(); class Carinf String name; String color; double weight;
2、 int passenger; Carinf(String s,String b,double d,int i) name=s; color=b; weight=d; passenger=i; String show() return品牌: +name+ 颜色: +color+ 自重:+weight+公斤 搭载的人数: +passenger; 题目2:课本P53 15;题目2的运行结果截图:题目2的源程序:package ch1;public class Reverse public static void main(String args) System.out.println(逆序输出);
3、 for(int i=args0.length()-1;i=0;i-) System.out.println(args0.charAt(i); 选作题 题目3:参考下列要求,修改Snowman.java:(1)在图片的右上角添加文本;(2)给雪人增加更多的装饰;(3)给画面添加更多内容,比如云朵,圣诞树,房屋等。题目3的运行结果截图:题目3的源程序:package ch1;import java.awt.*;import java.applet.Applet;public class Snowman extends Applet public void init() setBackground
4、(Color.cyan); /设置背景色 / 绘制雪人 public void paint(Graphics g) final int MID=150; final int TOP=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,TO
5、P+35,70,50); /upper torso g.fillOval(MID-50,TOP+80,100,60); /lower torso 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); / left eye
6、 g.fillOval(MID+5,TOP+10,5,5); / right eye g.setColor(Color.black); g.fillOval(MID-2,TOP+45,8,10); g.setColor(Color.blue); 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); / left arm g.drawLine(MID+25,TOP+60,MID+55,TOP+60); / righ
7、t arm g.drawLine(MID-20,TOP+5,MID+20,TOP+5); / brim of heat g.fillRect(MID-15,TOP-20,30,25); / top of hat g.setColor(Color.red); g.fillOval(MID-2,TOP+18,5,5); / 鼻子 g.setColor(Color.black); g.drawString(有点冷啊!, MID+10, TOP-30); g.setColor(Color.gray); g.fillRect(MID-120,TOP+35,20,90); g.setColor(Color
8、.green); g.fillOval(MID-150,TOP+10,75,60); 题目4:编写一个Applet程序,在上面展示自己设计的名片,包括文字和图形。(注:下图名片中的背景图案仅为参考样式。)题目4的运行结果截图:题目4的源程序:package ch1;import java.awt.*;import java.applet.Applet;public class Design extends Applet public void init() setBackground(Color.lightGray); /设置背景色 public void paint(Graphics g)
9、final int MID=150; final int TOP=50; g.setColor(Color.white); g.drawString(西安电子科技大学, MID+10, TOP+130); g.drawString(Xidian University, 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.setColor(Color.yellow); g.drawOval(MID+10,TOP-20,50
10、,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); 第二次上机报告必做题 题目1:课本P92 12;题目1的运行结果截图:题目1的源程序:package ch2;import java.io.IOException;import java.io.BufferedReader;import java.io.InputStreamReader;public class APP1 public
11、static void main(String args)throws IOException BufferedReader buf; buf=new BufferedReader(new InputStreamReader(System.in); String str; int x; System.out.println(输入一个整数); System.out.println(输入quit,回车后退出); while(true) str=buf.readLine(); if(str.equals(quit) break; x=Integer.parseInt(str); System.out
12、.println(x+项:+factorial(x); static long factorial(int k) long fib=new longk; fib0=1; fib1=1; if(k1) for(int i=2;i=k-1;i+) fibi=fibi-1+fibi-2; return fibk-1; 题目2:课本P92 21;题目2的运行结果截图:题目2的源程序:package ch2;import java.io.*;public class APP2 public static void main(String args) throws IOException int a,b,
13、c; double r,h; BufferedReader buf; buf=new BufferedReader(new InputStreamReader(System.in); String str; System.out.print(请输入立方体边长:); str=buf.readLine(); a=Integer.parseInt(str); System.out.println(立方体的体积: +volume(a); System.out.println(); System.out.print(请输入长方体的长:); str=buf.readLine(); a=Integer.pa
14、rseInt(str); System.out.print(请输入长方体的宽:); str=buf.readLine(); b=Integer.parseInt(str); System.out.print(请输入长方体的高:); str=buf.readLine(); c=Integer.parseInt(str); System.out.println(长方体的体积: +volume(a,b,c); System.out.println(); System.out.print(请输入圆柱体的底面半径:); str=buf.readLine(); r=Double.parseDouble(s
15、tr); System.out.print(请输入圆柱体的高:); str=buf.readLine(); h=Double.parseDouble(str); System.out.println(圆柱体的体积: +volume(r,h); System.out.println(); static int volume(int a) int volume; volume=a*a*a; return volume; static int volume(int a,int b,int c) int volume; volume=a*b*c; return volume; static doubl
16、e volume(double r,double h) double volume; volume=Math.PI*r*r*h; return volume; 题目3:课本P145 1;题目3的运行结果截图:题目3的源程序:package ch2;public class APP3 public static void main(String args) MyRectangle obj=new MyRectangle( 1,5,6,2);System.out.println(obj.getW();System.out.println(obj.getH();System.out.println(
17、obj.area();System.out.println(obj.toString(); class MyRectangle int xUp; int yUp; int xDown; int yDown; MyRectangle(int xUp,int yUp,int xDown, int yDown ) this.xUp=xUp; this.yUp=yUp; this.xDown=xDown; this.yDown=yDown; public String getW() return 宽+(yUp-yDown); public String getH() return 长+(xDown-x
18、Up); public String area() return 面积+(yUp-yDown)*(xDown-xUp); public String toString() return 宽+(yUp-yDown)+长+(xDown-xUp)+面积+(yUp-yDown)*(xDown-xUp); 题目4:课本P145 4题目4的运行结果截图:题目4的源程序:package ch2;public class User String name; private String command; public static int num=0; public User(String name) thi
19、s(); this.name=name; public User(String name, String command) this(name); mand=command; public String toString() return 姓名:+name+ 指令:+command; public User() num+;public String getcommand() return command;public void setcommand(String command) mand=command; package ch2;public class APP4 public static
20、 void main(String args) User obj1=new User(张三,go home); System.out.println(obj1.toString(); obj1.setcommand(come to school); System.out.println(obj1.toString(); System.out.println(指令为:+obj1.getcommand(); System.out.println(用户个数:+obj1.num); 选作题 题目1:设计Circle类,在小程序窗口中绘制多个圆。每个圆的大小、颜色利用构造方法的参数指定。题目1的运行结果
21、截图:题目1的源程序:package ch2;import java.awt.*;import java.applet.Applet;public class APP5 extends Applet Circle c1,c2,c3,c4,c5,c6,c7; public void init() c1=new Circle(10,Color.red,100,100); c2=new Circle(20,Color.yellow,35,35); c3=new Circle(30,Color.green,70,70); c4=new Circle(40,Color.blue,0,0); setBac
22、kground(Color.black); public void drawcircle(Graphics g) c1.draw(g); c2.draw(g); c3.draw(g); c4.draw(g); public class Circle int diameter; int x; int y; Color color; public Circle(int size,Color shade,int x1,int y1) diameter=size; color=shade; x=x1; y=y1; public void draw(Graphics g) g.setColor(colo
23、r); g.fillOval(x, y, diameter,diameter); 题目2:计Building类,在小程序窗口中绘制若干幢大楼。大楼的宽度与高度利用构造方法的参数指定。每幢大楼的颜色均为黑色,上面有一些黄色窗户,窗户的个数为110之间的随机数,窗户的位置在整栋大楼上随机分布。题目2的运行结果截图:题目2的源程序:package ch2;import java.awt.*;import java.applet.Applet;public class Rectangle double m; double n; double x; double y; Color color; Rect
24、angle(double Downx,double Downy,Color shade,double Upx,double Upy) m=Downx; n=Downy; color=shade; x=Upx; y=Upy; public void draw(Graphics g) g.setColor(color); g.fillRect(int)m, (int)n,(int) x,(int) y); java.awt.*;import java.applet.Applet;import java.math.*;import java.util.Random;public class Buil
25、ding extends Applet Rectangle r1,r2,r3,r4,r5,r6,r7,r8; Random rd1=new Random(); int i; public void init() r1=new Rectangle(10,80,Color.black,20,80); r2=new Rectangle(40,60,Color.black,35,100); r3=new Rectangle(80,90,Color.black,30,70); r4=new Rectangle(120,110,Color.black,10,50); public void paint(G
26、raphics g) r1.draw(g); r2.draw(g); r3.draw(g); r4.draw(g); int num1=rd1.nextInt(10); for(i=1;i=num1;i+) r5=new Rectangle(Math.floor(Math.random()*(30-20+1)+20),Math.floor(Math.random()*(160-80+1)+80),Color.yellow,2,2); r5.draw(g); int num2=rd1.nextInt(10); for(i=1;i=num2;i+) r6=new Rectangle(Math.fl
27、oor(Math.random()*(75-40+1)+40),Math.floor(Math.random()*(160-60+1)+60),Color.yellow,2,2); r6.draw(g); int num3=rd1.nextInt(10); for(i=1;i=num3;i+) r7=new Rectangle(Math.floor(Math.random()*(110-80+1)+80),Math.floor(Math.random()*(160-90+1)+90),Color.yellow,2,2); r7.draw(g); int num4=rd1.nextInt(10); for(i=1;i=num4;i+) r8=new Rectangle(Math.floor(Math.random()*(130-120+1)+120),Math.floor(Math.random()*(160-110+1)+110),Color.yellow,2,2); r8.draw(g); 第三次上机报告必做题 题目1:课本P146 5题目1的运行结果截图:题目1的源程序:package mylib.myclasses;/MyCube继承MyRectangle构造一个立方体public class MyCube extends MyRecta
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1