PDA

View Full Version : More Java questions



doc_bean
05-02-2006, 09:34
So I've got another project due soon, and little time to do it, so I figured I might beg for your :help: here

Situation:

1 superclass DiskItem
3 subclasses File, Directory and Link

I have to write the Link class, the problem is that the name you can give to Directory and a File obeys different rules, and the Link has to follow the rules of the DiskItem it refers to, but since it is a subclass it has to use the superconstructor, which doesn't account for a reference when giving names.

Is there any way around this without making a new DiskItem constructor especially for the subclass ?

2nd question:

The Link refers to a DiskItem, but you shouldn't be able to access/alter the DiskItem from the Link (so no link.getReference().changeName or things like that). How do you do this ? I've been looking at the cloneable interface but the info given by Sun is vague to say the least. It would also seem like this would create a new item to be returned, which would be bad since then I'd essentially have the same DiskItem twice in a Directory. I could make a new class DiskItemClone, but that seems a little unpractical.

So can somebody give me some :help: ?

Blodrast
05-02-2006, 20:36
I'm not sure the questions were very clear to me (not necessarily your fault, no worries - I'm tired and other things), but I'll give it a shot.

1.
What exactly is the problem ?
Also, can you modify any of the interfaces of the "given" classes (the other 3, I presume) ?
Wouldn't evey subclass have its own get/set methods for handling the name ? So, if the object you're calling setName() for is a Link, the appropriate method from the Link class will be invoked. You don't have to do anything special for this.

Are you asking how to create a constructor for the Link class, that does take a reference as an argument ? Or what exactly ? Sorry, I don't get it.

2.
Polymorphism allows you to invoke the right type of method for the appropriate type of object - you don't have to do anything to get that functionality.
For instance, assume you wanna call setName() for some DiskItem, which is actually a Link. Then Link.setName() will be called, because the object actually belongs to the Link class.
You can also define virtual methods in the DiskItem class.

From what I've seen, a common approach is to define the get/setName methods in the DiskItem class as abstract methods - e.g., String getName (DiskItem x) = 0; (or return false, or some error code).
This will force people to define them in the subclasses, for the appropriate type of objects.

I really don't know if any of this was any help, if you can be more specific, it would help.
Good luck anyway !

edit: oh, I forgot to mention one thing: if you have a DiskItem object, there are methods that check if you can downcast it to a subclass type (IF it is, indeed, of that class type). I don't remember the class name and the methods, though. But this may be what you want - of course, you can always downcast it yourself if you know that the DiskItem is in fact a Link.

doc_bean
05-03-2006, 13:02
I'm not sure the questions were very clear to me (not necessarily your fault, no worries - I'm tired and other things), but I'll give it a shot.

1.
What exactly is the problem ?
Also, can you modify any of the interfaces of the "given" classes (the other 3, I presume) ?
Wouldn't evey subclass have its own get/set methods for handling the name ? So, if the object you're calling setName() for is a Link, the appropriate method from the Link class will be invoked. You don't have to do anything special for this.

Are you asking how to create a constructor for the Link class, that does take a reference as an argument ? Or what exactly ? Sorry, I don't get it.

The problem was that I wanted 1 class with 2 name conventions, 1 for links to directories and 1 for links to files. I've now just made 2 subclasses and made the Link class abstract.

SetName() was handled solely in DiskItem and name was a private variable. I used isValidName() with an override in the subclasses to check if a name could be given.

The real problem was the I had to invoke the superconstructor first in my subclassconstructer (as always in java i believe) which meant I couldn't give data relating to the reference (no references in the superconstructor) to the constructor.

This probably doesn't make too much sense either :embarassed:



2.
Polymorphism allows you to invoke the right type of method for the appropriate type of object - you don't have to do anything to get that functionality.
For instance, assume you wanna call setName() for some DiskItem, which is actually a Link. Then Link.setName() will be called, because the object actually belongs to the Link class.
You can also define virtual methods in the DiskItem class.

From what I've seen, a common approach is to define the get/setName methods in the DiskItem class as abstract methods - e.g., String getName (DiskItem x) = 0; (or return false, or some error code).
This will force people to define them in the subclasses, for the appropriate type of objects.

I really don't know if any of this was any help, if you can be more specific, it would help.

yes I use a similar method except that all variables (for example this.name) have to be declared private, so I can't use a setName() if it doesn't invoke its super-method. I got an 'acceptable' solution that way.



Good luck anyway !

Thanks, and thanks for your help !


edit: oh, I forgot to mention one thing: if you have a DiskItem object, there are methods that check if you can downcast it to a subclass type (IF it is, indeed, of that class type). I don't remember the class name and the methods, though. But this may be what you want - of course, you can always downcast it yourself if you know that the DiskItem is in fact a Link.

'instanceof' ?

I'm forced to use that quite a bit :2thumbsup:

Blodrast
05-03-2006, 17:13
okay, cool, I guess I don't have anything more to add - glad you figured it out ! :)

doc_bean
05-08-2006, 16:16
thanks, I have another project comming this week, so I'll probably be back :embarassed:

Blodrast
05-08-2006, 19:46
I'll be around :D

doc_bean
05-13-2006, 09:18
And the big project has started :sweatdrop:

We have to created a sort of inventory system for an RPG, honestly, it sounds more fun than it is....

Okay: my current big problem : I have to store items in a backpack, it has to be possible to retrieve both the heaviest and lightest item in O(1) (so independent of the amount of items), adding and removing has to happen in at least O(log(n)) (typically a binary search is used to achieve this). The problem: in order to get the heaviest and lightest item in constant time I need to sort the items by weight, but weight isn't a unique number, I need a unique number to sort the items in an ordered way. I was thinking of using a number that is a combination of the weight (as a whole number ) and the HashCode(as the decimals), but not even the HashCode is unique :furious3:


Then there are some demands about memory management, I have no idea how to this. The game has Dukats as a currency, they're actual objects. The amount of memory needed to handle Dukats in every game has to be the same.
I was thinking of just remembering the amount of dukats stored, with references to the static variables in Dukats (they have a constant weight and value), but interpreting the assignment it seems like they want links to the actual objects (the dukats). Any ideas ?


Similarly, Dukats can be stored in a Purse, the amount of memory needed for storing the Dukats has to be constant for every Purse. Any ideas ?

There still some details about heroes and monsters and potions that haven't been revealed yet, so these probably aren't my last questions. For what is supposedly a 30hour project it's friggin' huge though ! :help:

Blodrast
05-15-2006, 20:42
I don't get the idea about the Dukats. Do you need to do more than just remember one value, i.e. the sum of Dukats ? Or are you trying to say that you want to store each and every single Dukat as a distinct object - which I don't see the point of, not to mention it would be horribly inefficient, and completely non-sensical (one Dukat is identical for all purposes to any other Dukat). So why would you ever need more than one Dukat object, which stores a number (which represents how "many" Dukats the holder actually has) ?


Similarly, Dukats can be stored in a Purse, the amount of memory needed for storing the Dukats has to be constant for every Purse. Any ideas ?

Again, I'm not sure I get it. Do you need more than 4 bytes or such to store the Dukats ?
(4 bytes assuming 2^^32 is enough for the amount of Dukats, of course).


For the "current big problem": I dunno, do you HAVE to come up with a unique identifier ?
I assume you know it's okay to have collisions in hash tables...so...
Also, the choice of the data structure that you'll use should be interesting - at a first glance, I'd say hash tables, since they're generally the most efficient ones...

sigh, I'm afraid that instead of help you're just getting more questions here...sorry.

doc_bean
05-15-2006, 21:40
I don't get the idea about the Dukats. Do you need to do more than just remember one value, i.e. the sum of Dukats ? Or are you trying to say that you want to store each and every single Dukat as a distinct object - which I don't see the point of, not to mention it would be horribly inefficient, and completely non-sensical (one Dukat is identical for all purposes to any other Dukat). So why would you ever need more than one Dukat object, which stores a number (which represents how "many" Dukats the holder actually has) ?


I asked that question too, it's horribly contrived imho, but the way it's worded it seems like they expect us to make links to individual objects. Can you make a class that can only have a single object ?



Again, I'm not sure I get it. Do you need more than 4 bytes or such to store the Dukats ?
(4 bytes assuming 2^^32 is enough for the amount of Dukats, of course).

we can choose how we implement it, but he (the professor) seems to be a big fan of the non-limited numbers, such as BigInteger. It really depends on the context which we're not getting...



For the "current big problem": I dunno, do you HAVE to come up with a unique identifier ?
I assume you know it's okay to have collisions in hash tables...so...
Also, the choice of the data structure that you'll use should be interesting - at a first glance, I'd say hash tables, since they're generally the most efficient ones...

Well, we can use any strucutre we want, but they want to be able to ask for the lightest and heaviest item in real time and to add and remove items in O(log n), I think hashmaps have near O(1) so they're defiantly okay, but I need to get the weight in there somehow...

I'm not too experienced with ahshmaps either, we've only used them once. (did I mention I wrote my first java class 10 weeks or so ago, the current structure is over 60pages already and only halfway done :sweatdrop: )


sigh, I'm afraid that instead of help you're just getting more questions here...sorry.

nah, I appreciate a different view on the matter :2thumbsup:
I wish they made what they want'ed more clear though...

Alexanderofmacedon
05-15-2006, 22:41
System.out.println("I'm sorry, I'm not that far in Java right now.");
System.out.println("Maybe I can help you next year.");

Blodrast
05-16-2006, 01:35
I asked that question too, it's horribly contrived imho, but the way it's worded it seems like they expect us to make links to individual objects. Can you make a class that can only have a single object ?


If you mean: "Can I make a class that I only ever instantiate once ?", then the answer is most definitely yes.
Now, we might be getting into murky territory (or maybe we're already there :D), and I honestly don't know the scope of your assignment, BUT, here's an idea. It may be totally off, since, again, I don't know if using design patterns is or is not an issue for your assignment.
From what I remember from design patterns (which is admittedly not much), one of the classical design patterns is the Singleton - which ensures that only one instance of some class exists throughout the duration of the program.
I'm mentioning this because, IF you are supposed to use design patterns, this may be a good candidate for a Singleton.
If you're not dealing with design patterns at all, then ignore this.



we can choose how we implement it, but he (the professor) seems to be a big fan of the non-limited numbers, such as BigInteger. It really depends on the context which we're not getting...


Well, ok, then I guess you can just use BigInteger, which you already have implemented, I guess. Or, does it not have fixed storage ? Can't you ask him whether he is ok with something "large enough" (i.e., 2 ^^32, or 2^^64, etc), or does he want something that can potentially increase to accomodate numbers as large as one may desire ?
Because I sense a contradiction here: if you want _fixed_ storage, then it can't increase, so whether it's a simple integer or some intricate crap that allows you to store numbers up to 2^xxxxxxx, but no larger than that, it's a technicality.
But if you want to accomodate potentially ANY number, that can't be done with a fixed storage - you'd need a class that increases keeps adding storage until you can accomodate whatever large number you want.
So, I'm a bit confused (yet again).



Well, we can use any strucutre we want, but they want to be able to ask for the lightest and heaviest item in real time and to add and remove items in O(log n), I think hashmaps have near O(1) so they're defiantly okay, but I need to get the weight in there somehow...

I'm not too experienced with ahshmaps either, we've only used them once. (did I mention I wrote my first java class 10 weeks or so ago, the current structure is over 60pages already and only halfway done :sweatdrop: )

Ok, you don't have to use hashmaps. You could use a sorted array. First element would be lightest, last element would be the heaviest one. Since it's an array, well, you have O(1) access. You can also satisfy the O(log n) sorting requirement by using quicksort.

Ok, if you only started java 10 weeks ago, then I congratulate you, you're doing very very well. Honestly.
Btw, if you want, you can point me to the assignment text, and I'll try to take a look at it (time permitting), see if I can make more sense out of it. But really, you should ask the prof/instructor to clarify all these little things - 'cause they may well grade you on them...

Please take all my opinions with a grain of salt, since I'm by no means a Java expert or anything, and I also don't know the exact level of detail/techniques that your assignments are supposed to fit in. I also don't even know what kind of class this is - believe me, it makes a difference whether it is a Programming 101 class, or Data Structures, or Programming Techniques and Algorithms, or the focus is on Complexity, etc, etc.
I think you get my point - depending on which of these is the case, the focus of the assignments will be on completely different things.

Anyway, best of luck from now on as well. If any of my drivel turns out to be any help at all, I'll be happy.:2thumbsup:

doc_bean
05-16-2006, 09:24
If you mean: "Can I make a class that I only ever instantiate once ?", then the answer is most definitely yes.
Now, we might be getting into murky territory (or maybe we're already there :D), and I honestly don't know the scope of your assignment, BUT, here's an idea. It may be totally off, since, again, I don't know if using design patterns is or is not an issue for your assignment.
From what I remember from design patterns (which is admittedly not much), one of the classical design patterns is the Singleton - which ensures that only one instance of some class exists throughout the duration of the program.
I'm mentioning this because, IF you are supposed to use design patterns, this may be a good candidate for a Singleton.
If you're not dealing with design patterns at all, then ignore this.

I'll look into this if I have the time:2thumbsup:




Well, ok, then I guess you can just use BigInteger, which you already have implemented, I guess.

It's a standard java class.


Or, does it not have fixed storage ? Can't you ask him whether he is ok with something "large enough" (i.e., 2 ^^32, or 2^^64, etc), or does he want something that can potentially increase to accomodate numbers as large as one may desire ?
Because I sense a contradiction here: if you want _fixed_ storage, then it can't increase, so whether it's a simple integer or some intricate crap that allows you to store numbers up to 2^xxxxxxx, but no larger than that, it's a technicality.
But if you want to accomodate potentially ANY number, that can't be done with a fixed storage - you'd need a class that increases keeps adding storage until you can accomodate whatever large number you want.
So, I'm a bit confused (yet again).

My thoughts exactly.



Ok, you don't have to use hashmaps. You could use a sorted array. First element would be lightest, last element would be the heaviest one. Since it's an array, well, you have O(1) access. You can also satisfy the O(log n) sorting requirement by using quicksort.

I'll look into it, I'm was now thinking of using a binary search tree, which should also satisfy the requirements.


Ok, if you only started java 10 weeks ago, then I congratulate you, you're doing very very well. Honestly.

I think so too :book:


Btw, if you want, you can point me to the assignment text, and I'll try to take a look at it (time permitting), see if I can make more sense out of it. But really, you should ask the prof/instructor to clarify all these little things - 'cause they may well grade you on them...

It's only available in Dutch unfortuantly, i've alrady sent 2 emails with questions, the general reply is "if it sin't specified in the assignment, you can do whatever you like, we'll grade you on the result".


Please take all my opinions with a grain of salt, since I'm by no means a Java expert or anything, and I also don't know the exact level of detail/techniques that your assignments are supposed to fit in. I also don't even know what kind of class this is - believe me, it makes a difference whether it is a Programming 101 class, or Data Structures, or Programming Techniques and Algorithms, or the focus is on Complexity, etc, etc.

Object Oriented Programming, it's the second java class for most people who take it iirc. The focus is mainly on using the elements of OOP, such as references, overrides, static and non static variables, instance variables etc.



I think you get my point - depending on which of these is the case, the focus of the assignments will be on completely different things.

Honestly, I have little idea what the focus is on, they really like well documented code though, which means I spend more time writing the docs than writing the code :furious3:


Anyway, best of luck from now on as well. If any of my drivel turns out to be any help at all, I'll be happy.:2thumbsup:

Thanks for the help :2thumbsup:
I'll look into a few things and see what I can do with them, of course, my time is limited so i'll just do what can be done :sweatdrop: