程序答题总结.docx

上传人:b****2 文档编号:1426161 上传时间:2022-10-22 格式:DOCX 页数:13 大小:18.63KB
下载 相关 举报
程序答题总结.docx_第1页
第1页 / 共13页
程序答题总结.docx_第2页
第2页 / 共13页
程序答题总结.docx_第3页
第3页 / 共13页
程序答题总结.docx_第4页
第4页 / 共13页
程序答题总结.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

程序答题总结.docx

《程序答题总结.docx》由会员分享,可在线阅读,更多相关《程序答题总结.docx(13页珍藏版)》请在冰豆网上搜索。

程序答题总结.docx

程序答题总结

实验题4写一程序统计纯文本文件“Early-Precaution.txt”的大写字母、小写字母个数,并将所有小写字母转换为大写字母,输出到result.txt。

代码:

importjava.io.*;

publicclassGetchar{

FileReaderfr;

publicstaticvoidmain(String[]args){

FileReaderfr;

try{

fr=newFileReader("F:

\\Early-Precaution.txt");

Filefile=newFile("F:

\\result.txt");

FileWriterfos=newFileWriter(file);

BufferedReaderbr=newBufferedReader(fr);

BufferedWriterbw=newBufferedWriter(fos);

Stringaline;

intk=0;

intm=0;

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

=null)

{

Stringstr=newString(aline);

char[]s=newchar[str.length()];

s=str.toCharArray();

for(inti=0;i

{

if(s[i]>='a'&&s[i]<='z')

{

k++;

}

elseif(s[i]>='A'&&s[i]<='Z')

{

m++;

}

}

StringSTR=str.toUpperCase();

bw.write(STR+"\n");

}

br.close();

bw.close();

System.out.println("小写字母的个数为:

");

System.out.println(k);

System.out.println("大写字母的个数为:

");

System.out.println(m);

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

实验题5对象输入与输出流

将Student类的一个实例写到文件中student.txt中,并从student.txt 中读取这个实例,

代码:

packageobjectobject;

importjava.io.*;

publicclassReadObject{

publicstaticvoidmain(Stringargs[]){

Studentstu=null;

try{

FileInputStreamfi=newFileInputStream("c:

\\date.ser");

ObjectInputStreamsi=newObjectInputStream(fi);

stu=(Student)si.readObject();

si.close();

System.out.println("ID:

"+stu.id+"name:

"+

stu.name+"age:

"+stu.age+"dept.:

"+stu.department);

}catch(Exceptione){

System.out.println(e.toString());}

}

}

packageobjectobject;

importjava.io.Serializable;

classStudentimplementsSerializable{

privatestaticfinallongserialVersionUID=1L;

intid;

intage;

Stringname;

Stringdepartment;

publicStudent(intid,Stringname,intage,Stringdepartment){

this.id=id;

this.name=name;

this.age=age;

this.department=department;

}

}

packageobjectobject;

importjava.io.*;

publicclassWriteObject{

publicstaticvoidmain(Stringargs[]){

Studentstu=newStudent(981036,"LiMing",16,"CSD");

try{

FileOutputStreamfo=newFileOutputStream("c:

\\date.ser");

ObjectOutputStreamso=newObjectOutputStream(fo);

so.writeObject(stu);

so.close();

}catch(Exceptione){}

}

}

任务一:

火车售票

假设有火车票1000张,创建10个线程模拟10个售票点,每个售票点100毫秒买一张票。

打印出售票过程,注意使用synchronized确保同一张票只能卖出一次。

代码:

publicclassTaskone{

publicstaticvoidmain(String[]args){

Yxt=newYx();

newThread(t,"1").start();

newThread(t,"2").start();

newThread(t,"3").start();

newThread(t,"4").start();

newThread(t,"5").start();

newThread(t,"6").start();

newThread(t,"7").start();

newThread(t,"8").start();

newThread(t,"9").start();

newThread(t,"10").start();

}

}

publicclassYxextendsThread{

intticket=200;

Stringname="";

publicvoidrun(){

while(true){

synchronized(name){

if(ticket<=0)

break;

System.out.println("第"+Thread.currentThread().getName()

+"售票点卖出第"+ticket--+"张票");

try{

Thread.sleep(100);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

}

}

任务二:

银行存款

假设某家银行,它可接受顾客的汇款,每做一次汇款,便可计算出汇款的总额。

现有两个顾客,每人都分3次,每次100元将钱到入。

试编写一个程序,模拟实际作业。

代码:

classCBank

{

privatestaticintsum=0;

publicstaticvoidadd(intn){

inttmp=sum;

tmp=tmp+n;//累加汇款总额

try{

Thread.sleep((int)(10000*Math.random()));//小睡几秒钟

}

catch(InterruptedExceptione){}

sum=tmp;

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

}

}

classCCustomerextendsThread//CCustomer类,继承自Thread类

{

publicvoidrun(){//run()method

for(inti=1;i<=4;i++)

CBank.add(1000);

//System.out.println("sum="+CBank);//将1000元分三次汇入

}

}

publicclassbank

{

publicstaticvoidmain(Stringargs[])

{

CCustomerc1=newCCustomer();

CCustomerc2=newCCustomer();

c1.start();

c2.start();

}

}

任务三:

生产者和消费者问题

生产者生产面包,消费者消费面包,“生产”和“消费”各代表一个进程,当生产者没有生产出面包的时候,消费者不能消费面包。

当消费者没有消费完面包的时候,生产者不能再生产。

代码:

packageProduct;

publicclassThreadAextendsThread{

Waterwater;

publicThreadA(WaterwaterArg){

water=waterArg;

}

publicvoidrun(){

System.out.println("开始生产……");

for(inti=1;i<=5;i++){//循环5次

try{

Thread.sleep(1000);//休眠1秒,模拟1分钟的时间

System.out.println(i+"个");

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

water.setWater(true);//设置水塘有水状态

System.out.println("生产完毕。

");

synchronized(water){

water.notify();//线程调用notify()方法

}

}

}

packageProduct;

publicclassThreadBextendsThread{

Waterwater;

publicThreadB(WaterwaterArg){

water=waterArg;

}

publicvoidrun(){

System.out.println("启动消费");

if(water.isEmpty()){//如果水塘无水

synchronized(water){

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

当前位置:首页 > 经管营销 > 人力资源管理

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

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