Java:实现子类与父类的继承以及各自类的方法,且运用Comparator进行多排序输出(第十周)

阅读: 评论:0

Java:实现子类与父类的继承以及各自类的方法,且运用Comparator进行多排序输出(第十周)

Java:实现子类与父类的继承以及各自类的方法,且运用Comparator进行多排序输出(第十周)

题目来源:大工慕课 链接
作者:Caleb Sung

题目要求

注:本题基于上周的题目进行方法升级操作,并删除了一个子类。
查看上周题目请移步文章:Java:实现多子类与父类的继承,并实现各自类的方法(第九周)

实现此UML类图中所示的三个类:

1. 运动员的getDescription()方法应该返回一个字符串,例如“An Athlete XXXName with height of XXX and weight of XXX”。
2. BasketballPlayer中的getDescription()应该返回一个字符串,例如“A basketball player XXXName with shooting level of XXX”。
3. BaseballPlayer中的getDescription()应该返回一个字符串,例如“A baseball player XXXName with hitting level of XXX”。
4. 运动员之间的默认比较基于他们的名字。
5. BasketballPlayer的默认比较基于他们的shootLv
6. 在主函数中,实现一个由3位运动员组成的数组,然后按以下顺序对数组进行排序:名字升序、名字降序、身高降序。
7. 在主功能中,实现一个由3名篮球员组成的数组,然后按以下顺序对数组进行排序:shootLv升序、shootLv降序。


参考代码

Athlete类(父类)

class Athlete implements Comparable<Athlete>{private String name;private int height;private int weight;public Athlete(String n, int h, int w) {name = n;height = h;weight = w;}public String getName() {return name;}public int getHeight() {return height;}public int getWeight() {return weight;}public String toString() {return String.format("An Athlete %s with height of %d and weight of %d", name, weight, height);}public int compareTo(Athlete arg0) {return this.namepareTo(arg0.name);}
}

用于按名字/身高比较运动员的三个类

class CompAthleteNameAc implements Comparator<Athlete>{public int compare(Athlete arg0, Athlete arg1) {Name()Name());}
}class CompAthleteNameDec implements Comparator<Athlete>{public int compare(Athlete arg0, Athlete arg1) {Name()Name());}
}class CompAthleteHeightDec implements Comparator<Athlete>{public int compare(Athlete arg0, Athlete arg1) {Height() - Height();}
}

BasketballPlayer类(子类)

class BasketballPlayer extends Athlete{private int shootLv;public BasketballPlayer(String n, int h, int w, int sl) {super(n, h, w);shootLv = sl;}public String toString() {return String.format("A basketball player %s with shooting level of %d", getName(), shootLv);}public int getShootLv() {return shootLv;}public int compareTo(Athlete arg0) {BasketballPlayer p = (BasketballPlayer) arg0;return this.shootLv - p.shootLv;}
}

用于按ShootLv比较运动员的两个类

class CompBasketballPlayerShootLvAc implements Comparator<BasketballPlayer>{public int compare(BasketballPlayer arg0, BasketballPlayer arg1) {ShootLv() - ShootLv();}
}class CompBasketballPlayerShootLvDec implements Comparator<BasketballPlayer>{public int compare(BasketballPlayer arg0, BasketballPlayer arg1) {ShootLv() - ShootLv();}
}

imports、主函数与测试用例

import java.util.Arrays;
import java.util.Comparator;public class ArraySortTest {public static void main(String[] args) {Athlete[] athletes = new Athlete[3];BasketballPlayer[] basketballplayers = new BasketballPlayer[3];//Testsathletes[0] = new Athlete("HanMei", 180, 60);athletes[1] = new Athlete("Lilei", 190, 70);athletes[2] = new Athlete("Tom", 170, 50);basketballplayers[0] = new BasketballPlayer("Jerry", 175, 55, 4);basketballplayers[1] = new BasketballPlayer("Sam", 195, 75, 6);basketballplayers[2] = new BasketballPlayer("Amy", 165, 65, 5);//System.out.String(athletes));System.out.println("The default comparison between Athletes based on their names:");Arrays.sort(athletes);for(Athlete a : athletes)System.out.println(a);System.out.println("nThe default comparison between BasketballPlayer based on their shootLv(s):");Arrays.sort(basketballplayers);for(BasketballPlayer b : basketballplayers)System.out.println(b);System.out.println("nSort the Athlete array in the order of Name ascending:");Arrays.sort(athletes, new CompAthleteNameAc());for(Athlete a : athletes)System.out.println(a);System.out.println("nSort the Athlete array in the order of Name descending:");Arrays.sort(athletes, new CompAthleteNameDec());for(Athlete a : athletes)System.out.println(a);System.out.println("nSort the Athlete array in the order of Height descending:");Arrays.sort(athletes, new CompAthleteHeightDec());for(Athlete a : athletes)System.out.println(a);System.out.println("nSort the BasketballPlayer array in the order of ShootLv ascending:");Arrays.sort(basketballplayers, new CompBasketballPlayerShootLvAc());for(BasketballPlayer b : basketballplayers)System.out.println(b);System.out.println("nSort the BasketballPlayer array in the order of ShootLv descending:");Arrays.sort(basketballplayers, new CompBasketballPlayerShootLvDec());for(BasketballPlayer b : basketballplayers)System.out.println(b);}
}

运行结果

在IDEA里会提示Athlete类的getWeight()未被使用,注释掉也无伤大雅。

The default comparison between Athletes based on their names:
An Athlete HanMei with height of 60 and weight of 180
An Athlete Lilei with height of 70 and weight of 190
An Athlete Tom with height of 50 and weight of 170The default comparison between BasketballPlayer based on their shootLv(s):
A basketball player Jerry with shooting level of 4
A basketball player Amy with shooting level of 5
A basketball player Sam with shooting level of 6Sort the Athlete array in the order of Name ascending:
An Athlete HanMei with height of 60 and weight of 180
An Athlete Lilei with height of 70 and weight of 190
An Athlete Tom with height of 50 and weight of 170Sort the Athlete array in the order of Name descending:
An Athlete Tom with height of 50 and weight of 170
An Athlete Lilei with height of 70 and weight of 190
An Athlete HanMei with height of 60 and weight of 180Sort the Athlete array in the order of Height descending:
An Athlete Lilei with height of 70 and weight of 190
An Athlete HanMei with height of 60 and weight of 180
An Athlete Tom with height of 50 and weight of 170Sort the BasketballPlayer array in the order of ShootLv ascending:
A basketball player Jerry with shooting level of 4
A basketball player Amy with shooting level of 5
A basketball player Sam with shooting level of 6Sort the BasketballPlayer array in the order of ShootLv descending:
A basketball player Sam with shooting level of 6
A basketball player Amy with shooting level of 5
A basketball player Jerry with shooting level of 4

本文发布于:2024-02-04 08:26:38,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170703079453950.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:子类   方法   Java   Comparator
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23