程序答题总结Word文档下载推荐.docx
- 文档编号:14342169
- 上传时间:2022-10-22
- 格式:DOCX
- 页数:13
- 大小:18.63KB
程序答题总结Word文档下载推荐.docx
《程序答题总结Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《程序答题总结Word文档下载推荐.docx(13页珍藏版)》请在冰豆网上搜索。
=null)
{
Stringstr=newString(aline);
char[]s=newchar[str.length()];
s=str.toCharArray();
for(inti=0;
i<
str.length();
i++)
{
if(s[i]>
='
a'
&
s[i]<
z'
)
{
k++;
}
elseif(s[i]>
A'
&
Z'
m++;
}
StringSTR=str.toUpperCase();
bw.write(STR+"
\n"
}
br.close();
bw.close();
System.out.println("
小写字母的个数为:
"
System.out.println(k);
大写字母的个数为:
System.out.println(m);
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
实验题5对象输入与输出流
将Student类的一个实例写到文件中student.txt中,并从student.txt
中读取这个实例,
packageobjectobject;
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());
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;
publicclassWriteObject{
Studentstu=newStudent(981036,"
LiMing"
16,"
CSD"
FileOutputStreamfo=newFileOutputStream("
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();
2"
3"
4"
5"
6"
7"
8"
9"
10"
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;
sum="
+sum);
}
classCCustomerextendsThread//CCustomer类,继承自Thread类
publicvoidrun(){//run()method
for(inti=1;
=4;
i++)
CBank.add(1000);
//System.out.println("
+CBank);
//将1000元分三次汇入
publicclassbank
publicstaticvoidmain(Stringargs[])
{
CCustomerc1=newCCustomer();
CCustomerc2=newCCustomer();
c1.start();
c2.start();
任务三:
生产者和消费者问题
生产者生产面包,消费者消费面包,“生产”和“消费”各代表一个进程,当生产者没有生产出面包的时候,消费者不能消费面包。
当消费者没有消费完面包的时候,生产者不能再生产。
packageProduct;
publicclassThreadAextendsThread{
Waterwater;
publicThreadA(WaterwaterArg){
water=waterArg;
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);
//设置水塘有水状态
生产完毕。
synchronized(water){
water.notify();
//线程调用notify()方法
publicclassThreadBextendsThread{
publicThreadB(WaterwaterArg){
启动消费"
if(water.isEmpty()){//如果水塘无水
synchronized(water){
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序 答题 总结