• Post Reply Bookmark Topic Watch Topic
  • New Topic

Homework Questions - What do you think?  RSS feed

 
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am self studying Java as a way to boost my skills and expand on my programming hobby. I plan on going through the local cattle drive, as soon as I am sure I'm not going to make too much of a fool out of myself.

I have been reading a lot of these "homework" questions, sometimes even doing them for fun, just to see what i came up with compared to what the OP came up with.

I wont quote specific posts, but a lot of these questions seem to set restrictions based on fields you can have and methods you will require, how many getters/setters, it seems like some of them are trying to make the code unnecessarily hard or telling you to do it a certain way even when it could be done neater and safer another.

I will make a small example, it might not prove my point exactly...> why would a class called square, only be allowed two fields, length and width, surely a square knows it has an area?

I don't know if its a lack of details on the OP's part, whether the homework exercises are stupid or I'm not getting what they are trying to teach?

I just realized how many sentences I write start with I.

What do you think?

Daniel
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how your example would contribute to safety or neatness. Frankly, a square only requires one field. Then, since the area of a square is calculated from width x height (which are equivalent) then this gives the opportunity to create a getter or accessor method that returns a calculated result based on the single existing value. While you (and others) might see the attribute area as a useful field, others might see it as unnecessary.
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wont quote specific posts, but a lot of these questions seem to set restrictions based on fields you can have and methods you will require

It's hard to comment without knowing which questions you are referring to but generally homework type questions are aimed at testing a specific subject area and so aren't necessarily real world examples requiring production standard code. If the question sets limits then they are there for the purposes of that question only and not as a guide to what you must limit yourself to in a real scenario.

why would a class called square, only be allowed two fields, length and width, surely a square knows it has an area?

Yes a rectangle does have an area and it also has a perimeter but both of these can be calculated from the length and width so there is no need to store their values.

BTW A Square shouldn't have a length and width variable as by definition they have to be the same value.
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how your example would contribute to safety or neatness.


Greg, sorry it wasn't meant to, more the field restriction part i was aiming for, and obviously not a great example.

BTW A Square shouldn't have a length and width variable as by definition they have to be the same value.

generally homework type questions are aimed at testing a specific subject area


Tony, your right on both counts, rectangle is what i should have said, I just look at some of the questions and ask my self why would I? I think maybe what I'm missing is the precursor to the assignment (the specific class lesson being implemented in the code).

Maybe some of the questions would look better and make students think/learn more, if they came with a back story, stating why it needs y fields and why it needs x method and why class a can't know about the fields in class b?

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe some of the questions would look better and make students think/learn more, if they came with a back story, stating why it needs y fields and why it needs x method and why class a can't know about the fields in class b?

They probably do come with a back story if you do the course the homework questions are part of.

BTW Class A should never know about the fields in class B - fields should be private access and their values only accessible via getter methods.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I plan on going through the local cattle drive, as soon as I am sure I'm not going to make too much of a fool out of myself.

The cattle drive is designed for beginners so I wouldn't worry about making a fool of yourself. Why go through the pain and confusion of trying to teach yourself and then sign up for a course that teaches you it all again. I suggest you bite the bullet and sign up ASAP.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Hirning wrote:I will make a small example, it might not prove my point exactly...> why would a class called square, only be allowed two fields, length and width, surely a square knows it has an area?

I've always felt - and I am sure I am not alone - that you should never store a piece of data that can be computed from other values you have stored. Area is a perfect example. Let's say you input a length of 5, a width of 5, and an area of 25.

Now, somewhere down the line, you need a bigger square. So, you change the length to 6. You may remember that a square has the same length and width, so you update width to 6 as well. But you forget to update the area. That makes for a very strange square indeed.

And unless you are VERY good and make your square thread-safe, even if your setter methods re-set the area, you can have a brief moment when things are out of sync, making bugs VERY hard to find.

Since the area can be computed from other values, it would be better to make a method that computes the area as needed. That way you know the area you get will always be up to date.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I've always felt - and I am sure I am not alone - that you should never store a piece of data that can be computed from other values you have stored.

You're certainly not.

@Daniel: FYI, that sort of data is called "derived data" and, as Fred says, should generally be avoided at all costs.

Winston
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
The cattle drive is designed for beginners so I wouldn't worry about making a fool of yourself. Why go through the pain and confusion of trying to teach yourself and then sign up for a course that teaches you it all again. I suggest you bite the bullet and sign up ASAP.


Well hope that was ASAP enough, I just parted with a few dollars..your kickback should be coming soon

Winston Gutkowski wrote:
@Daniel: FYI, that sort of data is called "derived data" and, as Fred says, should generally be avoided at all costs.


So I just created a small Rectangle Build program, it works once, is small and only takes integers for the moment, but this would cut out "derived data" and be properly encapsulated?



 
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
fred rosenberger wrote:I've always felt - and I am sure I am not alone - that you should never store a piece of data that can be computed from other values you have stored.

You're certainly not.

@Daniel: FYI, that sort of data is called "derived data" and, as Fred says, should generally be avoided at all costs.

Winston


Well, in many cases you DO store derived data, usually for efficiency reasons. Think of storing a pointer to
the last node of a LinkedList.

@Daniel: as an exercise, try to store the area in a field in such a way that it is always "up to date", even when
the height or length is adjusted.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Hirning wrote:


I don't like this method. Here's why. What if I have to take the area, and pass it to some other method - like a compute cost method for a carpet seller? You should have a method that computes the area and returns it to the caller. THEN, if you want to print something, you can have a "printArea" method that would call the calculateArea, take the return value, and then print out the area and the text around it.
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
I don't like this method. Here's why. What if I have to take the area, and pass it to some other method - like a compute cost method for a carpet seller? You should have a method that computes the area and returns it to the caller. THEN, if you want to print something, you can have a "printArea" method that would call the calculateArea, take the return value, and then print out the area and the text around it.


Thanks Fred, Like so?



Is there a way around not calculating this in both methods? I thought of putting area in printArea as an argument, but that would require me to pass it a value for area in the method call/constructor?
(I understand in this example there is not much different between the calculation line {int area = length*width;..} and {int ar = area;..} but that wont always be the case on perhaps a larger calculation)

Is there a good way to do this?

cheers,
Daniel

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Hirning wrote:

Thanks Fred, Like so?



Is there a way around not calculating this in both methods? Is there a good way to do this?

note that any place you want the area as a number, you can simply call your calculateArea() method. So you just do this:


 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred, I didn't know I could do that from a print statement with a method.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Well, in many cases you DO store derived data, usually for efficiency reasons. Think of storing a pointer to
the last node of a LinkedList.

Not quite the same thing. That's a redundancy (specifically, a structural redundancy), whereas what Fred is talking about is avoiding redundancy.

Also: a 'final' node is a reference to a single item; an area() method ensures that you don't have to update a redundant value made up of more than one component, either of which might change.

Derived data is often used in applications like data mining; however, I suspect that's a discussion for another thread...

Winston
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forget that you are using a method. If you look at the return type of the method, you will find it is int. So anywhere you want an int, you can put that method call. Or any method call which returns an int.
If you are printing out Strings using the + operator, you can use a String on one side and just about anything except void on the other side.
 
Piet Souris
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Not quite the same thing. That's a redundancy (specifically, a structural redundancy), whereas what Fred is talking about is avoiding redundancy.


Well, it can be derived from base data, so what's the difference?

Winston Gutkowski wrote:Also: a 'final' node is a reference to a single item; an area() method ensures that you don't have to update a redundant value made up of more than one component, either of which might change.

?

Anyway, I was referring to Fred's use of the word 'never'.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Well, it can be derived from base data, so what's the difference?

I believe I explained that.

In data modelling, "derived data" refers to any value that can be created from two or more pieces of existing data, and is analogous to a View, but more restrictive.

The relationship is usually direct (area = height * width), but not always: as in the case of things like monthly or cumulative totals in accounting systems; but the main thing from a data modelling point of view is to avoid them.

If you want to expand the discussion, I suggest you start another thread.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!