-4

How do I make a method for my Car class called isAntique that returns a Boolean indicating if the cars is more than 45 years old?

Here is the code that I already have:

package Ch4PP5;

public class Car {
    private String make, model;
    private int year;

    public Car(String userMake, String userModel, int userYear) {
        make = userMake;
        model = userModel;
        year = userYear;
    }

    public String setMake(String newMake) {
        make = newMake;
        return make;
    }

    public String getMake() {
        return make;
    }

    public String setModel(String newModel) {
        model = newModel;
        return model;
    }

    public String getModel() {
        return model; 
    }

    public int setYear(int newYear) {
        year = newYear;
        return year;
    }

    public int getYear() {
        return year;
    }

}
agrogan
  • 11
  • 2
  • Search Stack Overflow before posting. – Basil Bourque Oct 02 '16 at 00:13
  • You're only cheating yourself if you don't first attempt to solve this and then show us your attempt. Please also have a look at [How do I ask and answer Homework questions](http://meta.stackexchange.com/a/10812/162852). This information is valid whether or not the question is for homework or home work (self-study). Also please read and take to heart: [Open letter to students with homework problems](http://meta.programmers.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems). – Hovercraft Full Of Eels Oct 02 '16 at 00:24

2 Answers2

1

something like this?

public Boolean isAntique(){
   int year = Calendar.getInstance().get(Calendar.YEAR);

   if(year - this.year >= 45) {
       return true;
   } else {
       return false;
   }
}
TimeToCode
  • 901
  • 2
  • 16
  • 34
Murillio4
  • 485
  • 5
  • 17
  • 1
    In Java 8, use [`Year.now()`](https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#now--), e.g. `return Year.now().getValue() - this.year >= 45` – Andreas Oct 02 '16 at 00:17
  • This worked thank you, but is there away to do it without importing Calendar or using an if else statement? – agrogan Oct 02 '16 at 00:17
  • `return (year - this.year >= 45);`, without importing calendar, don't know. – Murillio4 Oct 02 '16 at 00:25
0
public boolean isAntique() {
    return year > 45;
}

I might miss something, but if my answer is what you looking for, you should learn java basics and watch some examples.

If murillio4 answer is what you looking for, you should search for how do I get today's date instead posting that question.

Erik
  • 221
  • 1
  • 7