Advertisement

07.12.2008 at 07:39PM PDT, ID: 23560298
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.9

Program Compiles and runs - but crash's if i make a selection

Asked by neonate928 in Java Programming Language, New to Java Programming

Tags:

I have an assignment for creating a mortgage calculater with a GUI, that allows a user to input the loan amount in, and then select the length of the loan. The program then is to calculate the payments made and the remaining balance. The below code snippit gives a further explination.

The problem I am having is when I make a selection in the ComboBox, I get a bunch of garbage in command prompt that I do not understand. Can someone please review my code and tell me what I am doing wrong?

FYI - currently I only have the principal text field, the Combo box and the button showing in the window. This is to help me try and take baby steps. The button event is commented out and therefore wont do anything either.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
/***************************************************************************
* Week 3: Individual Assignment #2
*
* Complete Change Request #5 in Service Request SR-mf-003. Insert comments 
* in the program to document the program Attach a design flow chart to the 
* source code of the program.
*
* Change Request #5:
* Write the proram in Java (with a graphical user interface) and have it 
* calculate and display the mortgage payment amount from user input of the 
* amount of the mortgage and the user's selection from a menu of avaliable
* mortgage loans:
*
* - 7 years at 5.35%
* - 15 years at 5.5%
* - 30 years at 5.75%
*
* Use an array for the mortgage data for the different loans. Display the 
* mortgage payment amount followed by the loan balance and interest paid for 
* each payment over the term of the loan. Allow the user to loop back and 
* enter a new amount and make a new selection or quit. Please insert comments
* in the progam to document the program.
*
*
*****************************************************************************/
import javax.swing.*;
 
 
//imported io file for the math method
import java.io.*;
//imported decimalformt for its format method
import java.text.DecimalFormat;
 
 
public class MortgageCalGUI extends javax.swing.JFrame
{
 
	/** Creating a new form MortgageCalGUI **/
	public MortgageCalGUI()
	{
		//call to method Components() to initilize the new from
		Components();
		
	}
 
	/** The Components() method is called within our MortgageCalGUI constructor to initilize our new MortgageCalGUI form **/
	private void Components()
	{
		//creating text fields
		txtfld_principal = new javax.swing.JTextField();
 
		//creating labels 
		//principal label field
		label_principal = new javax.swing.JLabel();
		label_principal.setText("Principal (No comma's)"); 
		//term label field
		label_term = new javax.swing.JLabel();
		label_term.setText("Select your term year: ");
		
		//create a combo box for our terms
		bx_term = new javax.swing.JComboBox(terms);
		bx_term.setSelectedIndex(0);
		
		/*
		create a monthly label to be used for monthly payment output
		label_monthly = new javax.swing.JLabel();
		label_monthly.setText("$0.00"); 
		*/
 
		//creating Button
		btn_calculate = new javax.swing.JButton();
		btn_calculate.setText("Calculate");
 
		//create out event handler for our combo box
		bx_term.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				HandleComboBox(evt);
			}
		});
		
		//create our event handler for our calculate button
		btn_calculate.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				CalculationPerformed(evt);
			}
		});		
		
		//create our "escape" route
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		
		//create a new frame nameing it Mortgage Calculator
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //using our "escape" route for our new frame
		
		//set the layout of the window
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		
		/*** HORIZONTAL POSITION LAYOUT ***/
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
 
				/*** THE PRINCIPAL TEXT FIELD HORIZONTAL POSITION ***/
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(layout.createSequentialGroup()
						.addComponent(txtfld_principal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(label_principal))
 
				/* THE TERM LABEL FIELD HORIZONTOAL POSITION ***/
					.addGroup(layout.createSequentialGroup()
						.addComponent(label_term))
 
				/*** THE COMBO BOX HORIZONTAL POSITION ***/
					.addGroup(layout.createSequentialGroup()
						.addComponent(bx_term))
 
				/*** THE BUTTON HORIZONTAL POSITION ***/
						.addComponent(btn_calculate))
 
				.addContainerGap(27,Short.MAX_VALUE))
		);
		
		layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { 
			btn_calculate,
			bx_term,
			txtfld_principal,
			label_term }
		);
 
		/*** VERTICAL POSITION LAYOUT ***/
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
 
				/*** THE PRINCIPAL TEXT FIELD VERTICAL POSITION ***/
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(txtfld_principal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
					.addComponent(label_principal))
 
				/*** THER TERM LABEL FIELD VERTICAL POSITION ***/
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(label_term))
 
				/*** THE COMBO BOX VERTICAL POSITION ***/
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(bx_term))
 
				/*** THE CALCULATE BUTTON VERTICAL POSITION ***/
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(btn_calculate))
 
				.addContainerGap(21, Short.MAX_VALUE))
		);
		pack();
	}
	
	//HandleComboBox is called by the event handle for our combo box
	//Creates a temporary combo box to help set the arrays and to set the correct interest rates into the interest text field
	private void HandleComboBox(java.awt.event.ActionEvent evt)
	{
		//creating a temporary combo box
		//the source of our event is our combo box (bx_term)
		javax.swing.JComboBox temp_cb = (JComboBox)evt.getSource();
		
		//creating a temporary Sting to hold our selection from our combo box
		String temp_term = (String)temp_cb.getSelectedItem();
		txtfld_term.setText(temp_term + " years");
 
		//setting the index to our interest array
		int index = 0;
		switch((int)(Integer.parseInt(temp_term)))
		{
			case 7 :
				index = 0;
				break;
			case 15 :
				index = 1;
				break;
			case 30 :
				index = 2;
				break;
		}
		//using the index for our interest array
		txtfld_interest.setText(interest[index]+"");
	}
		
	//CalculationPerformed is called by the event handler for out button
	//calculates the inputed values and then updates the monthly payment label
	private void CalculationPerformed(java.awt.event.ActionEvent evt)
	{
		
		/*//calculate payment based on inputed values
		final double temp_mthly = (temp_principal * (temp_interest/12)) / (1-Math.pow(1 + temp_interest/12,-(temp_term*12)));
		
		label_monthly.setText(DecimalPlaces.format(temp_mthly) + " Monthly"); */
	}	
 
	public static void main(String[] args)
	{
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new MortgageCalGUI().setVisible(true);
			}
		});
	}
 
	/*** VARIABLES AND OBJECT DECLARATIONS ***/
	//labels
	private javax.swing.JLabel label_principal;
	private javax.swing.JLabel label_term;
	private javax.swing.JLabel label_interest;
	private javax.swing.JLabel label_monthly;
	private javax.swing.JLabel label_pymntschedule;
	
	//text fields
	private javax.swing.JTextField txtfld_principal;
	private javax.swing.JTextField txtfld_term;
	private javax.swing.JTextField txtfld_interest;
	private javax.swing.JTextField txtfld_monthly;
	private javax.swing.JTextField txtfld_pymntschedule;
	
	//arrays
	private String[] terms    = { "7", "15", "30" };
	private double[] interest = { 0.0535, 0.055, 0.0575 };
	
	//buttons
	private javax.swing.JComboBox bx_term;
	private javax.swing.JButton btn_calculate;
 
	//scroll pane
	private javax.swing.JScrollPane scroll_payments;
 
	//Delcaing object
	//declaring "decimalPlaces" as a new object type of DecimalFormat with the format of 0.00
	//this ensures any vaiables used by decimalPlaces has a "currancy" value of two decimal places
	private DecimalFormat DecimalPlaces = new DecimalFormat("$#,###,###.##");
}
 
Loading Advertisement...
 
[+][-]07.13.2008 at 12:44AM PDT, ID: 21991621

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.13.2008 at 04:31AM PDT, ID: 21992043

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java Programming Language, New to Java Programming
Tags: Java
Sign Up Now!
Solution Provided By: CEHJ
Participating Experts: 2
Solution Grade: A
 
 
[+][-]07.13.2008 at 12:33PM PDT, ID: 21993849

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628