永发信息网

急!求助!java数组构造问题

答案:2  悬赏:20  手机版
解决时间 2021-01-27 01:27
  • 提问者网友:刺鸟
  • 2021-01-26 14:27
1、 数组的使用
1) 假定每个学生都必须修3门课程,现为Student类增加一个Course类型的数组变量(变量名为courses),表示学生修的课程;
2) 为courses变量定义set与get方法;
3) 为Student类增加一个成员方法,该方法打印出学生所修的每门课程的课程名称与课程学分(遍历数组),方法名为printCoursesInfo。
2、 Set集合的使用
1) 假定每个学生所修的课程数量不定,“课程”变量不再适合用数组类型,现改用Java集合存储课程对象。首先用Set集合,修改数组类型的courses变量,改用HashSet或TreeSet类型定义courses变量,并且在Student类的构造方法中对courses变量进行实例化。
2) 定义一个addCourse(Course course)方法,向courses中添加课程对象;
3) 定义一个deleteCourse(Course course)方法,从courses中删除course对象;
4) 修改printCoursesInfo方法,遍历集合输出课程信息。
3、 List集合的使用
1) 用List集合,修改数组类型的courses变量,改用ArrayList类型定义courses变量,并且在Student类的构造方法中对courses变量进行实例化。
2) 定义一个addCourse(Course course)方法,向courses中添加课程对象;
3) 定义一个deleteCourse(Course course)方法,从courses中删除course对象;
4) 修改printCoursesInfo方法,遍历集合输出课程信息。
4、 测试
1) 在Demo类中实例化学生对象与课程对象,分别测试上述定义的printCoursesInfo方法。
Course类:
package exp3;

public abstract class Course {
public String name;
public float credit;
public float score;
public abstract boolean obtainCredit();

}
我就是不知道get和set方法该怎么办。。
最佳答案
  • 五星知识达人网友:胯下狙击手
  • 2021-01-26 15:37
set/get 都是eclipse右键,source 下面生成的
全部回答
  • 1楼网友:青尢
  • 2021-01-26 16:55
如果不需要按照提示来写的话可以这样写: import java.io.*; import java.util.arraylist; import java.util.list; import java.util.set; import java.util.treeset; public class studentscore implements comparable { private string id; private string name; private int maths; private int english; private int program; private int average; public studentscore(string id, string name, int maths, int english, int program) { this.id = id; this.name = name; this.maths = maths; this.english = english; this.program = program; this.average = (maths english program) / 3; } public int compareto(studentscore that) { return that.average - this.average; } public string tostring() { return id "\t" name "\t" maths "\t" english "\t" program "\t" average; } public static void main(string[] args) throws ioexception { list scores = new arraylist(); set sorted = new treeset(); system.out.print("请输入学生人数: "); int students = getint(); system.out.println("请输入学生信息: "); system.out.println("========================================================="); while(scores.size() < students) { system.out.print("输入学号: "); string id = getstring(); system.out.print("输入姓名: "); string name = getstring(); system.out.print("高等数学: "); int maths = getint(); system.out.print("英 语: "); int english = getint(); system.out.print("程序设计: "); int program = getint(); studentscore score = new studentscore(id, name, maths, english, program); scores.add(score); sorted.add(score); } system.out.println("学 号\t姓 名\t高等数学\t英 语\t程序设计\t总评成绩"); system.out.println("========================================================="); for(studentscore score: scores) system.out.println(score); system.out.println("排序后的学生信息"); system.out.println("学 号\t姓 名\t高等数学\t英 语\t程序设计\t总评成绩"); system.out.println("========================================================="); for(studentscore score: sorted) system.out.println(score); } private static string getstring() throws ioexception { bufferedreader reader = new bufferedreader(new inputstreamreader(system.in)); return reader.readline(); } private static int getint() throws ioexception { bufferedreader reader = new bufferedreader(new inputstreamreader(system.in)); string line = reader.readline(); return integer.parseint(line); } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯