# Mortgage Calculator

• 01-21-2011, 06:54 PM
rsmithjr316
Mortgage Calculator
Getting error on compiling. Need some assistance on where I went wrong. Thanks

[CODE]

public void onButtonChart()
{
Chart = new Chart((int) (loanTerm / 12)); // Create Instance of Class
System.out.println("" + loanTerm);

}

// Display Methods to Print Values for Morgage Calculator

public void assignLoanAmount(double loanAmount)
{
principalBalance = loanAmount;
}

public void assignLoanRate(double interestAmount)
{
loanRate = interestAmount;
}

public void assignLoanTerm(double termAmount)
{
loanTerm = termAmount;
}

public double getAssignLoanAmount()
{
return principalBalance;
}

public double getAssignLoanRate()
{
return loanRate ;
}

public double getAssignLoanTerm()
{
return loanTerm;
}

public void calculateMonthlyPayment()
{
monthlyRate = (loanRate / 100) / 12;
monthlyPayment = principalBalance *((monthlyRate*(Math.pow((1 + monthlyRate),loanTerm)))
/(Math.pow((1 + monthlyRate),loanTerm)-1));
}
public void calculateMonthlyInterest()
{
monthlyInterestRate = (loanRate / 12) / 100;
interestMonthly = principalBalance * monthlyInterestRate;

}

public void calculateMonthlyPrinciple()
{
monthlyPrinciple = monthlyPayment - interestMonthly;
}

public void calculateLoanBalance()
{
loanBalance = principalBalance - monthlyPrinciple;

}

public void calculateNewBalance()
{
newBalance = loanBalance - monthlyPrinciple;
}
public void monthlyLoanAmount()
{
result.append("Month\tLoanBalance\tInterest Paid\n");
double newloanTerm = loanTerm;
while (newloanTerm>0)
{
double loanMonths = loanTerm-newloanTerm;

double newMonthlyBalance = principalBalance *((Math.pow((1 + monthlyInterestRate),loanTerm))
-(Math.pow((1 + monthlyInterestRate),loanMonths)))/(Math.pow((1 + monthlyInterestRate),loanTerm)-1);

double monthlyamountInterest = newMonthlyBalance * monthlyInterestRate;

result.append("" + Math.round(newloanTerm) + "\t" + Dollar.format(newMonthlyBalance)
+ "\t" + Dollar.format(monthlyamountInterest) + "\n");

newloanTerm -= 1;

}
}

[CODE]

ERROR:

308 : cannot find symbol symbol : variable Chart location: class MortgageCalculatorWK4 Chart = new Chart((int) (loanTerm / 12));
308 : cannot find symbol symbol : class Chart location: class MortgageCalculatorWK4 Chart = new Chart((int) (loanTerm / 12));
• 01-21-2011, 06:57 PM
Fubarable
• 01-21-2011, 06:58 PM
Fubarable
Also, where is your Chart class that you are trying to use? Can you post the code for it (with tags) as well?
• 01-21-2011, 07:00 PM
rsmithjr316
Error
I have the error posted at the bottom of the post. That is the messag in its entirity. Sorry out the tag, missed the /.
• 01-21-2011, 07:04 PM
rsmithjr316
Here is the code.

Code:

import java.awt.BorderLayout;              // Code to Import and Provide Layout of Window
import java.awt.GridLayout;                // Code to Import the Layout in Grid Format
import java.awt.event.ActionEvent;          // Code to Import Action
import java.awt.event.ActionListener;      // Code to Import Listener for Action
import javax.swing.JButton;                // Code to Import Use of Buttons
import javax.swing.JFrame;                  // Code to Import Use of Frame
import javax.swing.JLabel;                  // Code to Import Use of Label
import javax.swing.JPanel;                  // Code to Import Use of Panel
import javax.swing.JTextField;              // Code to Import Use of Text Field
import javax.swing.JTextArea;              // Code to Import Use of Text Area
import javax.swing.JScrollPane;            // Code to Import Scroll Pane
import javax.swing.JComboBox;              // Code to Import JCombo Box
import java.text.DecimalFormat;            // Code to Import Decimal Format
import javax.swing.ButtonGroup;            // Code to Import Button Group
import javax.swing.JOptionPane;            // Code to Import Option Dialog Box
import java.awt.Dimension;                  // Code to Import Dimension Class
import java.io.*;                          // Code to Import ALL classes of input and output
import javax.swing.JCheckBox;              // Code to Import CheckBox

// Create Mortgage Payment Class

public class MortgageCalculatorWK4 extends JFrame
{

DecimalFormat Dollar = new DecimalFormat("\$###,###.00");

// Beginning of Main
public static void main(String[] args)
{

MortgageCalculatorWK4 newloan = new MortgageCalculatorWK4();  // Create Instance of Class

}

// Declare Variables and Values

private double principalBalance;        // Amount of Loan
private double loanTerm;                // Term of Loan
private double loanRate;                // Interest Rate of Loan
private double monthlyRate;              // Amount of Monthly Interest
private double monthlyPayment;          // Amount of Monthly Payment
private double monthlyInterestRate;      // Amount of Monthly Interest Rate
private double interestMonthly;          // Amount of Monthly Interest
private double monthlyPrinciple;        // Amount of Monthly Principle
private double loanBalance;              // Balance of Loan
double newBalance;                      // New Monthly Balance of Loan

private static int numberOfLinesGenerated = 1;

// GUI elements to Display Current Information
private JLabel labelPrincipal = new JLabel("  Mortgage Principal : ");
private JTextField txtPrincipal = new JTextField(10);

private JLabel labelYears = new JLabel("  Term : ");
private JTextField txtYears = new JTextField(10);

private JLabel labelRate = new JLabel("  Interest Rate : ");
private JTextField txtRate = new JTextField(10);

private JCheckBox buttonEnterValue = new JCheckBox("Enter Value");
private JCheckBox buttonSelectValue = new JCheckBox("Select Value");

private ButtonGroup buttongroup = new ButtonGroup();

private JTextArea result = new JTextArea(10, 20);
JScrollPane Result = new JScrollPane(result);

private JButton buttonCalc = new JButton("Calculate");
private JButton buttonClear = new JButton("Clear");

private JButton buttonNext = new JButton("Next");
private JButton buttonExit = new JButton("Exit");
private JButton buttonChart = new JButton("Chart");

private JPanel panel = new JPanel();
private JPanel nextButtonPanel = new JPanel();
JPanel pane = new JPanel(new BorderLayout());

String[] loanPlan = {"Select Loan", "7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"}; //Array for loan plan

private JLabel labelPlan = new JLabel("  Mortgage Plan : ");

private JComboBox comboBox = new JComboBox(loanPlan);

// Create Container for Information
public MortgageCalculatorWK4() {

{
public void actionPerformed(ActionEvent e)
{
onButtonCalculate();
}
});

{
public void actionPerformed(ActionEvent e)
{
onButtonReset();
}
});

{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
{
public void actionPerformed(ActionEvent e)
{
onButtonChart();
}
});

panel.setLayout(new GridLayout(5, 2,3,3));

buttonEnterValue.setText("Get Values OR ");
buttonSelectValue.setText("Select Values ");

public void actionPerformed(ActionEvent evt) {
buttonEnterValueActionPerformed(evt);
}
});
buttonEnterValue.setSelected(true);
public void actionPerformed(ActionEvent evt) {
buttonSelectValueActionPerformed(evt);
}
});

final Dimension d = new Dimension(200, 200);

setLayout(new BorderLayout());

buttonExit.setEnabled(false);
buttonChart.setEnabled(false);

pack();
setDefaultCloseOperation(EXIT_ON_CLOSE );
setResizable(false);
setTitle("Mortgage Calculator");
setVisible(true);
}

public void buttonEnterValueActionPerformed(ActionEvent evt)
{
if(buttonEnterValue.isSelected()){
panel.removeAll();

setVisible(true);
}
}
public void buttonSelectValueActionPerformed(ActionEvent evt)
{
if(buttonSelectValue.isSelected()){
panel.removeAll();

setVisible(true);
}
}
// Calculate Information
public void onButtonCalculate()
{
double loanAmt = Double.parseDouble(txtPrincipal.getText());
assignLoanAmount(loanAmt);      // Assign Amount of Loan

if(buttonEnterValue.isSelected())
{
try
{

StreamTokenizer tokenizer = new StreamTokenizer(new FileReader("loanData.txt"));

while(tokenizer.nextToken()!= StreamTokenizer.TT_EOF)
{
if(tokenizer.lineno() == numberOfLinesGenerated)
{
if(tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
assignLoanTerm(tokenizer.nval * 12);
}
txtYears.setText("" + tokenizer.nval);
tokenizer.nextToken();
if(tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
assignLoanRate(tokenizer.nval);
}
txtRate.setText("" + tokenizer.nval);

break;
}
System.out.println (tokenizer.lineno());
}

}catch (Exception addException){//Catch exception if any
}

}
else
{
String str = (String)comboBox.getSelectedItem();
if(str.equals("7 years at 5.35%"))
{
assignLoanRate(5.35);
assignLoanTerm(7 * 12);
}
else
if(str.equals("15 years at 5.5%"))
{
assignLoanRate(5.5);
assignLoanTerm(15 * 12);
}
else
if(str.equals("30 years at 5.75%"))
{
assignLoanRate(5.75);
assignLoanTerm(30 * 12);
}
}

calculateMonthlyPayment();    // Calculate Monthly Payment of Loan
calculateMonthlyInterest();    // Calculate Monthly Interest on Loan
calculateMonthlyPrinciple();  // Calculate Monthly Principle of Payment
calculateLoanBalance();        // Calculate Loan Balance
calculateNewBalance();        // Calculate New Balance Each Month
displayAmounts();              // Display All Amounts
monthlyLoanAmount();
monthlyBalanceInterest(getAssignLoanAmount(), getAssignLoanTerm(), getAssignLoanRate());
++ numberOfLinesGenerated;
if(numberOfLinesGenerated > 4) {
numberOfLinesGenerated = 1;
}
buttonCalc.setEnabled(true);
buttonClear.setEnabled(true);
buttonExit.setEnabled(true);
buttonNext.setEnabled(true);
buttonChart.setEnabled(true);

}

// Information for Button
public void onButtonReset()
{
txtPrincipal.setText("");
txtYears.setText("");
txtRate.setText("");
result.setText("");
}

public void onButtonChart()
{
Chart = new Chart((int) (loanTerm / 12)); // Create Instance of Class
System.out.println("" + loanTerm);

}

// Display Methods to Print Values for Morgage Calculator

public void assignLoanAmount(double loanAmount)
{
principalBalance = loanAmount;
}

public void assignLoanRate(double interestAmount)
{
loanRate = interestAmount;
}

public void assignLoanTerm(double termAmount)
{
loanTerm = termAmount;
}

public double getAssignLoanAmount()
{
return principalBalance;
}

public double getAssignLoanRate()
{
return loanRate ;
}

public double getAssignLoanTerm()
{
return loanTerm;
}

public void calculateMonthlyPayment()
{
monthlyRate = (loanRate / 100) / 12;
monthlyPayment = principalBalance *((monthlyRate*(Math.pow((1 + monthlyRate),loanTerm)))
/(Math.pow((1 + monthlyRate),loanTerm)-1));
}
public void calculateMonthlyInterest()
{
monthlyInterestRate = (loanRate / 12) / 100;
interestMonthly = principalBalance * monthlyInterestRate;

}

public void calculateMonthlyPrinciple()
{
monthlyPrinciple = monthlyPayment - interestMonthly;
}

public void calculateLoanBalance()
{
loanBalance = principalBalance - monthlyPrinciple;

}

public void calculateNewBalance()
{
newBalance = loanBalance - monthlyPrinciple;
}
public void monthlyLoanAmount()
{
result.append("Month\tLoanBalance\tInterest Paid\n");
double newloanTerm = loanTerm;
while (newloanTerm>0)
{
double loanMonths = loanTerm-newloanTerm;

double newMonthlyBalance = principalBalance *((Math.pow((1 + monthlyInterestRate),loanTerm))
-(Math.pow((1 + monthlyInterestRate),loanMonths)))/(Math.pow((1 + monthlyInterestRate),loanTerm)-1);

double monthlyamountInterest = newMonthlyBalance * monthlyInterestRate;

result.append("" + Math.round(newloanTerm) + "\t" + Dollar.format(newMonthlyBalance)
+ "\t" + Dollar.format(monthlyamountInterest) + "\n");

newloanTerm -= 1;

}
}
public void monthlyBalanceInterest(double loan, double term, double interestRate)
{

DecimalFormat df = new DecimalFormat("\$###,###.00"); // Format Decimal Point

double monthlyRate = (interestRate/(12*100));  // Get Monthly Interest Rate
// Calculate Discount Factor
double discountFactor = (Math.pow((1 + monthlyRate), term) -1) / (monthlyRate * Math.pow((1 + monthlyRate), term));
double payment1 = loan / discountFactor;  // Calculate Monthly Payment

// Display Program Results
result.append("\nLoan Amount = " + df.format(loan));
result.append("\nInterest Rate = " + interestRate + "%");
result.append("\nLength of Loan = " + Math.round(term/12) +" years");
result.append("\nMonthly Payment = " + df.format(payment1) + "\n");
}

public void displayAmounts()
{
System.out.println("Loan Amount = " + Dollar.format(principalBalance));      // Show Original Loan Amount
System.out.println("Interest Rate = " + loanRate + "%");                    // Show Interest Rate
System.out.println("Length of Loan = " + Math.round(loanTerm) + " months");  // Show Length of Loan
System.out.println("Monthly Payment = " + Dollar.format(monthlyPayment));    // Show Monthly Payment
}
}

• 01-21-2011, 08:15 PM
JosAH
Quote:

Originally Posted by rsmithjr316
308 : cannot find symbol symbol : variable Chart location: class MortgageCalculatorWK4 Chart = new Chart((int) (loanTerm / 12));
308 : cannot find symbol symbol : class Chart location: class MortgageCalculatorWK4 Chart = new Chart((int) (loanTerm / 12));

You're trying to use a variable named 'Chart' but you never defined it anywhere. Your compiler whines about that. b.t.w. Chart seems to be a class; it is not a wise thing to do to name a variable identical to a class name; there's a naming convention, stick to it.

kind regards,

Jos
• 01-21-2011, 08:46 PM
rsmithjr316
Missing It
Thanks Jos. Can you give me some guidance? I am completely missing it.
• 01-21-2011, 09:08 PM
Fubarable
It's more than that. It looks like you're trying to use a class named Chart but Java can't find it. That's why I asked for your code for this class. Do you have it?
• 01-21-2011, 11:02 PM
rsmithjr316
Here is what I changed; I created a variable private double chart.

double chart = ((int) (loanTerm / 12)); // Create Instance of Class
System.out.println("" + loanTerm);

Whenever I try and enter in the data manually, I get an error;

java.io.FileNotFoundException:loanData.txt(The system cannot find the file specified).

I have it declared above in the code. How do I get the manual entries to work when I hit calculate? Thanks
• 01-21-2011, 11:12 PM
Fubarable
You never answered my questions about the "Chart" class. Why try to assist if only to be ignored?
• 01-21-2011, 11:21 PM
rsmithjr316
I don't have the code for the class. Which is why I was having a problem in the first place and am very happy you pointed that out. I am very new to this and am learning on the fly. I didnt realize I created the class until you pointed it out. I thought it was looing for a vaiable that I thought I had declared but was wrong. I figured out some of it from yourlast post. I appreciate all of your help. I appologize, I am not trying to ignor you. I am trying to fix the issues as I am reading and posting in the forum.
• 01-21-2011, 11:26 PM
Fubarable
Your questions are somewhat simplistic -- nothing wrong with that, as we have to start somewhere -- but the code is complex, very complex. I need an honest answer from you -- is this your code? Did you create this from scratch? Or are you trying to "borrow" code from the internet, such as from here?

If so, this really isn't a good idea and will bite you in the end. Also, you should be up front with us about this type of stuff from the start.
• 01-21-2011, 11:31 PM
rsmithjr316
Bits and pieces are from the internet and no it is not from here. Some of the infromation i am trying to use from te previous program that I wrote.
• 01-21-2011, 11:33 PM
Fubarable
On analysis of code in the net, I think we can safely assume that > 95% of your code has been borrowed. Do yourself a favor and get rid of all the borrowed code, as otherwise all you'll create will be frankencode, and you'll understand very little of it or our attempts at help. Luck.
• 01-21-2011, 11:53 PM
gcalvin
I've made this rant before and I'll make it again... don't ever copy and paste code -- always type it into your editor. Whatever reason you think you have for wanting to copy-and-paste source code, I have a better reason why you shouldn't.

But I'm bad at typing, and if I try to type it all in, I'll make mistakes. You need to get better at typing if you're going to be a programmer, and this is a great opportunity to get some practice.

But it's too long and complicated. Then maybe it belongs in a library rather than directly in this code file. But either way, you need to type it yourself and be sure that you understand every variable, every operator, every method. If it's really that long and complicated, then chances are you will want to change it someday, and when you do, it won't be good to have copies of it floating around in a bunch of different source files. It should be in one place: in a library.

But I don't have time to type it in myself. If you have time to read it, you have time to type it. And if you don't have time to read it, then it doesn't belong in your project. Code you paste into your project and don't know and understand thoroughly will cost you much much more time than code you've typed yourself.

-Gary-