WHAT IS WRONG HERE PLEASE?
I have this question to deal with.....
Here is the question:
Create a class called Date that includes three pieces of information as instance variables—a
month (type int), a day (type int) and a year (type int). Your class should have a constructor that
initializes the three instance variables and assumes that the values provided are correct. Provide a set and a get method for each instance variable. Provide a method displayDate that displays the month, day and year separated by forward slashes (/). Write a test application named DateTest that demonstrates class Date’s capabilities.
Here are my two classes as I wrote them our:
Code Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @Elegbede M.D
* 27-08-2010
*/
public class Date {
private int year;
private int month;
private int day;
public Date(int yr, int mnt, int dy) {
year = yr;
month = mnt;
day = dy;
}
public void setYear(int yr){
year = yr;
}
public void setMonth(int mnt){
month = mnt;
}
public void setDay(int dy){
day = dy;
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
public int displayDate(){
System.out.println("Today's date is" +month+ "/" +day+ "/" +year+".");
return displayDate();
}
}
The Second Class, i.e the test class:
Code Java:
/**
*
* @Elegbede M.D
* 27-08-2010
*/
public class DateTest {
public static void main(String args[]){
Date theDate = new Date(8,27,2010);
System.out.println("Today's date is: " + theDate.displayDate());
}
}
It keeps repeating "Today's date is27/2010/8" and then comes with an error.
I am sure there is an error but where it is, I don't know.
Please help so that I may move to the next chapter.
Re: WHAT IS WRONG HERE PLEASE?
Next time try surrounding your code in [highlight=java] Code here [/highlight] tags.
You told your program to print twice, and for some reason that was causing an infinite loop. Here is the code a I changed for you
Code java:
public class date {
private int year;
private int month;
private int day;
public date(int yr, int mnt, int dy) {
year = yr;
month = mnt;
day = dy;
}
public void setYear(int yr){
year = yr;
}
public void setMonth(int mnt){
month = mnt;
}
public void setDay(int dy){
day = dy;
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
public void displayDate(){
System.out.println("Today's date is: " +month+ "/" +day+ "/" +year+".");
}
}
Code java:
public class DateTest {
public static void main(String args[]){
Date theDate = new Date(8,27,2010);
theDate.displayDate();
}
}
Re: WHAT IS WRONG HERE PLEASE?
Quote:
for some reason that was causing an infinite loop
Code :
public int displayDate(){
System.out.println("Today's date is" +month+ "/" +day+ "/" +year+".");
return displayDate(); [COLOR="Red"]// recursive call[/COLOR]
}
Re: WHAT IS WRONG HERE PLEASE?
The 2 guys above have already answered this and quite well I must add; but just in case (let me know if this is not needed lol ;) )
In your actual progam (the one with the main method); you've instantiated a date object from your date class
with the statement:
Code Java:
[B]Date theDate = new Date(8,27,2010);[/B]
Ok, so far so good.
With your next line of code you've written:
Code Java:
[B]theDate.displayDate();[/B]
Now, let's disect this a bit. On the new 'Date' object that you've just instantiated with the previous statement (called theDate as you know) you've invoked the 'displayDate' method. Let's go back to the method that you've written in your Date class and see what it does:
Code Java:
[B]public int displayDate(){
System.out.println("Today's date is" +month+ "/" +day+ "/" +year+".");
return displayDate();
}[/B]
Right then, first off the method makes a call to a the static 'println' method whose job it is to get text on the screen. It does this perfectly by the looks of things, but the problem arises next. After 'printing out' the information, control passes to the next line of code ( return displayDate(); ) Logically speaking this will 'return' to the caller of the original method (which in this case is your program) the result of the method specified (which is displatDate(); ).
The issue though is that you already within the display date method. So what you are in fact saying to the compiler is the following:
Do the 'displayDate' method
{* Inside the method
Now print the following (" ")
return the value of the 'displayDate' method
*oops we're already in that method, go back to the top then.
}
Control will thus go back to the top with the last statement, flow down until it reaches the return and then go back to the top again. We've got an infinite loop on our hands!
-------------------------------------------------------------------------------------------------------------------------------------
The solution is to have only one of your two options. Either a println method that will give you the data in question, or have the method return you the date in full on it's own (& not return the value of a method itself)