user10980219
user10980219

Reputation:

Writing a Main Method to call an Object?

We are learning Java with BlueJ and I am finding BlueJ very unclear and confusing. We've been given an assignment and one of the first steps is 'create a main method to call the Train object'. I'm sure he meant 'to call the Train method'.

Anyway, I'm trying to get the main method to call the Train method in the code below so when you pass a string argument in BlueJ, it executes the code in main. That's it. The Train method passes in a String and two int values:

public static void main(String[] args)
{
    Train train = new Train();
    train.Train();
}

I keep getting an error when trying to do this though and not sure why.

This is the code for the Train Class:

public class Train
{
    //Fields
    /* Destination of the Train */
    private String destination = new String();

    /* Train number - identifies the Train */
    private int TrainNumber;

    /* Capacity of the Train - how many customers can be in it */
    private int capacity;

    /* Number of customers currently in the Train */
    private int numberInTrain;

    /* Ticket price - how much a ticket costs. */
    private int ticketPrice;

    public static void main(String[] args)
    {
        Train train = new Train();
        train.Train();
    }

    /** Constructor for Train
     * @param dest the destination of the Train
     * @param num the number of the Train
     * @param cap the capacity of the Train
     */
    public void Train(String dest, int num, int cap)
    {
        destination = dest;
        capacity = cap;
        TrainNumber = num;

        numberInTrain = 0;
        ticketPrice = 50;
    }

    //mutators
    /* Records customer taking Train */
    public void enterTrain ()
    {
        numberInTrain = numberInTrain + 1;
    }

    /* Records customer leaving Train */
    public void leaveTrain ()
    {
        numberInTrain = numberInTrain - 1;
    }

}

This however, gives me an error and I cannot run the program from main. Any hints to the right direction will be appreciated.

The error is:

Error:(30, 14) java: method Train in class TrainAssignment.Train cannot be applied to given types;
required: java.lang.String,int,int
found: no arguments
reason: actual and formal argument lists differ in length

EDIT I have tried

public static void main(String[] args)
{
    Train train = new Train();
    train.Train(dest, num, cap);
}

and this also gives an error of cannot find variables. The values need to come from user input. When an object is created, we input the destination, the number of people and the capacity for each object.

Upvotes: 0

Views: 2146

Answers (1)

Joshua Burt
Joshua Burt

Reputation: 76

Your professor has it correct. You want to call the Train object. When you call Train train = new Train(); That is instantiating a Train object, and will call the constructor of the class.

Essentially, that is what you are trying to do here:

public void Train(String dest, int num, int cap)

This here, however, does not count as a constructor. The constructor has no return type, and as such it can't even be void. So it would be as such:

 public Train(String dest, int num, int cap)

With doing that, you can remove the train.Train(); and just have the Train train = new Train();. But there is one more issue regarding that. When you call the Train object, the constructor will be expecting 3 parameters, your String and two Ints. So when you call the object, you must fill it with the parameters that you want, such as Train train = new Train("Canada",2,3);

EDIT:

I see you now need the parameters to come from user input. I suggest looking up the Scanner class to find out how to get user input.

Upvotes: 3

Related Questions