-1

Sorry if this is a super basic question, for I am just learning about objects, but I don't understand what this error means. I had to make an object class and test it with a premade program, so I'll paste both.

Object:

 public class Vehicle{
  private int location=0;
  public Vehicle(int loc){
    if(loc<=20||loc>=-20){
      location=loc;
    }else{
      location=0;
    }
  }
  public void forward(){
    if(location<20){
      location++;
    }
  }
  public void backward(){
    if(location>-20){
      location--;
    }
  }
  public int getLocation(){
    return location;
  }
  public String toString(){
    String sloc=new String();
    for(int i=0;i<location+20;i++){
      sloc+=' ';
    }
    sloc+='@';
    return sloc;
  }
}

Checker:

import java.io.*;
import static java.lang.System.*;
import java.util.Scanner;


public class student_runner_Vehicle
{
       public static void main (String str[]) throws IOException {
            Vehicle v1 = new Vehicle (17);

            System.out.println(v1);

            for (int i = 1; i < 5; i ++)
            {
                 v1.forward();
                 System.out.println(v1);
            }
            System.out.println(v1.getLocation());

            for (int i = 1 ; i < 10; i ++)
            {
                 int d = (int)(Math.random() *2);
                 if (d ==0)
                      v1.forward();
                else
                     v1.backward();
                System.out.println(v1);


            }

            Vehicle v2 = new Vehicle (87);
            System.out.println(v2.getLocation());

            Vehicle v3 = new Vehicle(-18);
            System.out.println(v3);
            v3.backward();
            System.out.println(v3.getLocation());
            v3.backward();
            System.out.println(v3.getLocation());
            v3.backward();
            System.out.println(v3.getLocation());
            v3.backward();
            System.out.println(v3.getLocation());

       }

}

Here is the error:

Main.java:234: error: constructor Vehicle in class Vehicle cannot be applied to given types;
        Vehicle v1 = new Vehicle ();
                     ^
  required: int
  found: no arguments
  reason: actual and formal argument lists differ in length

I typically am able to look at these errors and fix it by myself, but I really don't know what to do here. Thanks

Mr Chikn
  • 33
  • 4
  • 1
    You need to provide an int when you create a Vehicle, as specified by your constructor method in the Vehicle class. `Vehicle v1 = new Vehicle(1);` – sleepToken Dec 18 '19 at 17:04
  • required: int found: no arguments – Thomas Cook Dec 18 '19 at 17:06
  • The problem doesn't seams to be in what you've provided since nowhere can we find a constructor with no parameters passed ( `new Vehicule()` ) – Nicolas Dec 18 '19 at 17:07
  • 1
    If you dont define a constructor a default constructor is defined for you. If you define one with arguments you dont get the default constructor automatically so you need to Provide your Class with a default constructor without parameters. – main.c Dec 18 '19 at 17:13
  • I think the intent of this line `if(loc<=20||loc>=-20){` was meant to check if -20 <= loc <= 20 (loc is between -20 and 20 inclusive). If that is the case, it should be a && (and) not a || (or). – bcr666 Dec 18 '19 at 17:16

1 Answers1

3

It's telling you that it is missing an argument to the constructor when you create the object. Specifically, in this case, it is expecting an integer. However, the code you provided seems correct:

Vehicle v1 = new Vehicle (17);

Is it possible you made the change and then forgot to save before compiling?

Travis Vroman
  • 364
  • 2
  • 9