Results 1 to 17 of 17
  1. #1
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Completing the method body

    I just need help writing the method body. The assignment said I need to create a loop to get credit.
    /**
    * A class to give students experience using loops. This class
    * creates and manipulates objects of Greg's Date class.
    */
    public class SpeedDating
    {
    // Note: this class has no instance variables!

    /**
    * Creates an empty SpeedDating object so that you can call the methods
    * (a constructor that takes no parameters is known as a "default"
    * constructor)
    */
    public SpeedDating()
    {
    } // Constructor has empty body


    /**
    * Computes and returns the Date on which Thanksgiving will fall
    * in a given year.
    *
    * NOTE: By law, Thanksgiving is the 4th Thursday in November
    *
    * @param year the year for which to compute the date of Thanksgiving
    * @return the Date of Thanksgiving for the specified year
    */
    public Date getThanksgiving(int year)
    {
    // TO DO: write body of this method here
    }

    }

    Have the user enter another year, call the getThanksgiving method, and print the Date object returned. Print the Date in the main method, not in SpeedDating.

    Have the user enter another year, call getThanksgiving again, and print the Date object returned again. I've been trying to figure this out for over four hours now. I'm so stressed. Thank you.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Completing the method body

    You have posted your assignment and incomplete code. Since you did not ask a specific question your post is simply "Do my homework for me". If that is not your intention then you need to be more specific about what your problems are or what you don't understand. For example are you having trouble getting user input? Are you having trouble calling a method? Are you having trouble creating a Date object? etc.

  3. #3
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Completing the method body

    I just need help writing the method. I don't even know how to start :( I need some guidance. I'm pretty sure I can figure out how to write the test class once I have the method code down. Creating the date object should be something like this Date d1 = new Date(11,25,2012). User input I will just write the code using the parseinput thingy. I'm completely lost about creating the loop...

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Completing the method body

    I still don't see a specific question. "How do I write the method?" is not specific.

  5. #5
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Completing the method body

    public Date getThanksgiving(int year)
    {
    // TO DO: write body of this method here
    }

    }

    is it something like for (int i=0; i<2; i++)
    {
    int yearInput =
    System.out.println(getThanksgiving(yearI…
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Completing the method body

    Is what "something like..."?

    One of the other regulars often gives the following advice: put as much effort into your posts as you expect us to put into our replies.

    So far you have been very vague and non specific. I still have no idea of what you are trying to do or what your problem is. As far as I can tell the getThanksgiving method does not need a loop. It takes a year as a parameter, creates a Date object that represents Thanksgiving day for that year (ie calculate which Thursday it is for that year) and returns the Date object.

    Looks like your main method (or driver method) needs to have a loop that keeps going asking users to input a year, calls the getThanksgiving method and displays the Date returned.

  7. #7
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Completing the method body

    Oh "Looks like your main method (or driver method) needs to have a loop that keeps going asking users to input a year, calls the getThanksgiving method and displays the Date returned." Doesn't the loop have to be written in the other class? The one that is not the main method. What you said makes sense though. But when will the loop terminate? When the user clicks cancel? I thought the loop must be in here :
    public Date getThanksgiving(int year)
    {
    // HERE
    }
    And then in the test class I use parse to display the dialog box. I am sorry if I'm being vague but is that I am legitimately lost. I've spent hours trying to figure out how to write the loop in the method body.
    These are the instructions...
    You will modify the SpeedDating class, but only by completing the method bodies of the three methods already declared. Do not add any other methods or instance variables to the SpeedDating class, do not modify the constructor, and do not modify the method declarations (“headings”) provided! (Not necessary!)

    To receive credit for a method, it must use one of the Java loops (your choice). Nested loops are not necessary. So the loops are not supposed to be in the main method?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Completing the method body

    The instructions say there are 3 methods in the SpeedDating class. Maybe the loop should go in one of the other 2. I really have no idea as I didn't write the instructions. Perhaps you should ask the person who did.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Completing the method body

    How to write body methods

    Duplicate post.

  10. #10
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Completing the method body

    I'm sorry there are actually 3 methods in the code but I deleted the other two because I thought I would get a faster response that way. I thought if someone helped me on completing one method I can figure out how to complete the other two methods. This is the complete code.

    /**
    * A class to give students experience using loops. This class
    * creates and manipulates objects of Greg's Date class.
    */
    public class SpeedDating
    {
    // Note: this class has no instance variables!

    /**
    * Creates an empty SpeedDating object so that you can call the methods
    * (a constructor that takes no parameters is known as a "default"
    * constructor)
    */
    public SpeedDating()
    {
    } // Constructor has empty body

    /**
    * Prints the day of the week (e.g. "Thursday") on which Halloween will
    * fall for 10 consecutive years.
    * @param startYear the first of the 10 consecutive years
    */
    public void printHalloweens(int startYear)
    {
    // TO DO: write body of this method here
    }

    /**
    * Computes and returns the Date on which Thanksgiving will fall
    * in a given year.
    *
    * NOTE: By law, Thanksgiving is the 4th Thursday in November
    *
    * @param year the year for which to compute the date of Thanksgiving
    * @return the Date of Thanksgiving for the specified year
    */
    public Date getThanksgiving(int year)
    {
    // TO DO: write body of this method here
    }

    /**
    * Computes and returns the number of days between two dates,
    * counting the end date but not the start date. E.g., the
    * number of days between 11/1/2012 and 11/5/2012 is 4, not 5.
    *
    * Precondition: The start date must occur on or before the end date.
    *
    * @param start the earlier of the two dates
    * @param end the later of the two dates
    *
    * @return the number of days elapsed between the start date and the
    * end date
    */
    public int countingTheDays(Date start, Date end)
    {
    // TO DO: write body of this method here
    }
    }

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Completing the method body

    You will definately need a loop in printHalloweens method.
    You could use a loop in the countingTheDays method but you could also write the code without a loop.
    As already explained I see no reason to use a loop in the getThanksgiving method.

  12. #12
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Completing the method body

    Yea. For the halloween one I can see why a loop would be needed. But like you said, I also find no reason in using a loop in the thanksgiving one.... Is just that the instructions throw me off when it stated that loops must be used in order the obtain credit for the method. I guess I will have to email the professor. Thank you.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Completing the method body

    But do the instructions say "must use loops in all three methods"? If you must then about the only thing I can think of is have a variable to track the day and then use a loop to increment it until you reach the 4th Thursday. But that is not very efficient.

  14. #14
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Completing the method body

    Yes yes! I remember my professor saying something like that in class! Thank you. So let's say I write the code and it has a minor error like a syntax or logic error you would help me correct it?

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Completing the method body

    Please use [code] tags [/code] when posting code.
    Unformatted code is hard to read, and you want to make life as easy for us as possible...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Completing the method body

    Hey, so I came up with something like this

    Java Code:
     public Date getThanksgiving(int year)
        {
           
            Date Thanksgiving = new Date(11, 1, year);
           
            int count = 1 ;
            int weekCount = 0;
            for (  ; count <= 30; count++) 
            {
                if (Thanksgiving.getDayOfWeek().equals("Thursday")) {
                    weekCount++;
                }
                if (weekCount == 4) {
                    break;
                }
                
             
            }
            return Thanksgiving ;
    And for the tester class i wrote this

    Java Code:
    SpeedDating thanksgiving = new SpeedDating() ; 
    
    
    input = JOptionPane.showInputDialog
    			           ("Enter the year for november ") ;
           
           int year = Integer.parseInt(input) ;
      
           
           System.out.println(thanksgiving.getThanksgiving(year)) ;
    Instead of printing out the date like 11,1, and whatever year the user inputs something like Date@1e91a4d get printed. What's going on? How do I go about fixing this error?

  17. #17
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Completing the method body

    ... Please look at your other thread. /facepalm

Similar Threads

  1. Replies: 5
    Last Post: 10-27-2011, 08:47 AM
  2. Replies: 2
    Last Post: 09-27-2011, 04:59 PM
  3. Replies: 3
    Last Post: 05-25-2009, 09:50 PM
  4. Need help in completing this code
    By nn12 in forum New To Java
    Replies: 9
    Last Post: 09-09-2008, 07:02 AM
  5. Completing A Program..
    By Louise1875 in forum New To Java
    Replies: 1
    Last Post: 05-11-2008, 06:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •