-1

I don't understand why I get a compilation error, I hope someone can help out here. I would appreciate it a lot. I'm trying to make a system for a lot of cars.

public class Car {
    //Instance Variables
    String make;
    String model;
    int year;
    boolean isNew;
    double miles;
    String owner;
    public void sell (String newOwner) {
        owner=newOwner;
        if (isNew){
            isNew=false;
        }
    }
    public boolean isOld() {
        int thisYear=Calendar.getInstance().get(Calendar.YEAR);
        if (thisYear-year > 10) {
            return true;
        } else {
            return false;
        }
    } 
}
public static void main(String[] args) {
    Car myCar = new Car(); //myCar is a variable and new the keyword
    myCar.make = "Audi";
    myCar.model = "A4";
    myCar.year =2014;
    myCar.isNew=true;
    myCar.miles =0;
    myCar.owner ="Jeyson";
    boolean isMyCarOld = myCar.isOld();
    myCar.sell("John Doe");
    System.out.println("Car owned by" + myCar.owner);
}
gprathour
  • 14,813
  • 5
  • 66
  • 90
Jeyson Mg
  • 150
  • 1
  • 6
  • 1
    it seems that the main methos is outside your class and this is not correct :) – Youcef LAIDANI Jun 19 '17 at 10:09
  • 1
    just replace the last `}` before the main method and put is after the `}` of the main methos `}}` – Youcef LAIDANI Jun 19 '17 at 10:11
  • 1
    If you indented your code properly it would be clear where the errors were. – khelwood Jun 19 '17 at 10:13
  • As you are new user here, so let me tell you one thing, if an answer solves your problem then you should mark it as accepted. To accept an answer just click on the right tick mark sign on left side of answer. – gprathour Jun 19 '17 at 10:34

1 Answers1

1

You cannot write main method out side of your class. It should be written inside class.

The following code should work,

public class Car {
    //Instance Variables
    String make;
    String model;
    int year;
    boolean isNew;
    double miles;
    String owner;
    public void sell (String newOwner) {
        owner=newOwner;
        if (isNew){
            isNew=false;
        }
    }
    public boolean isOld() {
        int thisYear=Calendar.getInstance().get(Calendar.YEAR);
        if (thisYear-year > 10){
            return true;
        } else {
            return false;
        }
    }
    public static void main(String[] args){
        Car myCar = new Car(); //myCar is a variable and new the keyword
        myCar.make = "Audi";
        myCar.model = "A4";
        myCar.year =2014;
        myCar.isNew=true;
        myCar.miles =0;
        myCar.owner ="Jeyson";
        boolean isMyCarOld = myCar.isOld();
        myCar.sell("John Doe");
        System.out.println("Car owned by" + myCar.owner);
    }
}
gprathour
  • 14,813
  • 5
  • 66
  • 90