Java第三次实验:
正文开始@Assassin
1. 实验目的:
- (1)理解
List 的应用场合 - (2)掌握
Comparable 和Comparator 接口的使用方法 - (3)掌握迭代器接口
Iterator 和ListIterator 遍历集合的方
2. 实验内容:
2.1 处理扑克牌:
每张扑克牌有点数和花色,定义Poker 类,要求如下:
- (1)成员变量包括点数(
point )和花色(color ); - (2)定义构造方法初始化所有成员变量;
- (3)省略
setter() ,定义getter() 方法; - (4)重写
toString() 方法返回扑克牌的信息。
定义PokerComparator 类实现Comparator 接口,重写compare(Object obj1,Object obj2) 方法,首先按照花色排序,如果花色相同则按照点数从大到小排序。 定义游戏类Game ,要求如下:
- (1) 定义变量
ArrayLIst 类型保存Poker 对象,并定义空参构造; - (2) 省略
setter 和getter 方法; - (3) 定义
showAll() 方法显示成员变量ArrayList 对象的所有内容; - (4) 定义成员方法
sort 对成员变量ArrayList 元素按照花色点数排序。
定义测试类TestDemo ,完成以下任务:
- (1) 创建
ArrayList 对象 al ,有若干个不容的Poker 对象; - (2) 创建
Game 对象,参数是al ; - (3) 调用
Game 中的showAl() 方法显示所有牌; - (4) 调用
Game中 的sort() 方法对al 进行排序
直接贴代码了~~
Poker.java:
package com.haut.iot.ninghai;
@SuppressWarnings({"all"})
enum Color {
DIAMOND,
CLUB,
HEART,
SPADE;
private String[] colors = {"方块", "梅花", "红桃", "黑桃"};
@Override
public String toString() {
return colors[this.ordinal()];
}
}
@SuppressWarnings({"all"})
public class Poker {
private int point;
private Color color;
public Poker(int point, Color color) {
this.point = point;
this.color = color;
}
public int getPoint() {
return point;
}
public Color getColor() {
return color;
}
@Override
public String toString() {
return "\nPoker{" +
"point=" + point +
", color='" + color.toString() + '\'' +
'}';
}
}
PokerComparator.java:
package com.haut.iot.ninghai;
import java.util.Comparator;
import java.util.Objects;
public class PokerComparator implements Comparator<Poker> {
@Override
public int compare(Poker obj1, Poker obj2) {
if (!Objects.equals(obj1.getColor(), obj2.getColor())) {
return Integer.compare(obj2.getColor().ordinal(), obj1.getColor().ordinal());
} else {
return Integer.compare(obj2.getPoint(), obj1.getPoint());
}
}
}
Game.java:
package com.haut.iot.ninghai;
import java.util.ArrayList;
import java.util.Collections;
public class Game {
private ArrayList<Poker> al;
public Game(ArrayList<Poker> al) {
this.al = al;
}
public Game() {
}
@Override
public String toString() {
return "Game{" +
"al=" + al.toString() +
'}';
}
public void showAll() {
String ret = toString();
System.out.println(ret);
}
public void sort() {
PokerComparator pokerComparator = new PokerComparator();
for (int bound = 0; bound < al.size(); bound++) {
for (int cur = 0; cur < al.size() - 1 - bound; cur++) {
if (pokerComparator.compare(al.get(cur), al.get(cur + 1)) >= 1) {
Collections.swap(al, cur, cur + 1);
}
}
}
}
}
TestDemo.java:
package com.haut.iot.ninghai;
import java.util.ArrayList;
public class TestDemo {
public static void main(String[] args) {
ArrayList<Poker> al = new ArrayList<>();
al.add(new Poker(2, Color.SPADE));
al.add(new Poker(3, Color.HEART));
al.add(new Poker(4, Color.CLUB));
al.add(new Poker(5, Color.DIAMOND));
al.add(new Poker(7, Color.DIAMOND));
al.add(new Poker(6, Color.SPADE));
Game game = new Game(al);
game.showAll();
System.out.println("\n===========排序后===========\n");
game.sort();
game.showAll();
}
}
实验结果:
由图可知:完全按照先花色后点数来排序
2.2 学生成绩管理:
定义学生类(Student )实现Comparable 接口,要求:
- (1) 成员变量包括学号
id ,姓名name ,年龄age ,课程成绩ArrayList (至少三门课的成绩); - (2) 定义构造方法初始化所有成员变量
- (3) 省略
getter 和setter ; - (4) 功能方法:重写
Comparable 接口中的compareT() 方法按照总分从大到小的顺序排序,重写toString 方法返回学生的所有成员变量。
定义学生管理类Manager ,要求:
- (1) 成员变量包括
TreeSet ts ; - (2) 定义构造方法初始化成员变量
- (3) 定义功能方法:
show() 显示集合ts中的学生的学号、姓名、年龄、每门课程的成绩、总成绩,定义add(Student stu) 方法把一个学生信息添加到ts中,利用remove(Student stu) 方法删除一个学生,search(String id) 方法根据学号查找学生。
定义测试类TestDemo ,调用add() 方法添加至少10个学生信息,调用show() 方法显示学生信息,调用remove(String std) 方法删除学生信息,调用search(String id) 查找某学生信息。
Student.java:
package com.haut.iot._ninghai;
import java.util.List;
public class Student implements Comparable<Student> {
public String id;
public String name;
public List<Integer> scoreList;
@Override
public int compareTo(Student o) {
int scoreRet1 = 0;
int scoreRet2 = 0;
for (Integer next : scoreList) {
scoreRet1 += next;
}
for (Integer next : o.scoreList) {
scoreRet2 += next;
}
return Integer.compare(scoreRet2, scoreRet1);
}
public Student() {
}
public Student(String id, String name, List<Integer> scoreList) {
this.id = id;
this.name = name;
this.scoreList = scoreList;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", scoreList=" + scoreList +
'}';
}
}
Manager.java:
package com.haut.iot._ninghai;
import java.util.TreeSet;
public class Manager {
private TreeSet<Student> ts;
public Manager() {
}
public Manager(TreeSet<Student> ts) {
this.ts = ts;
}
public void show() {
for (Student student : ts) {
int total = 0;
for (int i = 0; i < student.scoreList.size(); i++) {
total = total + student.scoreList.get(i);
}
System.out.println("学生的学号为:" + student.id + ",姓名为:" + student.name +
",成绩分别是" + student.scoreList + ",总成绩为:" + total);
}
}
public void add(Student student) {
this.ts.add(student);
}
public void remove(Student student) {
this.ts.remove(student);
}
public void search(String id) {
int count = 0;
for (Student student : ts) {
if (student.id.equals(id)) {
System.out.print("找到了!!");
System.out.println(student);
break;
}else {
count++;
if (count == ts.size()) {
System.out.println("Sorry, the information could not be found!");
}
}
}
}
}
TestDemo.java:
package com.haut.iot._ninghai;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
public class TestDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<>();
Manager manager = new Manager(ts);
for (int i = 0; i < 10; i++) {
List<Integer> list = new ArrayList<>();
list.add(99 - i);
list.add(98 - i);
list.add(97 - i);
Student student = new Student("" + i, "boy0" + i, list);
manager.add(student);
}
manager.show();
System.out.println("\n==========我是分割线===========\n");
List<Integer> list = new ArrayList<>();
list.add(100);
list.add(100);
list.add(100);
Student s1 = new Student("666", "王源", list);
manager.add(s1);
manager.show();
System.out.println("\n==========我是分割线===========\n");
manager.search("666");
System.out.println("\n==========我是分割线===========\n");
manager.remove(s1);
manager.show();
}
}
实验结果:
添加10个信息: add 宁海 search 宁海 && delete 宁海 可以看出,完全是按照总分从大到小排序的。
|