0

I want to create a contructor(Motor) in Motor class, and I want to call it in Contructor1 class, but i have an error when I do that.....and I don't know why. I am learning java just from this week.

This is the code:

  class Motor{
        Motor(int type[]){
            int input;
            input=0;
            type = new int[4];
            for(int contor=0; contor<4; contor++){
                System.out.println("Ininsert the number of cylinders:");
                Scanner stdin = new Scanner(System.in);
                    input = stdin.nextInt();
                type[contor] = input;
                System.out.println("Motor with "+type[contor]+" cylinders.");
            }
        }
    }

    public class Contructor1 {
        public static void main(String[] args){
            Motor motor_type;
            for(int con=0; con<4; con++){
                motor_type = new Motor();
            }

            }

        }
1
  • 1
    Please post the error, otherwise we can't really help you.. Commented Oct 12, 2012 at 13:08

4 Answers 4

7

It's not clear why you've put a parameter in the constructor in the first place - you're not using it:

Motor(int type[]){
    int input;
    input=0;
    type = new int[4];

In that last line, you're basically overwriting whatever value was passed in. Why are you doing that?

If you do want to keep it, you need to create an array to pass in from the caller, e.g.

int[] types = new int[4];
// Populate the array here...
motor_type = new Motor(types);

The code at the moment looks a little confused though - are you actually intending for a single instance of Motor to have multiple values, or are you really interested in having multiple instances of Motor?

As a side note, this syntax:

int type[]

is discouraged. You should keep the type information in one place:

int[] type

Additionally, it's odd that you have no fields in Motor, and you never use the value that you're creating in the calling code either.

Sign up to request clarification or add additional context in comments.

Comments

2

The constructor has an argument of type int[], but you don't pass anything as argument. You need to create an array, and pass it as argument:

int[] type = new int[4];
Motor m = new Motor(type);

Note that this constructor argument isn't useful at all, since you don't do anything with it in the constructor. Instead, you overwrite it with a new array. I would simply remove the array argument.

Comments

1

You must pass in[] as argument to Motor constructor.

like

 new Motor (new int[] {0,1,2,3});

As you have defined parametrized constructor in Motor class it won't create no-arg constructor by default that why you are getting error on bellow line

 motor_type = new Motor();  // Error here , because no-arg constructor not defined. 

Comments

0

You can try following:

int intType[]={1,2,3}; //create some int array
Motor motor_type=new Motor(intType); //pass int array here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.