The Artima Developer Community
Sponsored Link

Java Answers Forum
How to Improve this Program(Dynamic Program)

1 reply on 1 page. Most recent reply: Jul 5, 2002 6:48 PM by DAYO

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
DAYO

Posts: 22
Nickname: done
Registered: Jun, 2002

How to Improve this Program(Dynamic Program) Posted: Jul 5, 2002 6:01 AM
Reply to this message Reply
Advertisement
I have written this piece of code that reads the content of an
array of textfield.The content of the textfields represent one of
several instructions.They are read,broken into two parts and they
are looked up in a switch statement and if valid the appropriate
action is executed.Also,i want to be able to add more commands.At the moment i have to add to the switch statement.This works but i was wondering if there is a more efficient way to design it.e.g to create a class for each class instruction and let the program determine (at runtime) which of these classes to execute.And to add a new one all i have to do is create a new class.
How do i do this?If you can think of any method that is more efficient kindly let me know.


private class do implements ActionListener
{ public void actionPerformed(ActionEvent event)
{ for(km=start2;km<Size && Stop==false;km++)
{ String do1=read.substring(0,2);
int do11=(int)(Integer.parseInt(do1));
int rem=read.length()-2+2;
String do2=read.substring(2,rem);
int do22=(int)(Integer.parseInt(do2));
Command(do11,do22);
}
}
}
public void Command(int code1,int code2)
{ switch(code1)
{ case 1:
String copy11=MemoryContent[operand].getText();
calculator2.setText(" "+copy11);
break;
case 2:
int copy31=(int)(Integer.parseInt(MemoryContent[operand].getText ()));
int copy32=(int)(Integer.parseInt(calculator2.getText()));
int sum=copy31+copy32;
//calculator2 is a label
calculator2.setText(sum+"");
break;
case 3:
//do something
break;
..
case n://n indicates any other integer that may be added in future
//do something
break;
default:
error.setText("Invalid Command");

}
}


DAYO

Posts: 22
Nickname: done
Registered: Jun, 2002

Re: How to Improve this Program(Dynamic Program) Posted: Jul 5, 2002 6:48 PM
Reply to this message Reply
P.S:the commands are in the form of integers e.g. 0000.This is split into two sets of integerand passed to command as in:
int do11=00 do22=00
command(do11,do22)

Flat View: This topic has 1 reply on 1 page
Topic: Loop Program Crashes.Why? Previous Topic   Next Topic Topic: copy/cut/paste under macos AND windows.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use