Homework 0
NPV Applet

Computational Finance

Copyright © Philip H. Dybvig 1998

Scenario After discussing the use of the NPV criterion in your organization, several people in your organization said they would feel more comfortable with the criterion if they had a better intuitive feel for how it worked. You offered to develop a simple java applet they could use to look at the NPV given different cash flows and interest rates.

Action To start getting familiar with Java programming, you will take the existing program discussed in the first class and work to get it running. Then, you will make some very minor changes as an initial step in learning to program yourself. There are also some questions to encourage further understanding. Additional challenges are available (in the "extra for experts" and in some homeworks the "challengers") for students with superior preparation and ambition.

Concept The simple NPV application is intended to help you to get started in java programming. Many parts of the computer program may remain mysterious to you even after this week's explanation, but that is not a bad thing. In my experience, learning computer programming is more interesting and more rapid when we start with a fully functional (albeit simple) application than when building from the bottom up. As when learning human languages, it is not much fun to learn all the grammar first! The Java Tutorial contains more of a detailed bottom-up perspective and will help to fill in the details.

Instructions In each of the following steps, provide a crisply written answer. For the program changes, write a short concise description of the changes you made and why. For thought questions, think about the question and answer in no more than two sentences of ordinary length. As in business, writing that is concise and to the point is appreciated. Unnecessarily long answers may be penalized.

  1. We have obtained the Java Development Kit from SUN for the PC Lab, and the specific instructions below assume that is what you are using. If you need a java compiler for use on another computer, I recommend downloading the same Java Development Kit which is available for free (with some limits to redistribution, hacking, and selling development products you create -- see the license agreement). The free version is available for SUN Solaris and Windows, and there are links to sources, which may or may not be free, for other platforms. I recommend downloading both the JDK and the associated documentation.
  2. Download the required Java program file (NpvA.java) and related web document (NpvA.html) and put them in a directory on your machine where you want to work. It may require some care to make sure the browser uses the name you intend for the file. When you look at the file NpvA.html, the browser looks at the applet referred to in the file as it should. When you ask to save the file to disk, make sure it gives the file the correct name. For example, the version of Internet Explorer in the lab gives the file some strange name related to the title of the page; you should change this to ``NpvA.html'' in the dialog box. Another very important point is that Internet Explorer in the lab would like to edit your file before saving it (!) which can be avoided by selecting ``html only'' in the ``save as type'' dialog box. Different browsers and different versions of the same browser have different problems. Another thing some browsers will do is to change the .html suffix to .htm without asking.
  3. Enter MS-DOS (use xterm or another terminal window in unix or linux), either by selecting the MS-DOS icon or by typing command in the Run dialog box (selected from the Start menu). In the MS-DOS window, go to the directory containing NpvA.java and NpvA.html. For example, to go to A:\javadocs, select the MS-DOS window with your mouse, type A:, press Enter, type cd \javadocs, then press Enter.
  4. Do a spot check to make sure you have downloaded the files. Type ``dir'' to see a listing of the files, which should include ``NpvA.java'' and ``NpvA.html.'' (As mentioned above, it is possible the browser renamed NpvA.html to NpvA.htm without asking. You can either change it back by entering ``rename NpvA.htm NpvA.html'' or you can use the name ``NpvA.htm'' in place of ``NpvA.html'' when following the instructions in the subsequent steps.) Take a look at the files to make sure they are what they should be: enter ``type NpvA.java'' and ``type NpvA.html'' and you should see what is shown in the Exhibits below. For a more orderly display of NpvA.java, either view it in an editor (``notepad NpvA.java'') or use the more command (``more < NpvA.java'').
  5. Compile NpvA.java (convert into bytecodes more usable by the computer) by typing javac NpvA.java. (Case matters, typing "npva.java" instead of "NpvA.java" will not work, even on systems in which case does not usually matter.) This should create the class file NpvA.class. It will also generate a warning about Deprecated functions you should not worry about. (This is because NpvA.java uses a slightly "old-fashioned" version 1.0 of the Java language that ensures compatibility with a wide array of browsers in current use.)
  6. View the Applet by pointing your Web browser at the the copy of NpvA.html in your directory, or by entering ``appletviewer NpvA.html.'' Tell the browser the complete file name (e.g. A:\compufin\NpvA.html) and on some systems you may have to include file: first (e.g. file:A:\compufin\NpvA.html).
  7. Modify the program file NpvA.java to use a default interest rate of 10% instead of 5% and a default initial investment of 80. Recompile and view in the browser. (Note: if your change does not show up in the browser, it is possible that Internet Explorer edited your file to point to the applet on my web site -- see the ``download'' and ``spot check'' steps above.)
  8. Modify the program file NpvA.java to handle cash flows ten years out into the future, instead of the current five years.
  9. Thought question As a user of the program, what other features might you like to see?

Extra for Experts

  1. Have the applet display the duration as well as the NPV.
  2. Modify the applet to show the NPV at a spread of five different interest rate values. Have the user pick the middle rate, and display both the rate and the NPV at that rate, 25 basis points above and below, and 50 basis points above and below. (Recall that a basis point is 1/100 of a percent.)
  3. Rewrite the program to have a class InputCell that constructs side-by-side a Label and a TextField (as we have for each cash flow). Its constructor should take as input the text for the Label and the class should have methods for reading the double number represented in the TextField and for setting the double number represented in the TextField.

Challenger

  1. Have the applet print out the IRR as well as the NPV. Be sure the modified applet gives a warning when there are multiple IRRs or no IRRs.

A Quick Tour of the program and web files.

This is a very quick overview. For much more detail, see the annotated versions in NpvAnotes.html.

The program file NpvA.java defining the Applet is shown in Exhibit B, and the web file NpvA.html at which you point your web browser to view the Applet is shown in Exhibit A.

The browser file file is written in HyperText Markup Language, or HTML for short. The important tag for our purposes is that APPLET tag that refers to a file NpvA.class, which will contain the compiled version of the Java program in Appendix B. NpvA is both the first part of the applet class defined in the java program as well as the first part of both the Java and class file names.

The Java program contains a little part that does the NPV calculation but mostly commands to set up the exact layout of the user interface. This is modular; the applet object includes a heading and a second "center" panel. The "center" panel includes right and left parts, and both the left and right parts contain individual parts. This modular design is pretty easy to understand when using the applet itself.

This first example only scratches the surface of Java's capabilities. It is probably unusual for a Java program to use only one class of objects (in addition to those already defined in the Java standard). However, having only one class probably makes for an easier introduction to Java.

Appendix A: the HTML file NpvA.html

<HTML>
<HEAD>
<TITLE>A Simple NPV APPLET</TITLE>
</HEAD>
<BODY>
<APPLET CODE=NpvA.class WIDTH=400 HEIGHT=350>
</APPLET>
</BODY>
</HTML>

Appendix B: the Java program file NpvA.java

/* NPV Applet (a lite application to get started) */

import java.applet.*;
import java.awt.*;

public class NpvA extends Applet {
TextField c0,c1,c2,c3,c4,c5,R;
Label npvchar;
public NpvA() {
setLayout(new BorderLayout());
add("North",new Label("Sample NPV Applet",Label.CENTER));
Panel centr = new Panel();
centr.setLayout(new FlowLayout());
Panel leftcentr = new Panel();
leftcentr.setLayout(new GridLayout(7,1));
leftcentr.add(new Label("Cash Flows",Label.CENTER));
Panel in0 = new Panel();
in0.setLayout(new FlowLayout(FlowLayout.LEFT));
in0.add(new Label("year 0"));
in0.add(c0 = new TextField("-100",12));
leftcentr.add(in0);
Panel in1 = new Panel();
in1.setLayout(new FlowLayout(FlowLayout.LEFT));
in1.add(new Label("year 1"));
in1.add(c1 = new TextField("5",12));
leftcentr.add(in1);
Panel in2 = new Panel();
in2.setLayout(new FlowLayout(FlowLayout.LEFT));
in2.add(new Label("year 2"));
in2.add(c2 = new TextField("5",12));
leftcentr.add(in2);
Panel in3 = new Panel();
in3.setLayout(new FlowLayout(FlowLayout.LEFT));
in3.add(new Label("year 3"));
in3.add(c3 = new TextField("5",12));
leftcentr.add(in3);
Panel in4 = new Panel();
in4.setLayout(new FlowLayout(FlowLayout.LEFT));
in4.add(new Label("year 4"));
in4.add(c4 = new TextField("5",12));
leftcentr.add(in4);
Panel in5 = new Panel();
in5.setLayout(new FlowLayout(FlowLayout.LEFT));
in5.add(new Label("year 5"));
in5.add(c5 = new TextField("105",12));
leftcentr.add(in5);
centr.add(leftcentr);
Panel rightcentr = new Panel();
rightcentr.setLayout(new GridLayout(4,1));
rightcentr.add(new Label("Interest Rate (%):"));
rightcentr.add(R = new TextField("5",12));
rightcentr.add(new Label("The NPV is:"));
rightcentr.add(npvchar = new Label("",Label.LEFT));
npvchar.resize(180,npvchar.size().height);
centr.add(rightcentr);
add("Center",centr);
recalc();}
public boolean action(Event ev, Object arg) {
if(ev.target instanceof TextField) {
recalc();
return true;}
return false;}
double text2double(TextField tf) {
return Double.valueOf(tf.getText()).doubleValue();}
public void recalc() {
npvchar.setText(String.valueOf(npv(text2double(R)/100.0,
text2double(c0),text2double(c1),text2double(c2),text2double(c3),
text2double(c4),text2double(c5))));}
float npv(double r, double c0, double c1, double c2, double c3,
double c4, double c5) {
double disc = 1.0/(1.0+r);
return((float) (c0 + disc*(c1 + disc*(c2 + disc*(c3 + disc*(c4 + disc*c5))))));}}