怎样用JAVA编写一个程序:
1.创建一个学生类,要求具有姓名、年龄、学号、密码、性别、对象的创建时间几个属性2.创建多个学生类的对象(至少10个以上),创建一个集合,将这些对象放入集合。3.对集合中...
1.创建一个学生类,要求具有姓名、年龄、学号、密码、性别、对象的创建时间几个属性2.创建多个学生类的对象(至少10个以上),创建一个集合,将这些对象放入集合。3.对集合中的学生对象进行排序,排序要求为先按年龄从小到大排序,当两名学生的年龄相同时在按这两名学生的学号由低到高排序。4.再创建一批对象,将这些对象按照上例中的排序规则插入到该集合适当的位置(要求考虑插入速度)
展开
2个回答
2013-05-28
展开全部
//1.创建一个学生类,要求具有姓名、年龄、学号、密码、性别、对象的创建时间几个属性public class Students {
private int stu_id;
private String stu_name;
private int stu_age;
private String stu_pwd;
private String stu_sex;
private String stu_date;
public Students() {}
public Students(int id,String name,int age,String pwd,String sex,String date) {
this.stu_id = id;
this.stu_name = name;
this.stu_age = age;
this.stu_pwd = pwd;
this.stu_sex = sex;
this.stu_date = date;
}
public int getStu_age() {
return stu_age;
}
public void setStu_age(int stu_age) {
this.stu_age = stu_age;
}
public String getStu_date() {
return stu_date;
}
public void setStu_date(String stu_date) {
this.stu_date = stu_date;
}
public int getStu_id() {
return stu_id;
}
public void setStu_id(int stu_id) {
this.stu_id = stu_id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public String getStu_pwd() {
return stu_pwd;
}
public void setStu_pwd(String stu_pwd) {
this.stu_pwd = stu_pwd;
}
public String getStu_sex() {
return stu_sex;
}
public void setStu_sex(String stu_sex) {
this.stu_sex = stu_sex;
}
}//学生列表类import java.util.Vector;public class StuList { private Vector<Students> v = new Vector<Students>();
public static void main(String[] args) {
StuList sl = new StuList();
sl.getStu();
Vector<Students> v = sl.getSort();
for(int i=0;i<v.size();i++) {
int id = v.get(i).getStu_id();
String name = v.get(i).getStu_name();
int age = v.get(i).getStu_age();
String pwd = v.get(i).getStu_pwd();
String sex = v.get(i).getStu_sex();
String date = v.get(i).getStu_date();
System.out.println(id+","+name+","+age+","+pwd+","+sex+","+date);
}
} //2.创建多个学生类的对象(至少10个以上),创建一个集合,将这些对象放入集合。
public Vector<Students> getStu() {
v.add(new Students(1001,"A",25,"aa","男","2011-01-26"));
v.add(new Students(1002,"B",22,"bb","男","2011-01-26"));
v.add(new Students(1003,"C",24,"cc","男","2011-01-26"));
v.add(new Students(1004,"D",20,"dd","男","2011-01-26"));
v.add(new Students(1005,"E",19,"ee","男","2011-01-26"));
v.add(new Students(1006,"F",20,"ff","男","2011-01-26"));
v.add(new Students(1007,"G",21,"gg","男","2011-01-26"));
v.add(new Students(1008,"H",20,"hh","男","2011-01-26"));
v.add(new Students(1009,"I",23,"ii","男","2011-01-26"));
v.add(new Students(1010,"J",20,"jj","男","2011-01-26"));
return v;
}/*3.对集合中的学生对象进行排序,排序要求为先按年龄从小到大排序,当两名学生的年龄相同时在按这两名学生的学号由低到高排序。*/
public Vector<Students> getSort() {
for(int i=0;i<v.size()-1;i++) {
for(int j=i+1;j<v.size();j++) {
if(v.get(i).getStu_age()>v.get(j).getStu_age()){
Students temp = v.get(i);
v.insertElementAt(v.get(j),i);
v.remove(i+1);
v.insertElementAt(temp, j);
v.remove(j+1);
}
else if(v.get(i).getStu_age()==v.get(j).getStu_age()){
if(v.get(i).getStu_id()>v.get(j).getStu_id()){
Students temp = v.get(i);
v.insertElementAt(v.get(j),i);
v.remove(i+1);
v.insertElementAt(temp, j);
v.remove(j+1);
}
}
}
}
return v;
}
}
4.再创建一批对象,将这些对象按照上例中的排序规则插入到该集合适当的位置(要求考虑插入速度)
第4是重复2,3的步骤再做一遍,我就不重复写了
private int stu_id;
private String stu_name;
private int stu_age;
private String stu_pwd;
private String stu_sex;
private String stu_date;
public Students() {}
public Students(int id,String name,int age,String pwd,String sex,String date) {
this.stu_id = id;
this.stu_name = name;
this.stu_age = age;
this.stu_pwd = pwd;
this.stu_sex = sex;
this.stu_date = date;
}
public int getStu_age() {
return stu_age;
}
public void setStu_age(int stu_age) {
this.stu_age = stu_age;
}
public String getStu_date() {
return stu_date;
}
public void setStu_date(String stu_date) {
this.stu_date = stu_date;
}
public int getStu_id() {
return stu_id;
}
public void setStu_id(int stu_id) {
this.stu_id = stu_id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public String getStu_pwd() {
return stu_pwd;
}
public void setStu_pwd(String stu_pwd) {
this.stu_pwd = stu_pwd;
}
public String getStu_sex() {
return stu_sex;
}
public void setStu_sex(String stu_sex) {
this.stu_sex = stu_sex;
}
}//学生列表类import java.util.Vector;public class StuList { private Vector<Students> v = new Vector<Students>();
public static void main(String[] args) {
StuList sl = new StuList();
sl.getStu();
Vector<Students> v = sl.getSort();
for(int i=0;i<v.size();i++) {
int id = v.get(i).getStu_id();
String name = v.get(i).getStu_name();
int age = v.get(i).getStu_age();
String pwd = v.get(i).getStu_pwd();
String sex = v.get(i).getStu_sex();
String date = v.get(i).getStu_date();
System.out.println(id+","+name+","+age+","+pwd+","+sex+","+date);
}
} //2.创建多个学生类的对象(至少10个以上),创建一个集合,将这些对象放入集合。
public Vector<Students> getStu() {
v.add(new Students(1001,"A",25,"aa","男","2011-01-26"));
v.add(new Students(1002,"B",22,"bb","男","2011-01-26"));
v.add(new Students(1003,"C",24,"cc","男","2011-01-26"));
v.add(new Students(1004,"D",20,"dd","男","2011-01-26"));
v.add(new Students(1005,"E",19,"ee","男","2011-01-26"));
v.add(new Students(1006,"F",20,"ff","男","2011-01-26"));
v.add(new Students(1007,"G",21,"gg","男","2011-01-26"));
v.add(new Students(1008,"H",20,"hh","男","2011-01-26"));
v.add(new Students(1009,"I",23,"ii","男","2011-01-26"));
v.add(new Students(1010,"J",20,"jj","男","2011-01-26"));
return v;
}/*3.对集合中的学生对象进行排序,排序要求为先按年龄从小到大排序,当两名学生的年龄相同时在按这两名学生的学号由低到高排序。*/
public Vector<Students> getSort() {
for(int i=0;i<v.size()-1;i++) {
for(int j=i+1;j<v.size();j++) {
if(v.get(i).getStu_age()>v.get(j).getStu_age()){
Students temp = v.get(i);
v.insertElementAt(v.get(j),i);
v.remove(i+1);
v.insertElementAt(temp, j);
v.remove(j+1);
}
else if(v.get(i).getStu_age()==v.get(j).getStu_age()){
if(v.get(i).getStu_id()>v.get(j).getStu_id()){
Students temp = v.get(i);
v.insertElementAt(v.get(j),i);
v.remove(i+1);
v.insertElementAt(temp, j);
v.remove(j+1);
}
}
}
}
return v;
}
}
4.再创建一批对象,将这些对象按照上例中的排序规则插入到该集合适当的位置(要求考虑插入速度)
第4是重复2,3的步骤再做一遍,我就不重复写了
2013-05-28
展开全部
这些是作业吧.为了你好 提醒你一句 这些 最好自己动手
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询