求各位计算机大师帮个帮java ,创建学生类,至少包括学号,姓名,年龄和身高属性,一个构造方法用于

2024-11-27 19:51:52
推荐回答(4个)
回答1:

public class Student {
private int sNo;
private String sName;
private int sAge;
private int sHeight;

/**
 * @return the sNo
 */
public int getsNo() {
return sNo;
}

/**
 * @param sNo
 *            the sNo to set
 */
public void setsNo(int sNo) {
this.sNo = sNo;
}

/**
 * @return the sName
 */
public String getsName() {
return sName;
}

/**
 * @param sName
 *            the sName to set
 */
public void setsName(String sName) {
this.sName = sName;
}

/**
 * @return the sAge
 */
public int getsAge() {
return sAge;
}

/**
 * @param sAge
 *            the sAge to set
 */
public void setsAge(int sAge) {
this.sAge = sAge;
}

/**
 * @return the sHeight
 */
public int getsHeight() {
return sHeight;
}

/**
 * @param sHeight
 *            the sHeight to set
 */
public void setsHeight(int sHeight) {
this.sHeight = sHeight;
}

/**
 * 
 */
public Student() {
}

/**
 * 
 * @param sNo
 * @param sName
 * @param sAge
 * @param sHeight
 */
public Student(int sNo, String sName, int sAge, int sHeight) {
this.sNo = sNo;
this.sName = sName;
this.sAge = sAge;
this.sHeight = sHeight;
}

/*
 * (non-Javadoc)
 * 
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
StringBuffer stu = new StringBuffer();
stu.append("学生详细信息\n");
stu.append("姓名:");
stu.append(this.sName + "\n");
stu.append("学号:");
stu.append(this.sNo + "\n");
stu.append("年龄:");
stu.append(this.sAge + "\n");
stu.append("身高:");
stu.append(this.sHeight + "\n");
return stu.toString();
}

public static Student[] comparTo(Student[] stus) {
int length = stus.length;
Student temp = null;
for (int i = 0; i < length - 2; i++) {
for (int j = i + 1; j < length; j++) {
if (stus[i].sAge > stus[j].sAge) {
continue;
} else if (stus[i].sAge < stus[j].sAge) {
temp = stus[i];
stus[i] = stus[j];
stus[j] = temp;
continue;
} else {
if (stus[i].sHeight > stus[j].sHeight) {
continue;
} else if (stus[i].sHeight < stus[j].sHeight) {
temp = stus[i];
stus[i] = stus[j];
stus[j] = temp;
continue;
} else {
if (stus[i].sNo > stus[j].sNo) {
continue;
} else if (stus[i].sNo < stus[j].sNo) {
temp = stus[i];
stus[i] = stus[j];
stus[j] = temp;
continue;
}
}
}
}
}
return stus;
}

public static void main(String[] args) {
Student st1 = new Student(1, "tom", 19, 175);
Student st2 = new Student(2, "jim", 19, 170);
Student st3 = new Student(3, "lily", 21, 185);
Student st4 = new Student(4, "lilei", 20, 175);
Student[] stus = { st1, st2, st3, st4 };
Student[] ssstus = comparTo(stus);
for (int i = 0; i < ssstus.length; i++) {
System.out.println(ssstus[i].toString());
}
}
}

回答2:

import java.util.List;



public class Student {

private int sId;

private String sName;

private int sAge;

private int sHeight;

public Student() {

super();

}


public Student(int sId, String sName, int sAge, int sHeight) {

super();

this.sId = sId;

this.sName = sName;

this.sAge = sAge;

this.sHeight = sHeight;

}


public int getsId() {

return sId;

}


public void setsId(int sId) {

this.sId = sId;

}


public String getsName() {

return sName;

}

public void setsName(String sName) {

this.sName = sName;

}

public int getsAge() {

return sAge;

}

public void setsAge(int sAge) {

this.sAge = sAge;

}


public int getsHeight() {

return sHeight;

}

public void setsHeight(int sHeight) {

this.sHeight = sHeight;

}

@Override

public String toString() {

// TODO Auto-generated method stub

return "学号:" + sId + ",姓名:" + sName + ",年龄:" + sAge + ",身高:" + sHeight;

}

public void sort(List list) {

int size = list.size();

for(int i = 0; i < size; i ++){

for(int j = i + 1; j < size; j ++){

if(list.get(i).getsAge() < list.get(j).getsAge()){

list = getOrder(list, i, j);

} else if (list.get(i).getsAge() == list.get(j).getsAge()){

if(list.get(i).getsHeight() < list.get(j).getsHeight()){

list = getOrder(list, i, j);

} else if(list.get(i).getsHeight() == list.get(j).getsHeight()){

if(list.get(i).getsId() < list.get(j).getsId()){

list = getOrder(list, i, j);

}

}

}

}

}

for(Student s : list){

System.out.println("学号:" + s.getsId() + ",姓名:" + s.getsName() + ",年龄:" + s.getsAge() + ",身高:" + s.getsHeight());

}

}

public List getOrder(List list, int i, int j){

Student temp = new Student();

temp.setsId(list.get(i).getsId());

temp.setsName(list.get(i).getsName());

temp.setsAge(list.get(i).getsAge());

temp.setsHeight(list.get(i).getsHeight());

list.get(i).setsId(list.get(j).getsId());

list.get(i).setsName(list.get(j).getsName());

list.get(i).setsAge(list.get(j).getsAge());

list.get(i).setsHeight(list.get(j).getsHeight());

list.get(j).setsId(temp.getsId());

list.get(j).setsName(temp.getsName());

list.get(j).setsAge(temp.getsAge());

list.get(j).setsHeight(temp.getsHeight());

return list;

}


}


回答3:

/**
* Student.java 2014-1-2 上午10:09:54
*
* 版权所有(C)2012 南京卓越能源技术有限公司。保留所有权利。
*/
package cn.com.sengis;

/**
* TODO
* @author 刘继全
* 2014-1-2 上午10:09:54
*/
public class Student implements Comparable{
// ,创建学生类,至少包括学号,姓名,年龄和身高属性,
// 一个构造方法用于设置属性,
// 一个toString()方法用于将学生类对象转换成String对象和一个用于比较大小的方法
// (实现Comparable接口的comparTo方法)。要求将10名学生从大到小排序输出。
// (排序规则:先按年龄,然后按身高,最后按学号)
private int id;
private String name;
private int age;
private double height;
public Student(int id, String name, int age, double height) {
super();
this.id = id;
this.name = name;
this.age = age;
this.height = height;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
// TODO Auto-generated method stub
return "id=" + id + "\t name=" + name + "\t age=" + age + "\t height=" + height;
}

@Override
public int compareTo(Student arg0) {
Student s = (Student) arg0;
int i = age > s.getAge()? 1:(age == s.getAge()? (height > s.getHeight()? 1:(height == s.getHeight()? (id > s.getId()? 1:(id == s.getId()? 0:-1)):-1)):-1);
System.out.println(age+ ":"+s.getAge() + ":" + i);
return i;
}
// 加上get、set方法
}

// 测试类
public class Test {
public static void main(String[] args) {
Student[] arrStudents = new Student[10];
Student objStudent = new Student(1, "s1", 18, 177);

arrStudents[0] = objStudent;
objStudent = new Student(2, "s2", 19, 172);
arrStudents[1] = objStudent;
objStudent = new Student(3, "s3", 17, 167);
arrStudents[2] = objStudent;
objStudent = new Student(4, "s3", 21, 187);
arrStudents[3] = objStudent;
objStudent = new Student(5, "s3", 19, 157);
arrStudents[4] = objStudent;
objStudent = new Student(6, "s3", 23, 171);
arrStudents[5] = objStudent;
objStudent = new Student(7, "s3", 21, 176);
arrStudents[6] = objStudent;
objStudent = new Student(8, "s3", 22, 177);
arrStudents[7] = objStudent;
objStudent = new Student(9, "s3", 18, 179);
arrStudents[8] = objStudent;
objStudent = new Student(10, "s3", 29, 177);
arrStudents[9] = objStudent;
Arrays.sort(arrStudents);
for(int i=0;i {
System.out.println(arrStudents[i]);
}
}
}

回答4:

class Student{
private String no;//学号
private String name;//姓名
private int age; //年龄
private int height;//身高mm
public Student(){
}
public Student(String no, String name, int age, int height){
this.no=no;
this.name=name;
this.age=age;
this.height=height;
}
//省略get/set方法
public String toString(){
return "Student:no="+no+", name="+name+", age="+age+", height="+height;
}
}

别的,请自行补充