I am having problems with the following code. Here is my assignment for this week, Write the program in Java (with a GUI) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit. Please insert comments in the program to document the program.

So the first code that I will post in the code I turned in for my last assignment in Java I, and I did get an "A" on it. But there are somethings I cannot figure out with it too. Such as, why I can't get the math formula to round my numbers. This is the code that is supposed to be used with the GUI code that I will post second.

/*Java Programming I - PRG/420
					Instructor: Richard Xue
					Student: Debora Beeman
					Assignment Due: Week 5, Day 7*/

import java.io.*;
import java.text.DecimalFormat;


public class MortgageCalculator
{
public static void main(String[] args) throws Exception
{
	//Assign variable for financial and term information
	double LoanAmt = 200000.00;
	double[] IntRate = {.0535, .0550, .0575};
	double Int = 0;
	int[] termMonths = {84, 180, 360};
	double Months = 360;
	double monthlyPayment = 0;
	double interestPaid = 0;
	double loanBal = 200000;
	double Temp = 0;
	int IntCounter = 0;
	DecimalFormat money = new DecimalFormat("$0.00");
	int a;
	int b;
	int c;
	int d;
	int e;
	int f;
	
	//Loop for going through array and displaying payment amount.
	
	for (a = 0; a <= 2; a++){
		IntCounter = IntCounter +1; 
		
	for (b = 0; b <= termMonths [b]; b++){
		LoanAmt = 200000;
		loanBal = 200000;
		
		//formula to calculate monthly payment amount
		monthlyPayment = (LoanAmt*(IntRate[a]/12))/(1-1/Math.pow((1+IntRate[a]/12), termMonths[a]));
		
		//Print monthly payment, over term of loan, years with interest rate of .0535.
		
		System.out.println("Monthly Payment for " + (money.format(LoanAmt))+ " and a term of ");
		System.out.println((termMonths[a]/12)+ " years with interest rate of " + (IntRate[a]*100)+ "% will be " +
		(money.format(monthlyPayment))+ " a month.");
		System.out.println();
		
		System.out.println("Press the Enter key to continue");
		System.in.read();
		
		//Loop statement - formula for loan balance and interest paid
		
		while(termMonths[a] > 0){
			
			//Reduce payments one month at a time
			Months = (termMonths[a] --);
			
			//Calculate Interest paid and gives loan balance
			
			Temp = (1 - 1 /Math.pow((1 + IntRate[a]/12), Months));
			interestPaid = (monthlyPayment * Temp);
			loanBal = (LoanAmt - monthlyPayment + interestPaid);
			
			//Prints out the payment amount, interest amount paid & loan balance
			System.out.println("Payment " + (IntCounter ++));
			System.out.println("After payment of " + (money.format(monthlyPayment)));
			System.out.println("Interest paid on loan is " + (money.format(interestPaid)));
			System.out.println("The current balance is " + (money.format(loanBal)));
			System.out.println();
			//Shows new loan amount after receiving payment.
			LoanAmt = loanBal;}
		
			//Pause the screen
			if (IntCounter > 0){				
				try {
					Thread.sleep(2500);
							
			//Loop statement - forumla for loan balance and interest paid
			
		while(termMonths[b] > 0){
			
			//Reduce payments one month at a time
			Months = (termMonths[b] --);
			
			//Calculate Interest paid and gives loan balance
			
			Temp = (1 - 1 /Math.pow(1 + IntRate[b]/12, Months));
			interestPaid = (monthlyPayment * Temp);
			loanBal = (LoanAmt - monthlyPayment + interestPaid);
			
			//Prints out the payment amount, interest amount paid and loan balance
			System.out.println("Payment " + (IntCounter ++));
			System.out.println("After payment of " + (money.format(monthlyPayment)));
			System.out.println("Interest paid on loan is " + (money.format(interestPaid)));
			System.out.println("The current balance is " + (money.format(loanBal)));
			System.out.println();
			
			//Shows new loan amount after receiving payment
			LoanAmt = loanBal;}
		
			//Pause the screen
			if (IntCounter > 0){
				try {
					Thread.sleep(2500);
				
					//Stop loop
					if (termMonths[b] <=0){
					System.out.println("Your Loan is paid off.");}}	
					
							
				
				 catch (InterruptedException e1) {					
					
					//Stop loop
					if (termMonths[b] <=0){
					System.out.println("Your Loan is paid off.");
					
					
					
				//}

The GUI code is as follows: The main problem with it right now is that I cannot get anything but a blank pink screen to come up when I have told it to add the Labels. Can you tell me what I'm doing wrong?

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.*;



public class BeemanGUI1 extends JFrame{
	
	//The window object
	static JFrame aWindow = new JFrame("Beeman's Mortgage Calculator");
	
	public static void main(String[] args)
	{
		Toolkit theKit = aWindow.getToolkit();									//Get window toolkit
		Dimension wndSize = theKit.getScreenSize();								//Get screen size
		
		//Set position of screen in center & size to half screen size
		aWindow.setBounds(wndSize.width/4, wndSize.height/4, 					//Position of window
						  wndSize.width/2, wndSize.height/2);					//Size of Window
		aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);					//Set exit method
		aWindow.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));	//Set cursor
		aWindow.getContentPane().setBackground(Color.PINK);						//Set color of screen background
		aWindow.setVisible(true);												//Display to screen
		
		}											
		
		//Set up variables
		private javax.swing.JLabel AddPanel;
		private javax.swing.JLabel PrincipalLabel;
		private javax.swing.JLabel YearLabel;
		private javax.swing.JLabel IntLabel;
		private javax.swing.JTextField PrincipalTextField;
		private javax.swing.JTextField YearTextField;
		private javax.swing.JTextField IntTextField;
		private javax.swing.JTextField ResultsTextField;
		private javax.swing.JButton CalculateButton;
		
	
					
		public void initializeComponents(){
			//Run Components
			AddPanel = new JLabel();
			PrincipalLabel = new JLabel("Amount");
			PrincipalTextField = new JTextField();
			YearLabel = new JLabel("Term");
			PrincipalTextField = new JTextField();
			IntLabel = new JLabel("Rate");
			PrincipalTextField = new JTextField();
			ResultsTextField = new JTextField();
			CalculateButton = new JButton("Calculate");
			
			//Set the attributes
			PrincipalTextField.setColumns(10);
			YearTextField.setColumns(4);
			IntTextField.setColumns(5);
			ResultsTextField.setColumns(10);
			ResultsTextField.setEditable(false);}
			
			public BeemanGUI1(){
			AddPanel.add(AddPanel);
			AddPanel.add(PrincipalTextField);
			AddPanel.add(PrincipalLabel);
			AddPanel.add(YearTextField);
			AddPanel.add(YearLabel);
			AddPanel.add(IntTextField);
			AddPanel.add(IntLabel);
			AddPanel.add(ResultsTextField);
			AddPanel.add(CalculateButton);
			setVisible(true);
			
		}
		public static void main1(String[] args) {
			new MortgageCalculator();
		}
		private void setResultValue(){
			double Principal = Double.parseDouble(PrincipalTextField.getText());
			double Term = Integer.parseInt(YearTextField.getText());
			double Rate = Double.parseDouble(IntTextField.getText())/100;
			double Results = (Principal * Rate / 12)/(1-1/Math.pow((1+Rate/ 12), Term));
			
			ResultsTextField.setText(Double.toString(Results));}
		
		public void actionPerformed(ActionEvent event){
			System.out.println("Results Button");
			event.getActionCommand();
		
			if("Calculate".equals(CalculateButton)){
				setResultValue();
			
		}}}

Recommended Answers

All 10 Replies

Couple of things to clear up:
Why do You have two JFrames, one extended and the other as a member in the class?

Where you you add any components to the JFrame?

What is the main1() method for?

What does this statement do?
AddPanel.add(AddPanel);

Good question, I am a total newbie. I'm not sure what I'm doing, so there you have it. Can you help me?

Start with a very simple project, learn how it works and expand on it.
Start by writing a class that extends JFrame. Have a main that creates an instance of the class.
Have the class's constructor add a JLabel("Hi there"); to the frame and show the frame.

When you get that to work move on to adding more components to the frame.
Nest components in JPanels and use the layout manager to position the panels on the screen.

When you get that working, come back to this project and look at it. The way it is now is a complete mess and would be easier to start from the beginning than to fix it.

I'd second Norm's advice. Start simple, build up.

In the mean time, what was the trouble you were having with rounding? I didn't see it in your code, and searching didn't turn up an instance of "round". Math has a lot of ways to turn floating-point numbers into integers, all of them could be considered "rounding", so tell us what you tried to do.
(or when you said "math formula", did you mean you were trying to write a rounding formula?)

Okay, I'll start over again, it is due tomorrow night so I don't think I'm going to get it done. Did you look at my first code? I do have a question about how to get the numbers to round and get the decimal points in.

Thank you for your help, I appreciate it, but I'm drowning.

Dear jon.kiparsky,

Down in the bottom where it is system.out.println, and then it starts the loop to show the mortgage payments, interest payments, and loan balance, I could not get those amounts to round or have two decimal points. I usually use $%.2f and that takes care of it, in this case I just kept getting errors.

Thanks, Debora

Okay, I'll start over again, it is due tomorrow night so I don't think I'm going to get it done.

Sure you will. You've already got the computation part done, so all you need to do is bolt on a gui.

Search around and you'll find plenty of examples to build on.


(crossposted - I'll take a look at that point)

Well, I can't reproduce the error. Once I put in some curly braces (and commented out a sleep that seemed superfluous) it printed out all of your dollar amounts in dollar format, as you asked it to.

How weird, it doesn't do that on mine. I wonder why that is? No wonder I got an A in that class, it's just my machine not working properly.

I've taken you guy's advice, I am rewriting from scratch. Thanks for all your help.

Debora

Tis is the output I'm seeing:


Payment 8
After payment of $2859.79
Interest paid on loan is $829.42
The current balance is $184007.12


What are you getting?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.