第9章 对象和类
第10章 面对对象思考
第11章 继承和多态
第12章 异常处理和文本I/O
第13章 抽象类和对象
主要:实现Comparable接口
题目描述:作业内容要求:
1、定义学生类Student,包含学号、姓名、成绩三个属性,默认构造方法、带参数构造方法;
2、Student类实现Comparable接口,并实现compareTo方法(按照成绩属性比较大小);
3、定义测试类,创建5个学生对象,并使用Arrays.sort()方法对5个学生对象排序。
可能的Java代码:
/**
* 方便输入和测试的原因,把属性 姓名和学号 给隐藏忽略输入了,需要的自己改下 几个null值为name1、mark1.....mark4。
*/
/**
* @author XXXXX
* 2、Student类实现Comparable接口,并实现compareTo方法(按照成绩属性比较大小);---->这句话后面一句不懂
*
*/
//暂时不想改了,出了错误,只满足第二局前一部分,后一部分,有可能在Student类中实现????应该不是在Student类中实现实现Comparable接口吧。。。。。。那样的话,怎么实现ComparableStudent???最多在Student类实现compareTo方法(按照成绩属性比较大小),然后在ComparableStudent类中实现Comparable接口吧。。。。。。。。。。。。
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
/*System.out.print("Enter the mark of Student1: ");
String mark1=sc.next();
System.out.print("Enter the name of Student1: ");
String name1=sc.next();*/
System.out.print("Enter the grade of Student1: ");
double grade1=sc.nextDouble();
/*System.out.print("Enter the mark of Student2: ");
String mark2=sc.next();
System.out.print("Enter the name of Student2: ");
String name2=sc.next();*/
System.out.print("Enter the grade of Student2: ");
double grade2=sc.nextDouble();
/*System.out.print("Enter the mark of Student3: ");
String mark3=sc.next();
System.out.print("Enter the name of Student3: ");
String name3=sc.next();*/
System.out.print("Enter the grade of Student3: ");
double grade3=sc.nextDouble();
/*System.out.print("Enter the mark of Student4: ");
String mark4=sc.next();
System.out.print("Enter the name of Student4: ");
String name4=sc.next();*/
System.out.print("Enter the grade of Student4: "