Task 1: create an ArrayList of strings representing a list of your hobbies, • Task 2: read from a file “student_hobbies.txt” and store the information in the file into an Arraylist of Student-type object. You will need to create a Student class for this. • Task 3: Sort the list of students by ascending order of names. • Task 4: For each of your hobby, find out how many students in the student Arraylist share that hobby with you. An example running result is given in Figure 2. This AppDriver.java file contains compile errors because of the missing method definitions, for example, createHobbies(), readFile(), and findSharedHobbies(). Do not change the main(). Just add definitions of the above methods to your program. Specifically, create createHobbies() for Task 1, readFile() for Task 2, and findSharedHobbies() for Task 4. For the sorting inTask 3 to work appropriately, you will need to have the Student class implement the Comparable interface.

时间: 2024-03-03 22:50:38 浏览: 21
好的,让我来帮您实现这个程序。以下是完整的代码,其中包含了Task 1到Task 4的实现: ```java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class AppDriver { private static ArrayList<String> hobbies = new ArrayList<String>(); private static ArrayList<Student> students = new ArrayList<Student>(); public static void main(String[] args) { createHobbies(); readFile(); sortStudents(); findSharedHobbies(); } // Task 1: create an ArrayList of strings representing a list of your hobbies public static void createHobbies() { hobbies.add("Reading"); hobbies.add("Swimming"); hobbies.add("Playing video games"); hobbies.add("Watching movies"); } // Task 2: read from a file “student_hobbies.txt” and store the information in the file into an Arraylist of Student-type object. public static void readFile() { try { File file = new File("student_hobbies.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); String[] parts = line.split(","); String name = parts[0].trim(); String hobby = parts[1].trim(); students.add(new Student(name, hobby)); } scanner.close(); } catch (FileNotFoundException e) { System.out.println("Cannot find file: student_hobbies.txt"); } } // Task 3: Sort the list of students by ascending order of names. public static void sortStudents() { Collections.sort(students); } // Task 4: For each of your hobby, find out how many students in the student Arraylist share that hobby with you. public static void findSharedHobbies() { for (String hobby : hobbies) { int count = 0; for (Student s : students) { if (s.getHobby().equals(hobby)) { count++; } } System.out.println("There are " + count + " students who share the hobby of " + hobby); } } } class Student implements Comparable<Student> { private String name; private String hobby; public Student(String name, String hobby) { this.name = name; this.hobby = hobby; } public String getName() { return this.name; } public String getHobby() { return this.hobby; } @Override public int compareTo(Student s) { return this.name.compareTo(s.getName()); } } ``` 在上述代码中,我们首先定义了一个静态的ArrayList变量hobbies来存储我们自己的爱好。然后,我们实现了Task 1,即在createHobbies()方法中向hobbies变量中添加了一些爱好。接着,我们实现了Task 2,即在readFile()方法中读取了文件“student_hobbies.txt”中的内容,并将每个学生的姓名和爱好存储到了一个Student对象中,并将这些对象存储到了静态的ArrayList变量students中。然后,我们实现了Task 3,即在sortStudents()方法中使用Collections.sort()函数对students变量进行升序排序。最后,我们实现了Task 4,即在findSharedHobbies()方法中遍历hobbies变量中的每个爱好,并统计有多少个学生的爱好与之相同,并输出结果。 注意,在实现Task 3时,我们需要让Student类实现Comparable接口,并覆盖其compareTo()方法,以便可以在Collections.sort()函数中使用。在compareTo()方法中,我们只需要比较两个Student对象的姓名即可。 请确保在运行程序之前将“student_hobbies.txt”文件放置在正确的路径下,并且文件内容格式为“姓名,爱好”的形式,例如: ``` Tom,Swimming Alice,Reading Bob,Running David,Singing ``` 如果您需要对文件路径进行更改,请在readFile()方法中修改相应的文件路径。

相关推荐

最新推荐

recommend-type

Java ArrayList.toArray(T[]) 方法的参数类型是 T 而不是 E的原因分析

主要介绍了Java ArrayList.toArray(T[]) 方法的参数类型是 T 而不是 E的原因分析的相关资料,需要的朋友可以参考下
recommend-type

C#中数组、ArrayList和List三者的区别详解及实例

主要介绍了C#中数组、ArrayList和List三者的区别详解及实例的相关资料,需要的朋友可以参考下
recommend-type

基于C/C++开发的单目控制机械臂的上位机程序+视觉识别和关节角反解+源码(高分优秀项目)

基于C/C++开发的单目控制机械臂的上位机程序+视觉识别和关节角反解+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 基于C/C++开发的单目控制机械臂的上位机程序+视觉识别和关节角反解+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 基于C/C++开发的单目控制机械臂的上位机程序+视觉识别和关节角反解+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 基于C/C++开发的单目控制机械臂的上位机程序+视觉识别和关节角反解+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这