-1

I've tried for several hours to find out why my code outputs zero "ages" for every "car". Before I have already written two similar code bits and they worked well...

class Car {
    public int age;
    public int getAge() {
        return age;
    }
    public void setAge (int a) {
        int age = a;
    }
    public void carAge() {
        if (age == 0) {
            System.out.println("Wow! That car's pretty new!");
        } else if (age < 0) {
            System.out.println("Invalid input");
        }
        else if (age <= 3) {
            System.out.println("Superb! That car's quite new!");
        } else if (age <= 7) {
            System.out.println("The car's not old");
        } else if (age <= 10) {
            System.out.println("You've got to be sure it'll work correctly");
        } else if (age <= 15) {
            System.out.println("That car's a little bit old");
        } else if (age <= 30) {
            System.out.println("Does it really work?!");
        } else if (age <= 100) {
            System.out.println("It must be antique otherwise you shouldn't buy it");
        } else {
            System.out.println("Invalid input");
        }
    }
}

public class CarAgeTest {
    public static void main(String[] args) {
        Car lamborgini = new Car();
        lamborgini.setAge (1);
        Car ferrari = new Car();
        ferrari.setAge (2);
        Car uazpatriot = new Car();
        uazpatriot.setAge (15);
        Car fordm1 = new Car();
        fordm1.setAge (95);
        System.out.println("Lamborgini is " + lamborgini.getAge() + " years old");
        lamborgini.carAge();
        System.out.println("Ferrari is " + ferrari.getAge() + " years old");
        ferrari.carAge();
        System.out.println("UAZ Patriot is " + uazpatriot.getAge() + " years old");
        uazpatriot.carAge();
        System.out.println("Ford model 1 is " + fordm1.getAge() + " years old");
        fordm1.carAge();
    }
}

Please help me with it... Probably it should more details in the post but I have no idea what more detail to put in

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

0

This is Java and not JavaScript. The problem is, in the setAge method you assign the value to a new(!) age variable and not to the class property „age“. That‘s it