简体   繁体   English

Java:调用方法并从另一个类实例化对象时出现问题

[英]Java: Problems when calling methods and instantiating objects from another class

I am an AP Computer Programming student and I am having quite a bit of trouble understanding how to call methods to a driver program defined in one class , as well as create objects in the driver class from the constructor of the non-driver class. 我是一名AP计算机编程专业的学生,​​在理解如何调用在一个类中定义的驱动程序的方法以及如何从非驱动程序类的构造函数中创建对象的过程中遇到了很多麻烦。 My book has the following class, with the program's intent being to simulate the flipping of a coin: 我的书有以下课程,该程序的目的是模拟硬币的翻转:

import java.util.Random;

public class Coin{

        private int HEADS = 0;
        private int TAILS = 1;

        private int face;

        Coin mycoin = new Coin();

        public Coin(){

        flip();
        }

        public void flip(){

            face = (int) (Math.random() * 2);
        }

        public boolean isHeads(){

            return (face == HEADS);
        }

        public String toString(){

            String faceName;

            if (face == HEADS)
                faceName = "Heads";
            else
                faceName = "Tails";

            return faceName;
        }
}

Here is an example driver program from the book as well: 这也是本书中的示例驱动程序:

public class FlipRace
{
  public static void main(String[] args)
  {
    final int NUM_FLIPS = 1000;
    int heads = 0, tails = 0;

    Coin myCoin = new Coin();

    for (int count = 1; count <= NUM_FLIPS; count++)
    {
      myCoin.flip();

      if (myCoin.isHeads())
        heads++;
      else
        tails++;
    }

    System.out.println("The number of flips: " + NUM_FLIPS);
    System.out.println("The number of heads: " + heads);
    System.out.println("The number of tails: " + tails);   
  }
}

The problem I have is that when I try to recreate the driver program or make any driver program, the driver program seems to be unable to call upon the customized classes of the constructor. 我遇到的问题是,当我尝试重新创建驱动程序或制作任何驱动程序时,驱动程序似乎无法调用构造函数的自定义类。 I get errors like: 我收到如下错误:

cannot find symbol
  symbol:  Class Coin
  location:  Class (name of driver program)

I honestly have no idea what the problem is. 老实说,我不知道问题是什么。 It had been said to me that a separate .java file was needed in order to access the constructor, but I couldn't find a way to do that. 有人告诉我,需要一个单独的.java文件来访问构造函数,但是我找不到解决方法。 Was that person wrong? 那个人错了吗? Am I supposed to put the main method and the constructor all on one program? 我是否应该将main方法和构造函数全部放在一个程序上? If not that, how do I call upon methods from another class? 如果不是那样,如何调用另一个类的方法?

I've been struggling with this for almost a week, so any help would be appreciated. 我已经为此苦苦挣扎了近一个星期,因此我们将不胜感激。

EDIT : The spelling errors in the for loop were my own. 编辑 :for循环中的拼写错误是我自己的。 They are not present in the actual program. 它们不在实际程序中。 I have now fixed them. 我现在修复了它们。

  1. Make sure the driver and the Coin class are in the same package (or that you import the package Coin is in), it appears that you have them both in the default package. 确保驱动程序和Coin类在同一个程序包中(或您导入的Coin程序包在其中),看来它们都在默认程序包中。
  2. Compile Coin.java before (or at the same time) as the driver javac *.java 在驱动程序javac *.java之前(或同时)编译Coin.java
  3. Make sure you specify a classpath that includes the current folder when you execute the driver like 执行以下驱动程序时,请确保指定包含当前文件夹的类路径

    java -cp . java -cp。 FlipRace FlipRace

Finally, 最后,

for (inc count = 1; counr <= NUM_FLIPS; count++)

should be 应该

for (int count = 1; count <= NUM_FLIPS; count++)

I haven't exhaustively searched your code for more typos. 我还没有详尽搜索您的代码是否有更多错别字。

Try out this by reoving this code from Coin Class 通过从Coin类中清除此代码来尝试一下

Coin mycoin = new Coin();

    public Coin(){

    flip();
    }

and made changes in the line of for loop in FlipRace 并在FlipRace中的for循环行中进行了更改

    for (int count = 1; count <= NUM_FLIPS; count++)

I think the best advice that can be given here is to research the basics of Java packages. 我认为这里可以提供的最佳建议是研究Java软件包的基础。 This is a good place to start: Java packages tutorial 这是一个不错的起点: Java软件包教程

It covers such topics as: 它涵盖了以下主题:

  • Creating and Using Packages 创建和使用包
  • Using Package Members 使用包成员
  • Managing Source and Class Files 管理源文件和类文件

Having read that, you should be able to understand the problems you are facing. 阅读这些内容后,您应该能够理解所面临的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM