Vector problems...

Hello everyone!

I'm having some trouble with some school assignment and I would greatly appreciate your help!

I'm working on a project where I have to create multiple classes which represent PC Software and Hardware components, such as Motherboard, CPU etc. On the "main" class of the application, I have used the following vectors:

privatestatic Vector<Vector> Components;

privatestatic Vector<Vector> Software;

privatestatic Vector<Vector> Products;

privatestatic Vector<Motherboard> Motherboards;

privatestatic Vector<Memory> Memories;

privatestatic Vector<OperatingSystem> OperatingSystems;

etc.

the first batch of vectors contains the second batch of vectors, such as:

Components.add(Motherboards);

Components.add(Memories);

Software.add(OperatingSystems);

Products.add(Components);

Products.add(Software);

and the second batch of vectors contains Objects of different types/classes such as:

Motherboards.add( Motherboard mobo1 =new Motherboard() );

Memories.add( Memory mem1 =new Memory() );

etc.

Now what I want is that every Motherboard, whose quantity is >0, to be printed.

{In class Motherboard, there is a private int field called "quantity", an int function called "getQuantity()", which returns the quantity and the method "toString" is overriden, in order to print all the elements I need.}

I'm using the following code to print the required components:

for (int i=0;i<(Components.elementAt(j).capacity();i++)

{

if(((Components.elementAt(j)).elementAt(i)).getQuantity() > 0)

{

System.out.println("Patiste " + i+1 +" gia:");

System.out.println(((Components.elementAt(j)).elementAt(i)).toString());

}

}

System.out.print("Epilogi: ");

However, the following compile-time error occurs:

ergasiaJava.java:457: cannot find symbol

symbol: method getQuantity()

location:class java.lang.Object

line 457:if (((Components.elementAt(j)).elementAt(i)).getQuantity()) > 0)

From which I get the impression, that java can't "understand" that ((Components.elementAt(j)).elementAt(i)).getQuantity())

is, in fact a Motherboard.

However, if the if-clause is removed, then the method toString() is correctly used, in its overridden form, as it would normally be used from a Motherboard.

Any ideas? Where do I have it wrong?

Best Regards,

Philimon

P.S. all the vectors are initialized ( Motherboards =new Vector<Motherboard>(0,1);

etc.) and the "Motherboards" vector is added to the "Components" vector, after the mobo1 is added to "Motherboards".

[3592 byte] By [philimonasa] at [2007-11-27 10:27:20]
# 1

From which I get the impression, that java can't "understand" that

((Components.elementAt(j)).elementAt(i)).getQuantity())

is, in fact a Motherboard.

You will probably have to cast each element you retrieve to a Motherboard before calling the getQuantity method However, I suggest a simply solution: create a temp Vector and assign it to your Motherboard Vector, then simply use an enhanced for loop:

Vector<Motherboard> temp = ....;

for(Motherboard m : temp) {

....

}

floundera at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...
# 2

Thank you for your prompt answer, however I must admit that I do not fully understand. Since I already have a Vector<Motherboard>, called "Motherboards", why should I create a temp one? This Vector is the first element of the "Components" Vector.

philimon

philimonasa at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...
# 3

Then use your existing Vector. I was just suggesting a way to avoid ugly code like this:

(Components.elementAt(j)).elementAt(i)

floundera at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...
# 4

Now I understand what you mean, but I wrote this "ugly" code, because it *should* be used for many types of components.

What I mean is that in

(Components.elementAt(0))

is the "Motherboards" Vector,

in

(Components.elementAt(1))

is the "CPUs" Vector and so on.

This code is inside a method that has an "int j" parameter, which should choose between the different components.

If i change the code to something like:

Motherboards.elementAt(i)

, then it wouldn't be possible to use the same method for different components, or so I think.

philimonas

philimonasa at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...
# 5

> If i change the code to something like:

> Motherboards.elementAt(i)

, then it

> wouldn't be possible to use the same method for

> different components, or so I think.

>

> philimonas

does each component derive from a common abstract class or implement a common interface?

petes1234a at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...
# 6

Well, the superclass of all classes is called "Item" and that's where the field int quantity is coming from.

Then there is the "Component" class, which extends Item and is the superclass of Motherboard.

So we have:

public class Item

|

public classComponent extends Item

|

public class Motherboard extends Component

the same is for each component.

philimonasa at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...
# 7

In that case I suggest you pass the Vector as a parameter to your method instead of the index number (why are all your other Vectors stored in another Vector anyway?).

Then you can use an enhanced loop like I said.

public void printList(Vector<Item> list) {

for(Item i : list) {

........

}

}

floundera at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...
# 8

Ok, thanks a lot, I will try that tomorrow and I will tell you how it goes.

Unfortunately, It's asked to have two Vectors called "Component" and "Software", otherwise I would only have used "Motherboards", "CPUs" and so on.

In case I use only the "Component" and "Software" Vectors, I find it difficult and more complicated to keep track of where the different types of components start and end (I would need two pointers for each different type, one for "start(Motherboard,CPU,...)" and another for "end(Motherboard,CPU,...)".

philimonas

philimonasa at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...
# 9

Another thing, you will have to change the declaration of your Vectors to hold Item objects.

Vector<Item> motherboards = new Vector<Item>();

P.S. Variable names should begin with lowercase.

floundera at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...
# 10

Hello again,

if I pass the Vector parameter, as you suggest

public void printList(Vector<Item> list) {

for(Item i : list) {

........

}

}

Then how will the "Components" Vector be "updated"? Will I have to rebuild it from scratch?

philimon

philimonasa at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...
# 11

I always try to avoid exposing vectors to the business logic that is using my function. I return a simple collection ( Memory[] memory) instead.

In the example below, you can print out the memory in a collection as follows:

Memory[] memorys= collection.getMemorys();

for(int ii=0; ii<memorys.length; ++ii){

System.out.println(memorys[ii].getName() );

}

Its much cleaner than exposing vectors.

Also, I privide a rich set of funtions so the caller of collection doesn't have to work hard on extracting information.

***************

public class Memory {

private name;

public Memory(String name){

this.name=name;

}

public String getName(){

return name;

}

}

public class MotherBoard {

private name;

public MotherBoard(String name){

this.name=name;

}

public String getName(){

return name;

}

}

public class Components {

private Vector><MotherBoard> motherBoards;

private Vector<Memory> memorys;

//constructor

public Components(){

motherBoards=new Vector<MotherBoard>();

memorys=new Vector<Memory>();

}

public void addMotherBoard(MotherBoard motherBoard){

motherBoards.add(motherBoard);

}

public void addMemory(Memory memory){

memorys.add(memory);

}

public MotherBoard getMotherBoard(int index){

return motherBoards.get(index);

}

public Memory getMemory(int index){

return memorys.get(index);

}

public MotherBoard[] getAllMotherBoards(){

return (MotherBoard[])motherBoards.toArray(new MotherBoard[motherBoards.size()]);

}

public Memory[] getAllMemorys(){

return (Memory[])memorys.toArray(new Memory[memorys.size()]);

}

}

George123a at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...
# 12

By the way, I think a class called 'Components' is too generic a name (it could be components of anything). Instead, how about calling it 'ComputerComponents'. I'm very big on carefully picking a proper name for a class (and proper names for functions).

If you really want to nit-pick, ComputerComponents isn't so hot either. Your items contain both hardware components and software components. So I would only put hardware components in a class called ComputerHardwareComponents, and create a separate class to hold software components called ComputerSoftwarePackages. Of course there is other ways to organize it all. Now the question is, is ComputerHardwareComponents class hold all the hardware for one computer, or components to make many computers (as in a company that manufactures computers). The javadoc for the class needs to specify this so no one misuses the class.

George123a at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...
# 13

Hello and thanks for your replies.

In the meantime, I have made some changes to my code and now everything runs - almost !- as needed.

Regarding your last post, George123, I believe you are right. However, all the classes' and fields' names are predefined by our professor, so I will stick to that. Just for the record, Component class only has subclasses that are Hardware components and Software is the equivalent for OS and Apps.

philimonas

philimonasa at 2007-7-28 17:44:54 > top of Java-index,Java Essentials,New To Java...