I need some help with a Java project I'm working on. It's fairly simple, but the guidelines set by the instructor are a little restrictive so I can't use a few methods that would make it easy.
At any rate, I need to find the minimum/maximum objects stored in an array, and those objects are vectors.
Here's what I need to do:
"Return the maximum object in this vector. You may NOT assume all elements are Comparable objects, BUT you may assume that all Comparable objects in this vector are of the same class. If no Comparable object exists in this vector, return null."
The name of the array is myArray[] as defined by:
private Object[] myArray;
the number of objects in the array is myCt, as defined by:
private int myCt;
and I need to fill in the code for:
public Comparable findMax() {}
I need to use Comparable, I assume, as we haven't learned linked lists or search trees or anything like that that would make it easier. Thanks for the help!
dlorde
January 29th, 2004, 08:01 PM
I need to find the minimum/maximum objects stored in an array, and those objects are vectors.Er, that's not what the question you quoted is asking...
Precise language is not the problem. Clear language is the problem...
R. Feynman
Judas1012
January 29th, 2004, 08:06 PM
Perhaps I don't understand what he's asking then... is he asking for the maximum index value of the Object? I don't understand it... :(
Magus
January 29th, 2004, 08:45 PM
I'm not familiar with the Comparable interface, but it seems it might allow you to determine whether a given object is "greater than" another, in which case your task would be a simple one of returning the maximum (greatest) value. Since it's stated that you may not assume all objects in the Vector are Comparable, you must verify that condition every time you get an Object from it by using the instanceof operator (if (object instanceof Comparable)). Once you get a hold of the idea behind the Comparable interface (which I haven't at this point, and I'm really tired right now, so reading a rather detailed technical explanation is currently incompatible with my brain cells :P), it should be easy.
You iterate through the Vector, always comparing two Objects and keeping the greater one, until all Objects have been picked for comparison at some point.
dlorde
January 30th, 2004, 07:27 AM
Originally posted by Judas1012
Perhaps I don't understand what he's asking then... is he asking for the maximum index value of the Object? I don't understand it... :( The quoted question asks you to "Return the maximum object in this vector". Since the code details specify an array, I presume he is using 'vector' in the sense of a one-dimensional array. So you have to find out which of the objects in the array is the maximum one. You can't rely on all the objects being Comparable, so you won't be able to compare some of them, but you can find the maximum comparable object.
Declare a Comparable reference to hold the maximum object.
Iterate through the array and for each Object, check if it is an instance of Comparable. If it isn't, ignore it and move on. If it is, cast it to Comparable. If the maximum object reference is null, or less than the new Comparable object (use the compareTo(...) method), assign the new Comparable object to it. When finished, return the maximum object reference.
Everything should be made as simple as possible, but not simpler...
A. Einstein
Judas1012
January 30th, 2004, 08:45 AM
Thanks for the help, guys! That makes more sense now.
I'm having another problem -- once I write this code, apparently I can't call this method like I do other methods since it's a "Comparable" method. My vectors in my main method are v[0], v[1], and so on... but when I try something like v[3].findMax() it's telling me it doesn't understand findMax(). Do I need to call this method differently from other methods?
(I know I'm not using all the right Java terminology yet, I just started learning most of this stuff.)
Thanks again!
Magus
January 30th, 2004, 10:46 AM
Ok, let's make things clear then:
The basic multiple-element structure that involves [ ] are called arrays. While they're intuitive, they have a maximum number of elements that can be stored in them, and all elements must be of the same type/class or have a common superclass or implemented interface (polymorphism). That means, for example, that you can't have an array of Integers (Integer[]) and store, say, a Double in them (Integer and Double are wrappers, in case you're not familiar with them). However, although not recommended, you could have an array of Objects (Object[]) and store an Integer, a Double and any other class instance whatsoever, since all classes are subclasses of Object - you'd need to cast the objects back to their original class signatures when attempting to get a stored value by doing something like this though.
Now let's focus on the Vector. There are classes intended as storage structures in the Java SE API, all more complex than a regular array, since they have their own methods for doing things, including one(s) for getting the value (Object) at a given position, instead of merely putting an index between [ ]. java.util.Vector is one of them. One essential advantage of these classes is that they're not limited by a pre-defined "population" (:P) limit, so you can add Objects without worrying about that (you don't have to force a limit before you start adding values; the only real limit is the available memory heheh). However, you cannot add primitive-type variables into any of those structures, because they work with Objects (that's where wrappers come in). All Objects stored in any of these structures are taken as simple Objects, so it's imperative that you cast every one of them back into their "proper selves" if you ever wish to make use of their own methods and make them be properly recognized by methods that require them to be more specific than just an Object again.
Well, you should seek a clear understanding of what exactly your teacher wants.
dlorde
January 30th, 2004, 01:15 PM
Originally posted by Judas1012
Thanks for the help, guys! That makes more sense now.
I'm having another problem -- once I write this code, apparently I can't call this method like I do other methods since it's a "Comparable" method. My vectors in my main method are v[0], v[1], and so on... but when I try something like v[3].findMax() it's telling me it doesn't understand findMax(). Do I need to call this method differently from other methods?I'm not sure, but I think you have misunderstood the whole task in hand.
My understanding of the assignment is that all you have is a single array of Objects (defined private Object[] myArray;). [Your tutor calls this a 'vector' which, although it can mean 'a one-dimensional array', is a little confusing when used in a Java context because Java has a Vector class. However, it appears that the Vector class is not involved in this exercise].
You simply have to find the maximum object out of all the comparable objects in this array. You don't know what these objects are, but you do know that some of them don't implement Comparable, so you have to skip those ones. The ones that do implement Comparable are all the same type, so you can compare them against each other.
I can't see any need to have vectors in your main method, but without seeing the code you've written, I don't know why you're using them.
The actions I specified at the end of my last message are a solution to the task that you posted - assuming that what you posted was the full requirement. I managed to implement a test program that does those actions, and it successfully returned the maximum object from the array it was given. If you follow those actions in your own findMax(...) method, you should get a similar program. Incidentally, in order to find the maximum object in the array, the findMax(...) method needs to be passed the array it's going to search, and since the task was to return the maximum object, it should return an Object, e.g:Object findMax(Object[] arrayOfObjects) {
Object maxObject = null;
// loop through the array to find the
// maximum object
return maxObject;
}To compare two objects that implement comparable:int result = object1.compareTo(object2);It's that simple. The the result will be -ve if object2 is larger, zero if they're the same, and +ve if object2 is smaller.
Out of clutter, find simplicity. From discord, find harmony. In the middle of difficulty, lies opportunity...
A. Einstein
Judas1012
January 30th, 2004, 06:08 PM
I finally got it... it turned out to be a combination of everyone's suggestions, so thanks to all of you!
Here's the code I finally ended up with... remember, this is a level 2 Java course, so of course the code is ugly. But it works. We'll get to optimizing code later in the course.
public Comparable findMax() {
if (isEmpty())
return null;
Integer maxObj = new Integer(0);
for (int i = 0; i < myCt; i++) {
if (myArray[i] instanceof Comparable) {
Integer compInt = (Integer)myArray[i];
if (compInt.compareTo(maxObj) > 0)
maxObj = compInt;
}
}
return maxObj;
}
I had to cast Integer types because the Integer class has the compareTo() method, while the Object class does not. That's where my problem was -- I was trying to apply compareTo() to an Object, and it wouldn't work, even if I cast the Object as an Integer -- I had to declare an Integer, then get the Object and turn it into an Integer.
Ah, well, it works, and thanks again!
If anyone is interested in seeing the entire project, I'd be happy to zip it up and post it here.
Magus
January 30th, 2004, 06:38 PM
Hmmm... I believe you should cast the objects as Comparable.
Next time, use the code tag.
<code> </code>, replacing < > with [ ].
Judas1012
January 30th, 2004, 06:41 PM
Well, I tried that, but since the project doesn't implement Comparable anywhere, it didn't work -- at least that's what I figured. Nowhere in the project are the lines "... implements Comparable".
Sure, the name of the method is "public Comparable xxxxxxx", but using Comparable didn't work... I just don't have a good enough understanding of Java yet to explain why it didn't work. :(
dlorde
January 31st, 2004, 10:09 AM
Your solution does not correctly answer the task as you posted it.
The task said the array contained Objects, some of which implemented Comparable. It didn't mention the Integer class anywhere. Your solution will only work if the Objects in the array happen to be Integers. According to the task instructions, you don't know what the underlying type is - it could be anything.
If the test "myArray[ i ] instanceof Comparable" returns true, then you can always cast the array element to Comparable - that's why you do the test!
Your code always returns a valid Integer, but the task specified that if there were no comparable objects in the array, you should return null. Suppose the array has objects in it but none are comparable? Your code will still return an object.
If you don't follow the requirements, you will not satisfy them...
dlorde
January 31st, 2004, 10:16 AM
Originally posted by Judas1012
Well, I tried that, but since the project doesn't implement Comparable anywhere, it didn't work -- at least that's what I figured. Nowhere in the project are the lines "... implements Comparable".If any of the objects in the array implement Comparable, then somewhere in their inheritance tree they must have been declared to implement Comparable.
What did you do that didn't work? Exactly what errors did you get?
All truths are easy to understand once they are discovered; the point is to discover them...
G. Galilie
Judas1012
January 31st, 2004, 01:20 PM
Well, we do know for a fact that all the Objects are Integers. That's the way the program is set up. All the Objects inserted into the array are Integer, so the test "myArray[i] instanceof Comparable" should test that the Object is indeed an Integer, which implements Comparable, right? And that same test should test whether or not the Object is an Integer, and if it is, then it compares it, if not, it skips it... right?
The error I got when I cast them as (Comparable) is something to the tune of "cannot implement method compareTo".
I do have an empty vector -- v[0] -- which contains no elements, and the capacity is 16. When i run the program as I wrote it, it returns null as it should.
I've got until Wednesday to complete this, so perhaps I do need to change my method. I've attached all three .java files if you'd like to take a look.
dlorde
February 1st, 2004, 12:53 PM
Originally posted by Judas1012
Well, we do know for a fact that all the Objects are Integers. That's the way the program is set up.<sigh>Well obviously some type that implements Comparable must be used for the example, but the question implies that for purpose of the exercise it could be any type (otherwise surely the type would have been mentioned ?).All the Objects inserted into the array are Integer, so the test "myArray[ i ] instanceof Comparable" should test that the Object is indeed an Integer, which implements Comparable, right?Clearly, if all the objects in the array are Integers, then any object you look at in the array will be an Integer and therefore it will implement Comparable. However, the question specified "You may NOT assume all elements are Comparable objects", so equally obviously, for the purpose of the exercise, you cannot assume all the objects in the array are Integers. I'll point out, for the last time, that the question doesn't mention Integers at all, just that the objects that are Comparable will be of the same type. This means that the array could consist of a mix of Strings (or any other class that implements Comparable), and some other class type(s) that don't. Or it could consist of no Comparable implementors at all... But hey, if you want to hard code your answer so it only works with arrays of Integers, go ahead. Just don't expect good marks for it.The error I got when I cast them as (Comparable) is something to the tune of "cannot implement method compareTo".That doesn't help at all, I'm afraid. I can't imagine a cast error that would say that. Either post the relevant code, or the exact error message if you want help with this.I've attached all three .java files if you'd like to take a look.OK, I'll have a look...
Documentation is like sex; when it's good, it's very, very good, and when it's bad, it's better than nothing...
Dick Brandon
dlorde
February 1st, 2004, 01:33 PM
At last all becomes clear... You have your own Vector interface which you subclass to get an ArrayVector that is based on an internal array of Objects, and you have to implement a findMax() method on it. It would have been helpful if you'd said that in the first place :rolleyes:
Part of the problem is that the ArrayTest main method doesn't test all the requirements of the findMax() exercise you posted, so it won't spot that you haven't followed the requirements specified. It's a poor example of a test method.
Looking at the code, your findMax() method will fail if ArrayTest uses anything other than Integers to test it, and that it will work for any Comparable type, without any errors, if you replace the Integer reference and the cast to Integer with a Comparable reference and a cast to Comparable. You should also change the initialization of maxObj to be null instead of Integer, and in the comparison condition, check for maxObj being null:if (maxObj == null || compInt.compareTo(maxObj) > 0)This way it will work for any Comparable type, instead of being hard coded for Integers.
Your check for isEmpty() at the start of the method is unnecessary if you make the changes I suggest, and anyway it will fail the requirement if ArrayTest sets up an array that is not empty but contains no Comparable objects ("You may NOT assume all elements are Comparable objects... If no Comparable object exists in this vector, return null").
Program testing can be used to show the presence of bugs, but never to show their absence!
Edsger Dijkstra
Judas1012
February 1st, 2004, 03:17 PM
Ah ha! That make more sense. That bit of code you wrote helped tremendously.
Thanks again for all the help on the project -- I know how frustrating it is helping people with something that you have a lot more knowledge of -- I'm in the same boat sometimes, just not with Java... ;) I don't know all the exact terminology and wording on how to explain what I've written, what needs to be done, and what the guidelines are, so that's why I decided to post the code... thanks so much for all the help!
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.