我找到了我认为可以帮助我完成作业的代码。但是,我收到标题中声明的错误。这是该计划。我是Java的新手,所以请原谅我格式化中的任何错误。该错误突出显示“公共课程中的第一个括号。
public class Student //Student Class
{
Address home = new Address("1027 Charleston St","Lincoln", "Ne", 68508);
Address school = new Address("1534 E St", "Lincoln", "Ne", 68508);
Student mike = new Student("Mike", "Vinci", home, school);
Student john = new Student("John", "Doe", home, school, 90, 80, 199);
//Below are the ints used
int test1 = 0;
int test2 = 0;
int test3 = 0;
int avg2;
int error = 0;
public void setTestScore(int testNum, int score)
{
if (testNum == 1)
test1 = score;
else if (testNum == 2)
test2 = score;
else if (testNum == 3)
test3 = score;
}
public int getTestScore(int testNum)
{
if (testNum == 1)
return test1;
else if (testNum == 2)
return test2;
else if (testNum == 3)
return test3;
else
return error;
}
public int average(int test1, int test2, int test3)
{
int avg2 = ((test1 + test2 + test3)/3); //finds the average of the tests
return avg2;
}
private String firstName, lastName; //private ints for coding
private Address homeAddress, schoolAddress;
public Student (String first, String last, Address home, Address school)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
}
public Student (String first, String last, Address home, Address school, int test11, int test22, int test33)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
test11 = test1;
test22 = test2;
test33 = test3;
}
public String toString()
{
String result;
result = firstName + " " + lastName + "\n";
result += "Home Address:\n" + homeAddress + "\n";
result += "School Address:\n" + schoolAddress + "\n";
result += "Average=" + avg2 + " with Tests: " + test1 + ", " + test2 + ", " + test3;
return result; //returns the result
}
class Address
{
private String streetAddress, city, state;
private long zipCode;
public Address(String street, String town, String st, long zip)
{
streetAddress = street;
city = town;
state = st;
zipCode = zip;
}
public String toString()
{
String result;
result = streetAddress + "\n";
result += city + "," + state + " " + zipCode;
return result; //returns the result
}
}
}
public class Course extends Student //Course class
{
private String course;
private Student s1, s2, s3, s4, s5;
private int studentcount = 1;
public Course (String name)
{
course = name;
}
public Student addStudent(String first, String last, Address home, Address school)
{
if (studentcount == 1){
s1 = new Student(first,last,home,school);
studentcount++;
return s1;
}
if (studentcount == 2) {
s2 = new Student(first,last,home,school);
studentcount++;
return s2;
}
else if (studentcount == 3){
s3 = new Student(first,last,home,school);
studentcount++;
return s3;
}
else if (studentcount == 4){
s4 = new Student(first,last,home,school);
studentcount++;
return s4;
}
else if (studentcount == 5) {
s5 = new Student(first,last,home,school);
studentcount++;
return s5;
}
else {
System.out.println("No More students allowed in the class");
return null;
}
}
public double average()
{
return (s1.average() + s1.average() + s1.average() + s1.average() + s1.average()) / 5.0;
}
public String roll()
{
String results = "";
if (studentcount == 1){
results += s1.toString () +"n";
return results;
}
if (studentcount == 2) {
results += s1.toString () +"n";
results += s2.toString () +"n";
return results;
}
else if (studentcount == 3){
results += s1.toString () +"n";
results += s2.toString () +"n";
results += s3.toString () +"n";
return results;
}
else if (studentcount == 4){
results += s1.toString () +"n";
results += s2.toString () +"n";
results += s3.toString () +"n";
results += s4.toString () +"n";
return results;
}
else if (studentcount == 5) {
results += s1.toString () +"n";
results += s2.toString () +"n";
results += s3.toString () +"n";
results += s4.toString () +"n";
results += s5.toString () +"n";
return results;
}
else{
return null;
}
}
}
答案 0 :(得分:4)
您的Course
课程延伸Student
(这没有多大意义,但这不是重点)。
任何类中的构造函数总是调用其超类构造函数。您可以使用
明确地执行此操作public Course() {
super(...);
}
在这种情况下,您可以使用您指定的参数调用超类构造函数,也可以隐式地调用:
public Course() {
// no explicit call to super(...);
}
在这种情况下,您隐式调用超类构造函数,不带参数。
在您的情况下,您没有显式调用super(...)
,因此编译器将隐式调用超类构造函数Student()
:但是,因为没有这样的构造函数({{1}中唯一的构造函数{1}}需要两个字符串和两个Student
es)您会收到编译错误。
答案 1 :(得分:1)
您正在Student
课程中扩展Course
课程。您需要显式调用super()
方法,该方法将与Student类的构造函数匹配。默认情况下,它会调用默认构造函数,在您的情况下不可用。
您可以在Student类中定义无参数构造函数,然后解决问题。
public Student() { }
答案 2 :(得分:1)
要修复该特定错误,您需要向Student添加一个no-arg构造函数。
public Student(){
//empty
}
您还有其他几个有些问题,例如课程扩展学生,但这是一个完全不同的讨论。