Java实验程序及报告.docx

上传人:b****1 文档编号:14864804 上传时间:2023-06-28 格式:DOCX 页数:31 大小:238.40KB
下载 相关 举报
Java实验程序及报告.docx_第1页
第1页 / 共31页
Java实验程序及报告.docx_第2页
第2页 / 共31页
Java实验程序及报告.docx_第3页
第3页 / 共31页
Java实验程序及报告.docx_第4页
第4页 / 共31页
Java实验程序及报告.docx_第5页
第5页 / 共31页
Java实验程序及报告.docx_第6页
第6页 / 共31页
Java实验程序及报告.docx_第7页
第7页 / 共31页
Java实验程序及报告.docx_第8页
第8页 / 共31页
Java实验程序及报告.docx_第9页
第9页 / 共31页
Java实验程序及报告.docx_第10页
第10页 / 共31页
Java实验程序及报告.docx_第11页
第11页 / 共31页
Java实验程序及报告.docx_第12页
第12页 / 共31页
Java实验程序及报告.docx_第13页
第13页 / 共31页
Java实验程序及报告.docx_第14页
第14页 / 共31页
Java实验程序及报告.docx_第15页
第15页 / 共31页
Java实验程序及报告.docx_第16页
第16页 / 共31页
Java实验程序及报告.docx_第17页
第17页 / 共31页
Java实验程序及报告.docx_第18页
第18页 / 共31页
Java实验程序及报告.docx_第19页
第19页 / 共31页
Java实验程序及报告.docx_第20页
第20页 / 共31页
亲,该文档总共31页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Java实验程序及报告.docx

《Java实验程序及报告.docx》由会员分享,可在线阅读,更多相关《Java实验程序及报告.docx(31页珍藏版)》请在冰点文库上搜索。

Java实验程序及报告.docx

Java实验程序及报告

Java实验报告

姓名:

班级:

学号:

教师:

实验一

1、JDK安装和熟悉eclipse基本操作

源程序1:

HelloWorl

//Java源程序在E盘

publicclassHelloWorld

{

publicstaticvoidmain(String[]args)

{

System.out.println("HelloWorld");

System.out.println("欢迎进入Java世界");

}

}

JDK配置:

JAVA_HOME(新建):

PATH:

CLASSPATH(新建):

源程序2:

求一元二次方程的解

importjava.util.*;

publicclassdfsgfdg

{

publicstaticvoidmain(String[]args)

{

doubles=0,x1,x2;

System.out.println("输入系数abc:

");

Scannerin=newScanner(System.in);

doublea=in.nextInt();

doubleb=in.nextInt();

doublec=in.nextInt();

s=b*b-4*a*c;

if(a==0)

{

System.out.println("二次方程系数a不能为0!

");

return;

}

elseif(s==0)

{

x1=-b/(2*a);

System.out.println("x1="+x1);

return;

}

elseif(s>0)

{

doublet=Math.sqrt(s);

x1=(-b+t)/(2*a);

x2=(-b-t)/(2*a);

System.out.println("x1="+x1);

System.out.println("x2="+x2);

return;

}

elseif(s<0)

{

System.out.println("该一元二次方程没有实根");

return;

}

}

}

截图:

源程序3:

求三角形面积

importjava.util.*;

publicclassrectangle

{

publicstaticvoidmain(String[]args)

{

while(true)

{

System.out.println("输入边长abc:

");

Scannerin=newScanner(System.in);

doublea=in.nextDouble();

doubleb=in.nextDouble();

doublec=in.nextDouble();

if(a+b<=c||a+c<=b||b+c<=a)

{

System.out.print("yourdateiserror!

\n");

System.out.print("Enteranykeytocontinue...\n");

continue;

}

else

{

doubles=(a+b+c)/2.0;

doublearea=Math.sqrt((s*(s-a)*(s-b)*(s-c)));

System.out.print("area="+area);

break;

}

}

}

}

截图:

源程序4:

输出杨辉三角

publicclassyanghui{

publicstaticvoidmain(String[]args)

{

inttriangle[][]=newint[10][];

for(inti=0;i

triangle[i]=newint[i+1];

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

if(i==0||j==0||j==i){

triangle[i][j]=1;

}

else{

triangle[i][j]=triangle[i-1][j]+triangle[i-1][j-1];

}

System.out.print(triangle[i][j]+"\t");}

System.out.println();

}

}}

截图:

 

实验二

1、要求:

三种方法求椎体体积

1、方法一:

抽象类求锥体体积

源程序:

package抽象类求椎体体积;

abstractclassBottom

{

abstractdoublegetArea();

}

classcircleextendsBottom

{

doubler=10;

doublegetArea()

{

return3.14*r*r;

}

}

classrectextendsBottom

{

doublel=10;

doublew=10;

doublegetArea()

{

returnw*l;

}

}

classcircular_coneextendscircle

{

doubleheight=3;

voidshow()

{

System.out.print("输出圆锥体积:

");

System.out.println(1/3.0*super.getArea()*height);

}

}

classpyramidextendsrect

{

doubleheight=3;

voidshow()

{

System.out.print("输出棱锥体积:

");

System.out.println(1/3.0*height*super.getArea());

}

}

publicclass抽象类求体积{

publicstaticvoidmain(String[]args){

circular_conea=newcircular_cone();

a.show();

pyramidb=newpyramid();

b.show();

}

}

截图:

2、方法二:

接口求锥体体积

源程序:

package接口求椎体体积;

interfaceBottom

{

doublegetArea();

}

classcircleimplementsBottom{

doubler=100;

publicdoublegetArea(){

return3.14*r*r;

}

}

classrectimplementsBottom

{

doublel=100;

doublew=10;

publicdoublegetArea()

{

returnw*l;

}

}

classcircular_coneextendscircle

{

doubleheight=9;

voidshow()

{

System.out.print("输出圆锥体积:

");

System.out.println(1/3.0*super.getArea()*height);

}

}

classpyramidextendsrect

{

doubleheight=3;

voidshow()

{

System.out.print("输出棱锥体积:

");

System.out.println(1/3.0*height*super.getArea());

}

}

publicclass接口求体积{

publicstaticvoidmain(String[]args){

circular_conea=newcircular_cone();

a.show();

pyramidb=newpyramid();

b.show();

}

}

截图:

3、方法三:

泛型类求锥体体积

源程序:

package泛型类求椎体体积;

classcircle

{

doubler=10;

publicStringtoString()

{

return3.14*r*r+"";

}

}

classrect

{

doublel=10;

doublew=10;

publicStringtoString()

{

returnw*l+"";

}

}

classcircular_cone

{

Tt;

doubleheight=600;

publicdoublegetVolume(Tt)

{

this.t=t;

doublearea=Double.parseDouble(t.toString());

return(1/3.0*height*area);

}

}

publicclass泛型类求体积{

publicstaticvoidmain(String[]args){

circular_conea=newcircular_cone();

circular_coneb=newcircular_cone();

circlea1=newcircle();

rectb1=newrect();

System.out.print("圆锥体积:

");

System.out.println(a.getVolume(a1));

System.out.print("棱锥体积:

");

System.out.println(b.getVolume(b1));

}

}

截图:

 

实验三

1、百分制

1、要求:

输入一个百分制成绩,输出等级制成绩;

2、源程序

importjava.util.Scanner;

publicclass等级制{

publicstaticvoidmain(String[]args)

{

//TODOAuto-generatedmethodstub

System.out.print("输入成绩:

");

Scannersc=newScanner(System.in);

intin=sc.nextInt();

if(in<0||in>100)

{

System.out.println("成绩输入有误!

程序退出!

");

sc.close();

return;

}

sc.close();

System.out.print("显示等级:

");

switch(in/10)

{

case10:

case9:

System.out.println("A");break;

case8:

System.out.println("B");break;

case7:

System.out.println("C");break;

case6:

System.out.println("D");break;

default:

System.out.println("E");

}

}

}

3、截图

2、简单线程实验

1、要求:

编写代码:

定义一个线程类,输出0~9这10个数字。

(1)在main()中启动二个线程,要求每个线程分别输出0~9这10个数字。

(2)在main()中启动二个线程,要求两个线程共同输出0~9这10个数字。

1-1、实验一:

源程序

classNumimplementsRunnable{

intnum=9;

publicvoidrun(){

while(true)

{

if(num>=0)

{

System.out.println(Thread.currentThread().getName()+""+num);

num--;

}

else

break;

}

}

}

publicclass输数{

publicstaticvoidmain(String[]args){

Nums1=newNum();

Nums2=newNum();

newThread(s1).start();

newThread(s2).start();

}

}

截图

Thread-09

Thread-19

Thread-08

Thread-18

Thread-07

Thread-17

Thread-06

Thread-05

Thread-04

Thread-03

Thread-02

Thread-01

Thread-00

Thread-16

Thread-15

Thread-14

Thread-13

Thread-12

Thread-11

Thread-10

1-2、实验二:

源程序

classNumimplementsRunnable{

intnum=0;

Objectobj=newObject();

publicvoidrun(){

while(true){

synchronized(obj){

if(num<=9){

try{Thread.sleep(100);}catch(Exceptione){e.printStackTrace();}

System.out.println(Thread.currentThread().getName()+""+num);

num++;

}

}

}

}

}

publicclass输数线程{

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

Nums=newNum();

newThread(s).start();

newThread(s).start();

}

}

截图

Thread-00

Thread-11

Thread-02

Thread-13

Thread-04

Thread-15

Thread-06

Thread-17

Thread-08

Thread-19

 

实验四

1、多线程售票实验

1、要求:

模拟售票:

实现使用100个窗口共同销售10000张票。

2、源程序:

classSellimplementsRunnable{

inttickets=10000;

Objectobj=newObject();

publicvoidrun(){

while(true){

synchronized(obj){

if(tickets>0){

try{Thread.sleep(100);}catch(Exceptione){e.printStackTrace();}

System.out.println(Thread.currentThread().getName()+""+tickets);

tickets--;

}

}

}

}

}

publicclass线程{

publicstaticvoidmain(String[]args){

intindex=0;

Sells=newSell();

while(index<100)

{

newThread(s).start();

index++;

}

}

}

截图

Thread-010000

Thread-819999

Thread-819998

Thread-999997

Thread-989996

Thread-979995

Thread-969994

Thread-959993

Thread-949992

Thread-939991

Thread-929990

Thread-689989

Thread-909988

Thread-919987

Thread-889986

Thread-899985

Thread-879984

Thread-869983

Thread-859982

Thread-859981

 

实验五

一、Java输入输出实验

1、要求:

编写代码:

把a.txt文件中的内容读出来,并写到b.txt文件中。

其中:

2、源程序:

importjava.io.*;

publicclass文件{

publicstaticvoidmain(String[]args){

//Filef=newFile("C:

\\Users\\Administrator\\Desktop\\a.txt");

try{

FileWriterfw=newFileWriter("C:

\\Users\\Administrator\\Desktop\\a.txt");

Stringstr="a"+"\r\n"+"b"+"\r\n"+"c";

fw.write(str);

fw.close();

FileReaderfr=newFileReader("C:

\\Users\\Administrator\\Desktop\\a.txt");

FileWriterfw2=newFileWriter("C:

\\Users\\Administrator\\Desktop\\b.txt");

BufferedReaderbr=newBufferedReader(fr);

BufferedWriterbw=newBufferedWriter(fw2);

Strings="";

inti=1;

while((s=br.readLine())!

=null)

{

//fw2.write("第"+i+"行:

"+s+"\r\n");

bw.write("第"+i+"行:

"+s);

bw.newLine();

i++;

}

bw.close();

br.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

3、截图

实验六

1、简易文本编辑器

1、要求:

如下图:

2、源程序:

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjava.awt.datatransfer.*;

publicclassTxtEditorextendsJFrame

{

JFramefrmAbout;

TextAreataArea;

Stringname=null;

Stringboard=null;

privateClipboardcb;

JPanelPanelNorth,PanelSouth,PanelWest,PanelEast,PanelCenter,PanelLeftFontSize,PanelLeftFontType;

JButtonbtnCopy,btnPaste,btnCut,btnDelete,btnSelectAll,btnHelp;

JRadioButtonjrbFontSize10,jrbFontSize20,jrbFontSize30;

JCheckBoxjcbBold,jcbItalic;

ButtonGroupjbgFontSize;

JLabellblTitle;

publicTxtEditor()throwsException

{

super("简易文本编辑器");

PanelNorth=newJPanel();

PanelSouth=newJPanel();

PanelWest=newJPanel();

PanelEast=newJPanel();

PanelCenter=newJPanel();

PanelLeftFontSize=newJPanel();

PanelLeftFontType=newJPanel();

btnCopy=newJButton("复制");

btnPaste=newJButton("粘贴");

btnCut=newJButton("剪切");

btnDelete=newJButton("删除");

btnSelectAll=newJButton("全选");

btnHelp=newJButton("帮助");

jrbFontSize10=newJRadioButton("10",true);

jrbFontSize20=newJRadioButton("20");

jrbFontSize30=newJRadioButton("30");

jcbBold=newJCheckBox("粗体",false);

jcbItalic=newJCheckBox("斜体",false);

taArea=newTextArea();

jbgFontSize=newButtonGroup();

lblTitle=newJLabel("简易文本编辑器");

jbgFontSize.add(jrbFontSize10);

jbgFontSize.add(jrbFontSize20);

jbgFontSize.add(jrbFontSize30);

PanelNorth.add(lblTitle);

PanelSouth.add(btnCopy);

PanelSouth.ad

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

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2