目标是产生这个:
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);
}
}
答案 0 :(得分:5)
错字:toString()
而非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;
}
答案 1 :(得分:1)
第一个错误:setStaffID()
您使用int调用它,但在您的方法上,您说它没有任何参数。
看一下你有其他错误导致的其他错误。先纠正他们......
答案 2 :(得分: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样式违规 - 一些教师对样式和清晰度以及功能进行评分。
另外,如果您失败的原因是因为您不了解如何编程,可能是因为过度使用堆栈溢出来解决问题。在现实世界中,如果你可以使用其他人的代码,这很好,但编程课的目的是教你如何提出你自己的代码,而不是如何使用别人的代码。
答案 3 :(得分:0)
嗯,这不容易帮助,因为我觉得你不知道你在做什么。但是,当您创建这样的set方法时,首先要做的事情是:
public void setPapers(){
this.papers = papers;
}
你应该声明这样的参数:
public void setPapers(String[] papers){
this.papers = papers;
}
你应该知道变量名是caseSensitive所以:
private boolean onLeave;
public boolean isOnLeave(){
//return OnLeave; this variable is not declared
return onLeave;
}
我认为你需要多学习一点,因为你无法阅读编译错误。