java实验总结.docx

上传人:小飞机 文档编号:3159566 上传时间:2023-03-11 格式:DOCX 页数:28 大小:44.85KB
返回 下载 相关 举报
java实验总结.docx_第1页
第1页 / 共28页
java实验总结.docx_第2页
第2页 / 共28页
java实验总结.docx_第3页
第3页 / 共28页
java实验总结.docx_第4页
第4页 / 共28页
java实验总结.docx_第5页
第5页 / 共28页
亲,该文档总共28页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《java实验总结.docx》由会员分享,可在线阅读,更多相关《java实验总结.docx(28页珍藏版)》请在三一办公上搜索。

1、java实验总结1. 设计一个Person类,包含:姓名,年龄,性别。要求:该类至多只能创建一男、一女两个对象。 2. 设计一个测试类Test,创建若干个Person类对象,测试是否符合要求。 package test8; class person String name; int age; char sex; static int flag1 = 1, flag2 = 1; private person(String n, int a, char s) name = n; age = a; sex = s; if (s = m) flag1-; if (s = f) flag2-; publ

2、ic static person newperson(String n, int a, char s) if (s = m & flag1 != 0) person p = new person(n, a, s); return p; else if (s = f & flag2 != 0) person p = new person(n, a, s); return p; else return null; public String toString return (name: + name + age: + age + sex: + sex); public class test pub

3、lic static void main(String args) person p1 , p2 , p3 ; p1= person.newperson(mary, 11, f); p2=person.newperson(tom, 14, m); p3=person.newperson(jane, 13, f); System.out.println(p1); System.out.println(p2); System.out.println(p3); 1. 设计一个数据单元类DataUnit, 它包含学号(Number)和姓名(Name)两个数据成员。 2. 设计两个线程,一个线程往数据单

4、元里写信息,一个线程从数据单元里读信息。要求使用线程同步技术,使得每写一次就往外读一次。例如,写和读的数据序列为: Write: 1, Name1 Read: 1, Name1 Write: 2, Name2 Read: 2, Name2 . package test7; class DataUnit int number; String name; int max; boolean flag=false; public DataUnit(int number, String name,int max) this.name=name; this.max=max; public synchron

5、ized void in if(flag) try wait; catch(Exception e) System.out.println(Write number:+(+number)+ write name:+(number); flag=true; notify; public synchronized void out if(!flag) try wait; catch(Exception e) System.out.println(read number:+(number)+ read name:+(number); flag=false; notify; class write e

6、xtends Thread DataUnit x=null; public write(DataUnit x) this.x=x; public void run while(x.numberx.max) x.in; class read extends Thread DataUnit x=null; public read(DataUnit x) this.x=x; public void run while(x.number=x.max) x.out; public class test7 public static void main(String args) DataUnit x=ne

7、w DataUnit(0,mary,10); new write(x).start; new read(x).start; 设计一个线程Thread类的子类DataThread, 使用DataThread构建两个线程,分别输出50以内的奇数和偶数,并使两个线程并发执行。 package test6; class datathread extends Thread private int number; String flag; public datathread(int num) number=num; if(number=-2) flag=even; else flag=odd; publi

8、c void run int result=number; System.out.println(thread +flag+ starts!); while(result+2=50) result=result+2; System.out.println(an +flag+ number:+result); System.out.println(thread +flag+ ends!); public class Main public static void main(String args) System.out.println(main thread starts!); datathre

9、ad thread1=new datathread (-2); datathread thread2=new datathread (-1); thread1.start; thread2.start; System.out.println(main thread ends!); 1. 设计一个学生Student类,包含学号(Sno),姓名(Name),所在系(Dept)等属性。创建若干Student类的对象,添加到一个向量Vector中,并遍历输出该向量各元素值。 package test5; import java.util.*; public class Student String s

10、no; String name; String dept; public Student(String no,String n,String p) this.sno=no; this.name=n; this.dept=p; public String toString return(sno:+sno+ name:+name+ dept:+dept); public static void main(String args) Student stu; stu=new Student3; stu0=new Student(001,Mary,SS); stu1=new Student(002,To

11、m,CS); stu2=new Student(003,Hellen,IS); Vector v=new Vector; for(int i=0;istu.length;i+) v.add(stui); Iteratornum=v.iterator; while(num.hasNext) System.out.println(num.next); 2. 已知:哈希表HashTable hTable = new HashTable。编程遍历hTable,要求写出两种方式。 package test5hash; import java.util.*; import java.lang.Iterab

12、le; public class Hash public static void main(String args) Hashtable hTable = new Hashtable; hTable.put(001, a); hTable.put(002, b); hTable.put(003, c); Iterator i = (Iterator)hTable.keys; while(i.hasNext) int j = i.next; System.out.print(j+ ); System.out.println(hTable.get(j); System.out.print(key+

13、 ); System.out.println( hTable.get(key); 1. 创建一学生类,创建若干该类的对象并保存到文件a.dat中,从文件a.dat读取对象显示在屏幕上。 package student; import java.io.*; class Student implements Serializable String name; int age; int Sclass; transient String password; public Student(String n,int a,int sc,String p) this.name=n; this.age=a; t

14、his.Sclass=sc; this.password=p; public class Main public static void main(String args) throws IOException ,ClassNotFoundException Student stu; stu=new Student3; stu0=new Student(Mary,14,1,123); stu1=new Student(Tom,14,2,145); stu2=new Student(Jim,16,2,456); ObjectOutputStream oos=new ObjectOutputStr

15、eam(new FileOutputStream(D:/a.dat); for(int i=0;i3;i+) oos.writeObject(stui); stui=null; oos.close; ObjectInputStream ois=new ObjectInputStream(new FileInputStream(D:/a.dat); for(int i=0;istu.length;i+) stui=(Student) ois.readObject; ois.close; for(int i=0;i3;i+) System.out.println(name:+stui.name+

16、age:+stui.age+ class+stui.Sclass); 1. 模仿文本文件复制的程序,编写对二进制文件进行复制的程序。 package filecopy; import java.io.*; class Copyer String sourcename,destname; DataInputStream source; DataOutputStream dest; int data; private boolean openfiles try source=new DataInputStream(new FileInputStream(sourcename) ); catch(I

17、OException iox) System.out.println(problem opening+sourcename); return false; try dest=new DataOutputStream(new FileOutputStream(destname) ); catch(IOException iox) System.out.println(problem opening+destname); return false; return true; private boolean copyf try while(data=source.read)!=-1) dest.wr

18、iteInt(data); catch(IOException iox) System.out.println(problem reading or writing); return false; return true; private boolean closef boolean retval=true; try source.close; dest.close; catch(IOException iox) System.out.println(problem closing); retval=false; return retval; public boolean copy(Strin

19、g src,String dst) sourcename=src; destname=dst; return openfiles©f&closef; public class main public static void main(String args) if(args.length=2) new Copyer.copy(d:a.txt,d:b.txt); else System.out.println(please enter file name); 2. 创建一个存储若干随机整数的文本文件。其中文件名、整数的个数及其范围均由键盘输入。 package creatfile; im

20、port java.io.*; import java.util.*; public class Main public static void main(String args) throws IOException catch (IOException iox) System.out.println(Error!); creat.close; public Main(String n, int c, int max, int min) public void createfile try FileWriter creat = new FileWriter(name); int limit

21、= Max - Min; Random random = new Random; for (int i = 1; i x.getDiag) return 1; return 0; else if (this.getDiag = x.getDiag) else return -1; return length * width; return Math.sqrt(width * width + length * length); public void setlength(double m) public void setwidth(double n) width = n; length = m;

22、 double length; double width; length = m; width = m; public class test System.out.println(正方形c的对角线长为: + c.getDiag + n正方形c的面积为: + System.out.println(正方形c,d比较结果: + pareTo(d); c.getArea + n正方形d的对角线长为: + d.getDiag + n正方形d的面积为: + d.getArea); Square c = new Square; Square d = new Square; c.setlength(5); d

23、.setlength(3); public static void main(String args) Rectangle a = new Rectangle; Rectangle b = new Rectangle; a.setlength(5); a.setwidth(4); b.setlength(3); b.setwidth(4); System.out.println(矩形a的对角线长为: + a.getDiag + n矩形a的面积为: + System.out.println(矩形a,b比较结果: + pareTo(b); a.getArea + n矩形b的对角线长为: + b.g

24、etDiag + n矩形b的面积为: + b.getArea); 1. 声明一个抽象类comparable, 用于对两个不同对象的比较,并满足: a) 包含一个抽象方法int compareTo(comparable b); 若小于对象b则返回-1,大于则返回1,相等则返回0. b) 声明两个子类,一个为复数类complex,另一个为矩形类rect; 在子类中分别实现父类comparable的抽象方法. c) 复数类的大小比较以模为基准,而矩形类的比较以面积为基准。 /* * To change this template, choose Tools | Templates * and ope

25、n the template in the editor. */ package test1; import java.lang.Math.*; /* * * author Administrator */ abstract class comparable public abstract int compareTo(comparable b); class rectangle extends comparable int length; int width; public void setlength(int len) length=len; public void setwidth(int

26、 wid) width=wid; public int compareTo(comparable b) rectangle a=(rectangle)b; if(this.length*this.widtha.length*a.width) return 1; if(this.length*this.width=a.length*a.width) return 0; else return -1; class complex extends comparable double real; double imaginary; public void setreal(double nreal) r

27、eal=nreal; public void setimaginary(double nimaginary) imaginary=nimaginary; public double Mo return Math.sqrt(this.real*this.real+this.imaginary*this.imaginary); public int compareTo(comparable b) complex a=(complex)b; if(this.Moa.Mo) return 1; if(this.Mo=a.Mo) return 0; else return -1; public clas

28、s test /* * param args the command line arguments */ public static void main(String args) / TODO code application logic here rectangle x=new rectangle ; rectangle y=new rectangle ; x.setlength(4); x.setwidth(6); y.setlength(6); y.setwidth(4); System.out.println(pareTo(y); complex m=new complex; comp

29、lex n=new complex; m.setreal(6); m.setimaginary(5); n.setreal(5); n.setimaginary(3); System.out.println(pareTo(n); 1. 编写一个矩形类rectangle: a. 成员变量包括:长和宽; b. 方法包括:计算面积、计算周长、设置长和宽的值; c. 重写toString方法,显示rectangle对象的详细信息; d. 重写equals方法,如果两个矩形的面积和周长都相等,则认为它们是同一。 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package rectangle; import java.util.*; /* * * author Administrator

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号