



|
|
P Dunn's Super CS1 Site
|
Programming Challenge Archive
Lab Challenge Archive
Number Machine Application
|
|
Java Application --
NumberMachineApp
// General Information
// --------------------------------------------------
// File: NumberMachineApp.java
// Type: java application file
// Date: Wed Oct 18, 1995
// Name: BLUE
// Line: Interpreter for simple arithmetic commands
// Application Description
// --------------------------------------------------
/*
A simple interpreter which reads an "operator" and
and "integer", performs a "real" computation, and
reports the "real" result.
Establish "current" as a "real" and assign it the
value 0.0. The prompt will consist of the value
of "current" followed by a colon. There are five
commands.
. terminate execution of the interpreter
+ <int> make current ( current + <int> )
- <int> make current ( current - <int> )
* <int> make current ( current * <int> )
/ <int> make current ( current / <int> )
In short, if the user types a period, the program
stops. If not, the value of current is changed.
Sample Session ...
indigo> java NumberMachineApp
0: + 5
5: - 3
2: * 5
10: / 3
3.33333: * 2
6.66667: * 100
666.667: - 212
454.667: / 10
45.4667: .
indigo>
*/
// Required Packages
// --------------------------------------------------
import blue.io.*;
// Application Class
// --------------------------------------------------
class NumberMachineApp
{
// establish current number
static private double current = 0.0;
// declare operation code variable and operand variable+
static private char opCode;
static private int operand;
// the main method
static public void main (String args[])
{
interpreter();
}
// the interpreter
static private void interpreter()
{
// issue the prompt
issuePrompt();
// read the operation code
readOpCode();
// return if "stop" command
if ( opCode == '.' )
{
return;
};
// read operand for arithmetic commands
operand = IO.read_int();
// dispatch to appropriate "action" method
if ( opCode == '+' )
{
processPlus();
};
if ( opCode == '-' )
{
processMinus();
};
if(opCode == '*')
{
processTimes();
};
if(opCode == '/')
{
processDivide();
}
// do it again
interpreter();
};
// issue the prompt
static private void issuePrompt()
{
IO.print(current + ":");
int length = Double.toString(current).length();
for ( int i = 1; i <= ( 10 - length ); i++ )
{
IO.print(" ");
};
}
// read the operation code
static private void readOpCode()
{
char c = IO.read_char();
boolean plus = ( c == '+' );
boolean minus = ( c == '-' );
boolean times = ( c == '*' );
boolean divides = ( c == '/' );
boolean stop = ( c == '.' );
if ( plus || minus || times || divides || stop )
{
opCode = c;
}
else
{
readOpCode();
};
}
// process the plus command
static private void processPlus()
{
current = current + operand;
}
// process the minus command
static private void processMinus()
{
current = current - operand;
}
// process the times command
static private void processTimes()
{
current = current * operand;
}
// process the divides command
static private void processDivide()
{
current = current / operand;
}
}
// Demo
// --------------------------------------------------
/*
[patdunn@columbia nm]$ java NumberMachineApp
0.0: +5
5.0: -3
2.0: +1
3.0: -10
-7.0: +44
37.0: .
[patdunn@columbia nm]$ javac NumberMachineApp.java
[patdunn@columbia nm]$ java NumberMachineApp
0.0: +11
11.0: -1
10.0: /3
3.3333333333333335:*12
40.0: *571
22840.0: /41
557.0731707317074:-1234
-676.9268292682926:/5
-135.38536585365853:.
[patdunn@columbia nm]$
*/
|
|
|