0

我的任务是这个链接

我的问题是我创建的导师课程的底部,我在这里得到了这些错误

这是两段代码:

公共类 Ex5Program {

public void start() {
    Tutor[] tutors = createTutorsArray();
    printTutors(tutors);
    printOnLeaveList(tutors);
    updateTutorDetails(tutors[1]);
    printNewTutorDetails(tutors[1]);
    Tutor tutorWithMostPapers = getTutorWithMostPapers(tutors);
    printTutorWithMostPapers(tutorWithMostPapers);
}

private Tutor[] createTutorsArray() {
    String[] noPapers = {};
    String[] introductoryPapers = {"CompSci101", "CompSci111"};
    String[] coreStage1Papers = {"CompSci101", "CompSci105"};
    String[] allStageOnePapers = {"CompSci111", "CompSci101", "CompSci105"};
    String[] stageTwoPapers = {"CompSci210", "CompSci220", "CompSci225", "CompSci230"};
    Tutor[] tutors = new Tutor[7];
    tutors[5] = new Tutor("Sad Sack", 86302, introductoryPapers, false);
    tutors[4] = new Tutor("Crystal Ball", 49123, introductoryPapers, false);
    tutors[2] = new Tutor("Earl Lee Riser", 40879, allStageOnePapers, true);
    tutors[3] = new Tutor("Tom Katt", 50876, stageTwoPapers, false);
    tutors[1] = new Tutor("Candy Kane", 30869, noPapers, false);
    tutors[0] = new Tutor("Carrie Oakey", 30987, coreStage1Papers, true);
    tutors[6] = new Tutor("Sonny Day", 49586, stageTwoPapers, true);
    return tutors;
}

private void printTutors(Tutor[] tutors) {
    System.out.println("Current Tutors");
    System.out.println("==============");
    for (int i = 0; i < tutors.length; i++) {
        System.out.print(i + 1 + ". ");
        System.out.println(tutors[i].toString());
    }
}

private void printOnLeaveList(Tutor[] tutors) {
    System.out.println();
    System.out.println("Tutors Currently on Leave");
    System.out.println("=========================");
    for (int i = 0; i < tutors.length; i++) {
        if (tutors[i].isOnLeave()) {
            System.out.println(tutors[i].getName());
        }
    }
}


private void updateTutorDetails(Tutor tutor) {
    tutor.setName("Ali Katt");
    tutor.setStaffId(23456);
    String[] stage1Papers = {"CompSci101", "CompSci105", "CompSci111"};
    tutor.setPapers(stage1Papers);
    tutor.setOnLeave(true);
}

private void printNewTutorDetails(Tutor tutor) {
    System.out.println();
    System.out.println("Updated details");
    System.out.println("===============");
    System.out.println("Name: " + tutor.getName());
    System.out.println("Id: " + tutor.getstaffId());
    String[] papers = tutor.getPapers();
    System.out.print("Papers: ");
    if (papers.length > 0) {
        for (int i = 0; i < papers.length; i++) {
            System.out.print(papers[i] + " ");
        }
    } else {
        System.out.print("None");
    }
    System.out.println();
    if (tutor.isOnLeave()) {
        System.out.println("Currently on leave");
    }
}

private Tutor getTutorWithMostPapers(Tutor[] tutors) {
    Tutor tutorWithMostPapersSoFar = tutors[0];
    for (int i = 0; i < tutors.length; i++) {
        if (tutors[i].teachesMorePapersThan(tutorWithMostPapersSoFar)) {
            tutorWithMostPapersSoFar = tutors[i];
        }
    }
    return tutorWithMostPapersSoFar;
}

private void printTutorWithMostPapers(Tutor tutorWithMostPapers) {
    System.out.println();
    System.out.println("Most papers");
    System.out.println("===========");
    System.out.println(tutorWithMostPapers.getName() + " teaches more papers than any other tutor.");
}

}

public class Tutor {

// instance variables

private String name;
private int staffId;
private String[] papers;
private boolean onLeave;

public Tutor(String name, int staffId, String[] papers, boolean onLeave) {
    // Complete this constructor method
    this.name = name;
    this.staffId = staffId;
    this.papers = papers;
    this.onLeave = onLeave;
}
// Insert getName() method here
public String getName(){
    return name;
}
// Insert setName() method here
public void setName(String name){
    this.name = name;
}
// Insert getStaffId() method here
public int getStaff(){
    return staffId;
}
// Insert setStaffId() method here
public void setStaffId(int StaffId){
    this.staffId = staffId;
}
// Insert getPapers() method here;
public  String[] getPapers(){
    return papers;
}
// Insert setPapers() method here
public void setPapers(String[] papers){
    this.papers = papers;
}
// Insert isOnLeave() method here
public boolean isOnLeave(){
    return onLeave;
}
// Insert setOnLeave() method here
public void setOnLeave(boolean OnLeave){
    this.onLeave = onLeave;
}
// Insert toString() method here
public String toString(){
    return name + "(Staff id:"+staffId+")";
}
// Insert teachesMorePapersThan() method here
public Tutor teachesMorePapersThan(Tutor other){
    return(papers.length>other.papers.length);
}
}

有人可以解释什么是错的,我找不到任何没有谷歌的东西。

4

1 回答 1

2

这个设置器有一个问题:

public void setStaffId(int StaffId){
    this.staffId = staffId;
}

因为StaffId是一个不同的变量staffId(不同的情况下),这个方法实际上什么都不做(它将成员变量分配给它自己)。

这个二传手也有同样的问题:

public void setOnLeave(boolean OnLeave){
    this.onLeave = onLeave;
}

使用这种方法:

public Tutor teachesMorePapersThan(Tutor other){
    return(papers.length>other.papers.length);
}

您试图boolean从比较中返回 a ,但您已声明它返回 a Tutor。我认为您的意思是声明它返回一个boolean.

有了这条线

System.out.println("Id: " + tutor.getstaffId());

您正在尝试调用getstaffId()a Tutor,但没有这样的方法。我能找到的最接近的是getStaff,这就是我认为你的意思。

于 2013-05-09T21:21:44.157 回答