Java练习编写一个程序能够对学生对象进行插入、修改、删除、浏览、统计人数等功能。
编写一个程序能够对学生对象进行插入、修改、删除、浏览、统计人数等功能。要求利用Vector和Enumeration等。提示如下1、建立一个学生类,类的成员变量:学号(st...
编写一个程序能够对学生对象进行插入、修改、删除、浏览、统计人数等功能。要求利用Vector和Enumeration等。提示如下1、建立一个学生类,类的成员变量:学号(string)、姓名(string)、性别(boolean)、年龄(string)、手机(string)和地址(string)。类的方法:构造方法,设置和读取各数据成员的方法。2、建立一个管理学生对象的向量类。类的成员变量:用于存放全部学生对象的数组(Vector类)和学生人数(int)。类的方法:构造方法(创建数组对象)、完成学生对象的添加、删除、修改、浏览和统计人数五个方法(对向量进行操作)3、定义主类进行测试
java的上机测试题,开发环境为eclipse求高手指导!!!!我的邮箱是dreamxiaoyue_430@163.com 展开
java的上机测试题,开发环境为eclipse求高手指导!!!!我的邮箱是dreamxiaoyue_430@163.com 展开
4个回答
展开全部
public class Student {
private String id;
private String name;
private boolean sex;
private String age;
private String mobile;
private String address;
public Student() {
}
public Student(String id, String name) {
this.id = id;
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (!id.equals(student.id)) return false;
return true;
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public String toString() {
return "Student{" +
"address='" + address + '\'' +
", id='" + id + '\'' +
", name='" + name + '\'' +
", sex=" + sex +
", age='" + age + '\'' +
", mobile='" + mobile + '\'' +
'}';
}
}
public class StudentManager {
private Vector<Student> students;
private int number;
public StudentManager() {
students = new Vector<Student>();
number = 0;
}
public void addStudent(Student student) {
if (students.contains(student)) {
System.out.println("学号为" + student.getId() + "的学生已经存在,不能有相同学号的学生。");
return;
}
if (students.add(student)) {
System.out.println("添加学生成功:" + student);
number++;
}
}
public void deleteStudent(Student student) {
if (students.remove(student)) {
System.out.println("成功删除学号为" + student.getId() + "的学生");
number--;
}
}
public void modifyStudent(Student student) {
if (students.remove(student)) {
students.add(student);
System.out.println("成功修改学号为" + student.getId() + "的学生,新学生的信息为" + student);
} else {
System.out.println("不存在学号为" + student.getId() + "的学生");
}
}
public void printStudents() {
Enumeration<Student> enumeration = students.elements();
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
}
public int getNumber() {
return number;
}
}
public class TestStudentManager {
public static void main(String[] args) {
StudentManager manager = new StudentManager();
Student student1 = new Student("001", "xiaowang");
Student student2 = new Student("002", "xiaohong");
manager.addStudent(student1);
manager.addStudent(student2);
System.out.println("学生个数:" + manager.getNumber());
manager.printStudents();
// a student with same id, cannot be added to manager
Student student3 = new Student("002", "xiaohong2");
manager.addStudent(student3);
System.out.println("学生个数:" + manager.getNumber());
manager.printStudents();
manager.deleteStudent(student1);
System.out.println("学生个数:" + manager.getNumber());
manager.printStudents();
Student student4 = new Student("002", "xiaohong2");
student4.setAge("25");
manager.modifyStudent(student4);
System.out.println("学生个数:" + manager.getNumber());
manager.printStudents();
}
}
追问
程序的语言是没有错误,可是就是没法运行出结果啊
追答
运行TestStudentManager没结果吗?
展开全部
性别 public enum SexType{
.....
}
public class Student{
private String id;
private String name;
private SexType sexType;
private String age;
private String phoneNumber;
private String address;
public Student(){}
public Student(String id){this.id=id;}
public Student(String name){this.name=name}
getter和sertter方法.....
}
至于vector
用集合Vector,
List<Student> students = new Vector<Student>();
学生人数用集合 length 学生的详细信息用Sudent属性get...();
.....
}
public class Student{
private String id;
private String name;
private SexType sexType;
private String age;
private String phoneNumber;
private String address;
public Student(){}
public Student(String id){this.id=id;}
public Student(String name){this.name=name}
getter和sertter方法.....
}
至于vector
用集合Vector,
List<Student> students = new Vector<Student>();
学生人数用集合 length 学生的详细信息用Sudent属性get...();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
大三的时候做个类似的,学生的增删改查吗?你可以如果有需要可以参考一下,不喜勿喷,不懂可以问。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
就是crud吗,你肯定学过。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询