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--;
}
}
public 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 {
public 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. 设计两个线程,一个线程往数据单元里写信息,一个线程从数据单元里读信息。要求使用线程同步技术,使得每写一次就往外读一次。例如,写和读的数据序列为:
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 synchronized 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 extends Thread{
DataUnit x=null;
public write(DataUnit x){
this.x=x;
}
public void run(){
while(x.number 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=new 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"; } public 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!"); datathread 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 sno; 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 Student[3]; stu[0]=new Student("001","Mary","SS"); stu[1]=new Student("002","Tom","CS"); stu[2]=new Student("003","Hellen","IS"); Vector v=new Vector(); for(int i=0;i v.add(stu[i]); } Iterator while(num.hasNext()){ System.out.println(num.next()); } } } 2. 已知:哈希表HashTable package test5hash; import java.util.*; import java.lang.Iterable; public class Hash { public static void main(String[] args) { Hashtable hTable.put(001, "a"); hTable.put(002, "b"); hTable.put(003, "c"); Iterator while(i.hasNext()) { int j = i.next(); System.out.print(j+" "); System.out.println(hTable.get(j)); } System.out.print(key+" "); 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; this.Sclass=sc; this.password=p; } } public class Main { public static void main(String[] args) throws IOException ,ClassNotFoundException{ Student stu[]; stu=new Student[3]; stu[0]=new Student("Mary",14,1,"123"); stu[1]=new Student("Tom",14,2,"145"); stu[2]=new Student("Jim",16,2,"456"); ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("D:/a.dat")); for(int i=0;i<3;i++){ oos.writeObject(stu[i]); stu[i]=null; } oos.close(); ObjectInputStream ois=new ObjectInputStream(new FileInputStream("D:/a.dat")); for(int i=0;i stu[i]=(Student) ois.readObject(); } ois.close(); for(int i=0;i<3;i++){ System.out.println("name:"+stu[i].name+" age:"+stu[i].age+" class"+stu[i].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(IOException 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.writeInt(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(String 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; import java.io.*; import java.util.*; public class Main { private String name; private int count; private int Max; private int Min; public Main(String n, int c, int max, int min) { this.name = n; this.count = c; this.Max = max; this.Min = min; } public void createfile() { try { FileWriter creat = new FileWriter(name); int limit = Max - Min; Random random = new Random(); for (int i = 1; i <= count; i++) { int number = Min + random.nextInt(limit); System.out.print(number); System.out.print(" "); creat.write(number + " "); } creat.close(); } catch (IOException iox) { System.out.println("Error!"); } } public static void main(String[] args) throws IOException { String filename; int count, min, max; Scanner in = new Scanner(System.in); System.out.println("请输入文件名"); filename = in.next(); System.out.println("输入随机数的个数"); count = in.nextInt(); System.out.println("输入随机数的最小值"); min = in.nextInt(); System.out.println("输入随机数的最大值"); max = in.nextInt(); Main M = new Main(filename, count, max, min); M.createfile(); } } 1. 编写求解几何图形(如直线、矩型,圆形)的周长和面积的应用程序, 要求使用接口实现多重继承和多态技术。 提示:声明Iperimeter和IArea分别表示周长接口和面积接口;声明抽象类Shape. package test3; interface Iperimeter{ public double imperimeter(); } interface IArea{ public double IArea(); } abstract class shape implements IArea,Iperimeter{ public abstract double imperimeter(); public abstract double IArea(); } class rectangle extends shape{ double length,width; public rectangle(double x,double y){ length=x; width=y; } public double imperimeter(){ return (length+width)*2; } public double IArea(){ return length*width; } } class circle extends shape{ static double pi=3.14; int r; public circle(int r0){ r=r0; } public double imperimeter(){ return 2*pi*r; } public double IArea(){ return pi*r*r; } } public class test { public static void main(String[] args) { shape s1=new rectangle(2,3); shape s2=new circle(3); System.out.println("the imperimeter of s1 is:"+s1.imperimeter()+"\nthe arae of s1 is:"+s1.IArea()); System.out.println("the imperimeter of s2 is:"+s2.imperimeter()+"\nthe arae of s2 is:"+s2.IArea()); } } 1. 定义一个接口EqualDiag,表示具有等斜边的图形对象,其中包含: a. 方法getDiag,用来计算图形的斜边的长度; b. 方法getArea,用来计算图形的面积; c. 方法compareTo, 用来比较两个对象的大小(按照斜边的大小进行比较); 2. 定义一个矩形类Rectangle,再派生一个正方形类Square: a. 在矩形类和正方形类中实现接口EqualDiag; package test2; import java.lang.Math.*; interface EqualDiag { public double getDiag(); public double getArea(); public int compareTo(Object a); } class Rectangle implements EqualDiag { double length; double width; public void setlength(double m) { length = m; } public void setwidth(double n) { width = n; } public double getDiag() { return Math.sqrt(width * width + length * length); } public double getArea() { return length * width; } public int compareTo(Object a) { Rectangle x; x = (Rectangle)a; if (this.getDiag() > x.getDiag()) return 1; else if (this.getDiag() == x.getDiag()) return 0; else return -1; } } class Square extends Rectangle { public void setlength(double m) { length = m; width = m; } } public class test { 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的面积为:" + a.getArea() + "\n矩形b的对角线长为:" + b.getDiag() + "\n矩形b的面积为:" + b.getArea()); System.out.println("矩形a,b比较结果:" + a.compareTo(b)); Square c = new Square(); Square d = new Square(); c.setlength(5); d.setlength(3); System.out.println("正方形c的对角线长为:" + c.getDiag() + "\n正方形c的面积为:" + c.getArea() + "\n正方形d的对角线长为:" + d.getDiag() + "\n正方形d的面积为:" + d.getArea()); System.out.println("正方形c,d比较结果:" + c.compareTo(d)); } } 1. 声明一个抽象类comparable, 用于对两个不同对象的比较,并满足: a) 包含一个抽象方法int compareTo(comparable b); 若小于对象b则返回-1,大于则返回1,相等则返回0. b) 声明两个子类,一个为复数类complex,另一个为矩形类rect; 在子类中分别实现父类comparable的抽象方法. c) 复数类的大小比较以模为基准,而矩形类的比较以面积为基准。 /* * To change this template, choose Tools | Templates * and open 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 wid){ width=wid; } public int compareTo(comparable b){ rectangle a=(rectangle)b; if(this.length*this.width>a.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){ real=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.Mo()>a.Mo()) return 1; if(this.Mo()==a.Mo()) return 0; else return -1; } } public class 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(x.compareTo(y)); complex m=new complex(); complex n=new complex(); m.setreal(6); m.setimaginary(5); n.setreal(5); n.setimaginary(3); System.out.println(m.compareTo(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 */ public class rectangle { int width,height; Scanner input; public void set(){ input=new Scanner(System.in); width=input.nextInt(); height=input.nextInt(); } public int Setarea(){ return height*width; } public int Setcircumference(){ return (height+width)*2; } public String toString(){ return("?╁舰?夸负锛?+width+"\n?╁舰瀹戒负锛?+height+"\n?╁舰?㈢Н涓猴?"+height*width+"\n?╁舰?ㄩ?夸负锛?"+(height+width)*2); } @Override public boolean equals(Object x){ if(this.getClass()!=x.getClass()) return false; rectangle y=(rectangle)x; return ((this.Setarea()==y.Setarea())&&(this.Setcircumference()==(y.Setcircumference()))); } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here rectangle a=new rectangle(); rectangle b=new rectangle(); a.set(); b.set(); a.equals(b); System.out.println(a); System.out.println(b); System.out.println(a.equals(b)); } } 1. 声明一个person类,包含: a) 属性:姓名,年龄,性别; b) 方法:设置person信息,显示person信息(重写toString方法); 2. 声明一个student类,作为person类的子类。包含: a) 属性:学号,英语成绩,数学成绩,物理成绩,平均成绩,总成绩; b) 方法:设置学号,设置各科成绩,计算平均成绩,计算总成绩,显示student信息(重写toString方法); 3. 声明一个测试类test, 生成若干个student类的对象,分别计算他们的各项成绩,并输出其信息。 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package test; /** * * @author Administrator */ class person{ String name; int age; char sex; void setname(String Nname){ name=Nname; } void setage( int Nage){ age=Nage; } void setsex(char Nsex){ sex=Nsex; } public String toString(){ return("name:"+name+"\nage:"+age+"\nsex"+sex); } } class student extends person{ int number; int englishscore; int mathscore; int physicsscore; int averagescore; int totalscore; void setnumber(int num){ number=num; } void setEscore(int english){ englishscore=english; } void setMscore(int math){ mathscore=math; } void setPscore(int physics){ physicsscore=physics; } int countaverage(){ return((englishscore+mathscore+physicsscore)/3); } int counttotal(){ return(englishscore+mathscore+physicsscore); } public String toString(){ return("name:"+name+"\nage:"+age+"\nsex:"+sex+"\nnumber:"+number+"\nEnglish:"+englishscore+"\nMath:"+mathscore+"\nPhysics:"+physicsscore+"\nAveragescore:"+averagescore+"\nTotalscore:"+totalscore); } } public class test { /** * @param args the command line arguments */ public static void main(String[] args) { student a=new student(); student b=new student(); a.setname("mary"); a.setage(11); a.setsex('f'); a.setnumber(000); a.setEscore(90); a.setMscore(88); a.setPscore(80); a.averagescore=a.countaverage(); a.totalscore=a.counttotal(); b.setname("bob"); b.setage(12); b.setsex('m'); b.setnumber(001); b.setEscore(91); b.setMscore(86); b.setPscore(79); b.averagescore=b.countaverage(); b.totalscore=b.counttotal(); System.out.println(a); System.out.println(b); // TODO code application logic here } } 1.定义一个Point类。该类具有以下特点: a. int型变量x,y表示屏幕坐标系上的一个点; b. 两种构造方法实现对象的初始化:Point(int x, int y)和Point(Point p); c. 计算两个Point对象之间距离的方法distance(Point a, Point b); d. 获得当前坐标x,y值的方法getX(),getY(); e. 设置坐标x,y值的方法setX(),setY(); f. 重写Point类的toString()方法,以格式(x,y)输出当前点的字符串; g. 统计应用程序中Point类对象的个数(使用Point类的类变量)。 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package point; /** * * @author Administrator */ public class app { static int count; int x; int y; public app(int x1,int y1){ x=x1; y=y1; count++; } public app(app p1){ x=p1.x; y=p1.y; count++; } public double distance(app a,app b,double d){ d=Math.sqrt(((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y))); return d; } public int getX(){ return x; } public int getY(){ return y; } public void setX(int x1){ x=x1; } public void setY(int y1){ y=y1; } public String toString(){ return("("+x+","+y+")"); } /** * @param args the command line arguments */ public static void main(String[] args) { app a=new app(0,0); app b=new app(a.x,a.y); int d=0; int m,n,p,q; a.setX(2); a.setY(5); b.setX(1); b.setY(1); m=a.getX(); n=a.getY(); p=b.getX(); q=b.getY(); System.out.println("m="+m+"\nn="+n+"\np="+p+"\nq="+q); System.out.println("the distance is:"+a.distance(a,b,d)); System.out.println("here is the cordinate:"+a); System.out.println("here is the cordinate:"+b); System.out.println("the number of the objects is:"+count); // TODO code application logic here } } 1. 设计一个复数类complex,包括: a. 成员变量:复数的实部和虚部; b. 构造函数:带参数和不带参数的构造函数,对成员变量进行初始化; c. 成员方法:复数的加、减、乘运算。 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package complex; /** * * @author Administrator */ public class app { int real; int imaginary; public app(){ real=0; imaginary=0; } public app(int initreal,int initimaginary){ real=initreal; imaginary=initimaginary; } public app add(app number1,app number2){ number1.real=number1.real+number2.real; number1.imaginary=number1.imaginary+number2.imaginary; return number1; } public app minus(app number1,app number2){ number1.real=number1.real-number2.real; number1.imaginary=number1.imaginary-number2.imaginary; return number1; } public app multiply(app number1,app number2,app num){ num.real=number1.real*number2.real-number1.imaginary*number2.imaginary; num.imaginary=number1.real*number2.imaginary+number1.imaginary*number2.real; return num; } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here app number1; number1=new app(5,7); app number2; number2=new app(6,8); app num; num=new app(0,0); System.out.println("the adding result is:"+number1.add(number1, number2).real+"+"+number1.imaginary+"i"); number1=new app(5,7); System.out.println("the minus result is:"+number1.minus(number1, number2).real+"+("+number1.imaginary+")i"); number1=new app(5,7); System.out.println("the multify result is:"+num.multiply(number1, number2,num).real+"+"+num.imaginary+"i"); } } 1. 使用数组编程,按下列格式输出数字: 1 3 6 10 15 2 5 9 14 4 8 13 7 12 11 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package array; /** * * @author Administrator */ public class app { /** * @param args the command line arguments */ public static void main(String[] args) { int myArray[][]; int m=4; myArray=new int[5][5];// TODO code application logic here myArray[0][0]=1; for(int i=1;i myArray[0][i]=myArray[0][i-1]+i+1; for(int i=0;i System.out.printf("%3d",myArray[0][i]); System.out.print("\n"); for(int i=1;i { for(int j=0;j {myArray[i][j]=myArray[0][j+i-1]+j+1; System.out.printf("%3d",myArray[i][j]); } System.out.print("\n"); m--; } } } 2. 定义一个学生类(Student),属性有private的名字(name), public的年龄(age); 设置name和age属性的方法:setName(),getName(); setAge(),getAge(),以及显示name和age值的方法showName(),showAge(); 编写Application,创建一个学生对象,设置name和age属性值,并显示name和age. /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package classpkg; /** * * @author Administrator */ public class Main { private String name; public int age; /** * @param args the command line arguments */ public void setName(String newN) { name=newN; // TODO code application logic here } public void setAge(int newA) { age=newA; // TODO code application logic here } public String getName() { return name; // TODO code application logic here } public int getAge() { return age; // TODO code application logic here } public void showName() { System.out.println(name); // TODO code application logic here } public void showAge() { System.out.println(age); // TODO code application logic here } public static void main(String[] args) { Main x; String m; x=new Main(); x.setName("bn"); x.setAge(11); m=x.getName(); x.getAge(); x.showName(); x.showAge(); System.out.println(m); // TODO code application logic here } // TODO code application logic here }