home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 3.3 KB | 125 lines |
- // Mort.java - Interactive Mortgage Calculator
- //
- // Copyright (C) 1996 by Dale Gass
- // Non-exclusive license granted to MKS, Inc.
- //
-
- import java.lang.*;
- import java.util.*;
- import java.awt.*;
- import java.net.*;
- import java.applet.*;
-
- // A labelled slider
-
- class LabelSlider extends Panel {
- Label label;
- public Slider slider;
-
- public LabelSlider(String name, int w, int h, int m, int x, int a,
- double s) {
- setLayout(new PackerLayout());
- label = new Label(name);
- slider = new Slider(w, h, m, x, a, s);
- add("label;side=right;anchor=e", slider);
- add("label;side=right;anchor=se;padx=3", label);
- resize(600, 40);
- }
- }
-
- // Mort - Interactive Morgage Calculator
-
- public class Mort extends Panel {
- LabelSlider Samt, Srat, Strm; // Sliders for variables
- Label result, biweek; // Result indicators
- static double amount = 100000.0; // The variables (with defaults)
- static double rate = 10.0;
- static double term = 25.0;
-
- // Update the mortgage amount
-
- public void setAmount(int a) {
- Samt.slider.SetValue(a / 1000);
- recalc();
- }
-
- // Perform the Canadian calculations, and update the results
-
- void recalc() {
- amount = Samt.slider.GetValue() * 1000.0;
- rate = Srat.slider.GetValue() * .1;
- term = Strm.slider.GetValue() * 1.0;
-
- String s, t;
- try {
- double value = amount / ((1-1/Math.pow((rate/200+1), term*2)) /
- (Math.pow(rate/200+1, 2.0/12)-1));
- s = "$" + String.valueOf(value) + "/mo.";
- double i = Math.pow(rate/200+1, 2*14/365.0);
- value = Math.log(1/(1-(amount / (value/2) * (i-1)))) /
- Math.log(i) * 14 / 365.25;
- t = "Bi-weekly payments pay off in " + value + " years.";
- } catch (Exception e) {
- s = "<Invalid>";
- t = "";
- }
-
- result.setText(s);
- biweek.setText(t);
- }
-
- // Constructor, with default price
-
- public Mort(int price) {
-
- PackerLayout pl;
- setLayout(pl = new PackerLayout());
- Label l = new Label("Mortgage Calculation (Canadian Method)");
- l.setFont(new Font("TimesRoman", Font.BOLD|Font.ITALIC, 20));
- add("title;side=top", l);
-
- result = new Label();
- result.setAlignment(Label.CENTER);
- result.setFont(new Font("TimesRoman", Font.BOLD, 25));
- add("label;side=top", result);
-
- biweek = new Label();
- biweek.setAlignment(Label.CENTER);
- biweek.setFont(new Font("TimesRoman", Font.BOLD, 20));
- add("short;side=top", biweek);
-
- Samt = new LabelSlider("Amount:", 500,35,30,400,price/1000,1000.0);
- Srat = new LabelSlider("Rate %:", 500, 35, 50, 200, (int)(rate/.1),.1);
- Strm = new LabelSlider("Years:", 500, 35, 1, 40, (int) term, 1.0);
-
- add("amt;side=top", Samt);
- add("rat;side=top", Srat);
- add("trm;side=top", Strm);
-
- recalc();
- resize(600, 350);
- }
-
- // Constructor, with no default price given (use current value or internal
- // default.)
-
- public Mort() {
- this((int) amount);
- }
-
- // Watch for slider moves; update when we see any
-
- public boolean handleEvent(Event e) {
- if (e.id == Slider.SLIDER_MOVE || e.id == Slider.SLIDER_STOP) {
- if (e.id == Slider.SLIDER_STOP) {
- new Sound(Sound.drip);
- }
- recalc();
- result.resize(result.preferredSize());
- biweek.resize(biweek.preferredSize());
- return true;
- }
- return false;
- }
- }
-