【发布时间】:2019-03-19 18:32:01
【问题描述】:
我已经为所述程序编写了一个程序和测试器,但是当我编译测试器时我得到了一些错误(如下所示)有人明白我为什么会得到这些错误吗?
主代码 -
package com.date.example;
import java.io.*;
import java.util.*;
public class Student {
public static void main(String[] args) {
Student student = new Student("Charles");
}
private String forName;
private String surName;
private String studentID;
private String degreeScheme;
//This is the Constructor of the
public Student(String forName) {
this.forName = forName;
}
public Student() {
// TODO Auto-generated constructor stub
}
//Assign the surname of the student
public void stuSurname(String stuSurname) {
surName = stuSurname;
}
//Assign the student ID to the student
public void stuID(String stuID) {
studentID = stuID;
}
//Assign the Degree of the Student
public void stuDegree(String stuDegree) {
degreeScheme = stuDegree;
}
//Print the student details
public void printStudent() {
System.out.println("Forname:" + forName);
System.out.println("Surename:" +
surName);
System.out.println("Student ID:" +
studentID);
System.out.println("Degree Scheme:" +
degreeScheme);
}
// setter
public void setForName(String forName) {
this.forName = forName;
}
// getter
public String getForName() {
return forName;
}
}
测试程序 -
package com.date.example;
import java.io.*;
public class StudentTest {
public static void main(String[] args) {
/*create three new objects using constructor*/
Student stuOne = newStudent1();
Student stuTwo = newStudent2();
Student stuThree = newStudent3();
//Invoking Methods for Each object Created
stuOne.setForName("James");
stuOne.stuSurname("Smith");
stuOne.stuID("0987");
stuOne.stuDegree("Computer Science");
stuTwo.setForName("Vanessa");
stuTwo.stuSurname("Peach");
stuTwo.stuID("0988");
stuTwo.stuDegree("Mathematics");
stuThree.setForName("George");
stuThree.stuID("0989");
stuThree.stuDegree("English");
//Invoking the printStudentmethod.
stuOne.printStudent();
System.out.println("\n");
stuTwo.printStudent();
System.out.println("\n");
stuThree.printStudent();
}
}
这就是编写代码的目的 -
Student 类应该包含一个构造函数、适当的 getter 和 setter,以及通常的 字符串方法。编译java源码得到一个.class文件,然后编写一个tester类 创建三个 Student 实例。对于本练习,以硬编码的形式提供学生详细信息 参数。与往常一样,确保您的测试程序提供 100% 的方法覆盖率。
然后这是我得到的编译错误-
TheRealFawcett:Lab8 therealfawcett$ javac StudentTest.java
StudentTest.java:8: error: cannot find symbol
Student stuOne = newStudent1();
^
symbol: class Student
location: class StudentTest
StudentTest.java:8: error: cannot find symbol
Student stuOne = newStudent1();
^
symbol: method newStudent1()
location: class StudentTest
StudentTest.java:9: error: cannot find symbol
Student stuTwo = newStudent2();
^
symbol: class Student
location: class StudentTest
StudentTest.java:9: error: cannot find symbol
Student stuTwo = newStudent2();
^
symbol: method newStudent2()
location: class StudentTest
StudentTest.java:10: error: cannot find symbol
Student stuThree = newStudent3();
^
symbol: class Student
location: class StudentTest
StudentTest.java:10: error: cannot find symbol
Student stuThree = newStudent3();
^
symbol: method newStudent3()
location: class StudentTest
6 errors
TheRealFawcett:Lab8 therealfawcett$
如果有人理解我为什么会遇到这些错误,我们将不胜感激,我是 java 新手,这就是我所得到的。
【问题讨论】:
-
您在代码中的何处定义
newStudent1()、newStudent2()或newStudent3()? -
尝试将所有
Student stuOne = newStudent1();替换为new Student();。还可以尝试使用 Junit 测试,甚至注释掉 student 中的 main,因为您现在有 2 个 main(以后可能会导致问题) -
另外,如果您尝试调用构造函数,这不是正确的方法。