Teen Programmers Unite  
 

 

Return to forum top

How to Improve this Program(Dynamic Program)

Posted by DONE [send private reply] at July 06, 2002, 08:33:45 PM

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.
the commands are
in the form of integers e.g. 0000.This is split into
two and passed to command as in:
int do11=00 do22=00
command(do11,do22)
[code]
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");
}
}
[/code]

How do i do this?If you can think of any method that is more efficient kindly let me know.

Posted by sphinX [send private reply] at July 06, 2002, 09:53:32 PM

Perhaps you need to inflict certain restrictions on the instructions, expecting certain behaviour. This is how modern compilers and assemblers work. In the case of assembly language, the instructions either require no operands, are unary (require 1 operand), or binary (require two operands), and a simplified instruction is "instruction <operands seperated by ,>". Once you've passed what instruction it is, I suppose it would be best to define a class for each instruction --- perhaps with a superclass of Instruction holding common information, like whether the instruction is unary or binary etc. Then define each instruction as an inherited class from the Instruction superclass. Then, within each instruction class, you can have a set method, say DoThings, so that it's common to all instructions, so you end up with InstructionName.DoThings(operands);

I don't know if that makes any sense at all, but that's the way I would do it.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.