java小问题,谁能帮我写写?自己写不来啊
答案:3 悬赏:10 手机版
解决时间 2021-04-26 19:13
- 提问者网友:我一贱你就笑
- 2021-04-26 00:29
java小问题,谁能帮我写写?自己写不来啊
最佳答案
- 五星知识达人网友:長槍戰八方
- 2021-04-26 01:33
【Course类】
import java.util.ArrayList;
import java.util.List;
public class Course {
private String name;
public static List courseList = new ArrayList();
public Course(String name) {
this.name = name;
this.createCourse(name);
}
public String getName() {
return name;
}
private void createCourse(String name) {
if (name != null && !"".equals(name)) {
courseList.add(name);
System.out.println("创建了课程【" + name + "】");
}
}
}
【Student类】
import java.util.HashSet;
public class Student {
private String name;
private HashSet courses;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public HashSet getCourses() {
return courses;
}
public void addCourse(Course c) {
if (this.courses == null) {
this.courses = new HashSet();
}
if (c != null) {
this.courses.add(c);
System.out.println(this.name + "选修了" + c.getName());
}
}
public void removeCourse(String name) {
if (this.courses == null) {
return;
}
for (Course course : this.courses) {
if (this.equals(course, name)) {
this.courses.remove(course);
System.out.println(this.name + "放弃了" + course.getName());
break;
}
}
}
private boolean equals(Course course, String name) {
if (course.getName() == null) {
if (name == null) {
return true;
} else {
return false;
}
}
return course.getName().equals(name);
}
}
【SchoolClass类】
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class SchoolClass {
HashSet students;
public void addStudent(Student s) {
if (this.students == null) {
this.students = new HashSet();
}
if (s != null) {
this.students.add(s);
}
}
public void removeStudent(String name) {
if (this.students == null) {
return;
}
for (Student student : this.students) {
if (this.equals(student, name)) {
this.students.remove(student);
break;
}
}
}
private boolean equals(Student student, String name) {
if (student.getName() == null) {
if (name == null) {
return true;
} else {
return false;
}
}
return student.getName().equals(name);
}
public Map account() {
Map results = new HashMap();
for (String courseName : Course.courseList) {
results.put(courseName, 0);
}
if (this.students != null) {
for (Student student : this.students) {
HashSet courses = student.getCourses();
if (courses == null) {
continue;
}
for (Course course : courses) {
Integer value = results.get(course.getName());
if (value == null) {
continue;
}
results.put(course.getName(), value + 1);
}
}
}
return results;
}
public static void main(String[] args) {
Course c1 = new Course("语文");
Course c2 = new Course("数学");
Course c3 = new Course("英语");
Course c4 = new Course("物理");
Course c5 = new Course("化学");
Student s1 = new Student("张三");
s1.addCourse(c1);
s1.addCourse(c3);
s1.addCourse(c4);
s1.removeCourse(c3.getName());
s1.addCourse(c5);
Student s2 = new Student("李四");
s2.addCourse(c2);
s2.addCourse(c4);
s2.addCourse(c5);
s2.removeCourse(c5.getName());
Student s3 = new Student("王五");
s3.addCourse(c1);
s3.addCourse(c2);
s3.addCourse(c3);
s3.addCourse(c4);
s3.addCourse(c5);
s3.removeCourse(c1.getName());
s3.removeCourse(c5.getName());
Student s4 = new Student("李雷");
s4.addCourse(c1);
s4.addCourse(c4);
Student s5 = new Student("韩梅梅");
s5.addCourse(c1);
s5.addCourse(c2);
s5.addCourse(c3);
s5.addCourse(c4);
s5.addCourse(c5);
Student s6 = new Student("Lucy");
s6.addCourse(c5);
Student s7 = new Student("Lily");
s7.addCourse(c3);
s7.addCourse(c5);
Student s8 = new Student("Jim");
SchoolClass c = new SchoolClass();
c.addStudent(s1);
c.addStudent(s2);
c.addStudent(s3);
c.addStudent(s4);
c.addStudent(s5);
c.addStudent(s6);
c.addStudent(s7);
c.addStudent(s8);
c.removeStudent(s4.getName());
Map map = c.account();
Set> set = map.entrySet();
Iterator> it = set.iterator();
while (it.hasNext()) {
Entry entry = it.next();
System.out.println("选了课程【" + entry.getKey() + "】共有" + entry.getValue() + "人");
}
}
}
import java.util.ArrayList;
import java.util.List;
public class Course {
private String name;
public static List
public Course(String name) {
this.name = name;
this.createCourse(name);
}
public String getName() {
return name;
}
private void createCourse(String name) {
if (name != null && !"".equals(name)) {
courseList.add(name);
System.out.println("创建了课程【" + name + "】");
}
}
}
【Student类】
import java.util.HashSet;
public class Student {
private String name;
private HashSet
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public HashSet
return courses;
}
public void addCourse(Course c) {
if (this.courses == null) {
this.courses = new HashSet
}
if (c != null) {
this.courses.add(c);
System.out.println(this.name + "选修了" + c.getName());
}
}
public void removeCourse(String name) {
if (this.courses == null) {
return;
}
for (Course course : this.courses) {
if (this.equals(course, name)) {
this.courses.remove(course);
System.out.println(this.name + "放弃了" + course.getName());
break;
}
}
}
private boolean equals(Course course, String name) {
if (course.getName() == null) {
if (name == null) {
return true;
} else {
return false;
}
}
return course.getName().equals(name);
}
}
【SchoolClass类】
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class SchoolClass {
HashSet
public void addStudent(Student s) {
if (this.students == null) {
this.students = new HashSet
}
if (s != null) {
this.students.add(s);
}
}
public void removeStudent(String name) {
if (this.students == null) {
return;
}
for (Student student : this.students) {
if (this.equals(student, name)) {
this.students.remove(student);
break;
}
}
}
private boolean equals(Student student, String name) {
if (student.getName() == null) {
if (name == null) {
return true;
} else {
return false;
}
}
return student.getName().equals(name);
}
public Map
Map
for (String courseName : Course.courseList) {
results.put(courseName, 0);
}
if (this.students != null) {
for (Student student : this.students) {
HashSet
if (courses == null) {
continue;
}
for (Course course : courses) {
Integer value = results.get(course.getName());
if (value == null) {
continue;
}
results.put(course.getName(), value + 1);
}
}
}
return results;
}
public static void main(String[] args) {
Course c1 = new Course("语文");
Course c2 = new Course("数学");
Course c3 = new Course("英语");
Course c4 = new Course("物理");
Course c5 = new Course("化学");
Student s1 = new Student("张三");
s1.addCourse(c1);
s1.addCourse(c3);
s1.addCourse(c4);
s1.removeCourse(c3.getName());
s1.addCourse(c5);
Student s2 = new Student("李四");
s2.addCourse(c2);
s2.addCourse(c4);
s2.addCourse(c5);
s2.removeCourse(c5.getName());
Student s3 = new Student("王五");
s3.addCourse(c1);
s3.addCourse(c2);
s3.addCourse(c3);
s3.addCourse(c4);
s3.addCourse(c5);
s3.removeCourse(c1.getName());
s3.removeCourse(c5.getName());
Student s4 = new Student("李雷");
s4.addCourse(c1);
s4.addCourse(c4);
Student s5 = new Student("韩梅梅");
s5.addCourse(c1);
s5.addCourse(c2);
s5.addCourse(c3);
s5.addCourse(c4);
s5.addCourse(c5);
Student s6 = new Student("Lucy");
s6.addCourse(c5);
Student s7 = new Student("Lily");
s7.addCourse(c3);
s7.addCourse(c5);
Student s8 = new Student("Jim");
SchoolClass c = new SchoolClass();
c.addStudent(s1);
c.addStudent(s2);
c.addStudent(s3);
c.addStudent(s4);
c.addStudent(s5);
c.addStudent(s6);
c.addStudent(s7);
c.addStudent(s8);
c.removeStudent(s4.getName());
Map
Set
Iterator
while (it.hasNext()) {
Entry
System.out.println("选了课程【" + entry.getKey() + "】共有" + entry.getValue() + "人");
}
}
}
全部回答
- 1楼网友:拾荒鲤
- 2021-04-26 03:30
哎!又要交作业了
- 2楼网友:北城痞子
- 2021-04-26 01:58
SimpleThread是动态内部类,创建这样对象必须有一个所在类的实例与之对应,程序是在静态方法(静态方法不属于任何实例)中直接调用动态内部类因此编译不通过。
这样的错误好比类中的静态方法不能直接调用动态方法.
修改如下:
package threadbag;
public class SimpleThreadDemo {
public static void main(String[] args){
new SimpleThread("thread1").start();
new SimpleThread("thread2").start();
}
public static class SimpleThread extends Thread{
public SimpleThread(String str)
public void run(){
for(int i=0;i<10;i++)
{
System.out.println(" "+getName());
try{
sleep((long)(Math.random()*3000));
}catch(Exception e){}
}
}
}
}
这样的错误好比类中的静态方法不能直接调用动态方法.
修改如下:
package threadbag;
public class SimpleThreadDemo {
public static void main(String[] args){
new SimpleThread("thread1").start();
new SimpleThread("thread2").start();
}
public static class SimpleThread extends Thread{
public SimpleThread(String str)
public void run(){
for(int i=0;i<10;i++)
{
System.out.println(" "+getName());
try{
sleep((long)(Math.random()*3000));
}catch(Exception e){}
}
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯