aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help with Java Mortgage Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help with Java Mortgage Program" Watch "Help with Java Mortgage Program" New topic
Author

Help with Java Mortgage Program

Terrence Adams
Greenhorn

Joined: Jan 09, 2008
Posts: 2
Hi everyone I am new here and this is my first thread. I am having trouble getting this GUI program to run. I am wondering If I coded this too much like an applet instead of a GUI. Is Gui code and applet code the same? I want this to run as a standalone program. My code compiles correctly but I keep getting the message : "Exception in the thread "main java.lang.NoSuchMethod Error: main".

I have no idea what I am doing wrong. I am hoping that someone can give me some insight. Here is my code :


import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
//import java.text.NumberFormat;
//import java.util.Locale;



public class TAMortgage extends JApplet implements ActionListener {

NumberFormat moneyFormat = NumberFormat.getCurrencyInstance( Locale.US );


//double aryAmounts= 0;
double Rates= 0; //basic loan variables (for use with radio buttons)
double Terms = 0; //basic loan variables(for use with radio buttons)
double rate = 0; //basic loan variables
double amount = 0; //basic loan variables
double term = 0; //basic loan variables
int i = 0;
int button = 0;
int l = 0;
int lStack = 361;//for dyanamic array size
int amtTrk = 1;

private static final long serialVersionUID = 1L;//elcipse suggested line



JPanel Panel = new JPanel();
JTextArea display = new JTextArea(17, 42);
JScrollPane scrollPane = new JScrollPane(display, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel title = new JPanel();
JPanel outputPanel = new JPanel();
JTextField t = new JTextField(10);
JLabel Label = new JLabel("Mortgage Amorization Calculator\n");
JLabel loanLabel = new JLabel("Select loan type:");
JLabel rbInstruct = new JLabel("Please enter mortgage amount");
JLabel rbInstruct2 = new JLabel("(no commas or dollar signs)");
ButtonGroup radio = new ButtonGroup();
JRadioButton rate1 = new JRadioButton("7 years @ 5.35%", true);
JRadioButton rate2 = new JRadioButton("15 years @ 5.5%", false);
JRadioButton rate3 = new JRadioButton("30 years @ 5.75%", false);
JRadioButton Custom = new JRadioButton("Custom Terms/Rate");
JButton Term = new JButton("Term in years");
JButton Rate = new JButton("Interest Rate");
JButton calcButton = new JButton("Calculate!");

public void init()
{
display.setEditable(false);
Container input = getContentPane();
setSize(new Dimension(750, 290));
input.setLayout(new FlowLayout(FlowLayout.CENTER));
radio.add(rate1);
radio.add(rate2);
radio.add(rate3);
radio.add(Custom);
Panel.setLayout(new GridLayout(10,1));

//add panels to container
input.add(Panel);
Panel.add(Label);
Panel.add(rbInstruct);
Panel.add(rbInstruct2);
Panel.add(t);
Panel.add(loanLabel);
Panel.add(rate1);
Panel.add(rate2);
Panel.add(rate3);
Panel.add(Custom);
Panel.add(calcButton);

input.add(outputPanel);
outputPanel.add(scrollPane);

//Listeners
rate1.addActionListener(this);
rate2.addActionListener(this);
rate3.addActionListener(this);
Custom.addActionListener(this);
t.addActionListener(this);
calcButton.addActionListener(this);

}
public class Mortgage {

private double payment;
private double H;
private double C;
private double Q;
private double interest;



public void setPayment ( double amount, double term, double rate) {
double J = (rate/(12*100));
double N = (term*12);
this.payment = (amount * J/(1 - Math.pow((1+J),-N)));
}

public double getPayment(){
return payment;
}

public void setH(double newamount, double interest) {
this.H = (newamount * interest);
}

public double getH(){
return H;
}

public void setC(double payamount, double H) {
this.C = (payamount - H);
}

public double getC(){
return C;
}

public void setQ(double newamount, double C) {
this.Q = (newamount - C);
}

public double getQ(){
return Q;
}

public void setInterest(double rate) {
this.interest = (rate /(12*100));
}

public double getInterest(){
return interest;
}

}

//applet start
public void start()
{

repaint();
}



//event driven actions
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();


String response = t.getText();
//t.append(text + newline);
t.selectAll();

double Amount = Double.parseDouble(response);


if ( rate1.isSelected() )
{
display.setText("");
Rates = 5.35;
Terms = 7;
}

if ( rate2.isSelected() )
{
display.setText("");
Rates = 5.5;
Terms = 15;
}

if ( rate3.isSelected() )
{
display.setText("");
Rates = 5.75;
Terms = 30;
}

if ( Custom.isSelected() )
{
display.setText("");
String response1 = JOptionPane.showInputDialog(null, "Enter the mortgage term");
Terms = Integer.parseInt(response1);
lStack = (int) (lStack + (Terms*12));
String response2 = JOptionPane.showInputDialog(null, "Enter the mortgage interest rate");
Rates = Double.parseDouble(response2);
source = calcButton;
}


if(source == calcButton)
{





display.setText("");

double [] H = new double[lStack];
double [] C = new double[lStack];
double [] Q = new double[lStack];
double [] newamount = new double[lStack];

rate = Rates;
amount = Amount;
term = Terms;
//double amount = aryAmounts;
Mortgage m = new Mortgage();
m.setPayment(amount, term, rate);
//System.out.println(m.getPayment()); Troubleshooting line


m.setInterest(rate);


display.append("\t The mortgage amount is " + moneyFormat.format(amount)+ "\n");
display.append("\t The mortgage term is " + term + " years"+ "\n");
display.append("\t The mortgage rate is " + rate + " percent"+ "\n");

double payamount = m.getPayment();

display.append("\t The monthly payment is: " + moneyFormat.format(payamount)+ "\n");
display.append("*******************************************************************************************\n");
int track = 0;

newamount[amtTrk] = amount; //variables initialized to work with Mortgage class
double interest = m.getInterest();//variables initialized to work with Mortgage class
display.append("Pmt #" + "\tPrevious Bal." + "\tInterest paid" + "\tPrinciple paid" + "\tBalance\n");



int ticker = 0;
while (newamount[amtTrk] > payamount)//amortization while loop
{
++track;


m.setH(newamount[amtTrk], interest);
H[track] = m.getH();
m.setC(payamount, H[track]);
C[track] = m.getC();
m.setQ(newamount[amtTrk], C[track]);
Q[track] = m.getQ();
display.append(track + "\t" + moneyFormat.format(newamount[amtTrk]) + "\t" + moneyFormat.format(H[track]) + "\t" + moneyFormat.format(C[track]) + "\t" + moneyFormat.format(Q[track])+ "\n");
++amtTrk;
newamount[amtTrk] = Q[track];
++ticker;
}

++track;
//newamount = m.getQ();
m.setH(newamount[amtTrk], interest);
H[track] = m.getH();
m.setC(payamount, H[track]);
C[track] = m.getC();
m.setQ(newamount[amtTrk], C[track]);
Q[track] = m.getQ();
//System.out.println(track + "\t" + moneyFormat.format(newamount[amtTrk]) + "\t" + moneyFormat.format(H[track]) + "\t\t" + moneyFormat.format(C[track]) + "\t\t" + moneyFormat.format(Q[track])+ "\n");
display.append(track + "\t" + moneyFormat.format(newamount[amtTrk]) + "\t" + moneyFormat.format(H[track]) + "\t" + moneyFormat.format(C[track]) + "\t" + moneyFormat.format(Q[track])+ "\n");
display.append("*******************************************************************************************\n");
display.append(" Please scroll up to see amount, term, rate, and payment information.\n");
display.append("*******************************************************************************************\n");
display.append("To amoritize a new loan, please adjust loan details as necessary and hit 'Calculate'.\n");
display.append("=================================================================");
int newlStack = lStack + track;
lStack = newlStack;


}
}
}
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Terrence Adams:
I am having trouble getting this GUI program to run. I am wondering If I coded this too much like an applet instead of a GUI. Is Gui code and applet code the same? I want this to run as a standalone program. My code compiles correctly but I keep getting the message : "Exception in the thread "main java.lang.NoSuchMethod Error: main".


Starting hooks differ between applets and applications.
Applets are launched using init() and start(), while
applications are launched using main(). That's why you
see the error about missing main() when you launch your
class as an application.

Here's one you can add to your class:


You don't need the app.start() at the end because your start()
method doesn't do anything [except call an unneeded repaint()].


bitguru blog
Terrence Adams
Greenhorn

Joined: Jan 09, 2008
Posts: 2
I added the start to the first main of my program and I am still getting the error.

I added that code to my class you suggested and it still wont run. Where exactly do I need to modify the "start" and add in the "main" in the program?
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Terrence Adams (emphasis added):
I added the start to the first main of my program and I am still getting the error.

I added that code to my class you suggested and it still wont run. Where exactly do I need to modify the "start" and add in the "main" in the program?


Don't add a "start". Add the main() method I provided somewhere to your class. It could be the very first thing (just before NumberFormat moneyFormat = ...), the very last thing (just before the final closing brace), or in the middle of your class (but not in the middle of method, of course).

You could even replace your start() method with my main() method, since your start() method doesn't do anything and the one inherited from JApplet would be fine.

If you're still having trouble, tell me this. Can you run any java application at all (for instance, the simple HelloWorld2002 example)?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Java Mortgage Program