程序答题总结
- 格式:doc
- 大小:55.50 KB
- 文档页数:12
实验题 4写一程序统计纯文本文件“Early-Precaution.txt”的大写字母、小写字母个数,并将所有小写字母转换为大写字母,输出到result.txt。
代码:import java.io.*;public class Getchar {FileReader fr;public static void main(String[] args) {FileReader fr;try {fr = new FileReader("F:\\Early-Precaution.txt");File file = new File("F:\\result.txt");FileWriter fos =new FileWriter(file);BufferedReader br = new BufferedReader(fr);BufferedWriter bw = new BufferedWriter(fos);String aline;int k=0;int m=0;while((aline = br.readLine())!=null){String str = new String (aline);char []s = new char[str.length()];s=str.toCharArray();for(int i=0;i<str.length();i++){if(s[i]>='a'&& s[i]<='z'){k++;}else if(s[i]>='A' && s[i]<='Z'){m++;}}String STR = 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 (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}实验题 5对象输入与输出流将Student类的一个实例写到文件中student.txt中,并从student.txt中读取这个实例,代码:package objectobject;import java.io.*;public class ReadObject {public static void main(String args[]) {Student stu=null;try {FileInputStream fi=new FileInputStream("c:\\date.ser");ObjectInputStream si=new ObjectInputStream(fi);stu=(Student) si.readObject();si.close();System.out.println("ID: "+stu.id+" name:"++" age:"+stu.age+" dept.:"+stu.department);}catch(Exception e) {System.out.println(e.toString()); }}}package objectobject;import java.io.Serializable;class Student implements Serializable{private static final long serialVersionUID = 1L;int id;int age;String name;String department;public Student(int id, String name,int age,String department){this.id=id;=name;this.age=age;this.department =department;}}package objectobject;import java.io.*;public class WriteObject{public static void main(String args[]) {Student stu=new Student(981036,"Li Ming",16,"CSD");try {FileOutputStream fo=new FileOutputStream("c:\\date.ser");ObjectOutputStream so=new ObjectOutputStream(fo);so.writeObject(stu);so.close();}catch(Exception e) { }}}任务一:火车售票假设有火车票1000张,创建10个线程模拟10个售票点,每个售票点100毫秒买一张票。
打印出售票过程,注意使用synchronized 确保同一张票只能卖出一次。
代码:public class Taskone {public static void main(String[] args){Yx t = new Yx();new Thread(t,"1").start();new Thread(t,"2").start();new Thread(t,"3").start();new Thread(t,"4").start();new Thread(t,"5").start();new Thread(t,"6").start();new Thread(t,"7").start();new Thread(t,"8").start();new Thread(t,"9").start();new Thread(t,"10").start();}}public class Yx extends Thread {int ticket = 200;String name = "";public void run() {while (true) {synchronized (name) {if (ticket <= 0)break;System.out.println("第" +Thread.currentThread().getName()+ "售票点卖出第" + ticket-- + "张票");try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}}}}任务二:银行存款假设某家银行,它可接受顾客的汇款,每做一次汇款,便可计算出汇款的总额。
现有两个顾客,每人都分3次,每次100元将钱到入。
试编写一个程序,模拟实际作业。
代码:class CBank{private static int sum=0;public static void add(int n){int tmp=sum;tmp=tmp+n; // 累加汇款总额try{Thread.sleep((int)(10000*Math.random())); // 小睡几秒钟}catch(InterruptedException e){}sum=tmp;System.out.println("sum= "+sum);}}class CCustomer extends Thread // CCustomer类,继承自Thread类{public void run(){ // run() methodfor(int i=1;i<=4;i++)CBank.add(1000);// System.out.println("sum= "+CBank); // 将1000元分三次汇入}}public class bank{public static void main(String args[]){CCustomer c1=new CCustomer();CCustomer c2=new CCustomer();c1.start();c2.start();}}任务三:生产者和消费者问题生产者生产面包,消费者消费面包,“生产”和“消费”各代表一个进程,当生产者没有生产出面包的时候,消费者不能消费面包。
当消费者没有消费完面包的时候,生产者不能再生产。
代码:package Product;public class ThreadA extends Thread {Water water;public ThreadA(Water waterArg) {water = waterArg;}public void run() {System.out.println("开始生产……");for (int i = 1; i <= 5; i++) { // 循环5次try {Thread.sleep(1000); // 休眠1秒,模拟1分钟的时间System.out.println(i + "个");} catch (InterruptedException e) {e.printStackTrace();}}water.setWater(true); // 设置水塘有水状态System.out.println("生产完毕。
");synchronized (water) {water.notify(); // 线程调用notify()方法}}}package Product;public class ThreadB extends Thread {Water water;public ThreadB(Water waterArg) {water = waterArg;}public void run() {System.out.println("启动消费");if (water.isEmpty()) { // 如果水塘无水synchronized (water) { // 同步代码块try {System.out.println("还没有生产出面包,等待中……");water.wait(); // 使线程处于等待状态} catch (InterruptedException e) {e.printStackTrace();}}}System.out.println("开始消费……");for (int i = 5; i >= 1; i--) { // 循环5侧try {Thread.sleep(1000); // 休眠1秒,模拟1分钟System.out.println(i + "个");} catch (InterruptedException e) {e.printStackTrace();}}water.setWater(false); // 设置水塘无水状态System.out.println("消费完毕。