java上机实验答案与解析.docx

上传人:b****5 文档编号:30107929 上传时间:2023-08-05 格式:DOCX 页数:18 大小:19.67KB
下载 相关 举报
java上机实验答案与解析.docx_第1页
第1页 / 共18页
java上机实验答案与解析.docx_第2页
第2页 / 共18页
java上机实验答案与解析.docx_第3页
第3页 / 共18页
java上机实验答案与解析.docx_第4页
第4页 / 共18页
java上机实验答案与解析.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

java上机实验答案与解析.docx

《java上机实验答案与解析.docx》由会员分享,可在线阅读,更多相关《java上机实验答案与解析.docx(18页珍藏版)》请在冰豆网上搜索。

java上机实验答案与解析.docx

java上机实验答案与解析

JAVA上机实验题答案与解析

实验一Java程序编程

1.编写一个Java应用程序,输出内容为Hello!

注:

文件位置位于e:

\2:

\编译:

(1)e:

(2)cd2(3)javac(4)javaHello

2.编写一个Java小应用程序,输出内容为我一边听音乐,一边学Java。

第一步编写

import.*;

import.*;

publicclassMyAppletextendsApplet{

publicvoidpaint(Graphicsg){

("我一边听音乐,我一边做java",25,25);

}

}

第二步在DOS环境中编译(....javac)

第三步使用记事本编写

第四步将记事本文件名命名为

第五步打开

实验二类的定义

1.编写Java应用程序,自定义Point类,类中有两个描述坐标位置的double变量x,y,利用构造方法,实现对Point对象p1,p2初始化,p1和p2对应坐标分别为(15,20),(10,30);定义方法getX(),getY()分别获得点的横坐标和纵坐标;定义方法setX(),setY()分别获得点的横坐标和纵坐标;并且把p1和p2输出;

publicclassPoint{

doublex,y;

Point(doublex,doubley){

=x;

=y;

}

doublegetX(){

returnx;

}

doublegetY(){

returny;

}

voidsetX(doublex){

=x;

}

voidsetY(doubley){

=y;

}

publicstaticvoidmain(String[]args){

Pointp1=newPoint(15,20);写Java应用程序,自定义Circle类,类中有两个double变量r,s,一个类变量pi,利用构造方法实现对半径是3和的初始化,自定义getArea方法实现圆面积求解;

publicclassCircle{

doubles,r;

publicCircle(doubler){

=r;

}

doublegetArea(){

=pi*r*r;

returns;

}

publicstaticvoidmain(String[]args){

Circlec1=newCircle(3);

Circlec2=newCircle;

Area());

Area());

}

}

实验三类的继承和多态性

1.

(1)编写一个接口ShapePara,要求:

接口中的方法:

intgetArea():

获得图形的面积。

intgetCircumference():

获得图形的周长

(2)编写一个圆类Circle,要求:

圆类Circle实现接口ShapePara。

该类包含有成员变量:

radius:

public修饰的double类型radius,表示圆的半径。

x:

private修饰的double型变量x,表示圆心的横坐标。

y:

protected修饰的double型变量y,表示圆心的纵坐标。

包含的方法有:

Circle(doubleradius)有参构造方法。

以形参表中的参数初始化半径,圆心为坐标原点。

doublegetRadius():

获取半径为方法的返回值。

voidsetCenter(doublex,doubley):

利用形参表中的参数设置类Circle的圆心坐标。

voidsetRadius(doubleradius):

利用形参表中的参数设置类Circle的radius域。

在主方法中产生半径为5的圆。

interfaceShapePara{

doublegetArea(doubler);

doublegetCircumference(doubler);

}义图形类Shape,该类中有获得面积的方法getArea();定义长方形类Rect,该类是Shape的子类,类中有矩形长和宽的变量doublea,doubleb,设置长和宽的方法setWidth()、setHeight(),使用getArea()求矩形面积;利用getArea方法实现题1中圆面积的求解。

classShape{

doublegetArea(doubler){

return0;

}

}

publicclassRectextendsShape{

doublea,b,area;

Rect(doublewidth,doubleheigh){

a=width;

b=height;;

}

voidsetWidth(doublewidth){

a=width;

}

voidsetHeight(doubleheight){

b=height;

}

doublegetWidth(){

returna;

}

doublegetHeight(){

returnb;

}

doublegetArea(){

area=a*b;

returnarea;

}

}

publicclassA{

publicstaticvoidmain(Stringargs[]){

Rectrect=newRect();

doublew=,h=;

(w);

(h);

}

}

答案:

圆的的面积:

矩形对象的宽:

高:

3.编写Java应用程序,定义Animal类,此类中有动物的属性:

名称name,腿的数量legs,统计动物的数量count;方法:

设置动物腿数量的方法voidsetLegs(),获得腿数量的方法getLegs(),设置动物名称的方法setKind(),获得动物名称的方法getKind(),获得动物数量的方法getCount()。

定义Fish类,是Animal类的子类,统计鱼的数量count,获得鱼数量的方法getCount()。

定义Tiger类,是Animal类的子类,统计老虎的数量count,获得老虎数量的方法getCount()。

定义SouthEastTiger类,是Tiger类的子类,统计老虎的数量count,获得老虎数量的方法getCount()。

publicclassAnimal{

Stringname;

intlegs;

staticintcount;

Animal(){count++;}

voidsetLegs(intlegs){

=legs;

}

intgetLegs(){

returnlegs;

}

voidsetKind(Stringname){

=name;

}

StringgetKind(){

returnname;

}

intgetCount(){

returncount;

}

}

publicclassFishextendsAnimal{

staticintcountFish;

Fish(){countFish++;}

intgetCount(){

returncountFish;

}

}

publicclassTigerextendsAnimal{

staticintcountTiger;

Tiger(){countTiger++;}

intgetCount(){

returncountTiger;

}

}

publicclassSouthEastTigerextendsTiger{}

publicclassA{

publicstaticvoidmain(Stringargs[]){

Fishf=newFish();

Tigert=newTiger();

SouthEastTigerst=newSouthEastTiger();

}

}

实验四异常处理

1.建立exception包,编写程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理。

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

intk;

switch(i){

case0:

intzero=0;k=911/zero;break;

case1:

intb[]=null;k?

=b[0];break;

case2:

intc[]=newint[2];k=c[9];break;

case3:

charch="abc".charAt(99);break;

}

}

packageException;

publicclassTestException{

publicstaticvoidmain(String[]args){

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

try{

intk;

switch(i){

case0:

intzero=0;k=911/zero;break;

case1:

intb[]=null;k=b[0];break;

case2:

intc[]=newint[2];k=c[9];break;

case3:

charch="abc".charAt(99);break;

}

}

catch(ArithmeticExceptione){

}

}

catch(ArrayIndexOutOfBoundsExceptione){

}

}

}

}

}

运行结果:

/byzero

null

9

Stringindexoutofrange:

99

2.建立exception包,建立Bank类,类中有变量doublebalance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(doubledAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如newBank(100),表示存入银行100元,当用方法withdrawal(150),withdrawal(-15)时会抛出自定义异常。

提示:

InsufficientFundsException,NagativeFundsException为自定义的类,分别产生余额不足异常和取款为负数异常,需继承Exception类。

通过输出结果了解java异常的产生,并将该Java程序放在exception包中。

packageException;;

publicclassA{

publicstaticvoidmain(Stringargs[]){

Bankb=newBank(100);

Scannersc=newScanner;

try{

());

}

catch(InsufficientFundsExceptione){

}

catch(NagativeFundsExceptione){

}

}

}

运行结果:

请输入一个数

80

银行里还剩余:

实验五输入输出

1.编写的Java应用程序,程序完成的功能是:

首先向中写入自己的学号和姓名,读取中信息并将其显示在屏幕上。

import.*;

publicclassTextRw{

publicstaticvoidmain(String[]args)throwsIOException{

Filef=newFile("f:

\\");

FileWriterfw=newFileWriter(f);

("学号姓名");

();

FileReaderfr=newFileReader(f);

inti;

while((i=())!

=-1)

();

}

}

2.编写的Java应用程序,程序完成的功能是:

首先读取源程序文件,再通过键盘输入文件的名称为,把的源程序存入

3.编写的Java应用程序,程序完成的功能是:

完成文件的复制,复制以后的文件的名称为自己的学号姓名.doc。

import.*;

publicclassBinIoDemo{

publicstaticvoidmain(String[]args)throwsIOException{

Filef1=newFile("e:

\\");

FileInputStreamin=newFileInputStream(f1);

Filef2=newFile("e:

\\“学号”+“姓名”.doc");

FileOutputStreamout=newFileOutputStream(f2);

byte[]buf=newbyte[2048];

intnum=(buf);

while(num!

=(-1)){

(buf,0,num);

();

num=(buf);

}

();

();

}

}

实验六多线程编程

1.随便选择两个城市作为预选旅游目标。

实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。

分别用Runnable接口和Thread类实现。

通过Thread

publicclassHw1ThreadextendsThread{

Stringa;

privateintsleepTime;

publicHw1Thread(Stringa){

super(a);

=a;

sleepTime=(int)()*6000);

}

publicvoidrun(){

intcount1=0,count2=0;

if().getName().equalsIgnoreCase("ShangHai")){

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

try{

(sleepTime);

count1++;

if(count1>count2&&count1==10){etName().equalsIgnoreCase("BeiJIng")){

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

try{

(sleepTime);

count2++;

if(count1>count2&&count1==10){

}

elseif(count2>count1&&count2==10){

}

}

catch(InterruptedExceptionexception){};etName().equalsIgnoreCase("td1")){

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

try{

(sleepTime);

count1++;

}

catch(InterruptedExceptionexception){};etName().equalsIgnoreCase("td2")){

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

try{

(sleepTime);

count2++;

if(count1>count2&&count1==10){tart();

newThread(td2,"td2").start();

}

}

2.用继承Thread类方法实现多线程程序。

有两个学生小明和小红,小明准备睡10分钟以后开始听课,小红准备睡1小时以后开始听课,雷老师大喊三声“上课了”,小明醒后把小红吵醒,他们开始听课。

学生:

小明、小红

睡觉:

明:

10min红:

60min

老师:

喊三声

明醒->红醒。

上课。

classStuextendsThread{

Threadstudent1,student2,teacher;

Stu(){

teacher=newThread(this);

student1=newThread(this);

student2=newThread(this);

("雷老师");

("小明");

("小红");

}

publicvoidrun(){

if()==student1){

(1000*60*10);

}

catch(InterruptedExceptione){

}

}

if()==student2){

(1000*60*60);

}

catch(InterruptedExceptione){

}

}

elseif()==teacher){

for(inti=1;i<=3;i++){

try{(500);

}

catch(InterruptedExceptione){}

}

();程实现如下界面:

当在第一个文本框中输入直接回车,会在第二个文本框中得第一个文本框输入值的平方,单击求和,会在第二个文本框中得到两个文本框中的和。

import.*;

publicclassTestimplementsActionListener{

TextFieldtf,tf1;

Buttonb;

Framef;

Test()

{

f=newFrame();

(150,150);

(300,300);

Panelp=newPanel();

Panelp1=newPanel();

Panelp2=newPanel();

tf=newTextField(10);

tf1=newTextField(10);

b=newButton("求和");

(tf);(tf1);(b);

(p,;

(p1,;

(p2,;

(true);

(newWindowAdapter()

{

publicvoidwindowClosing(WindowEvente)

{

(0);

}

});

(this);

(this);

}

publicstaticvoidmain(String[]args){

newTest();

}

publicvoidactionPerformed(ActionEvente)

{inti;

if()==tf){

i=());

(i*i));

}elseif()==b)

{

i=())+());

(i));

}

}

}

2.按照如下图所示的窗体布局,其中包含Label,TextField,Button控件,Label控件的名称为l1,l2,l3,l4,l5;TextField的大小为10,Button控件的名称如图所示。

所完成的功能是:

单击关闭按钮能够关闭窗体;单击“计算”按钮,将得出1号商品和2号商品的费用并显示在合计文本框中;输入1号商品的价格,如果2号商品的价格和1号商品价格相同,在1号商品显示价格控件中回车,2号商品的价格显示和1号商品相同;输入1号商品的数量,如果2号商品的数量和1号商品数量相同,在1号商品显示数量控件中回车,2号商品的数量和1号商品数量相同。

import.*;

publicclassPrice{

publicPrice(){

Framef=newFrame("商品计费");

Labell1=newLabel("1号商品价格");Labell2=newLabel("数量");

Labell3=newLabel("2号商品价格");Labell4=newLabel("数量");

Labell5=newLabel("合计");用access建立数据库ssm,库中有student表,表中字段有:

学号(number),姓名(name),性别(gender),年龄(age),成绩(score),科目(course)。

(1)使用“insertinto”向表中添加5个你熟悉的同学的学号、姓名、年龄、成绩和科目,并将添加以后的学生信息显示在控制台端。

(2)使用“select”条件查询得到成绩是80分以上的同学信息。

(3)使用“update”将表中学号等于自己学号的成绩修改为85分,并将修改以后的表中所有学生信息显示在控制台端。

(4)使用“delete”将表中成绩高于80分以上的学生信息删除,并将删除以后的表中所有学生信息显示在控制台端。

;

publicclassDataTest{

publicstaticvoidmain(String[]args)throwsSQLException{

try{

}catch(ClassNotFoundExceptione){

();

}

Connectioncon=("jdbc:

odbc:

ssm","test","1234");

Statementstate=();

/*

*/

//ResultSetrs1=("SELECT*FROMstudent");//查找student表中的所有信息

//ResultSetrs2=("select*FROMstudentWHEREscore>80");//选择成绩大于80的学僧

//ResultSetrs3=("select*FROMstudent");

("DELETEFROMstudentWHEREscore>80");//删除分数大于80的学生信息

ResultSetrs4=("select*FROMstudent");

while())

{

}

while())

{

}

while())

{

}*/

while())

{

}

();

}

}

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

当前位置:首页 > 医药卫生 > 药学

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

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