Solved

my program runs but doesn't calculate

Posted on 2004-10-04
10
336 Views
Last Modified: 2010-03-31
my program runs but doesn't calculate. I know it is something simple but I am drawing blanks right now.


/*MortgageCalculator

 Assignment Week 2 */

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;

public class MortgageCalculator2 extends JApplet {

JLabel label_1;
JLabel label_2;
JLabel label_3;
JTextField textfield_1;
JTextField textfield_2;
JTextField textfield_3;
JButton Calculate;
JLabel label_4;
JButton Start_Over;
JLabel label_5;

public void init() {

MortgageCalculator2Layout customLayout = new MortgageCalculator2Layout();

Container con = getContentPane();
con.setFont(new Font("Helvetica", Font.PLAIN, 12));
con.setLayout(customLayout);

label_1 = new JLabel("Loan Amount");
con.add(label_1);

label_2 = new JLabel("Interest Rate");
con.add(label_2);

label_3 = new JLabel("Term");
con.add(label_3);

textfield_1 = new JTextField("");
con.add(textfield_1);

textfield_2 = new JTextField("");
con.add(textfield_2);

textfield_3 = new JTextField("");
con.add(textfield_3);

JButton Calculate = new JButton("Calculate");
con.add(Calculate);
Calculate.addMouseListener(new Listener());

label_4 = new JLabel("Payment");
con.add(label_4);

JButton Start_Over = new JButton("Start Over");
con.add(Start_Over);

label_5 = new JLabel("$ ");
con.add(label_5);

setSize(getPreferredSize());

}
public class Listener extends MouseAdapter{
public void mouseClicked(MouseEvent me){
label_4.setText(label_5.getText());
}

public void main(String args[]) {
MortgageCalculator2 applet = new MortgageCalculator2();
JFrame window = new JFrame("Mortgage Calculator");

window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

applet.init();
window.getContentPane().add("Center", applet);
window.pack();
window.setVisible(true);
}
}

class MortgageCalculator2Layout implements LayoutManager {

public MortgageCalculator2Layout() {
}

public void addLayoutComponent(String name, Component comp) {
}

public void removeLayoutComponent(Component comp) {
}

public Dimension preferredLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);

Insets insets = parent.getInsets();
dim.width = 539 + insets.left + insets.right;
dim.height = 394 + insets.top + insets.bottom;

return dim;
}

public Dimension minimumLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
return dim;
}

public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();

Component c;
c = parent.getComponent(0);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,96,24);}
c = parent.getComponent(1);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,96,24);}
c = parent.getComponent(2);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+72,96,24);}
c = parent.getComponent(3);
if (c.isVisible()) {c.setBounds(insets.left+112,insets.top+8,112,24);}
c = parent.getComponent(4);
if (c.isVisible()) {c.setBounds(insets.left+112,insets.top+40,112,24);}
c = parent.getComponent(5);
if (c.isVisible()) {c.setBounds(insets.left+112,insets.top+72,112,24);}
c = parent.getComponent(6);
if (c.isVisible()) {c.setBounds(insets.left+112,insets.top+104,112,24);}
c = parent.getComponent(7);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+136,96,24);}
c = parent.getComponent(8);
if (c.isVisible()) {c.setBounds(insets.left+112,insets.top+192,112,24);}
c = parent.getComponent(9);
if (c.isVisible()) {c.setBounds(insets.left+112,insets.top+136,72,24);}
}
}

// This will add the applet to a JFrame

public static void main(String[] args)
{
JFrame frame = new JFrame();
MortgageCalculator2 cal = new MortgageCalculator2();
frame.getContentPane().add(cal);
cal.init();
frame.pack();
frame.show();
}
}

0
Comment
Question by:melverc
  • 2
  • 2
  • 2
  • +2
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12223785
i don't see any code in there to the calculation.
Or is that the problem ;)
0
 

Author Comment

by:melverc
ID: 12223829
that would help
0
 
LVL 92

Expert Comment

by:objects
ID: 12223852
sorry we can only help with your code, we can't write it for you.
0
 

Author Comment

by:melverc
ID: 12223863
is this what I need to add??
/*Program Variables*/
double I = rates[i];//*Interest rate
double P = 200000;//*Loan amount
double J = I/12; //*Monthy interest
int N = terms[i];

//calculate monthly payment
double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
double payment = P / discount; //*Rate Calculation
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
aviadbd earned 250 total points
ID: 12224067
That might help.

And use an ActionListener - You're using a MouseListener on a JButton. Its probably best practice to use an ActionListener for this kind of control.

Aviad.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12224121
>is this what I need to add??
>/*Program Variables*/
>double I = rates[i];//*Interest rate
>double P = 200000;//*Loan amount
>double J = I/12; //*Monthy interest
>int N = terms[i];
>//calculate monthly payment
>double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
>double payment = P / discount; //*Rate Calculation

I don't see any rates array, terms array any I...

First of all I suggest you to use only downcase for object instance; so for example P became p, etc.

I suggest you to design on a blank screen the input and the output for your program (mortgage maybe); then design something (button, choice, ...) you need to insert and specify the start of calculation; then a method who did the calculation you want.

Bye, Giant.
0
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 250 total points
ID: 12224720
Hi,

Well, your codes are not bad :).

Firstly, you have the formula already. So it is only last way left, get the values from your application and display the result. You can get all the value from your textbox by having

// Sample
JTextField textfield_1;
textfield_1 = new JTextField();

int a = Integer.parseInt(textfield_1.getText());  // This will give you the content of the textfield.
// End of Sample

Now, you have obtained each value that you required. You may have your rates hardcoded (it is fixed no matter how your application behave). For example, if you want to multiply the first value in array with the content of the textfield_1, you can do like this : rates[0] *  Integer.parseInt(textfield_1.getText());  

Another question comes.....where and how should I invoke or activate the calculation on my program? Pretty simple, the idea is to attach an ActionListener on your button. ActionListener will execute a bolck of Java codes that you created as a  response when you press your button or etc.

Look at the sample of ActionListener
1. http://k2pts.home.comcast.net/gbaguy/swing.htm
2. http://users.ox.ac.uk/~worc1535/ CalcApplication.java

I hope that helps....:). Come on, NEVER give up. You have good Java codes already......
 
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12482693
I don't understand why the split didn't even about my answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Saving Tweets with emojis using Twitter4J in a Mysql table 2 73
Java asynchronous logging 4 50
solarwind tftp server 2 44
factorial example 4 34
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now