In this article, I illustrate the process of writing a simple Java program.
Let’s create a program for a computer that has ten registers (0-9). Each register can hold a value of natural number >=0 and <=999. The computer will process instructions that consist of 3-digit values. These guidelines will be not be stored in the memory but handled one after another.
Following scheme is used to create or execute an instruction:
|1xx||means halt the execution. You can ignore the value of x|
|2dn||means the value of register d = n|
|3dn||means value of register d = value of register d + n|
|4dn||means value of register d = value of register d * n|
|5dn||means value of register d = value of register d / n|
|6ds||means value of register d = value of register s|
|7ds||means value of register d = value of register d + value of register s|
|8dx||means the value of register d = sqrt (value of register d). You can ignore the value of x|
|9ds||means value of register d = (value of register d) S|
|0xx||The system cannot process any instruction that is in the form of ‘0xx’. Show an error like following:Output: Instruction code ‘065’ is not supported by the system.|
Each register is initialized to 000. The contents of the memory are taken from the input and executed one after another. The result is always saved in the reduced format using a modulus of 1000.
We need to take an input of all the instructions that user wants to execute. To reduce the complexity, I will accept input in the form of a long string of instructions where each instruction is separated by a single space.
Our task is to read a string from the user, and repeatedly find a substring of 3 characters to decode and execute instruction until there is no other instruction to be processed.
275 226 373 426 692 672 100
0:000, 1:000, 2:000, 3:000, 4:000, 5:000, 6:000, 7:005, 8:000, 9:000
0:000, 1:000, 2:006, 3:000, 4:000, 5:000, 6:000, 7:005, 8:000, 9:000
0:000, 1:000, 2:006, 3:000, 4:000, 5:000, 6:000, 7:008, 8:000, 9:000
0:000, 1:000, 2:036, 3:000, 4:000, 5:000, 6:000, 7:008, 8:000, 9:000
0:000, 1:000, 2:036, 3:000, 4:000, 5:000, 6:000, 7:008, 8:000, 9:036
0:000, 1:000, 2:036, 3:000, 4:000, 5:000, 6:000, 7:036, 8:000, 9:036
I’ve designed the following diagram to assist with the better understanding of the requirements (Jarosciak, 2018a).
Jarosciak, J. (2018a) Using mathematical methods – Solution Diagram. Unpublished figure
- JDK 8
- IntelliJ IDEA
The Initial Setup
I have created an empty console Java program in IntelliJ IDEA in a package: com.jarosciak, which contained the Main class (Jarosciak, 2018b).
Jarosciak, J. (2018b) Initial Setup. Unpublished figure
The Main class contains String array named ‘register’ that has 0-9 registers initialized with ‘000’, and four methods:
- main method: comprises primary logic
- businessLogic method: includes all logic about processing instruction codes
- echoRegisters method: this method prints register outputs
- postProcessing method: this method allows me to post process generated values
The Main class and four methods it contains is illustrated in Jarosciak (2018c).
Jarosciak, J. (2018c) Main Class. Unpublished figure
The main method (Jarosciak, 2018d) follows the following logic:
- First I reduce the complexity of input by asking the user to input a long string of 3 digit long instructions, where a single space separates each direction, this is achieved by using Scanner.
- Once we have the long string of instructions, I removing leading and trailing whitespace and replace 2 or more empty spaces with a single space
- Next, I split the long series of instructions by single space
- Then I do the error check, to see if any of the instruction is not precisely three digits long
Once the initial input and test are done, I continue with the following process:
- Loop through split array of instructions
- Assign instruction to ‘instructionCode’ string variable
- Print Header – Instruction Code one by one
- Get each letter of instruction where I use substring feature
- At last, I execute business logic by calling the businessLogic method
Jarosciak, J. (2018d) Method: main. Unpublished figure
I decided to keep instruction code business logic in the method I called businessLogic (Jarosciak, 2018e), as it seemed natural to exclude the actual processing algorithm from the main method. Instead of if statements, which were my initial inclination, I decided to opt out for a switch statement, as it makes it easier to read the relatively simple logic.
Jarosciak, J. (2018e) Method: businessLogic. Unpublished figure
The following method is called from the businessLogic method as part of the set up of each of the register arrays.
As we can see illustrated in Jarosciak (2018f), I always save the integer number passed to this method in a reduced format using a modulus of 1000 and also add the leading zeroes to integer value so it’s returned in the 3 digit format back to the register array.
Jarosciak, J. (2018f) Method: postProcessing. Unpublished figure
The echoRegisters method (Jarosciak, 2018g) is a method that prints the content of the registers with “, “ delimiter. To do so, I simply run through the array using for loop.
Jarosciak, J. (2018g) Method: echoRegisters. Unpublished figure
Input and Output Demo
Input and output are illustrated in the console screenshot (Jarosciak, 2018h).
Jarosciak, J. (2018h) Input and Output Demo. Unpublished figure
Input and Output Online Demo (Java Playground using repl.it)