1

目标是产生这个:

任务摘要的图片在这里

这些是我尝试编译时遇到的错误: 屏幕截图

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

我有这个代码

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);
}
}
4

4 回答 4

5

Typo: toString()not tostring(),这导致Object.toString()正在调用并且没有返回预期的格式化字符串。改成:

@Override public String toString()

如果是方法名称,使用@Override注解会产生编译器错误tostring()并警告您该错误,因为超类中不存在该名称的方法。

一些 setter 方法缺少参数:

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

// Insert setOnLeave() method here
public void setOnLeave(){
    this.OnLeave = OnLeave;
}
于 2013-05-09T20:22:12.127 回答
1

您需要查看错误文本以查找问题。虽然新手可能会本能地将错误消息视为无用(由于多年单击 x 或取消或 Windows 对话框上的任何内容),但错误文本实际上是找出错误是什么的最有用的资源,90%的时间。

例如,第一个错误读取

File: F:\course related stuff\101\Lab06\Ex5\Ex5Program.java [line: 54]
Error: method setStaffId in class Tutor cannot be applied to given types;
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length

如果你仔细阅读,你会发现它告诉你文件名行号方法调用名包含该方法的类名,以及一些关于错误确切类型的附加信息。它甚至通过在需要“无参数”的地方放置一个“int”来告诉您在调用该方法时做错了什么,“实际参数列表和正式参数列表的长度不同”。

阅读其他错误消息,您会发现它们实际上告诉您问题所在。

这段代码还需要插入换行符来分组内容,添加注释以准确解释它是如何工作的,并且修复了一些 Java 样式违规 - 一些教师对样式和清晰度以及功能进行评分。

另外,如果你的课程不及格是因为你不了解如何编程,那可能是因为过度使用堆栈溢出来解决问题。在现实世界中,如果你可以只使用别人的代码,那就太好了,但编程课程的重点是教你如何编写自己的代码,而不是如何使用别人的代码。

于 2013-05-09T20:58:08.837 回答
1

第一个错误:setStaffID()

您使用 int 调用它,但在您的方法中您说它没有任何参数。

看看你有一些其他错误是由同样的错误引起的。先纠正他们...

于 2013-05-09T20:26:39.863 回答
0

嗯,帮助并不容易,因为我认为你不知道自己在做什么。但是,当您创建这样的 set 方法时,首先要做的是:

public void setPapers(){
    this.papers = papers;
}

你应该像这样声明参数:

public void setPapers(String[] papers){
    this.papers = papers;
}

你应该知道变量名是区分大小写的,所以:

private boolean onLeave;

public boolean isOnLeave(){
    //return OnLeave; this variable is not declared 
    return onLeave;
}

我认为您需要多学习一点,因为您无法阅读编译错误。

于 2013-05-09T20:45:20.847 回答