一道简单的JAVA题目 在线等高分
编写一个学生类STUDENT,包含的属性有:学号,姓名,性别,年龄等。将所有的学生储存在一个数组中,编写学生管理的程序实现如下操作1增加一个学生2根据学号删除某个学生3将...
编写一个学生类STUDENT,包含的属性有:学号,姓名,性别,年龄等。将所有的学生储存在一个数组中,编写学生管理的程序实现如下操作1 增加一个学生2根据学号删除某个学生3将所有的学生增加一岁4将数组中顺序显示所有学生信息5将所有学生按姓名顺序输出 JAVa的 该题目是北京邮电大学JAVA语言实用教程的第三章3-5题目
展开
3个回答
2013-09-22
展开全部
第五个问题 按GBK编码、拼音首字母还是笔画排序?
学生类
public class Student {
private String id;
private String name;
private int age;
private String sex;
public Student(String id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
测试代码
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args)
{
Student student1 = new Student("001", "张一", 18, "男");
Student student2 = new Student("002", "张二", 19, "男");
Student student3 = new Student("003", "张三", 20, "男");
Student student4 = new Student("004", "张四", 21, "女");
/** 将4个学生存入list*/
List<Student> students = new ArrayList<Student>();
students.add(student1);
students.add(student2);
students.add(student3);
students.add(student4);
/** 添加一个学生*/
students = addStudent( students, "0003", "王五", 23, "男" );
System.out.println("===========添加学生============");
/**将数组中顺序显示所有学生信息*/
showStudent(students);
/**将所有的学生增加一岁*/
addAge(students,1);
System.out.println("===========将所有的学生增加一岁============");
showStudent(students);
/**根据学号删除某个学生*/
students = delStudent( students, "003");
System.out.println("===========根据学号删除某个学生============");
showStudent(students);
System.out.println("===========将所有学生按姓名顺序输出============");
/**将所有学生按姓名顺序输出*/
showStudentName(students);
}
学生类
public class Student {
private String id;
private String name;
private int age;
private String sex;
public Student(String id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
测试代码
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args)
{
Student student1 = new Student("001", "张一", 18, "男");
Student student2 = new Student("002", "张二", 19, "男");
Student student3 = new Student("003", "张三", 20, "男");
Student student4 = new Student("004", "张四", 21, "女");
/** 将4个学生存入list*/
List<Student> students = new ArrayList<Student>();
students.add(student1);
students.add(student2);
students.add(student3);
students.add(student4);
/** 添加一个学生*/
students = addStudent( students, "0003", "王五", 23, "男" );
System.out.println("===========添加学生============");
/**将数组中顺序显示所有学生信息*/
showStudent(students);
/**将所有的学生增加一岁*/
addAge(students,1);
System.out.println("===========将所有的学生增加一岁============");
showStudent(students);
/**根据学号删除某个学生*/
students = delStudent( students, "003");
System.out.println("===========根据学号删除某个学生============");
showStudent(students);
System.out.println("===========将所有学生按姓名顺序输出============");
/**将所有学生按姓名顺序输出*/
showStudentName(students);
}
2013-09-22
展开全部
第五个问题应该要写个 排序的方法 你自己研究去吧!
import java.util.LinkedList;
import java.util.List;
public class Student {
private int id; // 学号
private String name; // 姓名
private int age; // 年龄
private int sex; // 性别 0,女;1,男
List<Student> students = new LinkedList<Student>();
/**
* 构造方法
*
* @param id
* @param name
* @param age
* @param sex
*/
public Student(int id, String name, int age, int sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
/**
* 增加学生
*
* @param s
*/
public void add(Student s) {
if (students != null)
students.add(s);
}
/**
* 根据学号删除学生
*
* @param id
*/
public void deleteById(int id) {
if (students != null) {
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
if (s.getId() == id)
students.remove(s);
}
}
}
/**
* 所有学生年龄增加一岁
*/
public void addAgeAll() {
if (students != null) {
for (int i = 0; i < students.size(); i++) {
students.get(i).setAge(students.get(i).getAge() + 1);
}
}
}
/**
* 按顺序显示所有学生信息
*/
public void printAllStudent() {
if (students != null) {
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println(s.getId() + "\t" + s.getName() + "\t"
+ s.getSex() + "\t" + s.getAge());
}
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
}
import java.util.LinkedList;
import java.util.List;
public class Student {
private int id; // 学号
private String name; // 姓名
private int age; // 年龄
private int sex; // 性别 0,女;1,男
List<Student> students = new LinkedList<Student>();
/**
* 构造方法
*
* @param id
* @param name
* @param age
* @param sex
*/
public Student(int id, String name, int age, int sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
/**
* 增加学生
*
* @param s
*/
public void add(Student s) {
if (students != null)
students.add(s);
}
/**
* 根据学号删除学生
*
* @param id
*/
public void deleteById(int id) {
if (students != null) {
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
if (s.getId() == id)
students.remove(s);
}
}
}
/**
* 所有学生年龄增加一岁
*/
public void addAgeAll() {
if (students != null) {
for (int i = 0; i < students.size(); i++) {
students.get(i).setAge(students.get(i).getAge() + 1);
}
}
}
/**
* 按顺序显示所有学生信息
*/
public void printAllStudent() {
if (students != null) {
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println(s.getId() + "\t" + s.getName() + "\t"
+ s.getSex() + "\t" + s.getAge());
}
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-22
展开全部
哎!也太懒了点吧!建议自己搞定,帮你做是害你啊!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询