java - 测试程序错误
问题描述
我已经为所述程序编写了一个程序和测试器,但是当我编译测试器时我得到了一些错误(如下所示)有人明白我为什么会得到这些错误吗?
主要代码 -
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 文件,然后编写一个创建三个 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 新手,这就是我所得到的。
解决方案
可能是因为您正在发明自己的 Java 语法:
Student stuOne = newStudent1();
应该:
Student stuOne = new Student();
就像您在 Student 类的main方法中所做的一样。
除此之外,真正的答案是:不要写那么多代码,到最后运行编译器。只写几行,就足以让你认为“这应该编译”。然后运行编译器。修复所有错误。多写几行。等等。
除此之外,这里的真正问题可能是您不了解在使用 javac 编译包中的类时需要遵循的复杂规则。我建议例如仔细阅读本教程。
推荐阅读
- solr - solr 模糊 vs 通配符 vs 词干分析器
- reactjs - 为什么说我的组件没有定义?
- pytorch - PyTorch 中的可微分图像压缩操作
- python - Python 摄取 pdf 而不使用需要 pip install 的包
- java - 尝试设置 buttonOnClick 侦听器以打开过滤的回收器视图列表时(交换)解析方法出错
- python - 如何将 Heruko 与本地机器的 Docker API(Python SDK)一起使用?
- angular - 从 5 到 7 的 PrimeNG 更新失去了我的风格
- expo - Expo:MediaLibrary.createAlbumAsync 正在创建多个同名专辑
- c# - 来自 github 的第一个项目将无法编译:SqlServerSpatial140.dll not found
- hadoop - HDFS 的 128M 默认块大小如何最小化 seekTime/transferTime 比率