Java Program Examples

Excel RATE, PV, FV to Java Functions

Here's a Java program that uses a few financial formulas that function like their Excel counterparts, namely the RATE, PV, and FV functions. I compiled this example using the Eclipse 3.6 64 bit IDE on a Macbook Pro. Below you'll find an example of the screen that you will see when you double click on the 'ExcelFinancial.jar included in the project download. Below that is the code. Be advised that it has no error exception code in it and you must enter a string, then a double, an int, then a double or you will get an error. I hope that you find it useful. Feel free to leave comments on my Forum reguarding this example.

Project Link: FinancialFunctions.zip

Enter Company Symbol => amd

Calculating for Symbol -> AMD

Enter Growth Rate (rate): .15
Enter # of Years into the Future (nper): 10
Enter Estimated Price per Share (pps): 1.43

Rate = 8.59%
PV = -42.90000000000003
FV = 173.5544268618693

Return on Investment Capitol = 1/5/10Yr. -> 304.56% 32.25% 15%
Growth Rate Sales = 1/5/10Yr. -> 304.56% 32.25% 15%
Growth Rate EPS = 1/5/10Yr. -> 304.56% 32.25% 15%
Growth Rate Equity = 1/5/10Yr. -> 304.56% 32.25% 15%
Growth Rate Cash = 1/5/10Yr. -> 304.56% 32.25% 15%
Future Earnings Per Share = $5.79
Future Stock Price = $173.55
Calulated Stock Price = $42.9
Margin of Safety Price = $21.45
--------------------------------------------------------

Enter Company Symbol =>

--------------------------------------------------------------------------------------

Financial.java

import acm.program.*;
import java.math.*;
import java.util.*;
import java.text.*;

public class ExcelFunctions extends ConsoleProgram
{

/**
* @param args
*/

private static final int FINANCIAL_MAX_ITERATIONS = 128;
private static final double FINANCIAL_PRECISION = 1.0e-08;

public static void main(String[] args) {
new ExcelFunctions().start(args);

}

public void run()
{
String Rate;
String Years;
String PricePerShare;
String Company = "AMD";
double rate = 0.0;
double pps = 0.0;
int years = 0;
double pmt = 0.0;
double fv;
double pv;

setFont("Courier-16");

while (true)
{

Company = readLine("Enter Company Symbol => ");
fv = 0.0;
pv = 0.0;
if(Company .equalsIgnoreCase(""))
{
// exit program
;break;
}
println();
println("Calculating for Symbol -> " + Company.toUpperCase());
println();
Rate = readLine("Enter Growth Rate (rate): ");
Years = readLine("Enter # of Years into the Future (nper): ");
PricePerShare = readLine("Enter Estimated Price per Share (pps): ");

println();

rate = Double.valueOf(Rate.trim()).doubleValue();
pps = Double.valueOf(PricePerShare.trim()).doubleValue();
years = Integer.parseInt(Years.trim());
pmt = 0;

//println("pv before calc = " + pv);

fv = calcFV(rate, years, pps);
pv = calcPV(rate, years, pmt, (fv*30), true);

//fv = calcFV(Double.parseDouble(Rate), Integer.parseInt(Years), Double.parseDouble(PricePerShare));
//pv = calcPV(Double.parseDouble(Rate), Integer.parseInt(Years), 0, (fv*30), true);

//println("pv after calc = " + pv);

// nper pmt pv fv due guess
//String rateString = calcRATE(years, pmt, pv, 0.0, 0, 0.1);
//String rateString = calcRATE(9, 0, -463, 1683, 0, 0.1); // result should = 15.42%
String rateString = calcRATE(9, 0, -930, 1952, 0, 0.1); // result should = 8.59%
println("Rate = " + rateString + "%");
println("PV = " + (-pv));
println("FV = " + (fv * 30));

println();
println("Return on Investment Capitol = 1/5/10Yr. -> " + calcRATE(1, pmt, -(pv), (fv * 30), 0, 0.1) + "% " + calcRATE(5, pmt, -pv, (fv * 30), 0, 0.1) + "% " + calcRATE(10, pmt, -pv, (fv * 30), 0, 0.1) + "%");
println("Growth Rate Sales = 1/5/10Yr. -> " + calcRATE(1, pmt, -(pv), (fv * 30), 0, 0.1) + "% " + calcRATE(5, pmt, -pv, (fv * 30), 0, 0.1) + "% " + calcRATE(10, pmt, -pv, (fv * 30), 0, 0.1) + "%");
println("Growth Rate EPS = 1/5/10Yr. -> " + calcRATE(1, pmt, -(pv), (fv * 30), 0, 0.1) + "% " + calcRATE(5, pmt, -pv, (fv * 30), 0, 0.1) + "% " + calcRATE(10, pmt, -pv, (fv * 30), 0, 0.1) + "%");
println("Growth Rate Equity = 1/5/10Yr. -> " + calcRATE(1, pmt, -(pv), (fv * 30), 0, 0.1) + "% " + calcRATE(5, pmt, -pv, (fv * 30), 0, 0.1) + "% " + calcRATE(10, pmt, -pv, (fv * 30), 0, 0.1) + "%");
println("Growth Rate Cash = 1/5/10Yr. -> " + calcRATE(1, pmt, -(pv), (fv * 30), 0, 0.1) + "% " + calcRATE(5, pmt, -pv, (fv * 30), 0, 0.1) + "% " + calcRATE(10, pmt, -pv, (fv * 30), 0, 0.1) + "%");
println("Future Earnings Per Share = " + customFormat("$###,###.##", fv));
println("Future Stock Price = " + customFormat("$###,###.##", (fv * 30)));

//pv = calcPV(rate, years, 0, (fv*30), true);

println("Calulated Stock Price = " + customFormat("$###,###.##", pv));
println("Margin of Safety Price = " + customFormat("$###,###.##", (pv *.5)));

println("--------------------------------------------------------");
println();
}

println();
println("Program Terminated ... ");
println("Exiting ...");
wait(1000);
System.exit(0);
}

/*
public double calcRATE(int years)
{

}
*/

//define('FINANCIAL_MAX_ITERATIONS', 128);
//define('FINANCIAL_PRECISION', 1.0e-08);
//function RATE($nper, $pmt, $pv, $fv = 0.0, $type = 0, $guess = 0.1);

public String calcRATE(int nper, double pmt, double pv, double fv, int type, double guess)
{
double y, y0, y1, x0, x1 = 0, f = 0, i = 0;

double rate = guess;

if (Math.abs(rate) < FINANCIAL_PRECISION)
{
y = pv * (1 + nper * rate) + pmt * (1 + rate * type) * nper + fv;
}
else
{
f = Math.exp(nper * Math.log(1 + rate));
y = pv * f + pmt * (1 / rate + type) * (f - 1) + fv;
}

y0 = pv + pmt * nper + fv;
y1 = pv * f + pmt * (1 / rate + type) * (f - 1) + fv;


// find root by secant method
i = x0 = 0.0;
x1 = rate;

while ((Math.abs(y0 - y1) > FINANCIAL_PRECISION) && (i < FINANCIAL_MAX_ITERATIONS))
{
rate = (y1 * x0 - y0 * x1) / (y1 - y0);
x0 = x1;
x1 = rate;

if (Math.abs(rate) < FINANCIAL_PRECISION)
{
y = pv * (1 + nper * rate) + pmt * (1 + rate * type) * nper + fv;
}
else
{
f = Math.exp(nper * Math.log(1 + rate));
y = pv * f + pmt * (1 / rate + type) * (f - 1) + fv;
}

y0 = y1;
y1 = y;
++i;
}

return customFormat("##.##", (rate * 100));
} //  function RATE()


public double calcFV(double rate, int nPer, double pps)
{
double sum = pps;
for(int i = 0; i < nPer; i++)
{
sum += sum * rate;
}
return sum;
}

public double calcPV(double rate, int nPer, double pmt, double fv, boolean Type)
{
double ann = Math.pow(1 + rate, nPer);
//return 0 / rate * (1 - Math.pow(1 + rate, -nPer));
return ((fv + pmt * (1 + (Type ? rate : 0)) * ((ann - 1) / rate)) / ann);

//return ((FV + Pmt * (1 + (Type ? Rate : 0)) * ((ann - 1) / Rate)) / ann);
}

public String customFormat(String pattern, double value )
{
DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(value);

//System.out.println(value + " " + pattern + " " + output);

return output;
}

public static void wait (int n)
{
long t0,t1;
t0 = System.currentTimeMillis();
do
{
t1 = System.currentTimeMillis();
}
while (t1 - t0 < 1500);
}

}

 

 

New !
PC Professionals Forums

Java C++ XCode Software Development

Ort Report

Mauled by Panda

Experts Exchange

Free META Tag Help Analyzer