• Post Reply Bookmark Topic Watch Topic
  • New Topic

About setting methods the right way  RSS feed

 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello forums members. First i wondered if the code I've written is OK. Second, i wanted to know that if I'm gonna make a a setDate() method do i need to copy the whole block of code the constructor for each set-method i make for the date to be valid? (somthing tells me it's not the best way) ? thanks .


*I've not not finished the documentation yet, just the first constructor and the copy constructor.
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it homework to do so?

Otherwise, have you found out that Java API already contains Date class as well as LocalDate class (since Java 8). You don't need to reinvent wheel, unless you have been told so.
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

And something right from the beginning doesn't look ok with this code (for quite few reasons). But first, lets wait your answer to the question above.
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is homework we are need to do.2 constructors , 3 getMethods and 5 setMethods, all regarding dates.
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:And welcome to the Ranch

And something right from the beginning doesn't look ok with this code (for quite few reasons). But first, lets wait your answer to the question above.


Thanks, not my first time though ;D
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dan kom wrote:Yes it is homework we are need to do.2 constructors , 3 getMethods and 5 setMethods, all regarding dates.
Alright. That is a very minimalistic information we could give some help on. In different words - we don't know what the instructions are. Either we need those, or we need to know what exact difficulties you're having with your code (regardless what kind of instructions are).

1. Without seeing exact question or instructions - I think you need to get rid of (fixed typo) those _______. Think about the other way to distinct variable names.
2. What is the point of the second constructor? Looks like a useless job. Like a date class within another date class (can't find the words to describe what it looks like). "I'll give you one apple so you'd have one to give me".
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
dan kom wrote:Yes it is homework we are need to do.2 constructors , 3 getMethods and 5 setMethods, all regarding dates.
Alright. That is a very minimalistic information we could give some help on. In different words - we don't know what the instructions are. Either we need those, or we need to know what exact difficulties you're having with your code (regardless what kind of instructions are).

1. Without seeing exact question or instructions - I think you need to get rid off those _______. Think about the other way to distinct variable names.
2. What is the point of the second constructor? Looks like a useless job. Like a date class within another date class (can't find the words to describe what it looks like). "I'll give you one apple so you'd have one to give me".





we are told to use _x for the instance variables(just because they said ;P). not quite sure about the use of the second constructor, we are just at the beginning, so i think they just gave us stuff to do, even thought it doesn't look too practical.

As for the problem, we need to make setDate(day, month, year) method, now my question is that if i need to copy the entire block of code to make the date valid, or is there another way?
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dan kom wrote:we are told to use _x for the instance variables
That is worse than I thought then.

dan kom wrote:not quite sure about the use of the second constructor, we are just at the beginning, so i think they just gave us stuff to do, even thought it doesn't look too practical.
That is the point - to do what? We don't know how to help until you tell us what you have been asked to do. And when you're not sure, you don't write stuff until you are (different from being mistaken).

So what is the task you have been given? Please give us wording of instructinos exactly as you were given.

dan kom wrote:now my question is that if i need to copy the entire block of code
If you catch yourself writing same code again, you stop and make it a method. Without even noticing you write same code again, all your code needs to be in a methods (small), so they would do only one job each.
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are told to make a Date class and then as second question, a Trip class. Both of the will be used by a Tour Company. (i guess i will have to use the copy constructor on the second question).

The Date class would have int_day, int_month, int_year as instance variables.
it would have two constructors:
public Date(int day, int month, int year)

public Date(date other)


and it would have 6 methods: getDay(), getMonth(), getYear(), setDay(), setMonth(), setYear().

All dates must be valid according to Georgian calendar, that means 29/02/2013 doesn't exist for example(not a leap year).
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
If you catch yourself writing same code again, you stop and make it a method.


So should i make boolean validDate() method? to check if date i valid then use it ? looks best to me, but i really hope i'm allowed to do that.
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dan kom wrote:
Liutauras Vilda wrote:
If you catch yourself writing same code again, you stop and make it a method.


So should i make boolean validDate() method? to check if date i valid then use it ? looks best to me, but i really hope i'm allowed to do that.
Yes. If you need to validate something - you create a validation method. If your Date needs to be a valid Georgian date, so you create a method to check isValidGeorgianDate(). If you need to check if it is a leap year, you create a method isLeapYear(). If you're allowed or not try to ask your teacher. If you have been told to use '_x' for instance  variable, then we don't know what to expect further.
 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
dan kom wrote:
Liutauras Vilda wrote:
If you catch yourself writing same code again, you stop and make it a method.


So should i make boolean validDate() method? to check if date i valid then use it ? looks best to me, but i really hope i'm allowed to do that.
Yes. If you need to validate something - you create a validation method. If your Date needs to be a valid Georgian date, so you create a method to check isValidGeorgianDate(). If you need to check if it is a leap year, you create a method isLeapYear(). If you're allowed or not try to ask your teacher. If you have been told to use '_x' for instance  variable, then we don't know what to expect further.


thanks, i will ask the teacher. and btw, why _x is bad? is it a newbie thing? ;)
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a convention thing.  Variable names should be in camelCase.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!