/*
Author: David Epstein (PSVM)
Name: JJS (JJ System, a built-in utility copied from JJ Classic)
Date: Aug 17, 2003
Summary: Input, Design-by-Contract, Timing utility and Conversions (fyi)
Offline version: The JJS class supplied here for JJ2 is similar in functionality to
the JJS class used in JJ Classic, running Java online. When running
online with JJ Classic the java.io package is not allowed (so input
comes from a TextField) and System.exit is not allowed.
Notes: Prompt idea from KeyboardReader.java by Martin Osborne and Ken Lambert
*/
import java.io.*;
public class JJS
{
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static long controlledStartTime=0; //for timing programs, 0 used for pre-condition to verify that it is set
public static int inputInt(String sPrompt) {
int iValue;
Integer inputAsInteger;
String sInput;
boolean bValidInt;
sInput = ""; // satisfy Java compiler, value never used
iValue = 0; // satisfy Java compiler, value never used
bValidInt = false;
while (true)
{
System.out.print(sPrompt);
try {
sInput = br.readLine();
inputAsInteger = new Integer(sInput);
iValue = inputAsInteger.intValue();
bValidInt = true;
} catch (Exception e){
if (sInput.length()==0) {
System.out.println("EMPTY INPUT: please supply an int value.");
} else if (sInput.trim().length()==0) {
System.out.println("BLANK INPUT: please supply an int value.");
} else {
System.out.println("BAD INPUT: ("+sInput+"), please supply an int value.");
}
}//end try
if (bValidInt) { break; }
}//end while-true loop
return iValue;
}
public static int inputInt(){
return inputInt("");
}
public static double inputDouble(String sPrompt) {
double dValue;
Double inputAsDouble;
String sInput;
boolean bValidDouble;
sInput = ""; // satisfy Java compiler, value never used
dValue = 0.0; // satisfy Java compiler, value never used
bValidDouble = false;
while (true)
{
System.out.print(sPrompt);
try {
sInput = br.readLine();
inputAsDouble = new Double(sInput);
dValue = inputAsDouble.doubleValue();
bValidDouble = true;
} catch (Exception e){
if (sInput.length()==0) {
System.out.println("EMPTY INPUT: please supply a double value.");
} else if (sInput.trim().length()==0) {
System.out.println("BLANK INPUT: please supply a double value.");
} else {
System.out.println("BAD INPUT: ("+sInput+"), please supply a double value.");
}
}//end try
if (bValidDouble) { break; }
}//end while-true loop
return dValue;
}
public static double inputDouble(){
return inputDouble("");
}
public static boolean inputBoolean(String sPrompt) {
boolean bValue;
String sInput;
boolean bValidBoolean;
bValue = false; // satisfy Java compiler, value never used
bValidBoolean = false;
while (true)
{
System.out.print(sPrompt);
try {
sInput = br.readLine(); //cause for try-catch (see below)
if (sInput.length()==0) {
System.out.println("EMPTY INPUT: please supply a boolean value.");
} else if (sInput.trim().length()==0) {
System.out.println("BLANK INPUT: please supply a boolean value.");
} else if (sInput.equalsIgnoreCase("true")) {
bValue = true;
bValidBoolean = true;
} else {
bValue = false;
bValidBoolean = true;
}
} catch (java.io.IOException e){
System.out.println("Completely unexpected IOException in inputBoolean(). This should never happen");
}//end try
if (bValidBoolean) { break; }
}//end while-true loop
return bValue;
}
public static boolean inputBoolean(){
return inputBoolean("");
}
public static String inputString(String sPrompt) {
String sValue;
sValue = ""; // satisfy Java compiler, value never used
System.out.print(sPrompt);
try {
sValue = br.readLine();
} catch (java.io.IOException e){
System.out.println("Completely unexpected IOException in inputString(). This should never happen");
}//end try
return sValue;
}
public static String inputString(){
return inputString("");
}
//these two for timing programs
public static void startTimer() {
controlledStartTime = System.currentTimeMillis();
}
public static int millisSinceStartTimer() {
int result;
JJS.preCheck((controlledStartTime != 0), "Must call JJS.startTimer() before JJS.millisSinceStartTimer()");
result = (int)(System.currentTimeMillis() - controlledStartTime);
return result;
}
//these three for Design-by-Contract
public static void check(boolean b, String s) {
if (b) {
// the check did not bounce
} else {
System.out.println("CHECK FAILED: "+s);
System.exit(0);
}
}
public static void preCheck(boolean b, String s) {
if (b) {
// the preCheck did not bounce
} else {
System.out.println("CONTRACT BROKEN: "+s);
System.exit(0);
}
}
public static void postCheck(boolean b, String s) {
if (b) {
// the postCheck did not bounce
} else {
System.out.println("CONTRACT BROKEN: "+s);
System.exit(0);
}
}
//these are output for those not wanting to teach System.out.
public static void outputString (String s) {
System.out.print(s);
}
public static void outputlnString (String s) {
System.out.println(s);
}
public static void outputInt (int i) {
System.out.print(i);
}
public static void outputlnInt (int i) {
System.out.println(i);
}
public static void outputDouble (double d) {
System.out.print(d);
}
public static void outputlnDouble (double d) {
System.out.println(d);
}
public static void outputBoolean (boolean b) {
System.out.print(b);
}
public static void outputlnBoolean (boolean b) {
System.out.println(b);
}
//the rest are various conversions for
public static String intToString(int x) {
String result;
result = ""+x;
return result;
}
public static String doubleToString(double d) {
String result;
result = ""+d;
return result;
}
public static String booleanToString(boolean b) {
String result;
result = ""+b;
return result;
}
public static int doubleToInt(double d) {
int result;
result = (int)d;
return result;
}
public static double intToDouble(int i) {
double result;
result = (double)i;
return result;
}
public static int stringToInt(String s) throws NumberFormatException {
int result;
Integer i;
//result = 0;
i = new Integer(s);
result = i.intValue();
return result;
}
public static double stringToDouble(String s) throws NumberFormatException {
double result;
Double d;
//result = 0;
d = new Double(s);
result = d.doubleValue();
return result;
}
public static boolean stringToBoolean(String s) {
//according to Java API, "true" ignoring case; otherwise false
boolean result;
result = s.equalsIgnoreCase("true");
return result;
}
public static void main(String[] args)
{
System.out.println("You have successfully loaded the JJS class.");
System.out.println("and now have access to input methods:");
System.out.println(" - JJS.inputInt()");
System.out.println(" - JJS.inputDouble()");
System.out.println(" - JJS.inputBoolean()");
System.out.println(" - JJS.inputString()");
System.out.println("as well as timing methods:");
System.out.println(" - JJS.startTimer()");
System.out.println(" - JJS.millisSinceStartTimer()");
System.out.println("as well as Design-by-Contract methods:");
System.out.println(" - JJS.preCheck()");
System.out.println(" - JJS.postCheck()");
System.out.println(" - JJS.check()");
System.out.println("as well as various conversion methods.");
}
}