java - 测试程序错误

标签 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文件,然后编写一个测试器类 创建三个 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 类的 ma​​in 方法中所做的那样。

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

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

关于java - 测试程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55247841/

相关文章:

java - Junit @Rule 和 Maven Checkstyle 插件

java - 软件导致连接中止 : socket write error

oop - 为什么还要费心使用公共(public)成员变量?

c# - 当列为字符串时动态排序 ObservableCollection<object>

jquery - 在 jQuery 中扩展原型(prototype)时如何保持对 this 关键字的控制?

Java 密码库偶尔无法启动

java.lang.NoClassDefFoundError : org/apache/poi/EncryptedDocumentException 错误

oop - 非 Objective-C 语言中 'receiver' 的等效术语

c# - 处理镜像继承树

java - CAC卡/读卡器的PKCS11驱动程序