# Intoduction to Java Assigment - Help needed

• November 20th, 2012, 05:49 PM
ilkkanen1
Intoduction to Java Assigment - Help needed
Hi folks,
I'm a total newbie, but please if you have the time and patience, your guys' help would be very much appreciated :)

I have the following assignment:
See program BlackScholes.java (BlackScholes.java) that takes s, x, r, sigma, and t from the command line and prints the Black-Scholes value.

Typically the volatility is the unknown value in the Black-Scholes formula. Write a program that reads s, x, r, t, and the current price of the option from the command line and uses binary search (see Exercise 2.1.29) to compute σ.

Code java:

```public class BlackScholesBinary {       // Black-Scholes formula (calculates the BS option value, using the class Gaussian. This method will be used in the binary search below to find the volatility)   public static double callPrice(double s, double x, double r, double sigma, double t) { double a = (Math.log(s/x) + (r + sigma * sigma/2) * t) / (sigma * Math.sqrt(t)); double b = a - sigma * Math.sqrt(t); return s * Gaussian.Phi(a) - x * Math.exp(-r * t) * Gaussian.Phi(b); }   public static double getSigma(double s, double x, double r, double t, double price, double lo, double hi, double delta) {   lo = 0.0; hi = 100000.0; delta = 0.01; double mid = lo + (hi - lo) / 2; if (price < callPrice(s, x, r, mid, t)) return getSigma(s, x, r, t, price, lo, mid, delta); // should look for a lower volatility else if (price > callPrice(s, x, r, mid, t)) return getSigma(s, x, r, t, price, mid, hi, delta); // should look for a higher volatility else if (price == callPrice(s, x, r, mid, t) && (hi - lo < delta)) return mid; //should return the found volatility as a return of the function }   public static void main(String[] args) { double s = Double.parseDouble(args[0]); double x = Double.parseDouble(args[1]); double r = Double.parseDouble(args[2]); double t = Double.parseDouble(args[3]); double price = Double.parseDouble(args[4]); double vol = getSigma(s, x, r, t, price);   System.out.println("The volatility is" + vol + " %"); } }```
Error message received when trying to compile the program: public static double getSigma() - This method must return a result of type double.

Could someone please tell me whats wrong with my program??
• November 20th, 2012, 07:56 PM
jps
Re: Intoduction to Java Assigment - Help needed
Quote:

Could someone please tell me whats wrong with my program??
The error message did:
Quote:

method must return a result of type double
public static double getSigma(.....) does not return a value of type double through every possible path of execution.
If the first second and last if are all false, execution falls out the bottom of the method without a return of type double.
• November 21st, 2012, 04:30 AM
ilkkanen1
Re: Intoduction to Java Assigment - Help needed
I modified the code in the following manner:

public class BlackScholesBinary {

// Black-Scholes formula (calculates the BS option value, to which the binary search below will be used to find the volatility)
public static double callPrice(double s, double x, double r, double sigma, double t) {
double a = (Math.log(s/x) + (r + sigma * sigma/2) * t) / (sigma * Math.sqrt(t));
double b = a - sigma * Math.sqrt(t);
return s * Gaussian.Phi(a) - x * Math.exp(-r * t) * Gaussian.Phi(b);
}

public static double getSigma(double s, double x, double r, double t, double price, double lo, double hi, double delta) { //delta = tolerance
// lo = 0.0;
// hi = 100000.0;
// delta = 0.01;
double mid = lo + (hi - lo) / 2;
if (price < callPrice(s, x, r, mid, t)) return getSigma(s, x, r, t, price, lo, mid, delta);
else if (price > callPrice(s, x, r, mid, t)) return getSigma(s, x, r, t, price, mid, hi, delta);
else if (price == callPrice(s, x, r, mid, t) && (hi - lo < delta)) return mid;
else return Double.NaN;
}

public static void main(String[] args) {
double s = Double.parseDouble(args[0]);
double x = Double.parseDouble(args[1]);
double r = Double.parseDouble(args[2]);
double t = Double.parseDouble(args[3]);
double price = Double.parseDouble(args[4]);
double vol = getSigma(s, x, r, t, price, 0.0, 100000.0, 0.01);
if (vol == Double.NaN) {
System.out.println("Invalid parameters inserted - results in nonreal volatility");
}
else {
System.out.println("The volatility is " + 100*vol + " %");
}
}
}

This works for some (apparently small values of volatility), as for e.g. when given an input
> run BlackScholesBinary 30.14 15.0 0.01 0.25 15.178

The program returns "The volatility is 42.30907986831588 %"

But when I give an input
> run BlackScholesBinary 30.14 15.0 0.01 0.25 15.177, the program crashes.

Whats wrong with the code??? :mad: