-2

I am very new to Java and trying to figure this problem out. Given the year, the month and day, I have to validate it first and do addition and subtraction to the date.

public class Date {



// fields

private int month;

private int day;

private int year;



// constructor

public Date(int month, int day, int year){

    this.month = month;

    this.day = day;

    this.year = year;

}


public void setMonth(int month){

    this.month = month;

}



public void setDay(int day){

    this.day = day;

}



public void setYear(int year){

    this.year = year;

}



public int getMonth(){

    return month;

}



public int getDay(){

    return day;

}



public int getYear(){

    return year;

}



public String displayDate(){

    return month + "/" + day + "/" + "/" + year;

}

}

user1471980
  • 10,127
  • 48
  • 136
  • 235

1 Answers1

1

The answer I can give based on the code is what your error message says. In your function

public int add( int n){...}

you are trying to use the variable m which does not exist. You can pass this variable as another parameter where you call the add function.

public int add( int n, int m){...}
...
myDateObject.add(3,6);

The other possibility is to save the validated month's value into the private int month field first. Then use it in the add function instead of the nonexistent m variable.

Edit:

There are a lot of problem with this code. For example in order to run this program you would need a main method where your program can start executing. The constructor of your class tries to give value to fields that does not exsist(this.d = d; instead of this.day = d;). If this truly is the whole code I'd suggest to try to read / write and understand a java Hello World example first. :)

Croo
  • 1,301
  • 2
  • 13
  • 32
  • I am very new to java. I included the whole program. – user1471980 May 25 '13 at 18:28
  • I modified the class Date. Can you help with how do I put this main? If I wanted to instantiate this class (Date), how would I do that? I am really stuggling with this. – user1471980 May 25 '13 at 20:37
  • Create another file named Program.java with this code: class Program { public static void main(string [] args) { Date date = new Date(5,25,2012); date.setYear(2013); System.out.println(date.displayDate()); } } – Croo May 25 '13 at 21:00