aspose file tools*
The moose likes Beginning Java and the fly likes Arrays are confusing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays are confusing" Watch "Arrays are confusing" New topic
Author

Arrays are confusing

Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
So I have this assignment that I am working on and my "instructor" is nowhere to be found.
Basically an array in the program holds the name, part number, unit price, quantity, and inventory value (qty*unit price) of 5 objects.
It displays all of this info in alphabetical order and then given a total inventory value at the bottom.

For the assignment I am supposed to create a subclass of my product class which will add another element to the array (I am trying to add top speed.)
I don't know how to get the information added into the current array and my brain hurts from reading so much into it. At the bottom of the product.java file, I have created a "Speed" class which holds methods I think should help get the job done but I'm just not sure if I am headed in the correct direction.

Some help would be greatly appreciated.

Thank you







Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
Anyone?

I basically don't understand how to add a new attribute to the String method right above where the Speed class is in the code. I'm trying to add top speed to that but it doesn't let me because the method is in a different class I guess?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Justin Bodin wrote:I basically don't understand how to add a new attribute to the String method right above where the Speed class is in the code.

Are you talking about the toString() method or... ?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
Whoops, yes I am.

You know, if I was trying to add to that method to display the "Top Speed:" using the getTopSpeed method from the Speed subclass. But it doesn't let me, and wants me to create another getTopSpeed method in that same Product class.

I may be going about it completely wrong, though...
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Ok then, NO you can't add "topSpeed" property to the toString() method of Product class as it's a property of the "Speed" class. You should override the "toString()" method in the Speed class to display the "Speed" class specific properties. And you still can reuse the one from the parent class "Product" though.
Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
Does that mean write another toString() method in the Speed subclass but adding the top speed?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Justin Bodin wrote:Does that mean write another toString() method in the Speed subclass but adding the top speed?

Yes, use the super class version to display all the inherited data (i.e: super.toString()).
Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
yyyeah I'm not quite getting it. I don't think I fully understand the use of the super keyword. I can't seem to relate the simple examples I'm learning from to how to make it work in this code.
Sorry to sound potentially idiotic, but I have to make use of super. inside of a new toString() method within the Speed subclass...or no?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Justin Bodin wrote:... but I have to make use of super. inside of a new toString() method within the Speed subclass...or no?

Yes. Simple example: (You might need to change a bit to suit your requirement)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Justin Bodin wrote:Anyone? . . .
Please! You can't expect answers within the hour. Read this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Vijitha Kumara wrote:
Nice example, but please don't go saying parent class and child class. It's superclass and subclass; Java classes and real parents and children have very different behaviour.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Justin Bodin wrote: . . .
Basically an array in the program holds the name, part number, unit price, quantity, and inventory value (qty*unit price) of 5 objects. . . .
What array? You don't have an array with name, part number, etc. You have an array of motor bicycles, with name, part number etc as attributes.

K Sierra, B Bates, Head First Java 2nd edition Sebastopol CA: O@Reilly Inc (2005), page 250ff.

You will there see diagrams showing objects consisting of outside parts and inside parts; the inside parts are created (I think, unchanged) from their superclasses. In the constructor, super() can be thought of as meaning "create the part of this object which comes unchanged from the superclass." That part is "inside" and has to be created first. That's why you have to say super(); or super(something); as the first statement in the constructor.

If you write super.something, that can be thought of as "gain access to that member "something" of the object which comes unchanged from the superclass." In the case of super.toString() it means call the toString() method which is unchanged from the superclass (and this gives you a String to play with).

The exact details of how objects are stored in memory vary from one implementation of the JVM to another.
Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
Yeah it was a good example, but I still couldn't get it to work.

Is it possible to do with the array set up the way it is? It seems like no matter what I tried, I couldn't get the Top Speed: to show display, only the original attributes.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hi Justin,

Here's a meta-tip that's good for both being a programmer AND asking programming questions:

Break your problem into little pieces and do prototypes (coding and testing) on the little pieces before putting your little pieces together into a big piece.

For example, when I looked at your code my eyes glazed over after about 5 seconds. For instance, there was no way I was going to look at all of your getters and setters

My guess is that the core of your question could be reduced to less than 15 lines of code. If you attempt to reduce the core of your question to 15 lines of code, I predict that lots of amazing things will happen...

- you might discover the answer yourself
- you'll get more focused answers from other ranchers
- your code will get better (more modular, perhaps more OO-ish, and so on)

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
Understandable, thanks for the tips sir.

So I edited the code a bit to try and make only the pertinent information appear.
I have tried adding such things as:



to the Speed subclass, but to no avail -- all I get to display is the old information from the Product class!!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Cool Justin,

So show us around 15 lines of code, designed to run as a standalone program (i.e. with psv main) that focus on just the problem at hand...
Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
Not to be difficult, but if I knew how to do that then I would probably be able to relate the simple problem that someone posted earlier to get the correct answer to my specific program.

My issue involves not knowing how to do something outside of the main method, so I just really don't know how to formulate an 'example problem' for you guys to help me out with.

Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Okay, I'm going to try not to show you this directly, but to try to give you some clues instead...

It *seems* like we should not worry about arrays of objects just yet... how about building a single instance of each type of object and just printing out the values of each of those (two?) instances... just to start.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hey Justin,

Did you ever figure out your problem?

Bert
Justin Bodin
Greenhorn

Joined: Oct 21, 2009
Posts: 15
Hey Bert,

I actually did not figure it out using the method that I was going about. I ended up rewritting the entire program with much more OO code and storing the info in an inventory, and it worked great. I still don't think I could tell you why this previous program wouldn't work for me! It got really cluttered and hard to understand I think.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays are confusing