java实验报告

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《Java程序设计》实验指导书计算机科学与软件学院
[ 软件151 ]
[ 张路]
[ 153200 ]
实验四图形用户界面程序设计
实验目的:掌握组件的使用方法,理解委托事件处理模型。

熟悉图形用户界面基本组件的使用方法,熟悉如何使用布局管理器对组件进行管理及如何使用Java 的事件处理机制。

实验内容:
1、输入一个整数,分别显示其百位、十位和个位数字,图形用户界面
如图4.1所示。

要求:整数文本行可编辑且能实现事件处理,当输
入数据错误时,处理异常,弹出对话框,提示重新输入信息;其他
文本行仅用于显示不可编辑。

1.import java.awt.*;
2.import java.awt.event.*;
3.import javax.swing.*;
4.
5.
6.public class ex_1 extends JFrame implements ActionListener
7.{
8./**
9. *
10. */
11.private static final long serialVersionUID = 1L;
12.private MessageDialog dialog; //对话框内部类对象
13.TextField text_num,text_hun,text_dec,text_uni;
14.
15.public ex_1()
16.{
17.super("显示整数数字");
18.this.setSize(300,240);
19.this.setLocation(300,240);
20.this.setBackground(Color.lightGray); //背景颜色:亮灰
21.//窗口关闭按钮,结束程序运行
22.this.setDefaultCloseOperation(3);
23.this.setLayout(new GridLayout(4,2)); //流体布局:4行2列
24.
25.this.add(new Label("整数 ",0));
26.text_num=new TextField();
27.this.add(text_num);
28.text_num.addActionListener(this); //注
册文本编辑事件监视器
29.
30.this.add(new Label("百位 ",0));
31.text_hun=new TextField("");
32.text_hun.setEditable(false); //只显示,不允许编辑
33.this.add(text_hun);
34.
35.this.add(new Label("十位 ",0));
36.text_dec=new TextField("");
37.text_dec.setEditable(false);
38.this.add(text_dec);
39.
40.this.add(new Label("个位 ",0));
41.text_uni=new TextField("");
42.text_uni.setEditable(false);
43.this.add(text_uni);
44.
45.this.setVisible(true);
46.dialog=new MessageDialog(this);
47.}
48.
49.
50.private class MessageDialog extends JDialog
51.{
52./**
53. *
54. */
55.private static final long serialVersionUID = 1L;
56.Frame frame; //对话框所依赖的框架窗口
57.Label label; //对话框中显示信息
58.MessageDialog(Frame frame)
59.{
60.super(frame,"消息",true);
61.this.frame=frame;
62.this.setSize(300, 80);
63.label=new Label("",Label.CENTER);
64.this.add(label);
65.this.setDefaultCloseOperation(HIDE_ON_CLOSE);
66.}
67.public void show(String string)
68.{
69.label.setText(string);
70.this.setLocation(frame.getX()+100,
frame.getY()+100);
71.this.setVisible(true);
72.}
73.
74.}
75.
76.public void actionPerformed(ActionEvent e)
77.{
78.try
79.{
80.int x=(int)
Double.parseDouble(text_num.getText());
81.text_uni.setText(uni(x));
82.text_dec.setText(dec(x));
83.text_hun.setText(hun(x));
84.}
85.catch(NumberFormatException nfe)
86.{
87.dialog.show("\""+text_num.getText()+"\"不能转换为整数,请重新输入");
88.}
89.finally{}
90.}
91.
92.public static String uni(int x)
93.{
94.String result=""+x%10;
95.return result;
96.}
97.public static String hun(int x)
98.{
99.int y=(x%1000-x%100)/100;
100.String result=""+y;
101.return result;
102.}
103.public static String dec(int x)
104.{
105.int y=(x%100-x%10)/10;
106.String result=""+y;
107.return result;
108.}
109.
110.public static void main(String arg[])
111.{
112.new ex_1();
113.}
114.}
2、模拟实现一个可视化的简单计算器,至少提供进行加法、减法、乘
法、除法等基本运算的功能,希望能支持加正负号、求平方根、清
零等其他功能。

1.import java.awt.*;
2.import java.awt.event.*;
3.import javax.swing.*;
4.
5.public class calculator extends JFrame implements ActionListener {
6.
7.private static final long serialVersionUID =
-169068472193786457L;
8.
9.private class WindowCloser extends WindowAdapter{
10.public void windowClosing(WindowEvent we){
11.System.exit(0);
12.}
13.}
14.
15.
16.private final String[] str = { "sqrt","+/-","CE","C","7",
"8", "9", "/", "4", "5", "6", "*", "1","2", "3", "-", ".", "0", "=", "+" };
17.JButton[] buttons = new JButton [str.length];
18.JTextField display = new JTextField("0");
19.double number = 0.0;
20.String operator = "=";
21.boolean isFirstDigit = true;
22.
23.public calculator() {
24.super("Calculator");
25.this.setSize(800,600);
26.this.setLocation(300,240);
27.
28.JPanel panel1 = new JPanel(new GridLayout(5, 4)); //面板1 网格布局
29.for(int i = 0; i < str.length; i++) { //加入数字符号键
30.buttons[i] = new JButton(str[i]);
31.panel1.add(buttons[i]);
32.}
33.
34.JPanel panel2 = new JPanel(new BorderLayout()); //面板2 边界布局
35.panel2.add("Center", display); //加入显示框
36.
37.getContentPane().setLayout(new BorderLayout());
38.getContentPane().add("North", panel2);
39.getContentPane().add("Center", panel1);
40.for (int i = 0; i < str.length; i++)
41.buttons[i].addActionListener(this);
42.display.addActionListener(this);
43.addWindowListener(new WindowCloser());
44.setVisible(true);
45.pack();
46.}
47.public void actionPerformed(ActionEvent e) {
48.String label = e.getActionCommand(); //调取按钮上字符串
49.if (label == "CE"||label == "C")
50.handleReset(); //清零
51.else if ("0123456789.".indexOf(label) >= 0)
52.handleNumber(label); //数字的输入
53.else if(label =="sqrt"||label=="+/-")
54.handleMonocular(label); //单目运算
55.else
56.handleOperator(label); //双目运算+-*/
57.}
58.
59.private void handleMonocular(String key) {
60.if (key.equals("+/-"))
61.number = -Double.valueOf(display.getText());
62.else if (key.equals("sqrt"))
63.number =
Math.sqrt(Double.valueOf(display.getText()));
64.display.setText(String.valueOf(number));
65.}
66.
67.public void handleNumber(String key) {
68.if (isFirstDigit)
69.display.setText(key);
70.else if ((key.equals(".")) &&
(display.getText().indexOf(".") < 0))
71.display.setText(display.getText() + ".");
72.else if (!key.equals("."))
73.display.setText(display.getText() + key);
74.isFirstDigit = false;
75.}
76.
77.public void handleReset() {
78.display.setText("0");
79.isFirstDigit = true;
80.operator = "=";
81.}
82.
83.public void handleOperator(String key) {
84.if (operator.equals("-"))
85.number -= Double.valueOf(display.getText());
86.else if (operator.equals("*"))
87.number *= Double.valueOf(display.getText());
88.else if (operator.equals("/"))
89.number /= Double.valueOf(display.getText());
90.else if (operator.equals("+"))
91.number += Double.valueOf(display.getText());
92.else if (operator.equals("="))
93.number = Double.valueOf(display.getText());
94.display.setText(String.valueOf(number));
95.operator = key;
96.isFirstDigit = true;
98.
99.public static void main(String[] args)
100.{
101.new calculator();
102.}
103.}
3、设计图形页面实现学生的信息录入,至少包括姓名、年龄、出生年
月日、java课程实验成绩,成绩使用浮点数,年龄使用整型,使用
数据存储输入对象,程序输出按年龄排序的学生信息。

1.public class student {
2.String name;
3.int age;
4.double score;
5.String birth;
6.public student(String name,int age,double score,String birth){
7.this.set(name, age, score, birth);
8.}
9.public void set(String name,int age,double score,String birth){
10.=name;
11.this.age=age;
12.this.score=score;
13.this.birth=birth;
14.}
15.public String getname(){
16.return name;
17.}
18.public int getage(){
19.return age;
20.}
21.public double getscore(){
22.return score;
24.public String getbirth(){
25.return birth;
26.}
27.public String toString(){
28.return
this.getname()+"\t"+this.getage()+"\t"+this.getbirth()+"\t"+this.getscore() +"\n";
29.}
30.
31./*public static void main(String arg[]){
32.student a[]=new student[100];
33.a[0]=new student("1",1,12,"123");
34.a[1]=new student("1",1,12,"123");
35.a[2]=new student("1",1,12,"123");
36.System.out.print(a[0].toString());
37.}*/
38.}
1.import java.awt.*;
2.import java.awt.event.*;
3.
4.
5.public class studentmessage extends Frame implements ActionListener{
6./**
7. *
8. */
9.private static final long serialVersionUID = 1L;
10.private final String[] st = {"Name","Age","Birth","Score"};
11.static int ii=0;
12.student a[]=new student[100];
13.TextField t_name,t_age,t_birth,t_score;
14.MDialog dia;
15.
16.public studentmessage() {
17.super("学生信息");
18.this.setSize(400,400);
19.this.setLocation(300,240);
20.
21.Panel panel1 = new Panel(new GridLayout(4, 1)); 22.for (int i = 0; i < st.length; i++) 23.panel1.add(new Label(st[i],0));
24.
25.Panel panel2 = new Panel(new GridLayout(4, 1)); 26.t_name =new TextField("",20);
27.t_age =new TextField("");
28.t_birth=new TextField("");
29. t_score=new TextField("");
30.panel2.add(t_name);
31.panel2.add(t_age);
32.panel2.add(t_birth);
33.panel2.add(t_score);
34.
35.Panel panel3 = new Panel(new FlowLayout()); 36.Button b_save = new Button("Save");
37.Button b_check= new Button("Check");
38.panel3.add(b_save);
39.panel3.add(b_check);
40.
41.this.setLayout(new BorderLayout());
42.this.add("West", panel1);
43.this.add("East", panel2);
44.this.add("South", panel3);
45.
46.addWindowListener(new WindowCloser());
47.b_save.addActionListener(this);
48.b_check.addActionListener(this);
49.this.setVisible(true);
50.dia=new MDialog(this);
51.pack();
52.}
53.
54.private class WindowCloser extends WindowAdapter{ 55.public void windowClosing(WindowEvent we){ 56.System.exit(0);
57.}
58.}
59.
60.private class MDialog extends Dialog{
61.
62.private static final long serialVersionUID = 1L;
63.Frame frame; //对话框所依赖的框架窗口
64.TextArea t_show;
65.MDialog(Frame frame){
66.super(frame,"记录",true);
67.this.frame=frame;
68.this.setSize(300, 80);
69.t_show=new TextArea(20,20);
70.this.add(t_show);
71.this.addWindowListener(new WindowCloser());
72.}
73.public void show(String s) {
74.t_show.setText(s);
75.this.setLocation(frame.getX()+100,
frame.getY()+100);
76.this.setVisible(true);
77.}
78.}
79.
80.public void actionPerformed(ActionEvent e) {
81.String label = e.getActionCommand();
82.if(label=="Save"){
83.String name=t_name.getText();
84.int age =Integer.parseInt(t_age.getText());
85.double
score=Double.parseDouble(t_score.getText());
86.String birth=t_birth.getText();
87.a[ii]=new student(name,age,score,birth);
88.ii++;
89.t_name .setText("");
90.t_age .setText("");
91.t_birth.setText("");
92.t_score.setText("");
93.selectsort(a);
94.}
95.if(label=="Check"){
96.dia.show(this.toshow());
97.}
98.
99.}
100.
101.public static student[] selectsort(student[] ex){ 102.for(int i=0;i<ii-1;i++){
103.int min=i;
104.for(int j=i+1;j<ii;j++){
105.if(ex[min].getage()>ex[j].getage()) 106.min=j;
107.}
108.if(min!=i){
109.student temp=ex[i];
110.ex[i]=ex[min];
111.ex[min]=temp;
112.}
113.}
114.return ex;
115.
116.}
117.
118.public String toshow(){
119.String s="姓名年龄生日成绩\n"; 120.for(int i=0;i<ii;i++)
121.s+=a[i].toString();
122.return s;
123.}
124.
125.public static void main(String arg[]){
126.new studentmessage();
127.}
128.}
实验五多线程程序设计
实验目的:理解多线程的概念,掌握创建、管理和控制Java线程对象的方法,包括创建Java线程对象、改变线程状态、设置线程优先级及控制线程调度等方法,掌握实现线程互斥和线程同步的方法。

实验内容:
1、编写一个有两个线程的程序,第一个线程用来计算2~1000之间的偶数及
个数,第二个线程用来计算1000~2000之间的偶数及个数。

1.public class ex_1 extends Thread{
2.
3.private int first , end, count;
4.
5.public ex_1(String name,int first,int end){
6.super(name);
7.this.first=first;
8.this.end =end;
9. }
10.public ex_1(String name){
11.this(name,0,0);
12.}
13.
14.public int getcount(){
15.return count;
16.}
17.
18.public void run(){
19.System.out.print("\n"+this.getName()+":");
20.if(first%2==1)
21.first++;
22.for(int i=first;i<=end;i+=2){
23.System.out.print(i+" ");
24.count++;
25.}
26.System.out.print("\n"+this.getName()+"结束,偶数个数
"+this.getcount()+" ");
27.}
28.
29.public static void main(String[] arg){
30.ex_1 thread1 = new ex_1("线程1",2,1000);
31.ex_1 thread2 = new ex_1("线程2",1000,2000);
32.thread1.start();
33.thread2.start();
34.}
35.}
线程1:
线程2:1000 1002 1004 2 1006 1008 4 1010 1012 6 8 10 12 14 16 18 20 22 1014 1016 1018 24 1020 1022 26 1024 1026 28 1028 1030 30 1032 32 1034 34 1036 36 1038 38 1040 1042 40 1044 42 1046 44 1048 46 1050 1052 1054 1056 1058 1060 1062 1064 1066 1068 1070 1072 1074 48 1076 50 1078 52 1080 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 1082 1084 1086 1088 1090 1092 1094 1096 1098 1100 1102 1104 1106 1108 1110 1112 1114 1116 1118 1120 1122 1124 1126 1128 1130 1132 1134 1136 1138 1140 1142 1144 1146 1148 1150 1152 1154 1156 1158 1160 1162 1164 1166 1168 1170 1172 1174 1176 1178 1180 1182 1184 1186 1188 1190 1192 1194 1196 1198 1200 1202 1204 1206 1208 1210 1212 1214 1216 1218 1220 1222 1224 1226 1228 1230 1232 1234 1236 106 1238 108 1240 1242 110 1244 1246 112 114 1248 1250 116 1252 1254 118 1256 1258 120 1260 1262 122 1264 1266 124 1268 1270
126 1272 1274 128 1276 1278 130 **** **** 132 **** **** 134 **** **** 1292 1294 136 **** **** 138 **** **** 1304 140 1306 1308 142 1310 1312 144 1314 1316 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198
200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230
232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262
264 266 268 270 272 274 276 278 280 282 284 286 288 290 292 294
296 298 300 302 304 306 308 310 312 314 316 318 320 322 324 326
328 330 332 334 336 338 340 342 344 346 348 350 352 354 356 358
360 362 364 366 368 370 372 374 376 378 380 382 384 386 388 390
392 394 396 398 400 402 404 406 408 410 412 414 416 418 420 422
424 426 428 430 432 434 436 438 440 442 444 446 448 450 452 454
456 458 460 462 464 466 468 470 472 474 476 478 480 482 484 486
488 490 492 494 496 498 500 502 504 506 508 510 512 514 516 518
520 522 524 526 528 530 532 534 536 538 540 542 544 546 548 550
552 554 556 558 560 1318 1320 562 564 566 568 570 572 574 576 578 580 582 584 586 588 590 592 594 596 598 600 602 604 606 608 610
612 614 616 618 620 622 624 626 628 630 632 634 636 638 640 642
644 646 648 650 652 654 656 658 660 662 664 666 668 670 672 674
676 678 680 682 684 686 688 690 692 694 696 698 700 702 704 706
708 710 712 714 716 718 720 722 724 726 728 730 732 734 736 738
740 742 744 746 748 750 752 754 756 758 760 762 764 766 768 770
772 774 776 778 780 782 784 786 788 790 792 794 796 798 800 802
804 806 808 810 812 814 816 818 820 822 824 826 828 830 832 834
836 838 840 842 844 846 848 850 852 854 856 858 860 862 864 866
868 870 872 874 876 878 880 882 884 886 888 890 892 894 896 898
900 902 904 906 908 910 912 914 916 918 920 922 924 926 928 930
932 934 936 938 940 942 944 946 948 950 952 954 956 958 960 962
964 966 968 970 972 974 976 978 980 982 984 986 988 990 992 994
996 998 1000 1322 1324 1326 1328 1330 1332 1334 1336 1338 1340 1342 1344 1346 1348 1350 1352 1354 1356 1358 1360 1362 1364 1366 1368 1370 1372 1374 1376 1378 1380 1382
线程1结束,偶数个数 500 1384 1386 1388 1390 1392 1394 1396 1398 1400 1402 1404 1406 1408 1410 1412 1414 1416 1418 1420 1422 1424 1426 1428 1430 1432 1434 1436 1438 1440 1442 1444 1446 1448 1450 1452 1454 1456 1458 1460 1462 1464 1466 1468 1470 1472 1474 1476 1478 1480 1482 1484 1486 1488 1490 1492 1494 1496 1498 1500 1502 1504 1506 1508 1510 1512 1514 1516 1518 1520 1522 1524 1526 1528 1530 1532 1534 1536 1538 1540 1542 1544 1546 1548 1550 1552 1554 1556 1558 1560 1562 1564 1566 1568 1570 1572 1574 1576 1578 1580 1582 1584 1586 1588 1590 1592 1594 1596 1598 1600 1602 1604 1606 1608 1610 1612 1614 1616 1618 1620 1622 1624 1626 1628 1630 1632 1634 1636 1638 1640 1642 1644 1646 1648 1650 1652 1654 1656 1658 1660 1662 1664 1666 1668 1670 1672 1674 1676 1678 1680 1682 1684 1686 1688 1690 1692 1694 1696 1698 1700 1702 1704 1706 1708 1710 1712 1714 1716 1718 1720 1722 1724 1726 1728 1730 1732 1734 1736 1738 1740 1742 1744 1746 1748 1750 1752 1754 1756 1758 1760 1762 1764 1766 1768 1770 1772 1774 1776 1778 1780 1782 1784 1786 1788 1790 1792 1794 1796 1798 1800 1802 1804 1806 1808 1810 1812 1814 1816 1818 1820 1822 1824 1826 1828 1830 1832 1834 1836 1838 1840 1842 1844 1846 1848 1850 1852 1854 1856 1858 1860 1862 1864 1866 1868 1870 1872 1874 1876 1878 1880 1882 1884 1886 1888 1890 1892 1894 1896 1898 1900 1902 1904 1906 1908 1910 1912 1914 1916 1918 1920 1922 1924 1926 1928 1930 1932 1934 1936 1938 1940 1942 1944 1946 1948 1950 1952 1954 1956 1958 1960 1962 1964 1966 1968 1970 1972 1974 1976 1978 1980 1982 1984 1986 1988 1990 1992 1994 1996 1998 2000
线程2结束,偶数个数 501
2、编写一个Java应用程序,在主线程中再创建两个线程,要求线程经历四
种状态:新建,运行、中断和死亡。

按模板要求,将【代码1】~【代码8】替换为Java程序代码。

1.Class Rabit extends Thread
2.{
3. int sleepTime=0, liveLength=0;
4. public Rabit(String name,int sleepTime, int liveLength)
5. {
6. super(name);
7. this.sleepTime=sleepTime;
8. this.liveLength=liveLength;
9. }
10. public void run()
12. while (true )
13. {
14. liveLength--;
15. System.out.println("*_*");
16. try{
17. Thread.sleep( sleepTime);
18. }
19. catch (InterruptedException e) {}
20. if (liveLength<=0 )
21. {
22. System.out.println(getName()+"进入死亡状态
\n");
23. break;
24. }
25. }
26. }
27.}
1.class Tortoise extends Thread
2.{ int sleepTime=0, liveLength=0;
3. public Tortoise(String name,int sleepTime, int liveLength) 4. {
5. super(name);
6. this.sleepTime=sleepTime;
7. this.liveLength=liveLength;
8.
9. }
10. public void run()
11. { while (true )
12. {
13. liveLength--;
14. System.out.println("@_@");
15. try{
16. Thread.sleep(sleepTime);
17. }
18. catch (InterruptedException e) {}
19. if (liveLength<=0 )
20. {
21. System.out.println(getName()+"进入死亡状态\n");
22. break;
23. }
24. }
26.}
1.public class ThreadExample {
2. public void main(String arg[])
3. {
4. Rabit rabit;
5. rabit =new Rabit("R",52,100); // 新建线程rabit
6. Tortoise tortoise = new Tortoise("T",30,100); // 新建线程tortoise
7. tortoise.start();// 启动线程tortoise
8. rabit.start();// 启动线程rabit
9. }
10.}
3、编写一个Java应用程序,在主线程中创建三个线程:zhangWorker,
wangWorker 和boss。

线程zhangWorker和wangWorker分别负责在命令行输出“搬运苹果”和“搬运香蕉”,这两个线程分别各自输出20行,每输出一行信息就准备休息10秒钟,但是boss线程负责不让zhangWorker 和wangWorker休息。

按模板要求,将【代码1】~【代码8】替换为Java 程序代码。

1.public class Shop implements Runnable {
2. Thread zhangWork,wangWork,boss;
3. public Shop(){
4.boss=new Thread(this);
5.zhangWork=new Thread(this);
6.wangWork=new Thread(this);
7.boss.setName("老板");
8.zhangWork.setName("张工");
9.wangWork.setName("王工");
10.}
11.p ublic void run(){
12.int i=0;
13.if(Thread.currentThread()==zhangWork){
14.while(true){
15.try{
16.i++;
17.System.out.println(zhangWork.getName()+"已搬运了"+i+"箱苹果");
18.if(i==20)
19.return;
20.Thread.sleep(10000);
21.}
22.catch(InterruptedException e){
23.System.out.print(boss.getName()+"让
"+zhangWork.getName()+"继续工作 ");
24.}
25.}
26.}
27.e lse if(Thread.currentThread()==wangWork){
28.while(true){
29.try{
30.i++;
31.System.out.println(wangWork.getName()+"已搬运了"+i+"箱香蕉");
32.if(i==20)
33.return;
34.Thread.sleep(10000);}
35.catch(InterruptedException e){
36.System.out.print(boss.getName()+"让
"+wangWork.getName()+"继续工作 ");}
37.}
38.}
39.e lse if(Thread.currentThread()==boss){
40.while(true){
41.wangWork.interrupt();//5
42.zhangWork.interrupt();//6
43.if(!(wangWork.isAlive()||zhangWork.isAlive())){
44.System.out.print(boss.getName()+"下班");
45.return;
46.}
47.}
48.}
49.}
50.}
1.public class ShopExample
2.{
3. public static void main(String a[])
4. { Shop shop=new Shop();
5. shop.wangWork.start();
6. shop.zhangWork.start();
7. shop.boss.start();
8. }
9.}
王工已搬运了1箱香蕉
张工已搬运了1箱苹果
老板让王工继续工作王工已搬运了2箱香蕉
老板让张工继续工作张工已搬运了2箱苹果
老板让张工继续工作张工已搬运了3箱苹果
老板让张工继续工作张工已搬运了4箱苹果
老板让张工继续工作张工已搬运了5箱苹果
老板让张工继续工作张工已搬运了6箱苹果
老板让张工继续工作张工已搬运了7箱苹果
老板让张工继续工作张工已搬运了8箱苹果
老板让张工继续工作张工已搬运了9箱苹果
老板让张工继续工作张工已搬运了10箱苹果
老板让张工继续工作张工已搬运了11箱苹果
老板让张工继续工作张工已搬运了12箱苹果
老板让王工继续工作老板让张工继续工作张工已搬运了13箱苹果
老板让张工继续工作王工已搬运了3箱香蕉
老板让王工继续工作王工已搬运了4箱香蕉
老板让王工继续工作王工已搬运了5箱香蕉
老板让王工继续工作王工已搬运了6箱香蕉
老板让王工继续工作王工已搬运了7箱香蕉
老板让王工继续工作王工已搬运了8箱香蕉
老板让王工继续工作王工已搬运了9箱香蕉
老板让王工继续工作王工已搬运了10箱香蕉
老板让王工继续工作王工已搬运了11箱香蕉
老板让王工继续工作王工已搬运了12箱香蕉
老板让王工继续工作王工已搬运了13箱香蕉
老板让王工继续工作张工已搬运了14箱苹果
王工已搬运了14箱香蕉
老板让张工继续工作张工已搬运了15箱苹果
老板让王工继续工作王工已搬运了15箱香蕉
老板让王工继续工作老板让张工继续工作王工已搬运了16箱香蕉
老板让王工继续工作王工已搬运了17箱香蕉
老板让王工继续工作王工已搬运了18箱香蕉
老板让王工继续工作王工已搬运了19箱香蕉
老板让王工继续工作王工已搬运了20箱香蕉
张工已搬运了16箱苹果
老板让张工继续工作张工已搬运了17箱苹果
老板让张工继续工作张工已搬运了18箱苹果
老板让张工继续工作张工已搬运了19箱苹果
老板让张工继续工作张工已搬运了20箱苹果
老板下班
4、编写一个Java应用程序,模拟5个人排队买票。

售票员只有1张五元的钱,
电影票五元钱一张。

假设5个人的名字及排队顺序是:赵、钱、孙、李、周。

“赵”拿1张二十元的人民币买2张票,“钱”拿1张二十元的人民币买1张票,“孙”1张十元的人民币买1张票,“李”拿1张十元的人民币买2张票,“周”拿1张五元的人民币买1张票。

要求售票员按如下规则找赎:(1)二十元买1张票,找零:1张十元;不许找零2张五元。

(2)二十元买1张票,找零:1张十元,1张五元;不许找零3张五元。

(3)十元买一张票,找零1张五元。

1.public class T implements Runnable{
2.Thread a[]=new Thread[5];
3.int i=0,
4.j=0,
5.k=0;
6.int flag[]={0,0,0,0,0};
7.
8.
9.T()
10.{
11.for(int i=0;i<5;i++)
12.a[i]=new Thread(this);
13.a[0].setName("赵");
14.a[1].setName("钱");
15.a[2].setName("孙");
16.a[3].setName("李");
17.a[4].setName("周");
18.a[0].setPriority(10);
19.a[1].setPriority(8);
20.a[2].setPriority(6);
21.a[3].setPriority(4);
22.a[4].setPriority(2);
23.}
24.
25.
26.public void run(){
27.if(Thread.currentThread()==a[0]){
28.while(true)
29.{
30.if(j>0)
31.{
32.j--;k++;
33.flag[0]=1;
34.System.out.print("赵买票完成\n");
35.if(flag[1]==0&&i>0&&j>0&&a[1].isAlive()) 36.a[1].interrupt();
37.if(flag[1]==0&&a[1].isAlive()==false) 38.a[1].start();
39.return;
40.}
41.else
42.{
43.System.out.print("赵等待\n");
44.try {
45.a[1].start();
46.Thread.sleep(100000);
47.}
48.catch (InterruptedException e)
49.{
50.System.out.print("回顾");
51.}
52.}
53.}
54.}
55.else if(Thread.currentThread()==a[1])
56.while(true)
57.{
58.if(i>0&&j>0)
59.{
60.i--;j--;k++;
61.flag[1]=1;
62.System.out.print("钱买票完成\n"); 63.if(flag[0]==0&&j>0)
64.a[0].interrupt();
65.if(flag[2]==0&&a[2].isAlive()==false) 66.a[2].start();
67.return;
68.}
69.else
70.try {
71.System.out.print("钱等待\n");
72.a[2].start();
73.Thread.sleep(100000);
74.} catch (InterruptedException e) { 75.System.out.print("回顾");
76.}
77.}
78.else if(Thread.currentThread()==a[2])
79.while(true){
80.if(i>0)
81.{
82.i--;j++;
83.flag[2]=1;
84.System.out.print("孙买票完成\n"); 85.if(flag[0]==0&&j>0)
86.a[0].interrupt();
87.if(flag[1]==0&&i>0&&j>0)
88.a[1].interrupt();
89.if(flag[3]==0&&a[3].isAlive()==false) 90.a[3].start();
91.return;
92.}
93.else
94.try {
95.System.out.print("孙等待\n");
96.a[3].start();
97.a[2].sleep(100000);
98.}
99.catch (InterruptedException e){ 100.System.out.print("回顾"); 101.}
102.}
103.else if(Thread.currentThread()==a[3]) 104.while(true){
105.j++;
106.flag[3]=1;
107.System.out.print("李买票完成\n"); 108.back();
109.a[4].start();
110.return;
111.}
112.else if(Thread.currentThread()==a[4]) 113.while(true){
个人精品文档资料
114.i++;
115.flag[4]=1;
116.System.out.print("周买票完成\n");
117.back();
118.return;
119.}
120.}
121.void set(int i,int j,int k)
122.{
123.this.i=i;
124.this.j=j;
125.this.k=k;
126.}
127.void back()
128.{
129.if(flag[0]==0&&j>0)
130.a[0].interrupt();
131.if(flag[1]==0&&i>0&&j>0)
132.a[1].interrupt();
133.if(flag[2]==0&&i>0)
134.a[2].interrupt();
135.}
136.}
1.public class TExample {
2.public static void main(String a[])
3.{
4.T t=new T();
5.t.set(1, 0, 0);
6.t.a[0].start();
7.}
个人精品文档资料8.}
实验结果:
赵等待
钱等待
孙买票完成
回顾赵买票完成
李买票完成
周买票完成
回顾钱买票完成
实验六输入输出流
实验目的:理解文件和流的概念、Java流的层次结构,掌握Java提供的各种字节流类和字符流类的功能和使用方法。

实验内容:
1、读入一个文件的存储的学生信息内容拷贝到另一个文件中去
import java.io.*;
public class FileStreamsTest {
public static void main(String args[]){
try{
FileInputStream fis=new FileInputStream("D:\\einput.txt");//创建文件输入流对象
FileOutputStream fos=new FileOutputStream("D:\\eoutput.txt");//创建文件输出流对象
int c;
while((c=fis.read())!=-1){//输入流未结束时读取一个字节赋给c
fos.write(c);//将c写入输出流
}
fis.close();//关闭输入流
fos.close();//关闭输出流
}
catch(FileNotFoundException e){//打开指定路径名表示的文件失败时,抛出此异常
System.err.println("FileStreamsTest:"+e);
}
catch(IOException e){//读写数据失败时,抛出此异常
System.err.println("FileStreamaTest:"+e);
}
}
}
2、从标准设备中输入若干行多名学生信息,直到输入"bye"结束,将这些字
符串写入文件。

1.import java.io.*;
2.public class Bye{
3. public static void main(String args[])throws IOException{
4. System.out.println("Please input some sentences end with 'bye':");
5. BufferedReader keyin=new BufferedReader(new InputStreamReader(System.in));
6. String s;
7. RandomAccessFile f=new RandomAccessFile("D:\\bye.txt","rw");
8. boolean ss;
9. while((s=keyin.readLine())!=null){
10.ss=s.endsWith("bye");
11.if(ss)
12.System.exit(0);
13.else{
14.System.out.println(s);
15. f.seek(f.length());
16. f.writeUTF(s);
17.}
18.}
19.}
20.}
3、编程完成下列功能:建立两个文件myfiel.txt和myfile2.txt,对文件myfile1.txt输入内容:“I am a student.”,并将其内容拷贝给myfile2.txt。

import java.io.*;
public class ex_3 {
private String filename;
public ex_3(String filename)
{
this.filename=filename;
}
public void writetofile()throws IOException
{
FileWriter fout=new FileWriter(this.filename);
BufferedReader keyin=new BufferedReader(new
InputStreamReader(System.in));
String s=keyin.readLine();
fout.write(s);
System.out.println("写入myfile1");
fout.close();
}
public void copyfile(String filename2)throws IOException
{
FileReader fin=new FileReader(this.filename);
FileWriter fout=new FileWriter(filename2);
BufferedReader bin=new BufferedReader(fin);
String s=bin.readLine();
if(s!=null)
{
fout.write(s);
}
fin.close();
fout.close();
}
public static void main(String arg[]) throws IOException
{
ex_3 a=new ex_3("D:\\myfile1.txt");
a.writetofile();
a.copyfile("D:\\myfile2.txt");
System.out.println("myfile1 copy to myfile2");
}
}
4、从键盘输入一个整型数,一个双精度型和一个字符串,用DataOutputStream 将这些数据输出到文件中,然后用DataInputStream从文件中读出这些数据并打印到标准输出设备。

1.import java.io.*;
2.
3.public class TestOutputStream {
4.private String filename;
5.public TestOutputStream(String filename)
6.{
7.this.filename = filename;
8.}
9.public void writetofile()throws IOException
10.{。

相关文档
最新文档