JAVA程序设计实验指导书v-.doc

上传人:小小飞 文档编号:5090026 上传时间:2020-02-01 格式:DOC 页数:52 大小:400KB
返回 下载 相关 举报
JAVA程序设计实验指导书v-.doc_第1页
第1页 / 共52页
JAVA程序设计实验指导书v-.doc_第2页
第2页 / 共52页
JAVA程序设计实验指导书v-.doc_第3页
第3页 / 共52页
JAVA程序设计实验指导书v-.doc_第4页
第4页 / 共52页
JAVA程序设计实验指导书v-.doc_第5页
第5页 / 共52页
点击查看更多>>
资源描述

《JAVA程序设计实验指导书v-.doc》由会员分享,可在线阅读,更多相关《JAVA程序设计实验指导书v-.doc(52页珍藏版)》请在三一文库上搜索。

1、实验报告实验一:安装与熟悉J2SDK实验目的:1 了解和使用J2sdk1.6.0(JDK)环境。2 熟悉J2sdk集成环境的基本命令、功能和基本用法。并学会配置path 等环境变量3 学习并熟练掌握使用J2SDK环境进行Java程序开发的全过程(包括:源程序的编辑、编译、调试、运行和结果查看)。4 掌握Java应用程序的构成特点实验内容1. J2SDK开发包的安装(可选)2. 查看系统环境变量的配置3. 编写一个JAVA程序,掌握在JDK开发包支持下编译和运行的过程实验数据记录及分析(或程序及运行结果)1. 写出在你的系统中,Java.exe和Javac.exe的安装位置 答: C:Progr

2、am FilesJavajdk1.6.0_18bin2. 写出运行Java程序所需的基本系统配置JAVA_HOME=C:Program FilesJavajdk1.6.0_18CLASSPATH=.;C:Program FilesJavajdk1.6.0_18libtools.jar; C:Program FilesJavajdk1.6.0_18libdt.jar; C:Program FilesJavajdk1.6.0_18jrelibrt.jarPATH=%PATH%;C:Program FilesJavajdk1.6.0_18bin2 调试下面程序,指出错误public Class He

3、lloWorldApp public void static main(string args) system.out.println(“Hello World!”);错误1:Class 应该写成 class错误2:void 和 static 位置写反了错误3:string应该写成 String错误4:system 应该写成 System3写出调试过程1) 编译命令 javac HelloWorldApp.java2) 运行命令 java HelloWorldApp3) 修改后的源程序 public class HelloWorldApp public static void main(Str

4、ing args) System.out.println(Hello World!); 4添加文档注释,用javadoc工具生成html文件,写出添加注释后的源程序文件。/* *一个向控制台输出字符串的应用程序 */public class HelloWorldApp public static void main(String args) System.out.println(Hello World!); 日期: 年 月 日 日期: 年 月 日实验三:JAVA语言编程基础实验目的:1. 综合掌握变量、数据类型、表达式以及基本程序结构的应用2. 掌握类的方法定义实验内容1. 掌握变量定义、赋值

5、表达式的应用。2. 掌握移位表达式的应用。3. 掌握控制语句的应用。4. 掌握有关类的方法定义及方法使用的形式。5. 理解变量作用域范围。6. 掌握Scanner类的使用方法。实验数据记录及分析(或程序及运行结果)1. 分析下面的程序,写出运行结果public class Ch22 public static void main(String args) boolean x,y,z; int a=12,b=24; x=(ab); y=(a!=b); z=(a+b=36); System.out.println(x=+x); System.out.println(y=+y); System.ou

6、t.println(z=+z); x=falsey=truez=true2写出下面程序运行的结果 class Expression public static void main(String args) int a=25, b=20, e=3, f=0;boolean d=ab;System.out.println(a=25,b=20,e=3,f=0);System.out.println(因为关系表达式 a5)System.out.println(因为e 非0 且a/e 为8 大于5,所以输出 a/e +a/e);if (f!=0 & a/f5)System.out.println(a/f

7、 = +a/f);elseSystem.out.println(因为f 值为0,所以输出 f = +f);因为关系表达式 ab 为假,所以其逻辑值为 : false因为e 非0 且a/e 为8 大于5,所以输出 a/e 8因为f 值为0,所以输出 f = 03假如一个颜色值包含了红黄兰三个分量,每个分量范围在0至256,可以定义一个int类型(4个字节)的变量color,利用它的从低到高连续三个字节分别表示三个颜色分量的值,请编写程序利用移位表达式求出每个颜色的值各是多少。int color=0x00F1AD07,red=0,yellow=0,blue=0;red=color & 0x0000

8、00FF;yellow= color & 0x0000FF00;blue= color & 0x00FF0000;biaodashi= 0x000000FF;red=color & biaodashi;yellow= color & (biaodashi8); /0x0000FF00blue= color & (biaodashi8; /0x0000FF00 blue=blue16; /0x00FF0000 System.out.println(red=+red); System.out.println(yellow=+yellow); System.out.println(blue=+blu

9、e); red=7yellow=173blue=2415 编写程序,读入任一个整数并将其各位数字之和赋给一个整数并输出各位上数字及数字和。import java.util.Scanner;public class Apublic static void main(String args) Scanner sc = new Scanner(System.in);System.out.println(请输入一个整数: );int num = sc.nextInt();int a,b,c,m;a=num/100;b=num%100/10;c=num%100%10;m=a+b+c;System.out

10、.println(a=+a);System.out.println(b=+b);System.out.println(c=+c);System.out.println(m=+m);5编写程序,接受任意从键盘输入的若干整数,请使用冒泡排序算法完成对这些整数排序,并输出排序结果。import java.util.Scanner;public class mypublic static void main(String args) Scanner sc = new Scanner(System.in);System.out.printf(请输入要排序整数个数: ); intnum=sc.nextIn

11、t();int arr=new intnum;for(int i=0;i0;i-)for(j=0;jarrj+1)temp=arrj;arrj=arrj+1;arrj+1=temp;System.out.println(冒泡排序的结果是:);for(i=0;ilen;i+)System.out.println(arri+ );6定义一个类,实现方法完成输出素数的要求:按照每行5个,输出n以内的所有素数。(注:n作为方法的参数传入)。根据题目,补充完成下面程序。 public class PrimeNumber public void listPrime (int n) /添加代码,将生成的n以

12、内的所有素数输出到控制台int m;/ 变量n为要判断的数字int temp=0;/记录输出的素数个数A: for (int f = 2;f=n; f+) for (m = 2; m =f / 2; m+) if (f % m = 0)continue A;/ 如果能被整除则变量n肯定不是素数,跳出内层循环System.out.print(f + + t);/输出素数temp+;if(temp%5=0)System.out.println(); public static void main(String args) PrimeNumber n =new PrimeNumber(); n.li

13、stprime(10); 7调试并写出下面程序运行的结果public class Variable static int i=10;public static void main(String args) int k=10;System.out.println(i=+i);System.out.println(k=+k);System.out.println(i=+i);System.out.println(k=+k); i=10k=10i=10 日期: 年 月 日实验四:面向对象编程基础一实验目的:1 掌握类的定义和生成机制2 了解构造函数的作用实验内容1. 简单类的定义。2. 对象创建和使

14、用。3. 使用关联类进行属性的定义。4. 访问控制修饰符的作用。5. 掌握静态属性、方法和初始化器的特点。6. 包的应用实验数据记录及分析(或程序及运行结果)1. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类提供计算面积的方法getArea()方法,以及修改width和height的值及获得width和height当前值的方法。要求:(1) 使用构造函数完成各属性的初始赋值(2) 使用getter和setter的形式完成属性的访问及修改public c

15、lass Rectangle private double width;private double height;private String color; public Rectangle(double width, double height, String color) this.width = width;this.height = height;this.color = color;public double getWidth() return width;public void setWidth(double width) this.width = width;public do

16、uble getHeight() return height;public void setHeight(double height) this.height = height;public double getArea()double area;return area=width*height;public static void main(String args) Rectangle a=new Rectangle(2.0,3.0,red);System.out.println(a.getArea();一个Fan有自己的品牌和型号,其功能有start,stop,speed,start和st

17、op可以改变Fan的状态status(on/off),speed可以调整Fan的速度(档级在0、1、2、3),请分析并提供Fan类的定义。并编写程序对其进行测试。public class Fan private String band; private String type;private String status ;private int speed;public Fan(String band,String type,String status,int speed)this.band=band;this.type=type;this.status=status;this.speed=s

18、peed;public String start()return status;public String stop()return status;public void speed(int a)this.speed=a;public static void main(Stringargs)Fan t=new Fan(ss,dd,off,1);System.out.printf(%s,%s,%b,%d,t.band,t.type,t.status,t.speed); 解释下面的程序运行结果输出为什么是null public class My String s; public void My()

19、 s = Constructor; public void go() System.out.println(s); public static void main(String args) My m = new My(); m.go(); 调用者没有传递值2.写出程序运行结果class StaticDemo static int x;int y; static x=10;public static int getX() return x;public static void setX(int newX) x = newX;public int getY() return y;public vo

20、id setY(int newY) y = newY;public static void main(String args) System.out.println(静态变量x=+StaticDemo.getX();System.out.println(实例变量y=+StaticDemo.getY(); /非法,编译时将出错StaticDemo a= new StaticDemo();StaticDemo b= new StaticDemo();a.setX(1);a.setY(2);b.setX(3);b.setY(4);System.out.println(静态变量a.x=+a.getX(

21、);System.out.println(实例变量a.y=+a.getY();System.out.println(静态变量b.x=+b.getX();System.out.println(实例变量b.y=+b.getY();静态变量x=10静态变量a.x=3实例变量a.y=2静态变量b.x=3实例变量b.y=42.一个计算机商店销售很多品牌的计算机,每台计算机都应该记录其配置信息,包括处理器、主板、显示器、内存、硬盘等基本设备,每个设备都有自己的品牌、价格、型号信息,请你尝试构造合适的类并利用组合的方法来表示计算机,并为该计算机类添加计算价格(各设备价格之和)、打印配置信息等方法。要求:将定

22、义的类都放在一个包product内。Cpu.javapackage product;public class cpuprivate String brand;private double value;private String type;public cpu(String brand,double value,String type)this.brand=brand;this.value=value;this.type=type;public String getbrand()return brand;public String gettype()return type;public dou

23、ble getvalue()return value;public void print()System.out.println(brand:+getbrand()+t+value:+getvalue()+t+type:+gettype();/System.out.println(brand:);Hardware.javapackage product;public class hardwareprivate String brand;private double value;private String type;public hardware(String brand,double val

24、ue,String type)this.brand=brand;this.value=value;this.type=type;public String getbrand()return brand;public String gettype()return type;public double getvalue()return value;public void print()System.out.println(brand:+getbrand()+t+value:+getvalue()+t+type:+gettype();/System.out.println(brand:);Mainb

25、oard .javapackage product;public class mainboard private String brand;private double value;private String type;public mainboard (String brand,double value,String type)this.brand=brand;this.value=value;this.type=type;public String getbrand()return brand;public String gettype()return type;public doubl

26、e getvalue()return value;public void print()System.out.println(brand:+getbrand()+t +value:+getvalue()+t+type:+gettype();/System.out.println(brand:);Monitor .javapackage product;public class monitorprivate String brand;private double value;private String type;public monitor(String brand,double value,

27、String type)this.brand=brand;this.value=value;this.type=type;public String getbrand()return brand; public String gettype()return type;public double getvalue()return value; public void print()System.out.println(brand:+getbrand()+t+value:+getvalue()+t+type:+gettype();/System.out.println(brand:);Ram.ja

28、vapackage product;public class ramprivate String brand;private double value;private String type;public ram(String brand,double value,String type)this.brand=brand;this.value=value;this.type=type;public String getbrand()return brand;public String gettype()return type;public double getvalue()return val

29、ue;public void print()System.out.println(brand:+getbrand()+t+value:+getvalue()+t+type:+gettype();/System.out.println(brand:);computer.javapackage product;public class computerprivate cpu cp;private hardware hard;private mainboard main;private monitor mon;private ram ra;public computer()this.cp=new c

30、pu(atm,232.2d,xora878);this.hard=new hardware(westdigital,300.2d,pc222);this.main=new mainboard(aim,321.2d,x-32);this.mon=new monitor(voa,900.2d,w-ss);this.ra=new ram(kingsdon,127.2d,s-24);public void kprint()cp.print();hard.print();main.print();mon.print();ra.print(); public double sum() double i=c

31、p.getvalue()+hard.getvalue()+main.getvalue()+mon.getvalue()+ra.getvalue();return i;public static void main(String args)computer com=new computer();com.kprint();System.out.printf(计算机硬件的总价格是:%f¥,com.sum();brand:atmvalue:232.2type:xora878brand:westdigitalvalue:300.2type:pc222brand:aimvalue:321.2type:x-

32、32brand:voavalue:900.2type:w-ssbrand:kingsdonvalue:127.2type:s-24计算机硬件的总价格是:1881.000000¥ 日期: 年 月 日实验五:面向对象编程基础二实验目的:1. 掌握抽象类和抽象方法的特点2. 掌握最终类3. 理解类的继承4. 父类和子类的转化实验内容假定银行的一个存取款系统有两类客户,一类是现金用户,一类是信用卡用户。银行对每个客户都要登记其姓名name,并为之分配一个唯一的账户号码aid,现金用户还要记录其卡的类型(工资卡、借记卡、理财卡),而信用卡用户则根据其信用级别有一定的透支限额lineOfCredit(A级

33、10000元、B级5000元、C级2000元、D级1000元)。每种客户都可以实现存deposit、取withdraw、和查询余额getBalance和交易历史showTransactions(信用卡用户还可以查询透支情况findOverdraw和信用showCredit情况)要求。对于现金用户,每次取款操作只能在账户实际额度balance内操作,允许现金用户改变自己的帐户类型。实验数据记录及分析(或程序及运行结果)1. 分析有那些属性和方法可以作为两个子类的共同属性和方法,然后写出抽象类Account的定义。两种卡的构造方法方案:1) Account(String name, String

34、aid, String category)Category:Cash, CreditAccount a=new CashAccount(“zhangsan”,”00001”,”Cash”)2) Account(String name, String aid)Aid: Cas0001,Cal0001,Cam0001, Cr000011) CashAccount(String name, String aid, String category)Category: 工资卡、借记卡、理财卡CashAccount caa=new CashAccount(“zhangsan”,”00001”,” 工资卡”

35、)2.分析CashAccount有那些新增的属性和方法,定义一个继承于Account的子类CashAccount。3.你认为信用等级是否可以作为一个类存在,如果是,请定义。Class enum CreditLevel 成员变量;Gold(20000),Silver(10000),Bronze(6000),Iron(3000),Normal(2000),Zero(0);构造2. 分析CreditAccount有那些新增的属性和方法,然后定义一个继承于Account的子类CreditAccount,添加增加的属性和方法。请按照要求编写一个程序用你所定义的类完成下列业务操作。(1) 用Account

36、作为类型定义两个变量,分别引用CreditAccount和CashAccount的对象,并完成存款500元的操作。CashAccount a=new CashAccount(张三,006,理财卡,1000);a.deposit(500); CreditAccount b=new CreditAccount(李四,007,B,600); b.deposit(500); (2) 分别用类型转换机制,将上述创建的对象用CreditAccount和CashAccount类型的变量引用。完成取款200元的操作。CashAccount a=new CashAccount(张三,006,理财卡,1000);

37、a.withdraw(200);CreditAccount b=new CreditAccount(李四,007,B,600); b.withdraw(200); (3) 每个对象再次取款400元,请输出各自的余额。 张三取款400成功!900李四取款400成功!500(4) 查看CreditAccount对象的透支额,并显示其可以透支的最大限额。b.getoutline();李四的可透支额为5000(5) 列出其中一个账户的交易历史。账户名李四Transaction时间:Mon Dec 05 23:44:47 CST 2011,业务类型:存款,金额500CreditAccount66848c

38、Transaction时间:Mon Dec 05 23:44:47 CST 2011,业务类型:取款,金额200CreditAccount66848cTransaction时间:Mon Dec 05 23:44:47 CST 2011,业务类型:取款,金额400CreditAccount66848cimport java.util.Date;public class transaction public static final String types=存款,取款,查询,转账;public static final int TYPE_DEPOSIT=0; public static final int TYPE_WITHDRAW=1;public static final int type_touzhi=2;public static final int

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

当前位置:首页 > 绩效管理


经营许可证编号:宁ICP备18001539号-1