一、任务简要描述
1、实现Java版本的快速排序。
2、编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类Person,并作为学生数据操作类Student 和教师数据操作类Teacher 的基类。
3、请设计 3 个类 , 分别是学生类 Student, 本科生类Undergaduate, 研究生类 Postgraduate, 其中 Student 类是一个抽象类 , 它包含一些基本的学生信息如姓名、所学课程、课程成绩等 , 而Undergraduate 类和 Postgraduate 都是 Student 类的子类 , 它们之间的主要差别是计算课程成绩等级的方法有所不同 , 研究生的标准要比本科生的标准高一些。假设某班级里既有本科生也有研究生 , 请编写程序统计出全班学生的成绩等级并显示出来。此题关键是设计一个学生数组 , 既能存放本科生对象 , 又能存放研究生对象。
二、问题及解决
1、算法思想:基于分治的思想,是冒泡排序的改进型。首先在数组中选择一个基准点(该基准点的选取可能影响快速排序的效率,后面讲解选取的方法),然后分别从数组的两端扫描数组,设两个指示标志(lo指向起始位置,hi指向末尾),首先从后半部分开始,如果发现有元素比该基准点的值小,就交换lo和hi位置的值,然后从前半部分开始扫秒,发现有元素大于基准点的值,就交换lo和hi位置的值,如此往复循环,直到lo>=hi,然后把基准点的值放到hi这个位置。一次排序就完成了。以后采用递归的方式分别对前半部分和后半部分排序,当前半部分和后半部分均有序时该数组就自然有序了。
排序过程:
输出结果:
2、
Person类:
1 public class person { 2 String name; 3 String number; 4 5 person(){ } 6 person(String number, String name){ 7 this.number = number; 8 this.name = name; 9 } 10 public void setNumber(String n){ 11 number = n; 12 } 13 public String getNumber(){ 14 return number; 15 } 16 public void setName(String c){ 17 name = c; 18 } 19 public String getName(){ 20 return name; 21 } 22 public void input(){ 23 Scanner sc = new Scanner(System.in); 24 System.out.println("Please input the name:"); 25 name = sc.nextLine(); 26 System.out.println("Please input the number:"); 27 number = sc.nextLine(); 28 } 29 public void showInfo(){ 30 System.out.println("The number is:" + getNumber() + ", name is:" + getName()); 31 } 32 }
Teacher类:
1 class Teacher extends person{ 2 String title; //职称 3 String dept; //部门 4 Teacher(){ 5 super(); 6 } 7 Teacher(String number, String name, String title, String dept){ 8 super(number, name); 9 this.title = title; 10 this.dept = dept; 11 } 12 public void setTitle(String t){ 13 title = t; 14 } 15 public String getTitle(){ 16 return title; 17 } 18 public void setDept(String d){ 19 dept = d; 20 } 21 public String getDept() { 22 return dept; 23 } 24 //子类重载父类方法 25 public void input(){ 26 super.input(); 27 System.out.println("Please input the title:"); 28 Scanner sc = new Scanner(System.in); 29 setTitle(sc.nextLine()); 30 //title = sc.nextLine(); 31 System.out.println("Please input the dept:"); 32 dept = sc.nextLine(); 33 } 34 public void showInfo(){ 35 super.showInfo(); 36 System.out.println("The teacher's title is:" + getTitle() + ", department is:" + getDept()); 37 } 38 }
Student类:
1 class Student extends person{ 2 String classnumber; //班号 3 int grade; //成绩 4 Student(){ 5 super(); 6 } 7 Student(String number, String name, String classnumber, int grade){ 8 super(number, name); 9 this.classnumber = classnumber; 10 this.grade = grade; 11 } 12 public void setClassnumber(String c){ 13 classnumber = c; 14 } 15 public String getClassnumber(){ 16 return classnumber; 17 } 18 public void setGrade(int g){ 19 grade = g; 20 } 21 public int getGrade() { 22 return grade; 23 } 24 //子类重载父类方法 25 public void input(){ 26 super.input(); 27 System.out.println("Please input the classnumber:"); 28 Scanner sc = new Scanner(System.in); 29 setClassnumber(sc.nextLine()); 30 //classnumber = sc.nextLine(); 31 System.out.println("Please input the grade:"); 32 grade = sc.nextInt(); 33 } 34 public void showInfo(){ 35 super.showInfo(); 36 System.out.println("The student's classnumber is:" + getClassnumber() + ", grade is:" + getGrade()); 37 } 38 }
输出结果:
3、
对象数组:
1 Student[] students = new Student[5];
Student类:
1 public abstract class Student 2 { 3 String name; 4 String type; 5 int courseScore; 6 String courseGrade; 7 8 public Student(String name) 9 { 10 this.name = name; 11 courseGrade=""; 12 } 13 14 public abstract void calculateGrade(); 15 16 public String getName() 17 { 18 return name; 19 } 20 21 public String getType() 22 { 23 return type; 24 } 25 26 public String getCourseGrade() 27 { 28 return courseGrade; 29 } 30 31 public int getCourseScore() 32 { 33 return courseScore; 34 } 35 36 public void setName(String name) 37 { 38 this.name = name; 39 } 40 41 public void setType(String type) 42 { 43 this.type = type; 44 } 45 46 public void setCourseScore(int courseScore) 47 { 48 this.courseScore = courseScore; 49 } 50 51 }
本科生类:
1 class Undergraduate extends Student 2 { 3 public Undergraduate(String name, int courseScore) 4 { 5 super(name); 6 type = "本科生"; 7 this.courseScore = courseScore; 8 } 9 10 public void calculateGrade() 11 { 12 if (courseScore >= 80 && courseScore < 100) courseGrade = "优秀"; 13 else if (courseScore >= 70 && courseScore < 80) courseGrade = "良好"; 14 else if (courseScore >= 60 && courseScore < 70) courseGrade = "一般"; 15 else if (courseScore >= 50 && courseScore < 60) courseGrade = "及格"; 16 else courseGrade = "不及格"; 17 18 } 19 }
研究生类:
1 class Postgraduate extends Student 2 { 3 public Postgraduate(String name, int courseScore) 4 { 5 super(name); 6 type = "研究生" ; 7 this.courseScore = courseScore; 8 } 9 public void calculateGrade() 10 { 11 if (courseScore >= 90 && courseScore < 100) courseGrade = "优秀"; 12 else if (courseScore >= 80 && courseScore < 90) courseGrade = "良好"; 13 else if (courseScore >= 70 && courseScore < 80) courseGrade = "一般"; 14 else if (courseScore >= 60 && courseScore < 70) courseGrade = "及格"; 15 else courseGrade = "不及格"; 16 17 } 18 } 19
成绩计算类:
1 public class Polymorphism 2 { 3 int total = 0; 4 double average; 5 6 public void showInfo(){ 7 Student[] students = new Student[5]; 8 9 students[0] = new Undergraduate("ABC", 95); 10 students[1] = new Undergraduate("DEF", 85); 11 students[2] = new Postgraduate("GHI", 75); 12 students[3] = new Undergraduate("JKL", 65); 13 students[4] = new Postgraduate("MNO", 55); 14 for (int i=0; i<5; i++) 15 { 16 total += students[i].courseScore; 17 students[i].calculateGrade(); 18 } 19 average = total / 5; 20 System.out.println("姓名" + " 类型" +" 成绩"); 21 System.out.println("-----------------------"); 22 23 for (int i=0; i<5; i++) 24 { 25 System.out.println(students[i].getName( )+" "+students[i].getType( )+" "+students[i].getCourseGrade( )); 26 } 27 28 //Student st = new Student("李明"); 29 System.out.println("平均成绩:" + average); 30 System.out.println(); 31 for(int i=0; i<5; i++) 32 { 33 if(students[i] instanceof Undergraduate ) 34 { 35 System.out.println(students[i].getName()+"是本科生"); 36 } 37 else 38 { 39 System.out.println(students[i].getName()+"是研究生"); 40 } 41 } 42 } 43 public static void main(String[] args) 44 { 45 Polymorphism show = new Polymorphism(); 46 show.showInfo(); 47 48 } 49 }
输出结果:
总结:
练习使用了创建对象数组的方法,使用面向对象的思想,子类继承父类的属性和方法,并对父类的方法进行重写,在题目三中,我本来将计算成绩的内容全部写在main方法里,经过老师讲解,我重新修改了程序,将创建对象数组和成绩计算的相关程序单独写在一个类里面。