java - 以前凌乱的任务仍然有一些我不明白的错误

标签 java class methods

我的任务是这个link

我的问题是我创建的导师类(class)的底部,我收到这些错误 here

这是两段代码:

公共(public)课 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);
}
}

有人可以解释一下出了什么问题吗?我无法通过谷歌找到任何东西。

最佳答案

此 setter 有问题:

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

因为 StaffIdstaffId 是不同的变量(只是情况不同),所以此方法实际上不执行任何操作(它将成员变量分配给自身)。

这个 setter 也有同样的问题:

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

使用此方法:

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

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

用这一行

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

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

关于java - 以前凌乱的任务仍然有一些我不明白的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16471273/

相关文章:

java - 如何监听 OutOfMemoryError 并退出 JVM

java - 重定向到 Spring MVC 中的外部 URL

objective-c - 如果我重写派生类中的方法,是否应该在派生类的 header 中再次声明它?

c++ - 无法理解类构造函数

ios - 意外删除了 View Controller 。程序不会编译

c# - 在属性内执行初始化是一种好习惯吗?

java - 构建一个仅打印基于文件的数组的特定部分的方法

java - 在本地主机中测试时如何模拟不同的位置?

java - 我在 Netbeans 中有一个使用 JOptionPane.showOptionDialog 的游戏,但我不知道如何执行 if 语句

javascript - javascript中可以调用子方法吗?