java - 在我的任务中创建方法时出现问题

标签 java class methods

目标是产生这个:

Picture of the task summary here

这些是我尝试编译时遇到的错误: screen shot

我已经更改并修复了我认为主要是我愚蠢的大多数更明显的错误。对不起。

我有这个代码

public class 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);
}
}

最佳答案

错别字:toString() 而不是 tostring(),这会导致 Object.toString() 被调用并且预期的格式化字符串没有被退回。更改为:

@Override public String toString()

tostring() 是方法名称的情况下,使用 @Override 注释会产生编译器错误并提醒您错误,因为没有方法该名称存在于父类(super class)中。

一些 setter 方法缺少参数:

// Insert setPapers() method here
public void setPapers(){
    this.papers = papers;
}

// Insert setOnLeave() method here
public void setOnLeave(){
    this.OnLeave = OnLeave;
}

关于java - 在我的任务中创建方法时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16470392/

相关文章:

java - 重写枚举中的抽象方法时避免代码重复

java - 我怎样才能设法使用 2 个持久单元并使我的应用程序识别要使用哪个?

python - 如何定义一个方法以便返回当前类的实例而不是它在 Python 中继承的类的实例?

java - feed方法java if语句

java - 声明一个最终的静态方法是个坏主意吗?

java - 根据Java中的类使用不同的方法

java - Gradle WAR 和 Ant 风格过滤器无法正常工作

c++ - 为类的成员创建别名

c# - 数据库工厂类设计

java - 单链表复制困惑