程序答题总结

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

实验题 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

{

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 block

e.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 serialV ersionUID = 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();

相关文档
最新文档