测试器程序错误

时间:2019-03-19 18:32:02

标签: java oop

我已经为该程序编写了一个程序和测试仪,但是当我编译测试仪时遇到一些错误(如下所示),有人能理解我为什么得到这些错误吗?

主代码-

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文件,然后编写一个测试器类, 创建学生的三个实例。在本练习中,以硬编码方式提供学生详细信息 参数。与往常一样,请确保您的测试程序提供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是新来的,这是我所得到的。

1 个答案:

答案 0 :(得分:2)

可能是因为您正在发明自己的Java语法:

include '/path/to/key.php';

应该是:

Student stuOne = newStudent1();

就像您在Student类的 main 方法中一样。

除此之外,真正的答案是:不要编写太多代码,然后最终运行编译器。只写几行,正好足以使您认为“应该编译”。然后运行编译器。修复所有错误。再写几行。等等。

除此之外,这里的真正问题可能是您不了解使用javac编译包中包含的类时需要遵循的复杂规则。我建议您仔细阅读此tutorial