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.
Extra for Experts
Challenger
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))))));}}