in this code i have logical error
its give me wrong output

import java.util.Scanner;
public class OneFile {
	    static final int MAX = 100;
    static final int READ = 10, WRITE = 11, LOAD = 20, STORE = 21;
    static final int ADD = 30, SUBTRACT = 31, MULTIPLY = 32, DIVIDE = 33;
    static final int BRANCH = 40, BRANCHNEG = 41, BRANCHZERO = 42, HALT = 43, SENTIAL=-99999;
    int memory[] ,accumulator, operand, operationCode, instructionCounter, instructionRegister;
	  public void execution(){
	  	Scanner input=new Scanner(System.in);
	  	memory = new int[MAX];
        accumulator = 0;
        operand = 0;
        operationCode = 0;
        instructionCounter = 0;
        instructionRegister =0;
        int index =0;
        //zeros all memory locations
        for (int i = 0; i < MAX; i++)
            memory[i] = 0;
              System.out.println("*** Welcome to Simpletron! ***\n" +
                "*** Please enter your program one instruction  ***\n" +
                "*** (or data word) at a time into the input    ***\n" +
                "*** text field. I will display the location    ***\n" +
                "*** number and a question mark (?). You then   ***\n" +
                "*** type the word for that location. Press the ***\n" +
                "*** Done button to stop entering your program  ***\n");
                while( true )
{
System.out.printf("%02d ", index);
memory[index] = input.nextInt();
if( memory[index] == -99999 )
break;
else
index++;
System.out.print("\n");
}
System.out.print("*** Program loading completed ***\n" +
"*** Program execution begins ***\n" );
instructionRegister = memory[instructionCounter];
operationCode = instructionRegister / 100;
operand = instructionRegister % 100;
switch( operationCode )
{
case READ:

            System.out.print( "Enter integer value : " );
            memory[index++] = input.nextInt();
            instructionCounter++;
        break;
        case WRITE:
            System.out.println("value for operand " + operand + " = " + memory[operand]);
            instructionCounter++;
        break;
        case LOAD:
            accumulator = memory[operand];
            instructionCounter++;
        break;
        case STORE:
            memory[operand] = accumulator;
            instructionCounter++;
        break;
        case ADD:
            accumulator += memory[operand];
            instructionCounter++;
        break;
        case SUBTRACT:
            accumulator -= memory[operand];
            instructionCounter++;
        break;
        case MULTIPLY:
            accumulator *= memory[operand];
            instructionCounter++;
        break;
        case DIVIDE:
            accumulator /= memory[operand];
            instructionCounter++;
        break;
        case BRANCH:
            instructionCounter = operand;
        break;
        case BRANCHNEG:
            if (accumulator < 0)
                instructionCounter = operand;
            else
                instructionCounter++;
        break;
        case BRANCHZERO:
            if (accumulator == 0)
                instructionCounter = operand;
            else
                instructionCounter++;
        break;
        case HALT:
        System.out.println("***Simpletron execution terminated***");
        break;
}
System.out. println( "\n*************END SIMPLETRON EXECUTION*************\n" );
int i;
  System.out. println( "REGISTERS:\n"+"accumulator = "+accumulator+"\n instructioncounter ="+
  instructionCounter+"\ninstructionregister= "+instructionRegister+
  "\noperationcode ="+operationCode+"\noperand ="+operand );

  System.out. println( "\n\nMEMORY:\n " );

  for ( i = 0; i <= 9; i++ ) {
   System.out. printf( "%5d ", i );
  }
  for ( i = 0; i <MAX; i++ ) {
  if ( i % 10 == 0 ) {
   System.out. printf( "\n%2d ", i );
  }
   System.out. printf( "%+05d ", memory[ i ] );
  }
   System.out. printf( "\n" );
}
	  public static void main(String [] args){
	  	OneFile obj= new OneFile();
	  	obj.execution();
	  }
}

Edited 4 Years Ago by programing: n/a

its give me wrong output

Can you post the output and add some comments to the output saying what is wrong with the output and show what the output should be?

The more you explain, the better we will be able to help.

its should print
Untitled.png - 0.05MB

its not allow me to enter other entegr
like this assembly :

00 ?1007

01 ?1008

02 ?2007

03 ?3008

04 ?2109

05 ?1109

06 ?4300

07 ?0000

08 ?0000

09 ?0000

10 ?-99999

Please post what the program currently prints out when you execute it.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Why did you post the list of numbers and ?
What is that list for?

Can you change your program so that all of the input data for Scanner is given in the Scanner definition? This will make it easier for anyone to test. Change the following statement to have all the answers you want returned when nextInt is called:

Scanner input=new Scanner("1007 1008 12 13 -99999\n"); //System.in);

output :

*** Welcome to Simpletron ***

*** Please enter your program one instruction ***
*** ( or data word ) at a time. I will type the ***
*** location number and a question mark ( ? ). ***
*** You then type the word for that location. ***
*** Type the sentinel -99999 to stop entering ***
*** your program. ***

00 ? 1007
01 ? 1008
02 ? 2007
03 ? 3008
04 ? 2109
05 ? 1109
06 ? 4300
07 ? 0000
08 ? 0000
09 ? 0000
10 ? -99999

************START SIMPLETRON EXECUTION************

Enter an integer: 4
Enter an integer: 4
Contents of 09: 8

*************END SIMPLETRON EXECUTION*************

REGISTERS:
accumulator            +0008
instructioncounter        06
instructionregister    +4300
operationcode             43
operand                   00

MEMORY:
     0     1     2     3     4     5     6     7     8     9
 0 +1007 +1008 +2007 +3008 +2109 +1109 +4300 +0004 +0004 +0008
10 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
20 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
30 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
40 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
50 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
60 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
70 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
80 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
90 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
Press any key to continue

1007
mean 10 refer to read
07 it location

- EXAMPLE

Operation code
 Meaning
 
Input/output operations
   


const int READ = 10;


 Read a word from the keyboard into a specific location in memory.
 


const int WRITE = 11;


 Write a word from a specific location in memory to the screen.
 
Load and store operations
   


const int LOAD = 20;


 Load a word from a specific location in memory into the accumulator.
 


const int STORE = 21;


 Store a word from the accumulator into a specific location in memory.
 
Arithmetic operations
   


const int ADD = 30;


 Add a word from a specific location in memory to the word in the accumulator (leave result in accumulator).
 


const int SUBTRACT = 31;


 Subtract a word from a specific location in memory from the word in the accumulator (leave result in accumulator).
 


const int DIVIDE = 32;


 Divide a word from a specific location in memory into the word in the accumulator (leave result in accumulator).
 


const int MULTIPLY = 33;


 Multiply a word from a specific location in memory by the word in the accumulator (leave result in accumulator).
 
Transfer-of-control operations
   


const int BRANCH = 40;


 Branch to a specific location in memory.
 


const int BRANCHNEG = 41;


 Branch to a specific location in memory if the accumulator is negative.
 


const int BRANCHZERO = 42;


 Branch to a specific location in memory if the accumulator is zero.
 


const int HALT = 43;


 Haltthe program has completed its task.

Change this statement so the Scanner will automatically answer all the questions.

Scanner input=new Scanner("1007 1008 12 13 -99999\n"); //System.in);

Replace the red part with the correct input.

What is wrong with the output you posted above? You need to add some comments to the output to show where the output is wrong.

Edited 4 Years Ago by NormR1: n/a

i can't change Scanner never ,,
and above output its perfect output
my output
false output :...

*** Welcome to Simpletron! ***
*** Please enter your program one instruction  ***
*** (or data word) at a time into the input    ***
*** text field. I will display the location    ***
*** number and a question mark (?). You then   ***
*** type the word for that location. Press the ***
*** Done button to stop entering your program  ***

00 ?1007

01 ?1008

02 ?2007

03 ?3008

04 ?2109

05 ?1109

06 ?4300

07 ?0000

08 ?0000

09 ?0000

10 ?-99999
*** Program loading completed ***
*** Program execution begins ***
Input an integer: 4

4

*************END SIMPLETRON EXECUTION*************

REGISTERS:
accumulator = 0
 instructioncounter =1
instructionregister= 1008
operationcode =10
operand =8


MEMORY:
 
    0     1     2     3     4     5     6     7     8     9 
 0 +1007 +1008 +2007 +3008 +2109 +1109 +4300 +0004 +0000 +0000 
10 -99999 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 
20 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 
30 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 
40 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 
50 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 
60 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 
70 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 
80 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 
90 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 

Process completed.

i can't change Scanner

The changes are for testing. Change it back when you are done testing.

If you want any help with your problem, please change the Scanner as I requested so that the program automatically answers the nextInt calls.

You have NOT shown what is wrong with the output from the program. Unless you explain EXACTLY which of the lines of output that are wrong, how can anyone make suggestions about what to change?

Edited 4 Years Ago by NormR1: n/a

This article has been dead for over six months. Start a new discussion instead.