[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

08/22/2009 at 07:11PM PDT, ID: 24674189
[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!

5.5

Please Help!!!! Pass Variable to another class

Asked by Kelela8 in New to Java Programming

Tags: Java

I know this is a stupid mistake but I am stuck and can't find it. I can't get my interestP and principalP variable to pass to the other class. I have slowly worked out my other problems. Now I have spent all day trying to figure this out and getting no where.
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:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.text.*;
import java.util.*;
import java.lang.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JComponent;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.text.NumberFormat;
 
// Create MortgageCalculator class
public class MortCal5 extends JFrame implements ActionListener
{
	//declare variables
	double principle = 0.0;
	double interest = 0.0;
	int years = 0;
	double months = 0;
	int termArray[] = {7, 15, 30};
 
	double[] rates;
 
	private double intTotal = 0.0;
	private double priTotal = 0.0;
	private double payTotal = 0.0;
 
	 double interestP = 0.0;
	double principalP = 0.0;
 
	double[] values;
	private String[] names;
 
 
 
	JFrame f = new JFrame();
 
	// Text for Mortgage
	JPanel row1 = new JPanel(); // Sets up Panels, Preselect Buttons, and Frame.
	JLabel mortgageLabel = new JLabel("MORTGAGE PAYMENT CALCULATOR",
	JLabel.CENTER);
 
	JPanel row2 = new JPanel(new GridLayout(1, 2));
	JLabel principalLabel = new JLabel("Mortgage Amount $", JLabel.LEFT);
	JTextField principalText = new JTextField(10);
 
	JPanel row3 = new JPanel (new GridLayout(1, 2));
	JLabel termLabel = new JLabel("Mortgage Term (Years)", JLabel.LEFT);
	JTextField termText = new JTextField(3);
 
	JPanel row4 = new JPanel(new GridLayout(1,2));
	JLabel intRateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT);
	JTextField intRateText = new JTextField(5);
 
	JPanel row5 = new JPanel(new GridLayout(1,2));
	JLabel paymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
	JTextField paymentText = new JTextField(10);
 
	JPanel row6 = new JPanel(new GridLayout (1, 4));
	JLabel payLabel = new JLabel("Payment", JLabel.LEFT);
	JLabel prinLabel = new JLabel("", JLabel.LEFT);//Left for spacing and future needs
	JLabel intLabel = new JLabel("Interest", JLabel.LEFT);
	JLabel balLabel = new JLabel("Balance", JLabel.LEFT);
 
	// Create buttons to calculate payment, clear payment field and exit program
	JPanel radioPanel = new JPanel();
	JRadioButton button7 = new JRadioButton("7 Years at 5.35%", false);
	JRadioButton button15 = new JRadioButton("15 Years at 5.50%", false);
	JRadioButton button30 = new JRadioButton("30 Years at 5.75%", false);
 
	JPanel button = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
	JButton calculateButton = new JButton("CALCULATE");
	JButton clearButton = new JButton("CLEAR");
	JButton exitButton = new JButton("EXIT");
 
	JTextArea displayArea = new JTextArea(10, 30);
	JScrollPane scroll = new JScrollPane(displayArea);
 
	JTextArea graphArea = new JTextArea (10, 30);
 
	JButton graphButton = new JButton("DISPLAY GRAPH");
 
	//sets decimal format
	DecimalFormat dollarAmount = new DecimalFormat("0.00");
    DecimalFormat percentAmount = new DecimalFormat("##.##");
 
    DataInputStream istream;
 
	//constructor Builds and displays GUI
	public void frame1()
	{
		this.setTitle("Mortgage Calculator");
		setSize(450, 450);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container pane = getContentPane();
		getRates();
 
		Border rowboarder = new EtchedBorder();
 
		pane.add(row1);
		row1.add(mortgageLabel);
		row1.setMaximumSize(new Dimension (10000, row1.getMinimumSize().height));
 
		pane.add(row2);
		row2.add(principalLabel);
		row2.add(principalText);
		row2.setMaximumSize(new Dimension (10000, row2.getMinimumSize().height));
		principalText.setText("200000");
 
		pane.add(row3);
		row3.add(termLabel);
		row3.add(termText);
		row3.setMaximumSize(new Dimension (10000, row3.getMinimumSize().height));
		termText.setEditable(false);
		termText.setText("30");
 
		pane.add(row4);
		row4.add(intRateLabel);
		row4.add(intRateText);
		row4.setMaximumSize(new Dimension (10000, row4.getMinimumSize().height));
		intRateText.setEditable(false);
		intRateText.setText("5.75");
 
		// set Radio Buttons as group so only one can be selected
		ButtonGroup buttonGrp = new ButtonGroup();
		buttonGrp.add(button7);
		buttonGrp.add(button15);
		buttonGrp.add(button30);
 
		//add radio buttons to GUI
		radioPanel.add(button7);
		radioPanel.add(button15);
		radioPanel.add(button30);
		pane.add(radioPanel);
		radioPanel.setMaximumSize(new Dimension(10000,
		radioPanel.getMinimumSize().height));
 
		pane.add(row5);
		row5.add(paymentLabel);
		row5.add(paymentText);
		row5.setMaximumSize(new Dimension (10000, row5.getMinimumSize().height));
		paymentText.setEditable(false);
 
		// Add Buttons for calculate, clear, and exit
		button.add(calculateButton);
		button.add(clearButton);
		button.add(exitButton);
		pane.add(button);
 
		// Add Listeners to buttons
		calculateButton.addActionListener(this);
		clearButton.addActionListener(this);
		exitButton.addActionListener(this);
		button7.addActionListener(this);
		button15.addActionListener(this);
		button30.addActionListener(this);
		graphButton.addActionListener(this);
 
		//Add labels above text box (Amortization)
		pane.add(row6);
		row6.add(payLabel);
		row6.add(balLabel);
		row6.add(intLabel);
		row6.add(prinLabel);//added for spacing and future need
		row6.setMaximumSize(new Dimension (10000, row6.getMinimumSize().height));
 
		pane.add(row6);
 
		scroll.setBorder(BorderFactory.createEtchedBorder());
 
		pane.add(scroll);
 
		pane.add(graphButton);
 
		pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
		setContentPane(pane);
		setVisible(true);
 
	}
	//Main method
	public static void main(String[] args)throws ParseException
	{
    	MortCal5 frame = new MortCal5();
    	frame.frame1();
 
    }
 
	//opens the rates.txt file and reads the interest rates into the array
	public void getRates()
	{
	    try
	    {
	      ArrayList<String> arrayRates = new ArrayList<String>();
 
	      BufferedReader inputfile = new BufferedReader(new FileReader("rates.txt"));
	      String data;
 
	      while ((data = inputfile.readLine()) != null)
	      {
	        System.out.println(data);
	        arrayRates.add(data);
	      }
 
	      rates = new double[arrayRates.size()];
	      for (int i = 0; i < rates.length; ++i)
	      {
	         rates[i] = Double.parseDouble(arrayRates.get(i));
	      }
 
	      inputfile.close();
	    }
	    catch(Exception ec)
	    {
	      JOptionPane.showMessageDialog(null,
	      "Could not find the file specified", "Error Message",
	      JOptionPane.ERROR_MESSAGE);
	    }
	  }
 
	//Verifies input for positive number and returns payment amount
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == button7)
		{
			years = 7;
			for (int i = 0; i < rates.length; ++i)
			{
				interest = rates[0];
	      	}
		}
		else if (event.getSource() == button15)
		{
			years = 15;
			for (int i = 0; i < rates.length; ++i)
			{
				interest = rates[1];
	      	}
		}
		else if (event.getSource() == button30)
		{
			years = 30;
			for (int i = 0; i < rates.length; ++i)
			{
				interest = rates[2];
	      	}
		}
			intRateText.setText(Double.toString(interest));
			termText.setText(Integer.toString(years));
	    	if (event.getSource() == calculateButton)
	   	{
			try
			{
				displayArea.setText(null);
				String principalTest = principalText.getText();
				principle = Double.parseDouble(principalTest);
			if (principle < 1)
			{
				throw new OutOfRangeException();
			}
			}
			catch (NumberFormatException e)
			{
				principalText.setText("Invalid Amount");
				JOptionPane.showMessageDialog(null, "Enter Positive Numbers Only ", "INVALD ENTRY", JOptionPane.ERROR_MESSAGE);
				return;
			}
			catch (OutOfRangeException e)
			{
				principalText.setText("Invalid Amount");
				JOptionPane.showMessageDialog(null, "Enter Positive Numbers Only ", "INVALD ENTRY", JOptionPane.ERROR_MESSAGE);
				return;
			}
				setResultValue();
	        }
	        //clears fields
		    if (event.getSource() == clearButton)
		    {
	        	principalText.setText("");
	        	termText.setText("");
	        	intRateText.setText("");
	        	paymentText.setText("");
	        	displayArea.setText(null);
	        }
	        if (event.getSource() == graphButton)
	        {
				JFrame f = new JFrame("Chart");
				f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				f.getContentPane().add(new ChartPanel(values, names, "Principle/Interest"));
				f.setSize(400, 300);
				f.setLocation(300,300);
				f.setVisible(true);
				double principleP = 0.0;
				double interestP = 0.0;
				double[] values = new double[2];
				String[] names = new String[2];
 
				values[0] = principleP;
				names[0] = "Principle";
 
				values[1] = interestP;
				names[1] = "Interest";
				System.out.println(interestP);
				f.getContentPane().add(new ChartPanel(values, names, "Principle/Interest"));
    		}
			if (event.getSource() == exitButton)
			{
				System.exit(0);
			}
		}
 
	//Calculates mortgage payment, creates and displays amortization schedule
	public void setResultValue()
	 	{
	   		double amount = Double.parseDouble(principalText.getText());
	   		double term = Double.parseDouble(termText.getText());
	   		double rate = Double.parseDouble(intRateText.getText()) / 100.;
	   		double result = (amount * ( rate/12))/(1-( Math.pow (1/( 1 +(rate/12)), (term*12))));
		    paymentText.setText("" + dollarAmount.format(result));
 
		    double intPaid = 0;
		    double principalPaid = 0;
		    double balance = amount;
		    double monthlyRate = rate / 12;
		    double months = term * 12;
 
 
		    int payment = 1;
 
		    for (int y = 1; y <= term; y++)
		    {
				displayArea.append("");
 
			// Loop to to calculate and display the payment schedule
			for (int m = 0; m < 12; m++)
			{ /* start inner loop */
				intPaid = balance * monthlyRate;
				principalPaid = result - intPaid;
				balance = balance - principalPaid;
 
				displayArea.append("Month " + payment + "\t" + "   " +"   "
				+ dollarAmount.format(balance) + "\t" +"              "
				+ dollarAmount.format(intPaid) + "\n");
				payment++;
	    		displayArea.setCaretPosition(0);
 
	    		intTotal += intPaid;
				priTotal += principalPaid;
				payTotal += result;
 
				interestP = (intTotal)/(payTotal) * 100;
				principalP = (priTotal)/(payTotal) * 100;
						}
 
					}
			System.out.println(intTotal);//temporary to check results of total (will delete)
			System.out.println(priTotal);
				}
 
			}
 
 
//class to extend Exception to validate numbers for negative and 0 input
class OutOfRangeException extends Exception //Burd, B. (2007). Java for Dummies. Indianapolis, IN: Wiley Publishing, Inc..
{
}
 
class ChartPanel extends JPanel {
 
  private double[] values;
  private String[] names;
  private String title;
 
 
 
  public ChartPanel(double[] v, String[] n, String t)
  {
    names = n;
    values = v;
    title = t;
  }
 
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    if (values == null || values.length == 0)
      return;
    double minValue = 0;
    double maxValue = 0;
    for (int i = 0; i < values.length; i++) {
      if (minValue > values[i])
        minValue = values[i];
      if (maxValue < values[i])
        maxValue = values[i];
  }
 
    Dimension d = getSize();
    int clientWidth = d.width;
    int clientHeight = d.height;
    int barWidth = clientWidth / values.length;
 
    Font titleFont = new Font("SansSerif", Font.BOLD, 20);
    FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
    Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
    FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);
 
    int titleWidth = titleFontMetrics.stringWidth(title);
    int y = titleFontMetrics.getAscent();
    int x = (clientWidth - titleWidth) / 2;
    g.setFont(titleFont);
    g.drawString(title, x, y);
 
    int top = titleFontMetrics.getHeight();
    int bottom = labelFontMetrics.getHeight();
    if (maxValue == minValue)
      return;
    double scale = (clientHeight - top - bottom) / (maxValue - minValue);
    y = clientHeight - labelFontMetrics.getDescent();
    g.setFont(labelFont);
 
    for (int i = 0; i < values.length; i++) {
      int valueX = i * barWidth + 1;
      int valueY = top;
      int height = (int) (values[i] * scale);
      if (values[i] >= 0)
        valueY += (int) ((maxValue - values[i]) * scale);
      else {
        valueY += (int) (maxValue * scale);
        height = -height;
      }
 
      g.setColor(Color.lightGray);
      g.fillRect(valueX, valueY, barWidth - 2, height);
      g.setColor(Color.black);
      g.drawRect(valueX, valueY, barWidth - 2, height);
      int labelWidth = labelFontMetrics.stringWidth(names[i]);
      x = i * barWidth + (barWidth - labelWidth) / 2;
      g.drawString(names[i], x, y);
    }
  }
}
Attachments:
 
rates.txt file
 
 
Keywords: Please Help!!!! Pass Variable to a…
 
Loading Advertisement...
 
[+][-]08/22/09 09:12 PM, ID: 25161330

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/22/09 09:32 PM, ID: 25161363

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

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

 
[+][-]08/22/09 09:39 PM, ID: 25161374

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/22/09 09:43 PM, ID: 25161379

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

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

 
[+][-]08/25/09 10:01 AM, ID: 25179638

View this solution now by starting your 30-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

Zone: New to Java Programming
Tags: Java
Sign Up Now!
Solution Provided By: stmani2005
Participating Experts: 2
Solution Grade: B
 
 
[+][-]08/31/09 01:54 AM, ID: 25221331

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-91 - Hierarchy / EE_QW_3_20080625