Home arrow static arrow Java Programming [Archive] - probs with LinkedList
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - probs with LinkedList
This topic has 31 replies on 3 pages.    « Previous | 1 | 2 | 3 | Next »

Posts:1,652
Registered: 8/4/03
Re: probs with LinkedList  
Aug 3, 2004 2:13 PM (reply 15 of 31)



 
There's something niggling around
the edges of my thoughts on this

Hey, buddy. There's no need for racial epithets.

Thanks dubwai, I needed a good laugh. Have I ever mentioned that I REALLY HATE the Struts framework?
 

Posts:13,769
Registered: 00-11-29
Re: probs with LinkedList  
Aug 3, 2004 2:18 PM (reply 16 of 31)



 
Thanks dubwai, I needed a good laugh. Have I ever
mentioned that I REALLY HATE the Struts framework?

If you want to see struts, come see me at the club.

There was a guy who almost got fired for using the word 'niggling' at work. Perhaps having a big vocabulary isn't always a good thing.
 

Posts:23
Registered: 8/4/03
Re: probs with LinkedList  
Aug 3, 2004 2:20 PM (reply 17 of 31)



 
This is the Componet file, i have left af bit out, but it is similar to the CPU & Bundkort classes. Btw Bundkort = Motherboard
public class Component{	public String fabrikant;	public String model;	public String frekvens;	public String information;	public String stoerrelse;	public int pris;	 		public Component()	{		}			public class CPU	{				public CPU(String f, String m, String z, int p)		{			fabrikant = f;			model = m;			frekvens = z;			pris = p;							}				public String toString()		{			return("'Fabrikant' " + fabrikant + 			" 'Model' " + model + 			" 'Frekvens' " + frekvens + 			" 'Pris' " + pris);		}		}		class Bundkort	{		public Bundkort(String f, String m, String i, int p)		{			fabrikant = f;			model = m;			information = i;			pris = p;					}				public String toString()		{			return("'Fabrikant' " + fabrikant + 			" 'Model' " + model + 			" 'Information' " + information + 			" 'Pris' " + pris);		}		}


this is the WareHouse class, here I also left out what was similar to CPU & Bundkort
import java.util.*;import java.util.LinkedList;import corejava.Console;  public class WareHouse{	public LinkedList myHardware = new LinkedList();	Component comp = new Component();				public void addNewCPU(String fabrikant, String model, String frekvens, int pris)	{		Component.CPU newCPU = comp.new CPU(fabrikant, model, frekvens, pris);		myHardware.addFirst(newCPU);	}		public void addNewBundkort(String f, String m, String i, int p)	{		Component.Bundkort newBundkort = comp.new Bundkort(f, m, i, p);		myHardware.addFirst(newBundkort);	}                     public void displayMyHardware()	{		ListIterator components = myHardware.listIterator();		while (components.hasNext())		{			System.out.println(components.next());		}	}	}


the last part which is important to you is the UserInterface class
public class UserInterface{private void addCPU()	{		String fabrikant, model, frekvens;		int pris;				//for adding a CPU			fabrikant = inputFromKeyboard.readString("\n Fabrikant -> ");	model = inputFromKeyboard.readString(" Model -> ");	frekvens = inputFromKeyboard.readString(" Frekvens -> ");	pris = inputFromKeyboard.readInt(" Pris -> ");	wareHouseObject.addNewCPU(fabrikant, model, frekvens, pris);                     }private void addBundkort{                  String fabrikan, model, information;                  int pris;                                                             //for adding a Motherboard                     fabrikant = inputFromKeyboard.readString(" Fabrikant ->");	model = inputFromKeyboard.readString(" Model ->");	information = inputFromKeyboard.readString(" Information ->");	pris = inputFromKeyboard.readInt(" Pris ->");	wareHouseObject.addNewBundkort(fabrikant, model, information, pris);}}

I've left quite a bit of this out, but its again similar to the addCPU and addBundkort methods
 

Posts:13,769
Registered: 00-11-29
Re: probs with LinkedList  
Aug 3, 2004 2:22 PM (reply 18 of 31)



 
This is the Componet file, i have left af bit out, but
it is similar to the CPU & Bundkort classes. Btw

Right off the bat, I'm going to guess that the two CPUs that always look the same have the same Component as a parent.
 

Posts:23
Registered: 8/4/03
Re: probs with LinkedList  
Aug 3, 2004 2:25 PM (reply 19 of 31)



 
true

but when i add a motherboard which has the same parent, it also overwrites the cpus where the attributes are similar
 

Posts:13,769
Registered: 00-11-29
Re: probs with LinkedList  
Aug 3, 2004 2:27 PM (reply 20 of 31)



 
OK looked closer. Yes. You are using a single Component. All those isntances of Component.CPU ad Component.Bundkort share a hidden reference to the parent instance. When you call you constructor, you are actually setting the properties of the Component instance, of which there is only one.

I'm going to also guess that what you are actually looking for is a inheritance hierarchy like:

Component{}
CUP extends Component{}
Bundkort extends Component{}
 

Posts:13,769
Registered: 00-11-29
Re: probs with LinkedList  
Aug 3, 2004 2:32 PM (reply 21 of 31)



 
but when i add a motherboard which has the same
parent, it also overwrites the cpus where the
attributes are similar

The same thing is true for the motherboard except that you have a slghtly different set of variables you set in the constructor.
 

Posts:23
Registered: 8/4/03
Re: probs with LinkedList  
Aug 3, 2004 2:43 PM (reply 22 of 31)



 
you are right about:
I'm going to also guess that what you are actually
looking for is a inheritance hierarchy like:

Component{}
CUP extends Component{}
Bundkort extends Component{}

but my teacher complains if i put CPU, Bundkort etc in separete files. so i have to make it work in one file. it is for an exam at my university, and this is my last chance for passing, otherwise i'm kicked out. So it has to be right this time
 

Posts:13,769
Registered: 00-11-29
Re: probs with LinkedList  
Aug 3, 2004 2:47 PM (reply 23 of 31)



 
Why your teacher requires that they are in different files is beyond me but you can work around that. Here's one way:
public class Component{    //...} class CPU extends Component{    //...} class Bundkort extends Component{    //...}

If everything must be in one file that will work.
 

Posts:23
Registered: 8/4/03
Re: probs with LinkedList  
Aug 3, 2004 2:56 PM (reply 24 of 31)



 
that did it, now it works.

Could you please explain to me why it works with adding the "extends Component", i thought it was done automatically when it was from the same file

thanks
Romaniuz
 

Posts:13,769
Registered: 00-11-29
Re: probs with LinkedList  
Aug 3, 2004 3:21 PM (reply 25 of 31)



 
that did it, now it works.

Could you please explain to me why it works with
adding the "extends Component", i thought it was done
automatically when it was from the same file

Not at all. Putting things the same file has little effect on anything.

Putting a class inside another class makes it an 'inner' class. That means it is defined inside the context of the enclosing class. If you put a class inside another and declare it static it's a nested class. That means it really isn't related to the enclosing class exception you have to use the enclosing class name to use the class.

There is a whole lot to learn. I wouldn't assume Java does anything for you. Make sure you know exactly what is going on with simple things before moving to more complicated things. Otherewise you'll never know what's going on.
 

Posts:6,750
Registered: 1/25/04
Re: probs with LinkedList  
Aug 3, 2004 3:48 PM (reply 26 of 31)



 
Putting a class inside another class makes it an
'inner' class. That means it is defined inside the
context of the enclosing class. If you put a class
inside another and declare it static it's a nested
class.

<pedantic ballsniffing>A nested class is any class defined inside another class. An inner class is a non-static nested class. Multiple top-level (non-nested) classes can exist in the same source file as long as only one is public.</pedantic ballsniffing>
 

Posts:24,036
Registered: 2/3/03
Re: probs with LinkedList  
Aug 3, 2004 7:27 PM (reply 27 of 31)



 
I may regret asking this, but post the code for the
whole thing please. There's something niggling around
the edges of my thoughts on this which Yaw or Dubwai
(both of whom are smarter than me) will probably see
instantly.

You give me too much credit and yourself too little.
 

Posts:24,036
Registered: 2/3/03
Re: probs with LinkedList  
Aug 3, 2004 7:30 PM (reply 28 of 31)



 
Thanks dubwai, I needed a good laugh. Have I ever
mentioned that I REALLY HATE the Struts framework?

If you want to see struts, come see me at the club.

There was a guy who almost got fired for using the
word 'niggling' at work. Perhaps having a big
vocabulary isn't always a good thing.

Whatever you do, don't say [url=http://www.washingtonpost.com/wp-srv/local/longterm/williams/williams020499.htm]"niggardly"[/url].

<pet-peeve>bastardizing equal rights and equal opportunity into equal outcome and political correctness</pet-peeve>
 

Posts:13,769
Registered: 00-11-29
Re: probs with LinkedList  
Aug 4, 2004 6:33 AM (reply 29 of 31)



 
Whatever you do, don't say
[url=http://www.washingtonpost.com/wp-srv/local/longter
/williams/williams020499.htm]"niggardly"[/url].

Actually, that was the thing I was thinking of.
 
This topic has 31 replies on 3 pages.    « Previous | 1 | 2 | 3 | Next »