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