hi >> can anyone help me to conver this code to GUI ??
/** * @(#)RegistorClass.java * *. * @author * @version 1.00 2011/12/16 */
import java.util.Scanner;
public class RegistorClass {
private int accumulator; private int instructionCounter; private int instructionRegister; private int operationCode; private int operand; private boolean halt; private MemoryClass memory;
// Operation codes
// Input/Output operations. private final int READ = 10; private final int WRITE = 11; // Load/Store operations. private final int LOAD = 20; private final int STORE = 21; // Arithmetic operations. private final int ADD = 30; private final int SUBTRACT = 31; private final int DIVIDE = 32; private final int MULTIPLY = 33; // Transfer-of-control operations. private final int BRANCH = 40; private final int BRANCHNEG = 41; private final int BRANCHZERO = 42; private final int HALT = 43; public RegistorClass(MemoryClass memory1)
{
accumulator = 0; instructionCounter = 0; instructionRegister = 0; operationCode = 0; operand = 0; halt = false; memory = memory1; } public int getOperationCode(int word) { int operationCode = word / 100; return operationCode; }
public int getOperand(int word)
{
int operand = word % 100;
return operand;
}
public boolean executeNextInstruction() {
if (halt) return false; // Read next instruction from memory. instructionRegister = memory.read(instructionCounter); operationCode = getOperationCode(instructionRegister); operand =getOperand(instructionRegister); boolean transferOfControl = false; // execute the instruction. switch (operationCode) { case READ: Scanner scan = new Scanner(System.in); // Read a word from the keyboard. System.out.print("Enter an integer: "); // Write word to memory. memory.write(operand, scan.nextInt()); break; case WRITE: // Read a word from memory. int data = memory.read(operand); // Display the word on the screen if (!halt) System.out.println(data); break; case LOAD: // Load a word from memory into the accumulator. accumulator = memory.read(operand); // accumulator=memory[operand]; break; case STORE: // Store accumulator into memory. memory.write(operand, accumulator); // memory[operand]=accumulator; break; case ADD: // Add word from memory to accumulator. accumulator += memory.read(operand); break; case SUBTRACT: // Subtract word from memory from accumulator. accumulator -= memory.read(operand); break; case DIVIDE: // Divide accumulator by a word loaded from memory. int divisor = memory.read(operand); //divisor=memory[oprand] ; if (!halt) if (divisor == 0) { System.out.println("*** Attempt to divide by zero ***"); } else accumulator /= divisor; break; case MULTIPLY: // Multiply accumulator by a word loaded from memory. accumulator *= memory.read(operand); break; case BRANCH: // Branch to location in memory. instructionCounter = operand; transferOfControl = true; break; case BRANCHNEG: // Branch to location in memory if accumulator is negative. if (accumulator < 0) { instructionCounter = operand; transferOfControl = true; } break;
case BRANCHZERO:
// Branch to location in memory if accumulator is zero. if (accumulator == 0) { instructionCounter = operand; transferOfControl = true; } break; case HALT: System.out.println("*** Simpletron execution terminated ***"); transferOfControl = true; halt = true; break; default: System.out.println("*** Invalid operation code ***"); halt= true; // break; } if (!transferOfControl) instructionCounter++; return !halt; }
public void dump()
{
System.out.printf(" accumulator\t\t\t%+05d\n", accumulator);
System.out.printf("instructionCounter\t\t %02d\n", instructionCounter);
System.out.printf("instructionRegister\t\t%+05d\n", instructionRegister);
System.out.printf("operationCode \t\t\t %02d\n", operationCode);
System.out.printf("operand \t\t\t\t %02d\n", operand);
}
public static void main(String[] args)
{ System.out.println("*** Welcome to Simpletron! ***"); System.out.println("*** Please enter your program one instruction ***"); System.out.println("*** (or data word) at a time. I will display ***"); System.out.println("*** the location number and a question mark (?) ***"); System.out.println("*** You then type the word for that location. ***"); System.out.println("*** Type -99999 to stop entering your program. ***"); System.out.println(); MachineClass machine = new MachineClass (); machine.inputProgramToMemory(); machine.executeProgram();
} }
class MachineClass {
private MemoryClass memory1;
private RegistorClass registor1 ;
public static final int MEMORY_WORD = 100;
public MachineClass() { memory1=new MemoryClass( MEMORY_WORD); registor1=new RegistorClass(memory1); }
public void inputProgramToMemory()
{
Scanner input =new Scanner(System.in);
int memoryWord=0; final int END_OF_INPUT = -9999; System.out.printf("%02d ? ", memoryWord); int data = input.nextInt(); while(data !=END_OF_INPUT) { memory1.write(memoryWord,data); memoryWord++; System.out.printf("%02d ? ", memoryWord); data = input.nextInt(); } System.out.println("*** Program loading completed ***"); System.out.println(); }
public void computerDump()
{ System.out.println();
System.out.println("REGISTERS:");
registor1.dump();
System.out.println();
System.out.println("MEMORY:");
memory1.dump();
}
// Execute program public void executeProgram() { System.out.println("*** Program execution begins ***"); while ( registor1.executeNextInstruction()) ; computerDump(); }
}
class MemoryClass {
private int[] memory; public MemoryClass(int amountOfMemoryWord )
{ memory = new int[amountOfMemoryWord];
} // Write a value to memory. public void write(int word, int value) { if (word < 0 || word >= memory.length) { System.out.println("*** Attempt to write to a non-existing memory location ***"); } else memory[word] = value; } public int read(int word) { int value = 0; if (word < 0 || word >= memory.length) { System.out.println("*** Attempt to read from a non-existing memory location ***"); } else value = memory[word]; return value; } public void dump() { System.out.print( " "); for (int i = 0; i < 10; i++) System.out.printf("%5d ", i); System.out.println(); for (int i = 0; i < memory.length; i += 10) { System.out.print(" "+ i+ " "); for (int j = 0; j < 10 && i+j < memory.length; j++) System.out.printf("%+05d ", memory[i + j]); //05>>> to print five digits ??? System.out.println(); } } }
this is my Solution < /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication60; import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.*; /**
*
* @author TOSHIBA
*/
public class RegistorClass extends JFrame { * To change this template, choose Tools | Templates
* and open the template in the editor.
*/ // Operation codes { setTitle("SIMPLETRON");
setLayout(new FlowLayout());
accumulator = 0; menubar.add(filemenu);
setJMenuBar(menubar); public int getOperand(int word)
{ public boolean executeNextInstruction()
{ /* switch (operationCode) case BRANCHZERO: } public void dump()
{ public static void main(String[] args) f.setVisible(true); class MachineClass
{ private MemoryClass memory1; public void inputProgramToMemory()
{ } public void computerDump()
{ JFrame frame=new JFrame();
JLabel ob =new JLabel("REGISTERS:"); } class MemoryClass { {
memory = new int[amountOfMemoryWord]; /*
private JFrame frame=new JFrame();
private int accumulator;
private int instructionCounter;
private int instructionRegister;
private int operationCode;
private int operand;
private JTextField accumulator1;
private JTextField instructionCounter1;
private JTextField instructionRegister1;
private JTextField operationCode1;
private JTextField operand1;
JMenu filemenu;
JMenuItem fileItem1,fileItem2,fileItem3,fileItem4,fileItem5,fileItem6,fileItem7,fileItem8,fileItem9,fileItem10,fileItem11,fileItem12;
private boolean halt;
private MemoryClass memory;
// Input/Output operations.
private final int READ = 10;
private final int WRITE = 11;
// Load/Store operations.
private final int LOAD = 20;
private final int STORE = 21;
// Arithmetic operations.
private final int ADD = 30;
private final int SUBTRACT = 31;
private final int DIVIDE = 32;
private final int MULTIPLY = 33;
// Transfer-of-control operations.
private final int BRANCH = 40;
private final int BRANCHNEG = 41;
private final int BRANCHZERO = 42;
private final int HALT = 43;
public RegistorClass(MemoryClass memory1)
instructionCounter = 0;
instructionRegister = 0;
operationCode = 0;
operand = 0;
halt = false;
accumulator1 = new JTextField(20);
instructionCounter1 =new JTextField(20);
instructionRegister1 = new JTextField(20);
operationCode1 = new JTextField(20);
operand1 = new JTextField(20);
memory = memory1;
JPanel g=new JPanel();
add(g,BorderLayout.NORTH);
g. add(accumulator1);
g. add(instructionCounter1);
g. add(instructionRegister1);
g.add( operationCode1);
g.add( operand1);
JMenuBar menubar = new JMenuBar();
filemenu = new JMenu("File");
filemenu.add(new JSeparator());
// JMenu editmenu = new JMenu("Edit");
//.add(new JSeparator());
fileItem1 = new JMenuItem("Read");
fileItem2 = new JMenuItem("Write");
fileItem3 = new JMenuItem("Load");
fileItem3.add(new JSeparator());
fileItem6 = new JMenuItem("Add");
fileItem4 = new JMenuItem("Store");
fileItem5 = new JMenuItem("Subtract");
fileItem3.add(new JSeparator());
fileItem7 = new JMenuItem("Divide");
fileItem8 = new JMenuItem("Multiplay");
fileItem3.add(new JSeparator());
fileItem9 = new JMenuItem("Branch");
fileItem10 = new JMenuItem("Branch_Zero");
fileItem11 = new JMenuItem("Branch_NEG");
fileItem12 = new JMenuItem("Halt");
filemenu.add(fileItem1);
filemenu.add(fileItem2);
filemenu.add(fileItem3);
filemenu.add(fileItem4);
filemenu.add(fileItem5);
filemenu.add(fileItem6);
filemenu.add(fileItem7);
filemenu.add(fileItem8);
filemenu.add(fileItem9);
filemenu.add(fileItem10);
filemenu.add(fileItem11);
filemenu.add(fileItem12);}
public int getOperationCode(int word)
{
int operationCode = word / 100;
return operationCode;
}
int operand = word % 100;
return operand;
} if (halt)
return false;
// Read next instruction from memory.
instructionRegister = memory.read(instructionCounter);
operationCode = getOperationCode(instructionRegister);
operand =getOperand(instructionRegister);
boolean transferOfControl = false;
// execute the instruction.
{
case READ: Scanner scan = new Scanner(System.in);
// Read a word from the keyboard.
System.out.print("Enter an integer: ");
// Write word to memory.
memory.write(operand, scan.nextInt());
break;
case WRITE:
// Read a word from memory.
int data = memory.read(operand);
// Display the word on the screen
if (!halt)
System.out.println(data);
break;
case LOAD:
// Load a word from memory into the accumulator.
accumulator = memory.read(operand); // accumulator=memory[operand];
break;
case STORE:
// Store accumulator into memory.
memory.write(operand, accumulator); // memory[operand]=accumulator;
break;
case ADD:
// Add word from memory to accumulator.
accumulator += memory.read(operand);
break;
case SUBTRACT:
// Subtract word from memory from accumulator.
accumulator -= memory.read(operand);
break;
case DIVIDE:
// Divide accumulator by a word loaded from memory.
int divisor = memory.read(operand); //divisor=memory[oprand] ;
if (!halt)
if (divisor == 0)
{
System.out.println("*** Attempt to divide by zero ***");
}
else accumulator /= divisor;
break;
case MULTIPLY:
// Multiply accumulator by a word loaded from memory.
accumulator *= memory.read(operand);
break;
case BRANCH:
// Branch to location in memory.
instructionCounter = operand;
transferOfControl = true;
break;
case BRANCHNEG:
// Branch to location in memory if accumulator is negative.
if (accumulator < 0)
{
instructionCounter = operand;
transferOfControl = true;
}
break;
// Branch to location in memory if accumulator is zero.
if (accumulator == 0)
{
instructionCounter = operand;
transferOfControl = true;
}
break;
case HALT:
System.out.println("*** Simpletron execution terminated ***");
transferOfControl = true;
halt = true;
break;
default:
System.out.println("*** Invalid operation code ***");
halt= true; // break;
}
if (!transferOfControl)
instructionCounter++; */
fileItem1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{ String s=" ";
JOptionPane.showInputDialog("Enter AN Integer");
int c=Integer.parseInt(s);
memory.write(operand, c);
}
});
fileItem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
// Read a word from memory.
int data = memory.read(operand);
// Display the word on the screen
if (!halt)
JOptionPane.showMessageDialog(null, data);
}
});
fileItem3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
accumulator = memory.read(operand);
}
});
fileItem4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
memory.write(operand, accumulator);
}
});
fileItem5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
accumulator += memory.read(operand);
}
});
fileItem6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
accumulator -= memory.read(operand);
}
});
fileItem7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
accumulator /= memory.read(operand);
}
});
fileItem8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
accumulator *= memory.read(operand);
}
});
fileItem9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
instructionCounter = operand;
}
});
fileItem9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
instructionCounter = operand;
}
});
fileItem10.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (accumulator < 0)
{
instructionCounter = operand;
}
}
});
fileItem11.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (accumulator == 0)
{
instructionCounter = operand;
}
}
});
fileItem11.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("*** Simpletron execution terminated ***");
halt = true;
}
});
return !halt;
/*System.out.printf(" accumulator\t\t\t%+05d\n", accumulator);
System.out.printf("instructionCounter\t\t %02d\n", instructionCounter);
System.out.printf("instructionRegister\t\t%+05d\n", instructionRegister);
System.out.printf("operationCode \t\t\t %02d\n", operationCode);
System.out.printf("operand \t\t\t\t %02d\n", operand);*/
accumulator1.setText(" "+accumulator) ;
instructionCounter1.setText(" "+instructionCounter);
instructionRegister1.setText(" "+instructionRegister);
operationCode1.setText(" "+ operationCode);
operand1.setText(" "+operand); }
{
JFrame ob=new JFrame();
MemoryClass g=new MemoryClass(100);
RegistorClass f=new RegistorClass(g);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,500);
JButton b=new JButton(" Welcom");
ob.add(b);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JTextArea a;
Box box=Box.createHorizontalBox();
String s="*** Welcome to Simpletron! ***"+"\n"+"*** Please enter your program one instruction ***"+
"\n"+"*** (or data word) at a time. I will display ***"+"\n"+"*** the location number and a question mark (?) ***"+
"\n"+"*** You then type the word for that location. ***"+"\n"+"*** Type -99999 to stop entering your program. ***";
a=new JTextArea(s,10,15);
box.add(new JScrollPane(a));
}
});
/*MachineClass machine = new MachineClass ();
machine.inputProgramToMemory();
machine.executeProgram();*/
}
}
private RegistorClass registor1 ;
public static final int MEMORY_WORD = 100;public MachineClass()
{
memory1=new MemoryClass( MEMORY_WORD);
registor1=new RegistorClass(memory1);
}
JFrame frame=new JFrame();
JButton b= new JButton(" input");
frame.add(b);
b.addActionListener(new ActionListener (){ public void actionPerformed(ActionEvent e)
{
Scanner input =new Scanner(System.in);
JPanel ob=new JPanel();
int memoryWord=0;
final int END_OF_INPUT = -9999;
System.out.printf("%02d ? ", memoryWord);
int data = input.nextInt();
while(data !=END_OF_INPUT)
{
memory1.write(memoryWord,data);
memoryWord++;
System.out.printf("%02d ? ", memoryWord);
data = input.nextInt();
}
System.out.println("*** Program loading completed ***");
System.out.println();
}
});
registor1.dump();
frame.add(ob,BorderLayout.SOUTH);
JLabel ob1 =new JLabel("MEMORY:");
frame.add(ob1,BorderLayout.CENTER);
memory1.dump();
} // Execute program
public void executeProgram()
{
JOptionPane.showMessageDialog(registor1, "*** Program execution begins ***");
while ( registor1.executeNextInstruction()) ;
computerDump();
}
private int[] memory;
public MemoryClass(int amountOfMemoryWord )
}
// Write a value to memory.
public void write(int word, int value)
{
if (word < 0 || word >= memory.length)
{
System.out.println("*** Attempt to write to a non-existing memory location ***");
}
else memory[word] = value;
}
public int read(int word)
{
int value = 0;
if (word < 0 || word >= memory.length)
{ System.out.println("*** Attempt to read from a non-existing memory location ***");
}
else value = memory[word];
return value;
}
public void dump()
{
System.out.print( " ");
for (int i = 0; i < 10; i++)
System.out.printf("%5d ", i);
System.out.println();
for (int i = 0; i < memory.length; i += 10)
{
System.out.print(" "+ i+ " ");
for (int j = 0; j < 10 && i+j < memory.length; j++)
System.out.printf("%+05d ", memory[i + j]); //05>>> to print five digits ???
System.out.println();
}
}
}
thanks >>>>
Ads