Java定义学⽣类Student,包含学号、姓名、成绩三个属性,默认构造⽅法、带
参数构造⽅。。。
第9章 对象和类
第10章 ⾯对对象思考
第11章 继承和多态
第12章 异常处理和⽂本I/O
第13章 抽象类和对象
主要:实现Comparable接⼝
题⽬描述:作业内容要求:
1、定义学⽣类Student,包含学号、姓名、成绩三个属性,默认构造⽅法、带参数构造⽅法;
2、Student类实现Comparable接⼝,并实现compareTo⽅法(按照成绩属性⽐较⼤⼩);
3、定义测试类,创建5个学⽣对象,并使⽤Arrays.sort()⽅法对5个学⽣对象排序。
可能的Java代码:
/**
* ⽅便输⼊和测试的原因,把属性姓名和学号给隐藏忽略输⼊了,需要的⾃⼰改下⼏个null值为name1、ark4。
*/
/**
* @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 ();
System.out.print("Enter the name of Student1: ");
String ();*/
System.out.print("Enter the grade of Student1: ");
double Double();
/*System.out.print("Enter the mark of Student2: ");
String ();
System.out.print("Enter the name of Student2: ");
String ();*/
System.out.print("Enter the grade of Student2: ");
double Double();
/*System.out.print("Enter the mark of Student3: ");
String ();
System.out.print("Enter the name of Student3: ");
String ();*/
System.out.print("Enter the grade of Student3: ");
double Double();
/*System.out.print("Enter the mark of Student4: ");
String ();
System.out.print("Enter the name of Student4: ");
System.out.print("Enter the name of Student4: ");
String ();*/
System.out.print("Enter the grade of Student4: ");
double Double();
/
*System.out.print("Enter the mark of Student5: ");
String ();
System.out.print("Enter the name of Student5: ");
String ();*/
System.out.print("Enter the grade of Student5: ");
double Double();
System.out.println();
ComparableStudent student1=new ComparableStudent(null, null, grade1);
ComparableStudent student2=new ComparableStudent(null, null, grade2);
ComparableStudent student3=new ComparableStudent(null, null, grade3);
ComparableStudent student4=new ComparableStudent(null, null, grade4);
ComparableStudent student5=new ComparableStudent(null, null, grade5);
ComparableStudent student6 =(ComparableStudent)Max.max(student1, student2); ComparableStudent student7 =(ComparableStudent)Max.max(student3, student4); ComparableStudent student8 =(ComparableStudent)Max.max(student6, student7);
ComparableStudent student9 =(ComparableStudent)Max.max(student5, student8);
System.out.println("The Student1's Grade is "+ Grade());
System.out.println("The Student2's Grade is "+ Grade());
System.out.println("The Student3's Grade is "+ Grade());
System.out.println("The Student4's Grade is "+ Grade());
System.out.println("The Student5's Grade is "+ Grade());
System.out.println();
ComparableStudent[] students={
new ComparableStudent(null, null, grade1),
new ComparableStudent(null, null, grade2),
new ComparableStudent(null, null, grade3),
new ComparableStudent(null,null, grade4),
new ComparableStudent(null, null, grade5),
};
java.util.Arrays.sort( students);
for(Student student:students)
{
System.out.Grade());
}
}
}
class Student
{
String mark;
String name;
double grade;
Student()
{
}
Student(String mark,String name,double grade)
{
this.mark=mark;
this.name=name;
}
void setMark(){
void setMark(){
this.mark=mark;
}
public String getMark(String mark){
return mark;
}
void setNamek(){
this.name=name;
}
public String getName(String name){
return name;
}
void setGrade(){
}
public double getGrade(){
return grade;
}
}
class ComparableStudent extends Student implements Comparable
{
public ComparableStudent(String mark,String name,double grade)
{
super(mark, name, grade);
}
public int compareTo(Object o)
{
if(getGrade()>((Student) o).getGrade())
{
return1;
}
java接口有没有构造方法else if(getGrade()<((Student) o).getGrade())
{
return-1;
}
else
{
return0;
}
}
//@Override
public String toString()
{
String()+"mark: "+getMark(mark)+" name"+getName(name)+"grade:"+getGrade(); /输出只有成绩,没有学号和姓名
}
}
class Max
{
public static ComparableStudent max(ComparableStudent o1, ComparableStudent o2)
{
if(o1pareTo(o2)>0)
return o1;
else
return o2;
}
}
样例运⾏结果:
END
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论