【问题标题】:Tester Program Errors测试程序错误
【发布时间】: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(以后可能会导致问题)
  • 另外,如果您尝试调用构造函数,这不是正确的方法。

标签: java oop


【解决方案1】:

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

Student stuOne = newStudent1();

应该是:

 Student stuOne = new Student();

就像您在 Student 类的 ma​​in 方法中所做的那样。

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

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

【讨论】:

  • 我在我的代码中更改了这些,但是我仍然得到相同的编译错误
  • 我不知道这是什么意思
  • 在我提出的代码中是否有任何地方可以看到这个问题?
  • @CallumFawcett 我稍微增强了我的答案。但请理解:这不是免费的辅导服务,人们可以帮助您完成作业的所有步骤。我们会提示您在哪里/研究什么,然后由您来应用您在此处告诉的内容。为了能够为您提供帮助,您需要布置您的目录结构以及您在哪里调用什么,以及您的类路径设置可能是什么,等等。
【解决方案2】:

2 我看到的错误:

  1. 实际上要在类中创建一个对象,您必须使用类对象,这意味着 StudentTest 而不是 Student,即 StudentTest stuTwo = newStudent2(); ,如果您不使用 StudentTest ,即 Student ,那么应该从哪里创建对象?如果你想使用主类中的对象?您必须使用继承来获取对象,否则新对象的创建将不会引用它。假设问题是指新对象创建找不到它的符号。

  2. newStudent(); -> 应该是 -> new Student();

结论使用主类对象应该使用继承方法。请找到关于继承

的教程

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 2016-10-09
    相关资源
    最近更新 更多