| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 471 人关注过本帖
标题:[求助]这个题应该写,大家看看
只看楼主 加入收藏
wanghongxian
Rank: 1
等 级:新手上路
帖 子:100
专家分:0
注 册:2005-12-13
收藏
 问题点数:0 回复次数:0 
[求助]这个题应该写,大家看看

就是这个JAVA的程序,当运行的时候,选择按1的时候,要把学生的全部数据进行排列,由小到大就可以,但是不知道怎么排列,我有过冒泡排序,但是没有成功,请大家帮帮忙,谢谢
import java.io.* ;
import java.util.* ;
abstract class Person implements Serializable
{
private String id ;
private String name ;
private int age ;

public Person()
{}
public Person(String id,String name,int age)
{
this.setId(id) ;
this.setName(name) ;
this.setAge(age) ;
}

public void setName(String name)
{
if(name!=null)
{
this.name = name ;
}
}
public void setAge(int age)
{
if(age>0&&age<150)
{
this.age = age ;
}
}
public void setId(String id)
{
if(id!=null)
{
this.id = id ;
}
}

public String getName()
{
return this.name ;
}
public int getAge()
{
return this.age ;
}
public String getId()
{
return this.id ;
}
}

// 学生ID号前加一个S,例:S001
class Student extends Person
{
private float score ;
public Student()
{}
public Student(String id,String name,int age,float score)
{
super("S"+id,name,age) ;
this.setScore(score) ;
}
public void setScore(float score)
{
this.score = score ;
}
public float getScore()
{
return this.score ;
}
public String toString()
{
return super.getId()+"\t"+super.getName()
+"\t"+super.getAge()+"\t"+this.getScore() ;
}
}

// 学生ID号前加一个W,例:W001
class Worker extends Person
{
private float salary ;
public Worker(String id,String name,int age,float salary)
{
super("W"+id,name,age) ;
this.setSalary(salary) ;
}
public void setSalary(float salary)
{
this.salary = salary ;
}
public float getSalary()
{
return this.salary ;
}
public String toString()
{
return super.getId()+"\t"+super.getName()
+"\t"+super.getAge()+"\t"+this.getSalary() ;
}
}

// 输入数据类
class InputData
{
private BufferedReader buf = null ;
public InputData()
{
buf = new BufferedReader(new InputStreamReader(System.in)) ;
}
public String getString()
{
String temp = null ;
try
{
temp = buf.readLine() ;
}
catch(Exception e)
{}
return temp ;
}
public int getInt()
{
int temp = 0 ;
boolean flag = true ;
while(flag)
{
try
{
temp = Integer.parseInt(buf.readLine()) ;
flag = false ;
}
catch(Exception e)
{
System.out.print("必须输入整数,请重新输入:") ;
}
}
return temp ;
}
public float getFloat()
{
float temp = 0.0f ;
boolean flag = true ;
while(flag)
{
try
{
temp = Float.parseFloat(buf.readLine()) ;
flag = false ;
}
catch(Exception e)
{
System.out.print("必须输入浮点数,请重新输入:") ;
}
}
return temp ;
}

}

// 文件操作类
class FileOpearate
{
private File f = new File("c:\\person.ini") ;
public void save(Object obj) throws Exception
{
ObjectOutputStream oos = null ;
oos = new ObjectOutputStream(new FileOutputStream(f)) ;
oos.writeObject(obj) ;
oos.close() ;
}
public Object load() throws Exception
{
Object obj = null ;
ObjectInputStream ois = null ;
ois = new ObjectInputStream(new FileInputStream(f)) ;
obj = ois.readObject() ;
return obj ;
}
}

// 分离工人或学生的信息
class Split
{
Map m = new HashMap() ;
public Split(Collection col)
{
Collection studentCol = new ArrayList() ;
Collection workerCol = new ArrayList() ;
Iterator iter = col.iterator() ;
while(iter.hasNext())
{
Object obj = iter.next() ;
// 如果是学生,则向学生集合中插入数据
if(obj instanceof Worker)
{
workerCol.add(obj) ;
}
// 如果是工人,则向工人集合中插入数据
if(obj instanceof Student)
{
studentCol.add(obj) ;
}
}
// 将全部信息保存在Map集合之中
m.put("students",studentCol) ;
m.put("workers",workerCol) ;
}
public Collection getPerson(String key)
{
return (Collection)m.get(key) ;
}
}
public class Demo04
{
public static void main(String args[])
{

Person p1 = new Student("001","李四",21,89.0f) ;
Person p2 = new Student("002","张三",20,78.0f) ;
Person p3 = new Student("003","王五",20,99.0f) ;
Person p4 = new Student("004","赵六",20,38.0f) ;
Person p5 = new Student("005","郭八",20,68.0f) ;

Person p6 = new Worker("001","小张",28,1000.5f) ;
Person p7 = new Worker("002","小李",32,1500.5f) ;
Person p8 = new Worker("003","小刘",55,2500.5f) ;
Person p9 = new Worker("004","小王",20,640.5f) ;
Person p10 = new Worker("005","小郭",35,7000.5f) ;

Collection col = new ArrayList() ;



col.add(p2) ;
col.add(p4) ;
col.add(p6) ;
col.add(p1) ;
col.add(p3) ;
col.add(p4) ;
col.add(p5) ;
col.add(p8) ;
col.add(p7) ;
col.add(p10) ;
col.add(p9) ;


FileOpearate fo = new FileOpearate() ;
//Collection col = null ;
try
{
col = (Collection)fo.load() ;
//fo.save(col) ;
}
catch(Exception e)
{}
System.out.println(col) ;

// System.out.println(col) ;
System.out.println("请选择查看类型:") ;
System.out.println("1、查看全部学生") ;
System.out.println("2、查看全部工人") ;
InputData id = new InputData() ;
System.out.print("请选择:") ;
// 将全部数据从文件之中读取出来,之后进行数据分离
Split s = new Split(col) ;
int i = id.getInt() ;
switch(i)
{
case 1:
{
System.out.println("全部学生信息:") ;
System.out.println("\t编号\t姓名\t年龄\t成绩") ;
Collection c = s.getPerson("students") ;
Iterator iter = c.iterator() ;
int ii = 1 ;
while(iter.hasNext())
{
System.out.println((ii++)+"\t"+iter.next()) ;

}
break;


}
case 2:
{
System.out.println("全部工人信息:") ;
System.out.println("\t编号\t姓名\t年龄\t工资") ;
Collection c = s.getPerson("wrokers") ;
Iterator iter = c.iterator() ;
int w = 1 ;
while(iter.hasNext())
{
System.out.println((w++)+"\t"+iter.next()) ;
}
break ;
}
default:
{
System.out.println("输入错误,程序退出!") ;
}
}

}

搜索更多相关主题的帖子: abstract private public import JAVA 
2006-09-07 15:28
快速回复:[求助]这个题应该写,大家看看
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023111 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved