Results 1 to 17 of 17
Thread: Completing the method body
- 11-01-2012, 10:54 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
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.
- 11-01-2012, 10:58 PM #2
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.
- 11-01-2012, 11:04 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
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...
- 11-01-2012, 11:10 PM #4
Re: Completing the method body
I still don't see a specific question. "How do I write the method?" is not specific.
- 11-01-2012, 11:34 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
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…
}
- 11-01-2012, 11:46 PM #6
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.
- 11-02-2012, 12:10 AM #7
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
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?
- 11-02-2012, 12:18 AM #8
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.
- 11-02-2012, 12:32 AM #9
Re: Completing the method body
How to write body methods
Duplicate post.
- 11-02-2012, 12:34 AM #10
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
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-02-2012, 12:42 AM #11
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.
- 11-02-2012, 12:49 AM #12
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
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.
- 11-02-2012, 12:59 AM #13
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.
- 11-02-2012, 02:34 AM #14
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
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?
- 11-02-2012, 10:20 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 11-04-2012, 05:26 AM #16
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
Re: Completing the method body
Hey, so I came up with something like this
And for the tester class i wrote thisJava 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 ;
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?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)) ;
- 11-04-2012, 05:58 AM #17
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Similar Threads
-
Regular expession that matchs a Java Method declaration and body
By rmp in forum Advanced JavaReplies: 5Last Post: 10-27-2011, 07:47 AM -
error: Missing method, body, or declare abstract public static void main
By MBD in forum New To JavaReplies: 2Last Post: 09-27-2011, 03:59 PM -
[SOLVED] Why does the compiler return "not a statement" for this method body please?
By trueblue in forum New To JavaReplies: 3Last Post: 05-25-2009, 08:50 PM -
Need help in completing this code
By nn12 in forum New To JavaReplies: 9Last Post: 09-09-2008, 06:02 AM -
Completing A Program..
By Louise1875 in forum New To JavaReplies: 1Last Post: 05-11-2008, 05:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks