• Post Reply Bookmark Topic Watch Topic
  • New Topic

Referencing Variable From Another Class  RSS feed

 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a big BlueJ project with 4 classes. One is "Date" and one is "Instructor". I'm stuck on the Instructor class. The last method is "public int howLongHaveTheyTaught()" and its supposed to return the number of years the instructor has taught. We are supposed to use a constant from the Date class: CURRENT_YEAR = 2015. As you can see on line 77, I have a return statement BlueJ doesn't like: "bad operand types for binary operator '_' first type int; second type: Date". Can someone help me with that method? I have the Instructor class below and part of Date class:




Thanks all!
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you suppose to store only "year" in started member variable in Instructor class, then declare it as "int" instead of "Date".
If you suppose to store date, month, year in started, then extract year from the date and then do the calculation.
 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tapas! Well my instructions start off with:

Instructor

Instance Variables
- private Name name //The name of the instructor
- private Date started //The date the instructor started teaching
Constructor
- public Instructor()
o sets the name and date objects by calling their default constructors

- public Instructor(Name name, Date started)
o stores the supplied paramters
o if either parameter is null, call its call’s default constructor instead

- public Instructor(String first, String last, String middle, int year, int month, int day)
o creates Name and Date objects from the parameters by calling the appropriate Name and Date constructors


So seems like I have to use "Date". Any other way and BlueJ is saying I'm combining and int and Date in my last calculation. Can't seem to find a way to use the Date class' CURRENT_YEAR variable with Instructor's "started" string.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you can go for the second solution.
Extract "year" from Date variable and then do the calculation.

There are few ways of extracting "year" from Date variable.
You can use either Calendar or SimpleDateFormat
If you are using Java 8, then I found LocalDate very useful.

Go through the above links to learn more.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please show us your complete instructions.why have you make the instructor class mutable do you really think it should have a setter methods or is it a part of instructions?in your class each instructor instance have a property of "name" and "when they had start",don't you think these things need not to be changed in future once set.
you can better use enum for your Date class.may be you have instruction to use class,in that case use a class and come to solve the same problem with enum when you learn about them.

Regarding your problem-can you provide a public getters in your date class.it will solve your problem though i will like to see your instruction first and then proceed in detail towards your problem.

Kind regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, started is a Date and you cannot do arithmetic on it with the − operator.
Why are you doing arithmetic on dates outside the Date class? Your Date class would do well to have a yearsSince(Date d) method which compares a date object with a similar object and calculates the difference in years.
 
Harry Peters
Ranch Hand
Posts: 39
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas: we never discussed Calendar or any of those other formats in lecture. We are supposed to figure it out with what we are given. Praveen: we never went over "enum". I do have getters and setters in my Date class. All of my classes are supposed to have them. Campbell: I followed your suggestion and created a method in my Date class:

Then this is how I did the last method in my Instructor class:


Now I can't compile my Date class yet because I'm working on fixing another method in it. When I try to compile the Instructor class, I get this error: "method yearsSince in class Date cannot be applied to given types; required: int, int; found: no arguments; reason: actual and formal argument lists differ in length". Do I have to compile the Date class first? Should I redo the howLongHaveTheyTaught() method?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neal wrote:Praveen: we never went over "enum". I do have getters and setters in my Date class.

It's okay!
campbell suggest you to provide a instance method with the signature--->public int yearsSince(Date d),it could be like:
This methods lets you compare "this" instance of Date with the Date d passed as an argument to the method.

###NOTE:this is a reference to a current object or more simply "this" is a reference to a object calling the method.

now what have you done is:
in this approach,you have to pass 2 int variables or value in the argument and even this is a incorrect implementation.you may be thinking that CURRENT_YEAR will be replaced by the constant value you have declared in a class,of course not,method have their own scope so one can declare the variable(even with the same name like in class variable) in it's scope.so CURRENT_YEAR of method has nothing to do with the constant value of variable in class,same is true for year.
REPLACEMENT:remove the CURRENT_YEAR variable declaration from method parameters.just use a year variable and it should be like:

now lets see how do you call it:
look in java,call of a method should be in accordance with its signature.compiler will check for it in the declaration and complains if not found.in your case you do not have call it properly it should be like:


Now do a correction in accordance with what you have learnt now.

Though the way your code looks to me is really very enticing..good indent style,appropriate names for methods and variable names.
Good Luck for the future

hope it helps!

Kind regards,
Praveen.
 
Harry Peters
Ranch Hand
Posts: 39
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praveen, that's very nice of you to help with this. This is what I have:


I know this isn't right but it compiles. I don't need just the year obviously. I tried all the combinations I know. When I try started.yearsSince(), its says: "method yearsSince in class Date cannot be applied to given types; required: int, int; found: no arguments; reason: actual and formal argument lists differ in length". When I try started.yearsSince(CURRENT_YEAR, year), its says can't find the variable CURRENT_YEAR . I'm just trying different things to find the solution but its not working.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neal wrote:I know this isn't right but it compiles. I don't need just the year obviously. I tried all the combinations I know. When I try started.yearsSince(), its says: "method yearsSince in class Date cannot be applied to given types; required: int, int; found: no arguments; reason: actual and formal argument lists differ in length". When I try started.yearsSince(CURRENT_YEAR, year), its says can't find the variable CURRENT_YEAR . I'm just trying different things to find the solution but its not working.

look:
-->started.yearsSince() is not a valid call of the "yearsSince" method,you have declared it to take 2 arguments which are both "int" that's why compiler complains you that formally(on call) this method(from Date class) is required to be given a 2 arguments of type int,int in accordance with the actual declaration you have made of such a method in Date class but you are not passing any argument in the method.
-->started.yearsSince(CURRENT_YEAR, year)-->CURRENT_YEAR is not a declared variable in your instructor class and neither it is declared in the scope of howLongHaveTheyTaught() method,where inside you are doing a call of yearsSince method.CURRENT_YEAR is static,final and public(i am assuming you are familiar with all these terms) i.e.,public provides--you can access it anywhere(in any class),static provide-it is a class variable so should be accessed with class name like "Date.CURRENT_YEAR".now just like CURRENT_YEAR,"year" is also not declared in the Instructor class.year is an instance variable i.e each instance of Date have their own year and it was declared private so you cannot access it outside Date class.provide getters like getYear(),getMonth(),getDay() in the Date class for ex:

then make a call like -->

i hope this time you have a good luck with "yearsSince"

Kind Regards,
Praveen.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Campbell suggested for Date.yearsSince(Date other) is object-oriented. What you guys have been discussing about Date.yearsSince(int from, int to) is NOT object oriented, it is very procedural. Don't do that. The Date object that you are calling the yearsSince() method already has the information for one of the dates, that's why the best name for the parameter would be "other".

Now the only problem you have are the semantics of the "since" part of the name fitting the use of the method in the given context. Grammatically speaking, "since" would imply that the parameter is a Date that was prior to the current Date object, the one whose yearsSince() method you're calling. However, since you'd be calling this method from a context where you have a past Date, the question then becomes what Date do we call the method on then, in order to compare the Instructor's start date to it? If you think about hard, you'll realize the "since" idea doesn't work in this context.

It seems to me that the Date to compare to the Instructor's startDate is implied to be the current date. That would be something that the Date class would already "know" intrinsically, therefore the method you would logically call should have no parameter. I think the name yearsSince might still work or maybe you could try something like howManyYearsAgo() with the caveat that the returned value is relative to the year defined by Date.CURRENT_YEAR.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's always easier to reason when you have some concrete examples. So let's look at some possible code:

I like how that last one reads, even though it's a little grammatically awkward, it sounds like something a typical teenager today might say.

One thing I've learned from many pair and group programming sessions is that programmers seldom come up with a sensible API right off the bat. Their first attempts usually don't express the idea clearly or unambiguously. You have to evaluate critically and experiment with different alternatives. Most importantly, you have to write your alternatives down and see them as actual code. Reading your code out loud helps you detect discrepancies and inconsistencies with natural language semantics. This process is not easy and you have to be diligent and patient in going through the process of discovery, experimentation, evaluation, and elimination before settling on something that is as clear, concise, and appropriate for the context in which it will be used. Once you find the right name, however, it usually results in beautiful code.

If I were to (interpretively) read the code out loud with this last option plugged in, it would be read like this: "This method tells us how many years this instructor has been teaching, so given we know his startDate, we just return how many years ago that start date was, relative to whatever the Date class considers to be the current date."

That works well enough for me. It's clear, concise, and expresses the idea well. It's actually shaping up to be pretty beautiful code.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu wrote:What Campbell suggested for Date.yearsSince(Date other) is object-oriented. What you guys have been discussing about Date.yearsSince(int from, int to) is NOT object oriented, it is very procedural. Don't do that. The Date object that you are calling the yearsSince() method already has the information for one of the dates, that's why the best name for the parameter would be "other".

please don't get me wrong,Yes i do understand that all things.even i am not encouraging him like he does in his previous post,i had also suggest him to go with the campbell suggestion.but eventually he came up with the problem and i am just guiding him what actually he done wrong in his previous implementation and it is good to learn from mistakes and even these are the things which let him know why Object Oriented principle,one suggested,is better here.

Kind Regards,
Praveen.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:please don't get me wrong,Yes i do understand that all things.even i am not encouraging him like he does in his previous post,i had also suggest him to go with the campbell suggestion.but eventually he came up with the problem and i am just guiding him what actually he done wrong in his previous implementation and it is good to learn from mistakes and even these are the things which let him know why Object Oriented principle,one suggested,is better here.

Don't take it personally. That's another one of those things I've seen over the years, programmers tending to take critique of their code too personally. You have to learn to separate your "self" and get rid of any notion of your competence being the subject of any critique of the code that you write. Always take any reasonable critique as directed at the CODE, not at *you* personally. Don't get too attached to code. Feel free to call your own code *ugly* and worthy of disdain. When you do that, you'll find it liberating and it sets your creativity loose, enabling it to find better ways to do things.  Attaching too much of your "self" to any code that you write shackles you to it and no matter how ugly it is, you'll fight to keep it or risk getting dragged away with it when it finally *does* have to get thrown out.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, as an example of me not being attached to any code that I write,
I wrote:The Date object that you are calling the yearsSince() method already has the information for one of the dates, that's why the best name for the parameter would be "other".

When I read that suggestion again, I think to myself, "Is 'other' really the *best* name? Does that mean there's nothing better? Really?" Then, I'd experiment:

With just two possible alternatives, I can already see something that I think is better than "other" because it expresses the expected relationship of the other date relative to this date. This name makes the signature more self-documenting. Therefore, I would gladly throw away my previously "best" choice and replace it with an even better choice.

Do you see how egoless programming works to your benefit?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really think i take it personally
actually i am not a native english speaker and several times it causes a problem for me to put my thoughts clearly .
though your last post just remind me about my maths teacher

Anyway,thanks for your suggestion and i will not take things personally next time.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's another nuance to this howManyYearsTaught() you might want to consider asking the instructor. How much of a stickler do you want to be with this? Do you also consider the months in the calculation? Do you want to make sure these are *whole* years you return as the result?

For example, if the date in question was October 11, 1970 and "today" is November 4, 2015, then it's fair to expect a method like date.howManyYearAgo() to return 45. However, if the date in question is November 8, 1970 and "today" is still only November 4, 2015, technically, date.howManyYearsAgo() would be 44 because it hasn't been a full 45 years yet between the two dates. Do you round up or do you not care and just take the difference between the years? By the way, if we ignored months, the result would be way off if "today" were January 1, 2015 and the date in question was December 31, 1970.  Just something to think about.
 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe my entire classes have issues. If nobody minds, I'm just going to post my entire classes if someone wants to check it all out.  I know its CRAZY long but its best if someone wants to take the time, but thanks again for all the time everyone has spent helping regardless. There is also a RunTest class the instructor has in the project that is supposed to test all classes. It prints a long list of random instructor names, courses, and dates. It has to match exactly. Just if anyone was curious. This is all due Monday:







 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!