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