《java程序设计》实验指导书完整.docx

上传人:b****5 文档编号:4525820 上传时间:2022-12-01 格式:DOCX 页数:33 大小:251.49KB
下载 相关 举报
《java程序设计》实验指导书完整.docx_第1页
第1页 / 共33页
《java程序设计》实验指导书完整.docx_第2页
第2页 / 共33页
《java程序设计》实验指导书完整.docx_第3页
第3页 / 共33页
《java程序设计》实验指导书完整.docx_第4页
第4页 / 共33页
《java程序设计》实验指导书完整.docx_第5页
第5页 / 共33页
点击查看更多>>
下载资源
资源描述

《java程序设计》实验指导书完整.docx

《《java程序设计》实验指导书完整.docx》由会员分享,可在线阅读,更多相关《《java程序设计》实验指导书完整.docx(33页珍藏版)》请在冰豆网上搜索。

《java程序设计》实验指导书完整.docx

《java程序设计》实验指导书完整

实验一Java实验环境的建立

一、实验目的

1.掌握Java编程环境的搭建过程;

2.掌握Jcreatorpro软件的使用方法;

3.能使用Javadoc文档。

二、实验内容

1.下载并安装,配置JDK环境变量;

2.下载Javadoc压缩包并解压在JDK安装路径下;

3.下载Jcreatorpro并安装和配置使用环境;

4.使用实验环境运行书中实例代码,在屏幕上输出“HelloJava”字符串。

①Javaapplication程序代码如下:

publicclassHelloJava

{

publicstaticvoidmain(Stringargs[])

{

"HelloJava!

");

}

}

②Javaapplet程序代码如下:

import.*;

publicclassHelloextends

{

publicvoidpaint(Graphicsg)

{

("Hello!

",30,30);

}

}

三、试验要求

1、预习试验内容并写出上机报告。

2、实验中出现的问题及实验体会。

 

实验二Java语言程序设计训练

一、实验目的:

1.输入、输出操作实现原理和方法

2.掌握程序流程控制的实现方法

3.掌握数组的定义和操作方法

二、实验内容

1.计算Result=1!

+2!

+3!

+…+10!

publicclassTestJieC{

publicstaticvoidmain(Stringarg[]){

longresult=1;

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

{result=i*result;

result+=result;

}

""+result);

}

}

2.计算1---50之间所有素数的和。

 

publicclassTest{

publicstaticvoidmain(Stringargs[]){

intcount=0;

for(inti=2;i<=50;i++){

for(intj=2;j<=i;j++){

if(i>j){

if(i%j==0){

count++;

}

}

}

if(count==0){

}

count=0;

}

}

}

}

3.产生10个100之内的随机整数输出,并把这10个数按从小到大的顺序输出。

publicclassTestMath{

publicstaticvoidmain(Stringargs[]){

intmath[]=newint[10];

for(inti=0;i<10;i++){

math[i]=(int)()*100);

"");

}

for(inti=0;i<10;i++){

for(intj=0;j<10;j++){

if(math[i]

intl=math[i];

math[i]=math[j];

math[j]=l;

}

}

}

for(inti=0;i<10;i++){

"");

}

}

}

4.随机产生20个50~100间的整数,输出这20个数并找出最大数及最小数输出。

publicclassTestMath{

publicstaticvoidmain(Stringargs[]){

intmath[]=newint[20];

intmax=0;intmin=100;

for(inti=0;i<20;i++){

math[i]=(int)()*50+50);

"");

}

for(inti=0;i<20;i++){

max=max>math[i]max:

math[i];

min=min

math[i];

}

"max:

"+max);

"min:

"+min);

}

}

5.试编写程序,实现求几何形状(长方形、正方形、圆形)的周长、面积。

(可任选其一)

importclassTest{

publicstaticvoidmain(Stringargs[])throwsException{

Scanneris=newScanner;

"请输入长方形的宽");

inta=();

"请输入长方形的高");

intb=();

"输入1求出周长,输入2求出面积,输入三求出周长与面积");

intc=();

if(c==1){

"周长"+(a+b)*2);

}elseif(c==2){

"面积"+a*b);

}elseif(c==3){

"周长"+(a+b)*2+",面积"+a*b);

}else{

"输入有误,退出");

}

}

}

6.验证书中的例题。

三、试验要求

1、预习试验内容并写出上机报告。

2、实验中出现的问题及实验体会。

 

实验三面向对象的程序设计

(一)

一、实验目的

1.熟悉类的创建方法。

2.掌握对象的声明与创建。

3.能利用面向对象的思想解决一般问题。

二、实验内容

1.以下程序能否通过编译上机验证并指明错误原因与改正方法

ClassLocation{

Privateintx,y;

PublicvoidLocation(inta,intb)

{X=a;y=b;}

PublicintgetX(){returnx;}

PublicintgetY(){returny;}

Publicstaticvoidmain(Stringargs[]){

Locationloc=newLocation(12,20);

}

2.?

创建一个图书类,类中包含的属性有:

书名、作者、出版社;包含的方法有:

设置书籍状态,查看书籍状态。

书籍状态有在馆和外借两种。

publicclassLib{设计一个Birthday类,其成员变量有:

year,month,day;提供构造方法、输出Birthday对象值的方法和计算年龄的方法。

编写程序测试这个类。

publicclassBirthday{

证书中的例题。

三、实验要求

1.事先预习,写出预习报告

2.上机后写出实验报告

实验四面向对象的程序设计

(二)

一、实验目的

1.熟悉类的定义

2.掌握对象的声明、实例化及成员的引用

3.掌握构造方法及实例方法的区别与用法

二、实验内容

1.编写一个类,描述汽车,其中用字符型描述车的牌号,用浮点型描述车的价格。

编写一个测试类,其中有一个修改价格的方法,对汽车对象进行操作,根据折扣数修改汽车的价格,最后在main()方法中输出修改后的汽车信息。

classCar{

StringchePai;

floatprice;

floatprice1;

Car(StringchePai,floatprice){

=chePai;

=price*4/5;

=price;

}

voiddismessage(){

"这辆车的品牌是"+chePai+"原价是"+price+"打折后为"+price1);

}

}

publicclassTestCar{

publicstaticvoidmain(String[]args){

Carc=newCar("奔驰S6OO",50000);

();

}

}

2.设计一个银行帐户类,成员变量包括账号、储户姓名、开户时间、身份证号码、存款余额等帐户信息,成员方法包括存款、取款操作。

publicclassTest{

publicstaticvoidmain(Stringargs[]){

Bankb1=newBank("鹿鹿","鹿容","2012-04-30",1,;

;;();

}

}

classBank{

privateStringuser;

privateStringname;

privateStringtime;

privateintid;

privatedoublemoney;

Bank(Stringuser,Stringname,Stringtime,intid,doublemoney){

=user;=name;=time;=id;=money;

}

publicvoidcun(doubleinMoney){

money=money+inMoney;

}

publicvoidqu(doubleoutMoney){

if(money-outMoney>=0){

money=money-outMoney;

}

}

publicvoidinfo(){

"余额还有"+money);

}

}

 

3.编写一个java程序,设计一个汽车类Vehicle,包含的属性有车轮的个数wheels和车重weight。

小汽车类Car是Vehicle的子类,包含的属性有载人数loader。

卡车类Truck是Car类的子类,其中包含的属性有载重量payload。

每个类都有构造方法和输出相关数据的方法。

publicclassVehicle{

intwheels;

doubleweights;

Vehicle(intwheels,doubleweights){验证书中的例题。

三、实验要求

1.事先预习,写出预习报告

2.上机后写出实验报告

实验五面向对象综合实验

一、实验目的

1.熟悉类的定义;

2.掌握对象的声明、实例化及成员的引用;

3.掌握构造方法及实例方法的区别与用法。

二、实验内容

多数用户对去银行办理存款、取款等业务并不默生,用户自然感觉到了通过计算机办理业务的方便、快捷,也自然对编写出银行系统程序的程序员发出由衷的敬意。

实际上,当我们具备了面向对象编程的知识以后,我们也能编写出相应的程序。

程序框架如下,将代码补充完整:

2.设计一个银行帐户类,成员变量包括账号、储户姓名、开户时间、身份证号码、存款余额等帐户信息,成员方法包括存款、取款操作。

 

packagebank;;catch间的语句中若产生异常,则捕获异常,直接进行异常处理

{

 

}

try{检查分数(score[i]);入成绩();

demo.输出成绩();

}

}

三、实验要求

1、根据题目要求完成程序中没有完成的模块。

2、写好上机报告。

 

实验七:

图形用户界面设计

(一)

一、实验目的:

1.巩固图形用户界面设计的方法

2.掌握事件处理的设计方法

二、实验内容:

1.绘制如下形式的图形界面,要求:

窗体背景为蓝色,中间为黄色方格。

import.*;

publicclassTest{

publicstaticvoidmain(Stringargs[]){

newF();

}

}

classFextendsFrame{

F(){

intx,y,w,h;

x=200;y=200;w=200;h=200;

setBounds(x,y,w,h);

setBackground;

Panelp=newPanel();

(x/4,y/4,w/2,h/2);

;

setLayout(null);

add(p);

setVisible(true);

}

}

 

2.编写程序,绘制如下格式的界面:

import.*;

publicclassTest{

publicstaticvoidmain(Stringargs[]){

Framef=newFrame();

(newGridLayout(2,1));

(300,300,300,300);

Panelp1=newPanel(newBorderLayout());

Panelp2=newPanel(newBorderLayout());

Panelp11=newPanel(newGridLayout(2,1));

Panelp21=newPanel(newGridLayout(2,2));

(newButton("button"),;

(newButton("button"),;

(newButton("button"));

(newButton("button"));

(p11,;

(newButton("button"),;

(newButton("button"),;

for(inti=0;i<4;i++){

(newButton("button"));

}

(p21,;

(p1);(p2);

(true);

}

}

 

3.编写程序,创建如下图所示的图形界面(不必为组件提供功能)。

import.*;

importclassTest{

publicstaticvoidmain(Stringargs[]){

Framef=newFrame();

(300,300,300,300);

(newBorderLayout());

Panelp1=newPanel();

(newGridLayout(1,0));

(newTextField());

Panelp2=newPanel();

(newGridLayout(4,4));

(newButton("7"));

(newButton("8"));

(newButton("9"));

(newButton("/"));

(newButton("4"));

(newButton("5"));

(newButton("6"));

(newButton("*"));

(newButton("1"));

(newButton("2"));

(newButton("3"));

(newButton("-"));

(newButton("0"));

(newButton("."));

(newButton("="));

(newButton("+"));

(p1,;

(p2,;

(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

(0);

}

});

(true);

}

}

 

4.验证书中例题。

三、实验要求:

1.事先预习,写出预习报告

2.上机验证后写出实验报告

 

实验八图形用户界面设计

(二)

一、实验目的

1.掌握各种组件的用法;

2.掌握布局管理器的布局方式;

3.掌握事件处理机制。

二、实验内容

1.验证书中例题。

2.试创建如下图所示的图形用户界面,颜色列表框为红色、绿色和蓝色。

import.*;

publicclassTest{

publicstaticvoidmain(Stringargs[]){

Framef=newFrame();

(300,300,300,300);

(newBorderLayout());

Panelp1=newPanel();Panelp2=newPanel();Panelp21=newPanel();Panelp22=newPanel();

(newGridLayout(1,0));

Choicec=newChoice();

("红色");("绿色");("蓝色");

(c);

(newGridLayout(2,1));

(newFlowLayout);(newFlowLayout);

(newCheckbox("背景"));

(newCheckbox("前景"));

(newButton("确定"));

(newButton("取消"));

(p21);(p22);

(p1,;

(p2,;

(true);

}

}

 

3.编写程序,创建如下图所示的图形界面。

(要求实现功能)

import.*;

importclassTestextendsFrame{

staticTextFieldtf1=newTextField();

staticTextFieldtf2=newTextField();

staticTextFieldtf3=newTextField();

staticButtonb1=newButton("求和");staticButtonb2=newButton("清除");

publicstaticvoidmain(Stringargs[]){

Testf=newTest();

(300,300,300,300);

(newGridLayout(3,3));

(newLabel("加数1:

"));

(tf1);

(newLabel());

();

(newLabel("加数2:

"));

(tf2);(newLabel());

(newTest().newT());

(newTest().newT());

(b1);(tf3);(b2);

(true);

}

classTimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

熟悉绘图类的基本用法

5.掌握绘图类中常用的绘图方法

二、实验内容

1.验证书中例题:

P200页例、P209页例。

2.设计一个程序,程序执行时,随机产生一条直线、一个矩形、一个椭圆,并且每个图形的颜色不同。

(说明:

可利用系统类Math中的静态方法random(),该方法产生一个0~1间的小数)

 

import.*;

import.*;

importclassFFrameextendsJFrame{

/**

*

*/

privatestaticfinallongserialVersionUID=-686L;

publicvoidlauchFFrame(){

setBounds(300,300,300,300);

addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

setVisible(false);

(0);

}

});

setVisible(true);

}

publicvoidpaint(Graphicsg){

Randomr=newRandom();

inti=(3);

if(i==0){

;

(50,50,100,100);

}elseif(i==1){

;

(50,50,100,100);

}else{

;

(50,50,100,100);

}

}

}

publicclassTest{

publicstaticvoidmain(Stringargs[]){

newFFrame().lauchFFrame();

}

}

 

3.设计如下形式的窗口,并实现窗口的关闭功能:

import.*;

import.*;

importclassFFrameextendsJFrame{

/**

*

*/

privatestaticfinallongserialVersionUID=-686L;

publicvoidlauchFFrame(){

setBounds(300,300,300,300);

addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

setVisible(false);

(0);

}

});

addMouseListener(newMouseAdapter(){

publicvoidmousePressed(MouseEvente){

intx=();

inty=();

"x:

"+x+",y:

"+y);

}

});

setVisible(true);

}

publicvoidpaint(Graphicsg){

("五星",200,200);

(81,55,37,190);

(37,190,159,93);

(159,93,32,96);

(32,96,155,188);

(155,188,81,55);

}

}

 

4.设计如下形式的窗口,并实现窗口的关闭功能:

 

packagepaint;

import.*;

import.*;

importclassFFrameextendsJFrame{

/**

*

*/

privatestaticfinallo

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

当前位置:首页 > 初中教育 > 数学

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

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