java上机实验答案与解析(3)
} }
运行结果:public class Hello{
public static void main(String[] args){ System.out.println(\}
}请目录和输入文本名e:\\\\Hello.text 且会在e目录下产生Hello.text
3.编写BinIoDemo.java的Java应用程序,程序完成的功能是:完成1.doc文件的复制,复制以后的文件的名称为自己的学号姓名.doc。 import java.io.*;
public class BinIoDemo{
public static void main(String[] args) throws IOException{ File f1=new File(\
FileInputStream in=new FileInputStream(f1);
File f2 = new File(\“学号”+“姓名”.doc\ FileOutputStream out=new FileOutputStream(f2); byte[] buf = new byte[2048]; int num = in.read(buf); while (num != (-1)) { out.write(buf, 0, num); out.flush();
num = in.read(buf); }
in.close(); out.close(); } }
实验六 多线程编程
1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。
通过Thread
public class Hw1Thread extends Thread{ String a; private int sleepTime; public Hw1Thread(String a){
super(a); this.a=a; sleepTime=(int)(Math.random()*6000); } public void run(){ int count1=0,count2=0; if(Thread.currentThread().getName().equalsIgnoreCase(\ for(int i=0;i<10;i++){ System.out.println(a); try{ Thread.sleep(sleepTime); count1++; if(count1>count2 && count1==10){ //判断哪个城市的输出次数先达到10,达到则终止输出 System.out.println(\ } else if(count2>count1 && count2==10){ System.out.println(\//判断哪个城市的输出次数先达到10,达到则终止输出 } } catch(InterruptedException exception){};//使用sleep方法需要抛出中断异常 } } if(Thread.currentThread().getName().equalsIgnoreCase(\ for(int i=0;i<10;i++){ System.out.println(a); try{ Thread.sleep(sleepTime); count2++; if(count1>count2 && count1==10){ System.out.println(\ } else if(count2>count1 && count2==10){ System.out.println(\ } } catch(InterruptedException exception){};//使用sleep方法需要抛出中断异常 (也可以在方法头抛出(throw)异常) } } } }
public class Hw1ThreadText { public static void main(String[] args) throws InterruptedException { Hw1Thread td1=new Hw1Thread(\ Hw1Thread td2=new Hw1Thread(\ td1.start(); td2.start(); } }
通过Runnable接口:
public class Hw1Thread implements Runnable{ String a; private int sleepTime; int count1=0,count2=0; public Hw1Thread(String a){ this.a=a; sleepTime=(int)(Math.random()*1000); } public void run(){ //重写run方法 if(Thread.currentThread().getName().equalsIgnoreCase(\ for(int i=0;i<10;i++){ System.out.println(\ try{ Thread.sleep(sleepTime); count1++; } catch(InterruptedException exception){}; //使用sleep方法需要抛出中断异常 if(count1>count2 && count1==10){ System.out.println(\ } else if(count2>count1 && count2==10){ System.out.println(\ } } } if(Thread.currentThread().getName().equalsIgnoreCase(\ for(int i=0;i<10;i++){ System.out.println(\ try{ Thread.sleep(sleepTime); count2++; if(count1>count2 && count1==10){ //判断哪个城市的输出次数先达到10,达到则终止输出
System.out.println(\ } else if(count2>count1 && count2==10){ System.out.println(\//判断哪个城市的输出次数先达到10,达到则终止输出 } catch(InterruptedException exception){};//使用sleep方法需要抛出中断异常 } } } }
public class Hw1ThreadText { public static void main(String[] args) throws InterruptedException { Hw1Thread td1=new Hw1Thread(\ Hw1Thread td2=new Hw1Thread(\ new Thread(td1,\ new Thread(td2,\ } }
2.用继承Thread类方法实现多线程程序。有两个学生小明和小红,小明准备睡10分钟以后开始听课,小红准备睡1小时以后开始听课,雷老师大喊三声“上课了”,小明醒后把小红吵醒,他们开始听课。 学生:小明、小红
睡觉:明:10min 红:60min 老师:喊三声 明醒->红醒。 上课。
class Stu extends Thread{
Thread student1,student2,teacher; Stu() {
teacher=new Thread(this); student1=new Thread(this); student2=new Thread(this); teacher.setName(\雷老师\ student1.setName(\小明\ student2.setName(\小红\ }
public void run() {
if(Thread.currentThread()==student1) {
try{ System.out.println(student1.getName()+\正在睡觉\
Thread.sleep(1000*60*10); }
catch(InterruptedException e) {
System.out.println(student1.getName()+\被老师叫醒了\ }
System.out.println(student1.getName()+\开始听课\ }
if(Thread.currentThread()==student2) {
try{ System.out.println(student2.getName()+\正在睡觉\ Thread.sleep(1000*60*60); }
catch(InterruptedException e) {
System.out.println(student2.getName()+\被小明叫醒了\ }
System.out.println(student2.getName()+\开始听课\ }
else if(Thread.currentThread()==teacher) { for(int i=1;i<=3;i++) {
System.out.println(\上课!\ try { Thread.sleep(500); }
catch(InterruptedException e){} }
student1.interrupt(); //吵醒student student2.interrupt(); } } }
public class A{
public static void main(String args[]) { Stu s=new Stu(); s.student1.start(); s.student2.start(); s.teacher.start(); } }
实验七 图像用户界面
1.编程实现如下界面:当在第一个文本框中输入直接回车,会在第二个文本框中得第一个文本框输入值的平方,单击求和,会在第二个文本框中得到 …… 此处隐藏:1985字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [实用模板]第八章:法国“新浪潮”与“左岸派”
- [实用模板]2021年北京上半年临床医学检验技师生物
- [实用模板]SAP GUI 7.10客户端安装配置文档
- [实用模板]2001年临床执业医师资格考试综合笔试试
- [实用模板]36机场工作实用英语词汇总结
- [实用模板](一)社会保险稽核通知书
- [实用模板]安全教育主题班会材料
- [实用模板]濉溪县春季呼吸道传染病防控应急演练方
- [实用模板]长沙房地产市场周报(1.30-2.3)
- [实用模板]六年级数学上册典中点 - 图文
- [实用模板]C程序设计(红皮书)习题官方参考答案
- [实用模板]中国证监会第一届创业板发行审核委员会
- [实用模板]桥梁工程复习题
- [实用模板]2011学而思数学及答案
- [实用模板]初中病句修改专项练习
- [实用模板]监理学习知识1 - 图文
- [实用模板]小机灵杯四年级试题
- [实用模板]国贸专业毕业论文模板
- [实用模板]教育学概论考试练习题-判断题4
- [实用模板]2015届高考英语一轮复习精品资料(译林
- 00Nkmhe_市场营销学工商管理_电子商务_
- 事业单位考试法律常识
- 诚信教育实施方案
- 吉大小天鹅食品安全检测箱方案(高中低
- 房地产销售培训资料
- 高一地理必修1复习提纲
- 新概念英语第二册lesson_1_练习题
- 证券公司内部培训资料
- 小学英语时间介词专项练习
- 新世纪英语专业综合教程(第二版)第1册U
- 【新课标】浙教版最新2018年八年级数学
- 工程建设管理纲要
- 外研版 必修一Module 4 A Social Surve
- Adobe认证考试 AE复习资料
- 基于H.264AVC与AVS标准的帧内预测技术
- 《食品检验机构资质认定管理办法》(质
- ABB变频器培训课件
- (完整版)小学说明文阅读练习题及答案
- 深思洛克(SenseLock) 深思IV,深思4,深
- 弟子规全文带拼音




