ExpTrend.java 0100640 0002037 0000025 00000003517 07243273370 013360 0 ustar pmelby sfiguest import corejava.*;
class ExpTrend
{ public ExpTrend(Position x, double d, double p, double c, double l)
{ equity = x;
weight = d;
newp = p;
coeff = c;
margin = l;
int i;
double price;
double oldprice;
double expectation;
} // end ExpTrend init
public void setPrice(double newprice)
{ oldprice = newprice;
price = newprice;
expectation = 0.0;
}
public void updatePrice(double newprice)
{
oldprice = price;
price = newprice;
}
public void updateMargin(double newmargin)
{ margin = newmargin;
}
public void updatePosition(double order)
{ equity.updatePosition(order, price);
}
public void printoutWealth()
{ equity.printWealth(price);
}
public double getProfits()
{ double profits;
double holdings;
double money;
double wealth;
double initmoney = Math.exp(8.0);
holdings = equity.getHoldings();
money = equity.getMoney();
wealth = money + Math.exp(price)*holdings;
profits = (wealth - initmoney)/initmoney;
return profits;
}
public double placeOrder()
{ double order;
double wants;
double realequity;
realequity = equity.getHoldings();
double money = equity.getMoney();
double w = money + Math.exp(price)*realequity;
w = w/price;
expectation = (1.0-weight)*expectation + weight*(price-oldprice);
wants = coeff*(expectation);
//wants = coeff*MathUtil.tanh(2.0*expectation);
//wants = w*MathUtil.tanh(2.0*expectation);
order = wants - realequity;
return order;
} // end placeOrder
private Position equity;
private double weight;
private double newp;
private double coeff;
private double margin;
private double price;
private double oldprice;
private int i;
private double expectation;
} // end ValueInvestor class
MarketMaker.java 0100640 0002037 0000025 00000002742 07243273373 014034 0 ustar pmelby sfiguest class MarketMaker
{ public MarketMaker(double l, Position x, double p, double k)
{equity = x;
liquidity = l;
price = p;
kickback = k;
initialliquidity = liquidity;
} //end MarketMaker init
public double getPrice(double totalorders)
{ double newprice;
newprice = price + (1.0/liquidity)*totalorders;
return newprice;
} //end getPrice
public void updatePrice(double newprice)
{ price = newprice;
}
public void updatePosition(double totalorder)
{ totalorder = -1.0*totalorder;
equity.updatePosition(totalorder, price);
}
public double placeOrder()
{ double holdings; double order;
holdings = equity.getHoldings();
order = -1.0*kickback*holdings;
return order;
}
public void printoutWealth()
{ equity.printWealth(price);
}
public double getProfits()
{ double profits;
double holdings;
double money;
double wealth;
double initmoney = Math.exp(8.0);
holdings = equity.getHoldings();
money = equity.getMoney();
wealth = money + Math.exp(price)*holdings;
profits = (wealth - initmoney)/initmoney;
return profits;
}
public void changeLiquidity(double volume)
{ liquidity = initialliquidity/(0.001*volume+1.0);
}
private double orders;
private Position equity;
private double liquidity;
private double price;
private double kickback;
private double initialliquidity;
private double initequity;
} // end MarketMaker class
MarketSim6.java 0100640 0002037 0000025 00000026264 07243273374 013621 0 ustar pmelby sfiguest import java.lang.*;
import corejava.*;
import java.io.*;
import java.util.*;
public class MarketSim6
{ public static void main(String args[])
{ int i; int j; int N=2; int totaltime = 10000;
// double[] orders; double[] actualorder;
Position[] standing = new Position[N];
Position[] trendstanding = new Position[N];
double[] orders = new double[N];
double[] actualorder = new double[N];
double[] b = new double[totaltime/2];
double[] a = new double[totaltime/2];
double[] aval = new double[totaltime/2];
double[] bval = new double[totaltime/2];
double dcval = 0.0;
double[] valpow = new double[totaltime/2];
double[] acorr = new double[totaltime/2];
double[] bcorr = new double[totaltime/2];
double[] powerspectrum = new double[totaltime/2];
double pricenoise = 0.00;
double[] pricenoisea = new double[totaltime/2];
double[] pricenoiseb = new double[totaltime/2];
double[] pricepow = new double[totaltime/2];
double[] smoothpricepow = new double[totaltime/2];
double[] pricefilter = new double[totaltime/2];
double freqindex = 0.0;
double filteradd = 0.0;
double periodindex;
double oldprice = 1.0;
double dc; double inside;
double pi = Math.PI; // 4.0*Math.atan(1.0);
double noiselevel = 0.0;
double makerorder;
double volatility = 0.0; double volume;
int fouriertop = (totaltime - 1)/2;
int fouriertop2 = (fouriertop - 1)/2;
double[] autopower = new double[totaltime/2];
double[] corrcalc = new double[totaltime/2];
double lambda = 10000.0;
double c_constant = 800.0;
double c_constant2 = 0.0;
double alpha = 2*c_constant/lambda;
double beta = 0.0;
double mispricing;
double noiseorder;
double[] mispricinga = new double[totaltime/2];
double[] mispricingb = new double[totaltime/2];
double[] mispricingcorr = new double[totaltime/2];
double[] mispower = new double[totaltime/2];
double[] smoothvalp = new double[totaltime/2];
double[] smoothretp = new double[totaltime/2];
double[] filter = new double[totaltime/2];
double[] volumea = new double[totaltime/2];
double[] volumeb = new double[totaltime/2];
double[] volumecorr = new double[totaltime/2];
double[] volumepow = new double[totaltime/2];
double misdc = 0.0; double miscorrdc = 0.0; double volumedc = 0.0; double volumecorrdc = 0.0;
int k;
double corrdc=0.0;
int[] delay = new int[N];
delay[0] = 10;
delay[1] = 10;
double[] trendorders = new double[N];
double[] actualtrendorder = new double[N];
double[] trendprofits = new double[N];
double[] valueprofits = new double[N];
double makerprofits;
double oldreturns = 0.0;
Random randnum = new Random(12381);
for (j=1; j <= fouriertop; j++)
{a[j] = 0.0; b[j] = 0.0;acorr[j] = 0.0;bcorr[j] = 0.0; autopower[j] = 0.0;
aval[j] = 0.0; bval[j] = 0.0; smoothretp[j] = 0.0; smoothvalp[j] = 0.0; filter[j] = 0.0;
mispricinga[j] = 0.0;mispricingb[j] = 0.0; mispricingcorr[j] = 0.0;mispower[j] = 0.0;
pricenoisea[j] = 0.0; pricenoiseb[j] = 0.0; pricepow[j] = 0.0; smoothpricepow[j] = 0.0; pricefilter[j] = 0.0;
volumea[j] = 0.0;volumeb[j] = 0.0;volumecorr[j] = 0.0; volumepow[j] = 0.0;
}
dc = 0.0;
for (j = 0; j < N; j++)
{standing[j] = new Position(0.0,0.0,8.0);
trendstanding[j] = new Position(0.0,0.0,8.0);
}
Position makerstanding = new Position(0.0, 0.0, 8.0);
// Position trendstanding = new Position(0.0,0.0, 8.0);
double price = 1.0; double newprice;
double[] value = new double[N];
double totalorder; double valueadd;
for (j = 0; j < N; j++)
{ value[j] = 1.0;
}
// ValueInvestor trader = new ValueInvestor(standing, value, price, 10.0, 0.5);
ValueInvestor[] trader = new ValueInvestor[N];
for (j=0; j < N; j++)
{ trader[j] = new ValueInvestor(standing[j], value[j], price, c_constant, 0.99999);
}
TrendFollower[] trender = new TrendFollower[N];
//ExpTrend[] trender = new ExpTrend[N];
for (j=0; j < N; j++)
{
// trender[j] = new ExpTrend(trendstanding[j], 1.0, price, c_constant2, 0.9999);
trender[j] = new TrendFollower(trendstanding[j], delay[j], price, c_constant2, 0.99999);
trender[j].setPrice(price);
}
MarketMaker maker = new MarketMaker(lambda, makerstanding, price, beta);
for (i=0; i < totaltime; i++)
{ totalorder = 0;
volume = 0.0;
mispricing = price - value[1];
for(j = 0; j < N; j++)
{orders[j] = trader[j].placeOrder();
trendorders[j] = trender[j].placeOrder();
totalorder = totalorder + orders[j] + trendorders[j];
} // end for j
makerorder = maker.placeOrder();
totalorder = totalorder + makerorder;
newprice = maker.getPrice(totalorder);
pricenoise = MathUtil.gRand(noiselevel, randnum);
newprice = newprice + pricenoise;
oldreturns = volatility;
volatility = (newprice - price);
valueadd = MathUtil.gRand(0.01, randnum);
maker.updatePrice(newprice);
totalorder = totalorder - makerorder;
maker.updatePosition(totalorder);
for(j = 0; j < N; j++)
{
value[j] = value[j] + valueadd;
trader[j].updatePrice(newprice);
trader[j].updateValue(value[j]);
trader[j].updatePosition(orders[j]);
trender[j].updatePrice(newprice);
trender[j].updatePosition(trendorders[j]);
if (trendorders[j] > 0){volume = volume + trendorders[j];}
if (orders[j] > 0){volume = volume + orders[j];}
} // end for j
if (totalorder < 0){volume = volume - totalorder;}
Format.printf("%6d ", i);
maker.printoutWealth();
trader[0].printoutWealth();
trader[1].printoutWealth();
//Format.printf(" %10.6f", orders[1]);
trender[0].printoutWealth();
//Format.printf(" %10.6f", trendorders[0]);
trender[1].printoutWealth();
Format.printf(" %10.6f", trendorders[1]);
trendprofits[0] = trender[0].getProfits();
trendprofits[1] = trender[1].getProfits();
valueprofits[0] = trader[0].getProfits();
valueprofits[1] = trader[1].getProfits();
makerprofits = maker.getProfits();
oldprice = price;
price = newprice;
Format.printf(" %10.6f", valueprofits[0]);
Format.printf(" %10.6f", valueprofits[1]);
Format.printf(" %10.6f", trendprofits[0]);
Format.printf(" %10.6f", trendprofits[1]);
Format.printf(" %10.6f", makerprofits);
Format.printf(" %10.6f", price);
Format.printf(" %10.6f ", value[0]);
Format.printf(" %10.6f ", value[1]);
Format.printf(" %10.6f ", volatility);
Format.printf(" %10.6f ", oldreturns);
Format.printf(" %10.6f ", oldprice);
Format.printf(" %10.4f", volume);
Format.printf(" %10.6f \n", mispricing);
// Fourier Transform of volatility
dc = dc + (1.0/totaltime)*volatility;
misdc = misdc + (1.0/totaltime)*mispricing;
dcval = dcval + (1.0/totaltime)*valueadd;
volumedc = volumedc + (1.0/totaltime)*volume;
for (j = 1; j <= fouriertop; j++)
{ inside = 2.0*pi*j*i/totaltime;
b[j] = b[j] + (2.0/totaltime)*volatility*Math.cos(inside);
a[j] = a[j] + (2.0/totaltime)*volatility*Math.sin(inside);
mispricinga[j] = mispricinga[j] + (2.0/totaltime)*mispricing*Math.sin(inside);
mispricingb[j] = mispricingb[j] + (2.0/totaltime)*mispricing*Math.cos(inside);
aval[j] = aval[j] + (2.0/totaltime)*valueadd*Math.sin(inside);
bval[j] = bval[j] + (2.0/totaltime)*valueadd*Math.cos(inside);
pricenoisea[j] = pricenoisea[j] + (2.0/totaltime)*pricenoise*Math.sin(inside);
pricenoiseb[j] = pricenoiseb[j] + (2.0/totaltime)*pricenoise*Math.cos(inside);
volumea[j] = volumea[j] + (2.0/totaltime)*volume*Math.sin(inside);
volumeb[j] = volumeb[j] + (2.0/totaltime)*volume*Math.cos(inside);
} //end for j (Fourier)
} //end for i
// Power spectrum of Fourier Transform
try{
FileOutputStream fout = new FileOutputStream("powerspec");
PrintStream powerout = new PrintStream(fout);
FileOutputStream fout2 = new FileOutputStream("autocorr");
PrintStream autoout = new PrintStream(fout2);
//corrdc = corrdc + dc*dc/totaltime;
miscorrdc = miscorrdc + misdc*misdc/fouriertop;
volumecorrdc = volumecorrdc + volumedc*volumedc/fouriertop;
for (j=1; j <= fouriertop; j++)
{ powerspectrum[j] = a[j]*a[j] + b[j]*b[j];
valpow[j] = aval[j]*aval[j] + bval[j]*bval[j];
pricepow[j] = pricenoisea[j]*pricenoisea[j] + pricenoiseb[j]*pricenoiseb[j];
mispower[j] = mispricinga[j]*mispricinga[j] + mispricingb[j]*mispricingb[j];
volumepow[j] = volumea[j]*volumea[j] + volumeb[j]*volumeb[j];
corrdc = corrdc + (1.0/fouriertop)*powerspectrum[j];
miscorrdc = miscorrdc + (1.0/fouriertop)*mispower[j];
volumecorrdc = volumecorrdc + (1.0/fouriertop)*volumepow[j];
for (k=1; k <=fouriertop2; k++)
{inside = -2.0*pi*k*j/totaltime;
acorr[k] = acorr[k] + (2.0/totaltime)*powerspectrum[j]*Math.sin(inside);
bcorr[k] = bcorr[k] + (2.0/totaltime)*powerspectrum[j]*Math.cos(inside);
mispricingcorr[k] = mispricingcorr[k] + (2.0/fouriertop)*mispower[j]*Math.cos(inside);
volumecorr[k] = volumecorr[k] + (2.0/fouriertop)*volumepow[j]*Math.cos(inside);
}
}
corrdc = corrdc + dc*dc/fouriertop;
// for (k=1; k <=fouriertop2; k++)
// { bcorr[k] = bcorr[k] + (2.0/fouriertop)*dc*dc;
// }
for (j=1; j < fouriertop/15 ; j++)
{ smoothvalp[j] = 0.0;
smoothretp[j] = 0.0;
smoothpricepow[j] = 0.0;
for (k = 1; k < 15; k++)
{ smoothvalp[j] = smoothvalp[j] + valpow[15*j + k];
smoothretp[j] = smoothretp[j] + powerspectrum[15*j + k];
smoothpricepow[j] = smoothpricepow[j] + pricepow[15*j + k];
}
pricefilter[j] = smoothretp[j]/smoothpricepow[j];
filter[j] = smoothretp[j]/smoothvalp[j];
filteradd = filteradd + pricefilter[j]/fouriertop;
smoothretp[j] /= 15.0;
smoothpricepow[j] /= 15.0;
smoothvalp[j] /= 15.0;
} // end j (smoothing)
for (j=1; j < fouriertop; j++)
{
freqindex = 12.0*j/totaltime;
periodindex = 1.0/freqindex;
powerout.println(" " + j + " " + powerspectrum[j] + " " + a[j] + " " + b[j] + " " + mispower[j] + " " + valpow[j] + " " + smoothretp[j] + " " + smoothvalp[j] + " " + filter[j] + " " + pricefilter[j] + " " + smoothpricepow[j] + " " + freqindex + " " + filteradd + " " + periodindex + " " + volumepow[j]);
}
autoout.println( " 0 " + corrdc*corrdc + " 0.0 1.0 1.0 1.0 1.0 1.0");
corrcalc[0] = 1.0;
corrcalc[1] = (1.0 - alpha)/(1.0-alpha*(1.0-beta));
for (j=1; j <= fouriertop2; j++)
{
acorr[j] = acorr[j]/corrdc;
bcorr[j] = bcorr[j]/corrdc;
mispricingcorr[j] = mispricingcorr[j]/miscorrdc;
volumecorr[j] = volumecorr[j]/volumecorrdc;
corrcalc[j+1] = (1.0 - alpha)*corrcalc[j] + alpha*(1.0 - beta)*corrcalc[j-1];
autopower[j] = acorr[j]*acorr[j] + bcorr[j]*bcorr[j];
autoout.println(" " + j + " " + autopower[j] + " " + acorr[j] + " " + bcorr[j] + " " + corrdc + " " + corrcalc[j] + " " + mispricingcorr[j] + " " + volumecorr[j]);
}
}
catch (IOException e) {
System.out.println("Error opening file: " + e);
System.exit(1);
}
} //end main
} //end MarketSim MathUtil.java 0100640 0002037 0000025 00000001317 07243273402 013346 0 ustar pmelby sfiguest import java.lang.*;
import java.util.*;
class MathUtil
{
public static double gRand(double w, Random randnum)
{
int i = 0;
double x = 0;
double y;
double arg;
double yarg;
while (i ==0)
{
x = 6*(randnum.nextDouble()-0.5);
y = randnum.nextDouble();
arg = -1.0*x*x/2.0;
yarg = Math.pow(Math.E, arg);
if(y <= yarg) {i = 1;}
} //end while
x = w*x;
return x;
}
public static double tanh(double x)
{
double exppos;
double expneg;
double hyptan;
exppos = Math.exp(x);
expneg = Math.exp(-1.0*x);
hyptan = (exppos - expneg)/(exppos + expneg);
return hyptan;
}
} // end class MathUtil Position.java 0100640 0002037 0000025 00000001467 07243273405 013434 0 ustar pmelby sfiguest import java.util.*;
import java.lang.*;
import corejava.*;
class Position
{ public Position(double x1, double x2, double m)
{ holdings = x1;
oldholdings = x2;
money = m;
money = Math.exp(money);
}
public double getHoldings()
{ return holdings;
}
public double getMoney()
{ return money;
}
public void updatePosition(double order, double price)
{ money = money - (order)*Math.exp(price);
oldholdings = holdings;
holdings = holdings + order;
}
public void printWealth(double price)
{ double wealth;
wealth = holdings*Math.exp(price) + money;
Format.printf(" %10.2f ", wealth);
Format.printf(" %10.2f ", holdings);
Format.printf(" %10.2f ", money);
}
private double holdings;
private double oldholdings;
private double money;
} TrendFollower.java 0100640 0002037 0000025 00000003403 07243273413 014405 0 ustar pmelby sfiguest import corejava.*;
class TrendFollower
{ public TrendFollower(Position x, int d, double p, double c, double l)
{ equity = x;
delay = d;
newp = p;
coeff = c;
margin = l;
int i;
double price[] = new double[1000];
// for (i=0; i < 1000; i++){price[i] = newp;}
} // end ValueInvestor init
public void setPrice(double p)
{ for (i=0; i < 1000; i++){price[i] = p;}
}
public void updatePrice(double newprice)
{
for (i=0; i <= delay; i++)
{ price[i] = price[i+1];
}
price[delay] = newprice;
}
public void updateMargin(double newmargin)
{ margin = newmargin;
}
public void updatePosition(double order)
{ double priceindex;
priceindex = price[delay];
equity.updatePosition(order, priceindex);
}
public void printoutWealth()
{ equity.printWealth(price[delay]);
Format.printf(" %10.6f", price[delay]);
Format.printf(" %10.6f", price[0]);
}
public double getProfits()
{ double profits;
double holdings;
double money;
double wealth;
double initmoney = Math.exp(8.0);
holdings = equity.getHoldings();
money = equity.getMoney();
wealth = money + Math.exp(price[delay])*holdings;
profits = (wealth - initmoney)/initmoney;
return profits;
}
public double placeOrder()
{ double order;
double wants;
double realequity;
realequity = equity.getHoldings();
wants = coeff*(price[delay] - price[0]);
order = wants - realequity;
return order;
} // end placeOrder
private Position equity;
private int delay;
private double newp;
private double coeff;
private double margin;
private double[] price = new double[1000];
private int i;
} // end ValueInvestor class
ValueInvestor.java 0100640 0002037 0000025 00000003066 07243273414 014433 0 ustar pmelby sfiguest class ValueInvestor
{ public ValueInvestor(Position x, double v, double p, double c, double l)
{ equity = x;
value = v;
price = p;
coeff = c;
margin = l;
} // end ValueInvestor init
public void updatePrice(double newprice)
{ price = newprice;
}
public void updateValue(double newvalue)
{ value = newvalue;
}
public void updateMargin(double newmargin)
{ margin = newmargin;
}
public void updatePosition(double order)
{ equity.updatePosition(order, price);
}
public void printoutWealth()
{ equity.printWealth(price);
}
public double getProfits()
{ double profits;
double holdings;
double money;
double wealthy;
double initmoney = Math.exp(8.0);
holdings = equity.getHoldings();
money = equity.getMoney();
wealthy = money + Math.exp(price)*holdings;
profits = ( wealthy - initmoney)/initmoney;
return profits;
}
public double placeOrder()
{ double order;
double wants;
double realequity = equity.getHoldings();
double w;
double money = equity.getMoney();
w = money + Math.exp(price)*realequity;
w = w/price;
wants = coeff*(value - price);
//wants = coeff*MathUtil.tanh(2.0*(value-price));
//wants = w*MathUtil.tanh(2.0*(value-price));
order = wants - realequity;
return order;
} // end placeOrder
private Position equity;
private double value;
private double price;
private double coeff;
private double margin;
private double initequity;
} // end ValueInvestor class
corejava/ 0040750 0002037 0000025 00000000000 07243273447 012557 5 ustar pmelby sfiguest corejava/Assertion.java 0100640 0002037 0000025 00000005740 07243273447 015375 0 ustar pmelby sfiguest package corejava;
/**
A class for assertion checking
@version 1.00 10 Oct 1998
@author Cay Horstmann
*/
public class Assertion
{
/**
Check an assertion
@param b the condition to check
@param s a string describing the check
@throws Assertion.Failure if condition not true
*/
public static void check(boolean b, String s)
{ if (doCheck && !b)
throw new Failure(s);
}
/**
Check an assertion
@param b the condition to check
@throws Assertion.Failure if condition not true
*/
public static void check(boolean b)
{ if (doCheck && !b)
throw new Failure();
}
/**
Check an assertion
@param obj an object to check
@param s a string describing the check
@throws Assertion.Failure if object is null
*/
public static void check(Object obj, String s)
{ if (doCheck && obj == null)
throw new Failure(s);
}
/**
Check an assertion
@param obj an object to check
@throws Assertion.Failure if object is null
*/
public static void check(Object obj)
{ if (doCheck && obj == null)
throw new Failure();
}
/**
Check an assertion
@param x a number
@param s a string describing the check
@throws Assertion.Failure if number is 0
*/
public static void check(double x, String s)
{ if (doCheck && x == 0)
throw new Failure(s);
}
/**
Check an assertion
@param x a number
@throws Assertion.Failure if number is 0
*/
public static void check(double x)
{ if (doCheck && x == 0)
throw new Failure();
}
/**
Check an assertion
@param x a number
@param s a string describing the check
@throws Assertion.Failure if number is 0
*/
public static void check(long x, String s)
{ if (doCheck && x == 0)
throw new Failure(s);
}
/**
Check an assertion
@param x a number
@throws Assertion.Failure if number is 0
*/
public static void check(long x)
{ if (doCheck && x == 0)
throw new Failure();
}
/**
Turn checking on or off
@param c true to turn checking on, false to turn checking off
*/
public static void setCheck(boolean c)
{ doCheck = c;
}
private static boolean doCheck = true;
/**
test stub
*/
public static void main(String[] args)
{ Assertion.check(args);
Assertion.check(args.length, "No command line arguments");
}
/**
A class for reporting assertion failures
*/
public static class Failure extends RuntimeException
{ public Failure()
{ super("Assertion failed");
}
/**
@param gripe a description of the reason for the failure
*/
public Failure(String gripe)
{ super(gripe);
}
}
}
corejava/Console.java 0100640 0002037 0000025 00000005122 07243273447 015022 0 ustar pmelby sfiguest package corejava;
/**
An easy interface to read numbers and strings from
standard input
@version 1.10 10 Mar 1997
@author Cay Horstmann
*/
public class Console
{ /**
print a prompt on the console but don't print a newline
@param prompt the prompt string to display
*/
public static void printPrompt(String prompt)
{ System.out.print(prompt + " ");
System.out.flush();
}
/**
read a string from the console. The string is
terminated by a newline
@return the input string (without the newline)
*/
public static String readLine()
{ int ch;
String r = "";
boolean done = false;
while (!done)
{ try
{ ch = System.in.read();
if (ch < 0 || (char)ch == '\n')
done = true;
else if ((char)ch != '\r') // weird--it used to do \r\n translation
r = r + (char) ch;
}
catch(java.io.IOException e)
{ done = true;
}
}
return r;
}
/**
read a string from the console. The string is
terminated by a newline
@param prompt the prompt string to display
@return the input string (without the newline)
*/
public static String readLine(String prompt)
{ printPrompt(prompt);
return readLine();
}
/**
read an integer from the console. The input is
terminated by a newline
@param prompt the prompt string to display
@return the input value as an int
@exception NumberFormatException if bad input
*/
public static int readInt(String prompt)
{ while(true)
{ printPrompt(prompt);
try
{ return Integer.valueOf
(readLine().trim()).intValue();
} catch(NumberFormatException e)
{ System.out.println
("Not an integer. Please try again!");
}
}
}
/**
read a floating point number from the console.
The input is terminated by a newline
@param prompt the prompt string to display
@return the input value as a double
@exception NumberFormatException if bad input
*/
public static double readDouble(String prompt)
{ while(true)
{ printPrompt(prompt);
try
{ return Double.parseDouble(readLine().trim());
} catch(NumberFormatException e)
{ System.out.println
("Not a floating point number. Please try again!");
}
}
}
}
corejava/Day.java 0100640 0002037 0000025 00000013737 07243273447 014150 0 ustar pmelby sfiguest package corejava;
import java.util.*;
import java.io.*;
/**
Stores dates and perform date arithmetic.
This is another date class, but more convenient that
java.util.Date or java.util.Calendar
@version 1.20 5 Oct 1998
@author Cay Horstmann
*/
public class Day implements Cloneable, Serializable
{ /**
Constructs today's date
*/
public Day()
{ GregorianCalendar todaysDate
= new GregorianCalendar();
year = todaysDate.get(Calendar.YEAR);
month = todaysDate.get(Calendar.MONTH) + 1;
day = todaysDate.get(Calendar.DAY_OF_MONTH);
}
/**
Constructs a specific date
@param yyyy year (full year, e.g., 1996,
not starting from 1900)
@param m month
@param d day
@exception IllegalArgumentException if yyyy m d not a
valid date
*/
public Day(int yyyy, int m, int d)
{ year = yyyy;
month = m;
day = d;
if (!isValid())
throw new IllegalArgumentException();
}
/**
Advances this day by n days. For example.
d.advance(30) adds thirdy days to d
@param n the number of days by which to change this
day (can be < 0)
*/
public void advance(int n)
{ fromJulian(toJulian() + n);
}
/**
Gets the day of the month
@return the day of the month (1...31)
*/
public int getDay()
{ return day;
}
/**
Gets the month
@return the month (1...12)
*/
public int getMonth()
{ return month;
}
/**
Gets the year
@return the year (counting from 0, not from 1900)
*/
public int getYear()
{ return year;
}
/**
Gets the weekday
@return the weekday (address@hidden Day#SUNDAY}, ...,
address@hidden Day#SATURDAY})
*/
public int weekday()
{ return (toJulian() + 1) % 7 + 1;
}
/**
The number of days between this and day parameter
@param b any date
@return the number of days between this and day parameter
and b (> 0 if this day comes after b)
*/
public int daysBetween(Day b)
{ return toJulian() - b.toJulian();
}
/**
A string representation of the day
@return a string representation of the day
*/
public String toString()
{ return "Day[" + year + "," + month + "," + day + "]";
}
/**
Makes a bitwise copy of a Day object
@return a bitwise copy of a Day object
*/
public Object clone()
{ try
{ return super.clone();
} catch (CloneNotSupportedException e)
{ // this shouldn't happen, since we are Cloneable
return null;
}
}
/**
Compares this Day against another object
@param obj another object
@return true if the other object is identical to this Day object
*/
public boolean equals(Object obj)
{ if (!getClass().equals(obj.getClass())) return false;
Day b = (Day)obj;
return day == b.day && month == b.month && year == b.year;
}
/**
Computes the number of days between two dates
@return true iff this is a valid date
*/
private boolean isValid()
{ Day t = new Day();
t.fromJulian(this.toJulian());
return t.day == day && t.month == month
&& t.year == year;
}
/**
@return The Julian day number that begins at noon of
this day
Positive year signifies A.D., negative year B.C.
Remember that the year after 1 B.C. was 1 A.D.
A convenient reference point is that May 23, 1968 noon
is Julian day 2440000.
Julian day 0 is a Monday.
This algorithm is from Press et al., Numerical Recipes
in C, 2nd ed., Cambridge University Press 1992
*/
private int toJulian()
{ int jy = year;
if (year < 0) jy++;
int jm = month;
if (month > 2) jm++;
else
{ jy--;
jm += 13;
}
int jul = (int) (java.lang.Math.floor(365.25 * jy)
+ java.lang.Math.floor(30.6001*jm) + day + 1720995.0);
int IGREG = 15 + 31*(10+12*1582);
// Gregorian Calendar adopted Oct. 15, 1582
if (day + 31 * (month + 12 * year) >= IGREG)
// change over to Gregorian calendar
{ int ja = (int)(0.01 * jy);
jul += 2 - ja + (int)(0.25 * ja);
}
return jul;
}
/**
Converts a Julian day to a calendar date
This algorithm is from Press et al., Numerical Recipes
in C, 2nd ed., Cambridge University Press 1992
@param j the Julian date
*/
private void fromJulian(int j)
{ int ja = j;
int JGREG = 2299161;
/* the Julian date of the adoption of the Gregorian
calendar
*/
if (j >= JGREG)
/* cross-over to Gregorian Calendar produces this
correction
*/
{ int jalpha = (int)(((float)(j - 1867216) - 0.25)
/ 36524.25);
ja += 1 + jalpha - (int)(0.25 * jalpha);
}
int jb = ja + 1524;
int jc = (int)(6680.0 + ((float)(jb-2439870) - 122.1)
/365.25);
int jd = (int)(365 * jc + (0.25 * jc));
int je = (int)((jb - jd)/30.6001);
day = jb - jd - (int)(30.6001 * je);
month = je - 1;
if (month > 12) month -= 12;
year = jc - 4715;
if (month > 2) --year;
if (year <= 0) --year;
}
public static int SUNDAY = 1;
public static int MONDAY = 2;
public static int TUESDAY = 3;
public static int WEDNESDAY = 4;
public static int THURSDAY = 5;
public static int FRIDAY = 6;
public static int SATURDAY = 7;
/** @serial */
private int day;
/** @serial */
private int month;
/** @serial */
private int year;
}
corejava/Format.class 0100640 0002037 0000025 00000020357 07243273447 015043 0 ustar pmelby sfiguest Êþº¾ -i Ñ Êþ Ò ó ô õ ö ÷ ø ù ú û ü !"address@hidden)*+,-./01
H ¢
K ¢
M £
E ¤
M ¤ E ¥
M ¦
M §
M ¨
M ©
E ª
L «
E ¬
E
E ® E ¯
E °
E ±
E ²
E ³
E ´
G µ E ¶ E ·
L ¸ N ¹
E º
E » E ¼
J ½ E ¾ E ¿
F À
E Á
E Â
E Ã
E Ä
E Å
E Æ E Ç E È
E É
L Ê
L Ë
M Ì
L Í
L Î
L Ï E Ð
address@hidden@address@hidden@address@hidden|íhs>éàü¯üF/)Äî±Uå ÿÿÿÿ?óÀÊB+ Cà ý Õ ý ß ý ë
×
Þ
ã
ê
è Ü ä$ Ú% Ú&