• Post Reply Bookmark Topic Watch Topic
  • New Topic

OOP a Method being a return type in a method?  RSS feed

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
in my assignment I stumbled upon a method that im not sure how to handle (yet).
Below the code:


The two last methods stumped me. The return type to each is "DateTime", according to JUnit complaints.
I know that I can use the "this" keyword to reference to the object. But how do I get these two methods to return the correct result?
Thanks!
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are constructor methods. They return "this" implicitly.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Those are constructor methods. They return "this" implicitly.

Im not sure I understand.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What methods? Those look like constructors. The last part of code is a factory method; since it is static you are not allowed to use the keyword this in it. You cannot get it to compile as it is; you would have to return a DateTime object reference from that method. You would probably create that object in that method, set its hour and minutes and then return it.
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell's right. The second method is semantically invalid and would not compile.

The first method is a constructor. Constructors are invoked by the object instantiator when the "new" operator is executed. The object instantiator allocates resources for the blank object (memory, object management data, etc.) and the constructor allows you to fill in the blanks. The initialized object is then passed upstream to the next level of the expression.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What methods? Those look like constructors. The last part of code is a factory method; since it is static you are not allowed to use the keyword this in it. You cannot get it to compile as it is; you would have to return a DateTime object reference from that method. You would probably create that object in that method, set its hour and minutes and then return it.


How do I return a DateTime object reference from that method?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return myDateTime;
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:return myDateTime;

It doesnt work.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify:
The Constructors / Methods are all given to me by the assignment.
I have to fill them with working code.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have now managed to correctly program the last method:


Now I just dont know how to do the second to last constructor.
I understand that the parameters are hour and minute but also DateTime day.
How do I use the parameter DateTime day within the constructor?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the date (minus time) out of that date time object, and set the details in this to be the same as that date.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:So I have now managed to correctly program the last method:
[code=java]

Now I just dont know how to do the second to last constructor.
I understand that the parameters are hour and minute but also DateTime day.
How do I use the parameter DateTime day within the constructor?


If I understand, you want to to set year, month and day (the field, not the object) in the last constructor like the first two. (I don't know if there is more code or if what you posted is all you got.)

I'm assuming there is an private int day; field that needs to be set. You are passed a DateTime day object. What about accessing the field or setter from the day object?

This is where seeing the rest of the code would be helpful. I don't know if the day field is public or if there is a setter for it.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code I have so far:
(The assignment is not finished yet thats why the methods toward the bottom of code are empty, trying to do this step by step)
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You have default access for your fields. In general this is bad practice, but we'll skip that for now.



This isn't going to work because you don't have a constructor with just a long as a parameter. But also, you would want to use the DateTime day object. If you have a DateTime object, how would you access the day field?

(I don't know how much of the code is inherited, but there are too many things called "day".)
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to have lots of repeated code in that class. Look at lines 49 and 85 for example.
You have the very confusing feature of methods and constructors with the same identifier. Or maybe you haven't, because the class won't compile. Remember: methods start with smallLetters and constructors have exactly the same name as the class so should start with CapitalLetters. So there should be no thought of a capital letter immediately like in this.DateTime... (line 171).
You cannot call a constructor as if it were a method. Methods are methods and they need to be called on the instance (unless they are static). Constructors are constructors and they are only executed after a new XXX(...) call. In that constructor you must get the time from the argument and use that information to set day month and year in the new object.

Move the main method into another class. It should run demonstrations of the DateTime class from outside that class.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:If you have a DateTime object, how would you access the day field?



Using the "this" keyword?
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You appear to have lots of repeated code in that class. Look at lines 49 and 85 for example.
You have the very confusing feature of methods and constructors with the same identifier. Or maybe you haven't, because the class won't compile. Remember: methods start with smallLetters and constructors have exactly the same name as the class so should start with CapitalLetters. So there should be no thought of a capital letter immediately like in this.DateTime... (line 171).
You cannot call a constructor as if it were a method. Methods are methods and they need to be called on the instance (unless they are static). Constructors are constructors and they are only executed after a new XXX(...) call. In that constructor you must get the time from the argument and use that information to set day month and year in the new object.

Move the main method into another class. It should run demonstrations of the DateTime class from outside that class.


I understand your concerns and will try to change as much as I can. But please be aware that all method and constructor declarations were given to us in the assignment and I cannot change them.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:
Knute Snortum wrote:If you have a DateTime object, how would you access the day field?



Using the "this" keyword?


"this" refers to the current object. You want to access the day field in the day object (like I said, too many "days"). Use a dot operator.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . .
Move the main method into another class. It should run demonstrations of the DateTime class from outside that class.

Also: The Main method has to stay in this class as per the assignment. Dont ask me why...
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:

"this" refers to the current object. You want to access the day field in the day object (like I said, too many "days"). Use a dot operator.


So I would use the dot operator but in relation to what?
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:
Knute Snortum wrote:

"this" refers to the current object. You want to access the day field in the day object (like I said, too many "days"). Use a dot operator.


So I would use the dot operator but in relation to what?


"DateTime.day = day;"

?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop guessing and write down what information you want to create a date time object. Then work out how you are going to extract it from the argument passed.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote: . . . The Main method has to stay in this class as per the assignment. . . .
Move it out of the class into another class, then move it back 5 minutes before you hand the work in.

And it is main not Main.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Stop guessing and write down what information you want to create a date time object. Then work out how you are going to extract it from the argument passed.


My reply was to Knute Snortum, not you. Please be aware that condescension does not help in any way to learn anything but much rather helps to accomplish the opposite.

Im not trying to get you to give me the answer and then just hand it in without doing my own work, I want to understand.

But the way you respond to me does not help me understand at all.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:
Dennis Von Valkenburgh wrote:
Knute Snortum wrote:

"this" refers to the current object. You want to access the day field in the day object (like I said, too many "days"). Use a dot operator.


So I would use the dot operator but in relation to what?


"DateTime.day = day;"

?


You're thinking about setting the whole DateTime object with the passing parameter day. Instead, think of setting the current object's day field with the same field in the passed-in DateTime object. The syntax is simple; you're just thinking the wrong way around.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
You're thinking about setting the whole DateTime object with the passing parameter day. Instead, think of setting the current object's day field with the same field in the passed-in DateTime object. The syntax is simple; you're just thinking the wrong way around.


Sorry if I dont understand right away.
I keep thinking that I have to use DateTime somehow within the constructor (Because it is passed in the argument) but that seems to be wrong.

The basic idea of this constructor is to create a new object with the same day as the parameter day, but with the hour and minutes set new.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:The basic idea of this constructor is to create a new object with the same day as the parameter day, but with the hour and minutes set new.


The constructor will create (return actually) the object so all you have to do is set this.day. So how would you access the day field in the passed-in day object?
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Dennis Von Valkenburgh wrote:The basic idea of this constructor is to create a new object with the same day as the parameter day, but with the hour and minutes set new.


The constructor will create (return actually) the object so all you have to do is set this.day. So how would you access the day field in the passed-in day object?


Ok I did try this.day (this accesses the current object field day right?) but was obviously missing the second half of the puzzle by not binding it to the passed in day field.
I thought to do this by somehow accessing DateTime (i was guessing), hence the this.day = DateTime(day); attempt which did not work.

So im thinking since day is the object passed should it not be:
this.day = day.day;
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:So im thinking since day is the object passed should it not be:
this.day = day.day;


But that's exactly what is should be. Again, too may "days". What is the type of the day field (not the object)?
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Dennis Von Valkenburgh wrote:So im thinking since day is the object passed should it not be:
this.day = day.day;


But that's exactly what is should be. Again, too may "days". What is the type of the day field (not the object)?


it is int.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, so you need to pass an int, not a DateTime object. day or new DateTime() are objects but day.day is the day field.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Right, so you need to pass an int, not a DateTime object. day or new DateTime() are objects but day.day is the day field.

Ok worked it out this is the final code that now works:



Thank you so much for patiently pointing in the right direction!
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats the part I didnt understand at first: that the parameter "DateTime day" was an object, not the field itself.
This was because I mistook it for the same name field "day".
You were right with the naming, too many days...
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done

I suggest you change the name of the parameter to something like otherDate in that constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!