November 3, 2003
The Car Loan Calculator
/*
Car Loan Calculator
*/
import java.awt.*; import java.applet.Applet; import java.awt.event.*;
public class CarLoan extends Applet implements ActionListener { // GUI variables Panel allPanel; TextField costText, cashText, termText, yRateText, sTaxText; Button calcButton; TextArea outText; // Program variables float cost, cash, yRate, sTax; int term; float monthlyPayment, amount, mRate, taxPaid, downPayment; float totalCost; public void init() { makeGUI(); } // init public void paint(Graphics g) { // print out results in outText text area outText.appendText("Cost of car $"+cost+"\n"); outText.appendText("Cash in hand $"+cash+"\n"); outText.appendText("Term of loan "+term+" months.\n"); outText.appendText("Yearly interest rate is "+yRate+"%\n"); outText.appendText("Sales Tax is "+sTax+"%\n"); outText.appendText("Sales Tax paid is $"+taxPaid+"\n"); outText.appendText("Down payment is $"+downPayment+"\n"); outText.appendText("Amount financed is $"+amount+"\n"); outText.appendText("Monthly payment will be $"+monthlyPayment+"\n"); outText.appendText("Total cost will be $"+totalCost+"\n\n\n"); } // paint void compute() { // do computations // Compute monthly rate mRate = (yRate/12.0f)/100.0f; // compute amount of loan // compute sales tax paid taxPaid = cost * sTax/100.0f; // compute down payment downPayment = cash - taxPaid; // compute amount financed amount = cost - downPayment; // compute monthly payment monthlyPayment = (float)((amount * mRate)/(1 - Math.exp(-1.0f*term*Math.log(1.0f+mRate)))); // compute total cost of car totalCost = cash + term*monthlyPayment; } // compute public void actionPerformed(ActionEvent e) { System.out.println("Button pressed."); // read all values from the applet cost = readValue(costText); cash = readValue(cashText); term =(int) readValue(termText); yRate = readValue(yRateText); sTax = readValue(sTaxText); // do computations compute(); repaint(); } // actionPerformed float readValue(TextField t) { // read a value from the text field, t and convert it to float float v; Float F; String s; // get string from t s = t.getText(); // make it into a Float (wrapper) F = new Float(s); // convert it to a numerical (float) value v = F.floatValue(); return v; } // readValue public void makeWidget(TextField t, String s, Panel all) { Label l = new Label(s); Panel p = new Panel(); p.setLayout(new FlowLayout(FlowLayout.RIGHT)); p.add(l); p.add(t); all.add(p); } // makeWidget public void makeGUI() { Panel bPanel; // set applet layout setLayout(new BorderLayout()); // create the all-panel containing data entry fields allPanel = new Panel(); allPanel.setLayout(new GridLayout(6, 1)); // Create and add Cost widget costText = new TextField(10); makeWidget(costText, "Cost of car: $", allPanel); // Create and add Cash widget cashText = new TextField(10); makeWidget(cashText, "Cash in hand: $", allPanel); // Create and add Term widget termText = new TextField(10); makeWidget(termText, "Loan Term(months):", allPanel); // Create and add Yearly rate widget yRateText = new TextField(10); makeWidget(yRateText, "Yearly Interest Rate (%):", allPanel); // Create and add Sales Tax widget sTaxText = new TextField(10); makeWidget(sTaxText, "Sales Tax (%):", allPanel); // create and add the calcButton calcButton = new Button("Calculate"); bPanel = new Panel(); bPanel.add(calcButton); allPanel.add(bPanel); // add the allPanel to applet add("West", allPanel);
// create and add the text area outText = new TextArea("Output\n\n", 20, 40, TextArea.SCROLLBARS_BOTH); outText.setBackground(Color.yellow); add("Center", outText); // define listener calcButton.addActionListener(this); } // makeGUI } // CarLoanBack to CS110 Examples