Reputation: 11
Can anybody tell me why the date prints 0/0/0
?
What part of the code is missing so the values input using Scanner are passed into the method DisplayDate
and printed on the screen?
import java.util.Scanner;
public class DateClass {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter day:");
int day = scanner.nextInt();
System.out.println("Enter month:");
int month = scanner.nextInt();
System.out.println("Enter year:");
int year = scanner.nextInt();
Date d = new Date(day,month,year);
d.DisplayDate();
}
}
class Date {
private int day;
private int month;
private int year;
public Date(int day, int month, int year) {
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public void DisplayDate() {
System.out.println(getDay() + "/" + getMonth() + "/" + getYear());
}
}
Upvotes: 0
Views: 190
Reputation: 1504121
This is the problem:
public Date(int day, int month, int year) {
}
Your constructor completely ignores its parameters, leaving your fields with their default values. For int
fields, that's 0.
Instead, it should save the values, e.g.
public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
or perhaps call your properties:
public Date(int day, int month, int year) {
setDay(day);
setMonth(month);
setYear(year);
}
I'd also strongly recommend that:
DisplayDate
to displayDate
, or separate the concern of formatting from writing to the console, by just overriding toString
instead)Upvotes: 6
Reputation: 68
The constructor of your Date class is empty, so the values are not set when you create a new Date object.
Upvotes: 0
Reputation: 1644
Because your Constructor of the Date class is empty!
Change it to the following and it will work:
public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
Upvotes: 0
Reputation: 468
Edit your contructor to:
public Date(int day, int month, int year) {
this.day=day;
this.month=month;
this.year=year;
}
The problem is that you never set the variables you are requesting later!
Upvotes: 1
Reputation: 61550
Your constructor does nothing:
public Date(int day, int month, int year) {
}
it accepts the appropriate values, but doesn't do anything with them. Notice how DisplayDate
calls your getter methods: getDay()
, getMonth()
, and getYear()
? The values returned by those getters are the instance variables defined at the top of your class: day
, month
, and year
respectively. Since these values were never initialized with the proper values, they default to 0.
A more sensible constructor would be:
public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
Upvotes: 2
Reputation: 22243
You are not saving your day, month and year. You should do it in the constructor:
public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
Upvotes: 6