Call alternate classes

Hello

I have a project with 3 classes all being extensions of one base class, all the added methods have the same names

i also have 3 option groups each with 3 toggle buttons.

Is it possible to call a different class for each instance of a toggle button but to give it the same name within an option group

i.e.

option group 1 has toggles 1, 2, & 3

if tog1

FIFO example1 =new FIFO()

if tog2

LFU example1 =new LFU()

if tog3

Optimal example1 =new Optimal()

and the same for group 2 example2 and group 3 example3

I have tried doing this in different ways including initiating them in the toggle event handlers but because they are not initiated i get errors in the rest of my code when calling elements of the class

The only way i have managed to get it to work is to initiate the 3 examples with the basic class and then change the class depending on which toggle is pressed.

Is this the only way of doing this or is there another way?

Many thanks for any help

[1266 byte] By [Kerrancea] at [2007-11-27 4:49:09]
# 1

> Hello

>

> I have a project with 3 classes all being extensions

> of one base class, all the added methods have the

> same names

>

> i also have 3 option groups each with 3 toggle

> buttons.

>

> Is it possible to call a different class for each

> instance of a toggle button but to give it the same

> name within an option group

>

> i.e.

> option group 1 has toggles 1, 2, & 3

>

> if tog1

> FIFO example1 = new FIFO()

> if tog2

> LFU example1 = new LFU()

> if tog3

> Optimal example1 = new Optimal()

>

> and the same for group 2 example2 and group 3

> example3

>

> I have tried doing this in different ways including

> initiating them in the toggle event handlers but

> because they are not initiated i get errors in the

> rest of my code when calling elements of the class

>

> The only way i have managed to get it to work is to

> initiate the 3 examples with the basic class and then

> change the class depending on which toggle is

> pressed.

>

Yes, and what is wrong with doing it this way?

> Is this the only way of doing this or is there

> another way?

>

> Many thanks for any help

abillconsla at 2007-7-12 10:02:11 > top of Java-index,Java Essentials,New To Java...
# 2
Nothing at all wrong with it, being a relative novice i wasn't sure if it was the correct way of doing it. Many thanks for the quick replyKerry
Kerrancea at 2007-7-12 10:02:11 > top of Java-index,Java Essentials,New To Java...
# 3

Sure. Writing code this way is more versitile because the variable will always hold a reference to the base class, even though the Object is really of one of the extended classes, so you can later change the implementation w/o breaking code. But it does pose a limit as to what methods the Object can access.

abillconsla at 2007-7-12 10:02:11 > top of Java-index,Java Essentials,New To Java...
# 4

Sorry i havent replied for a while i have been trying get this to work but i have not been able to, below is several sections of my code

public Main() {

initComponents();

}

pageGenerator pages = new pageGenerator();

BasicIO example1 = new BasicIO();

BasicIO example2 = new BasicIO();

BasicIO example3 = new BasicIO();

private void togEx1FIFOActionPerformed(java.awt.event.ActionEvent evt) {

togEx3Updated(evt);//these are the 3 event handlers for 3 toggle buttons in the same group

}// and are repeated for example 2 and 3

private void togEx1LFUActionPerformed(java.awt.event.ActionEvent evt) {

togEx3Updated(evt);

}

private void togEx1OptActionPerformed(java.awt.event.ActionEvent evt) {

togEx3Updated(evt);

}

public void togEx1Updated(java.awt.event.ActionEvent evt){//called after action on toggle button for example 1

if (evt.getActionCommand().equals("FIFO")){// this repeated for example 2 and 3

FIFO example1 = new FIFO();

}

if (evt.getActionCommand().equals("LFU")){

LFU example1 = new LFU();

}

if (evt.getActionCommand().equals("Optimal")){

Optimal example1 = new Optimal();

}

}

private void cmdNextActionPerformed(java.awt.event.ActionEvent evt) {

example1.updatePageArray(Integer.toString(pages.getCurrentPage()));

example2.updatePageArray(Integer.toString(pages.getCurrentPage()));

example3.updatePageArray(Integer.toString(pages.getCurrentPage()));

example1.writeTables(tbl1Stack, tbl1);

example2.writeTables(tbl2Stack, tbl2);

example3.writeTables(tbl3Stack,tbl3);

lblFaults1.setText("Page faults =" + example1.getFaults());

lblFaults2.setText("Page faults =" + example2.getFaults());

lblFaults3.setText("Page faults =" + example3.getFaults());

updatePageHistory();

pages.setNewPage();

pages.writeHoldingTables(tblHolding);

}

I was hoping with this to change, say, example 3 from basicIO to LFU on press of the relevant toggle button, then when cmdNext is pressed the update page array method would be the one from the LFU classs and not the BasicIO class

Although at first it appeared to be doing this on closer testing it does not.

Should this be working in this way?

If not can you suggest an alternative way of writing this?

Thanks again for your time

Kerrancea at 2007-7-12 10:02:12 > top of Java-index,Java Essentials,New To Java...
# 5

I must confess that I really do not understand or even have a clue what you are trying to do with the code you posted. Please don't take that the wrong way, I don't in any way mean that insultingly - just take it at face value ... I don't get it.

This bit here though I can at least comment on

public Main() {

initComponents();

}

// The following four lines of code appear to just be sticking out in

// nowhereland ... did you mean to have them as part of the constuctor?

pageGenerator pages = new pageGenerator();

BasicIO example1 = new BasicIO();

BasicIO example2 = new BasicIO();

BasicIO example3 = new BasicIO();

private void togEx1FIFO ...

Also, why are you passing ActionEvents into methods? I am not suggesting that one should never do this ... it is just unclear why you are doing it. Why not just let the actionPerformed methods do your processing?

abillconsla at 2007-7-12 10:02:12 > top of Java-index,Java Essentials,New To Java...
# 6

Not taken the wrong way at all I should have posted the whole code instead of trying to cut it to the relevant parts, in answer to your queries yes the 4 lines of code are part of the constructor they got moved around when i was copying and cutting the work, also i originally had the action events doing the processing

i.e private void togEx1FIFOActionPerformed(java.awt.event.ActionEvent evt) {

FIFO example1 = new FIFO();

}

but moved it into a method when it didn't work.

I will try to explain what i am doing.

I am designing a paging algorithm comparator. The top of the form has 3 groups of toggle buttons and 3 combo boxes one per group. Each group of toggle buttons has 3 buttons, one for FIFO, one for LFU and one for Optimal.

The user picks an algorithm for each example then sets the number of page frames. as shown here

http://img370.imageshack.us/img370/4595/clipboard01io0.png

All my algorithms work ok if i hard code them in the constructor, what i would like to do is enable the user to pick from the different algorithms and page frame sizes and compare them against each other. To do this i need to be able to set the class of example 1, 2, and 3 dependent upon the selection of the toggle button groups, this is where i am having difficulties, without initializing the classes in the constructor the rest of the methods called from each class wont work and ultimately the project wont run, but how can i initialize each class when i don't know which one it will be, i was hoping to 'change' the class later but it doesn't appear to be working.

Kerrancea at 2007-7-12 10:02:12 > top of Java-index,Java Essentials,New To Java...
# 7

The problem is in your togEx1Updated method. This method declares new variables called example1 and does not update the class member example1.

Try the following.

public void togEx1Updated(java.awt.event.ActionEvent evt){//called after action on toggle button for example 1

if (evt.getActionCommand().equals("FIFO")){// this repeated for example 2 and 3

example1 = new FIFO();

}

if (evt.getActionCommand().equals("LFU")){

example1 = new LFU();

}

if (evt.getActionCommand().equals("Optimal")){

example1 = new Optimal();

}

}

gte42ea at 2007-7-12 10:02:12 > top of Java-index,Java Essentials,New To Java...
# 8
Thats the one, thank you both for your time and help
Kerrancea at 2007-7-12 10:02:12 > top of Java-index,Java Essentials,New To Java...
# 9
Sure.
abillconsla at 2007-7-12 10:02:12 > top of Java-index,Java Essentials,New To Java...