Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: WHAT IS WRONG HERE PLEASE?

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    /*
     * 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:
    /**
     *
     * @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.
    Last edited by helloworld922; August 28th, 2010 at 09:53 AM.


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default 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

    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+".");
    	}
     
    }

    public class DateTest {
    	public static void main(String args[]){
    		Date theDate = new Date(8,27,2010);
    		theDate.displayDate();
    	}
    }
    Last edited by Brt93yoda; August 27th, 2010 at 03:25 PM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: WHAT IS WRONG HERE PLEASE?

    for some reason that was causing an infinite loop
    public int displayDate(){
      System.out.println("Today's date is" +month+ "/" +day+ "/" +year+".");
      return displayDate();  [COLOR="Red"]// recursive call[/COLOR]
    }

  4. The Following User Says Thank You to Norm For This Useful Post:

    Brt93yoda (August 27th, 2010)

  5. #4
    Junior Member
    Join Date
    Aug 2010
    Location
    UK
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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:


    [B]Date theDate = new Date(8,27,2010);[/B]

    Ok, so far so good.



    With your next line of code you've written:

    [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:

    [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)
    Last edited by Bacon n' Logic; August 27th, 2010 at 08:58 PM.

Similar Threads

  1. Not sure what is wrong with this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 29th, 2010, 02:23 PM
  2. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 07:47 AM
  3. What's wrong?!
    By deeerek in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 22nd, 2010, 07:11 PM
  4. don't know what's wrong
    By james in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 15th, 2010, 07:37 PM
  5. Where am I going wrong? :)
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 18th, 2009, 12:03 AM