Welcome to Dream.In.Code
Become a Java Expert!

Join 149,499 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,387 people online right now. Registration is fast and FREE... Join Now!




Need help

 
Reply to this topicStart new topic

Need help, Spacing and blocking

Ghost_Rider
26 Mar, 2007 - 02:14 PM
Post #1

New D.I.C Head
*

Joined: 16 Mar, 2007
Posts: 4


My Contributions
Need help getting my program to space and block correctly. Is there someone out there that can help me?


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java. text.*;

public class wk3_jrs extends JFrame implements ActionListener
{
JLabel amountLabel = new JLabel("Principle Loan");
JTextField amountField = new JTextField(7);
JLabel termLabel1 = new JLabel("Term of loan");
JTextField termField1 = new JTextField(4);
JLabel interestLabel1 = new JLabel("Interest Rate");
JTextField interestField1 = new JTextField(5);
JTextField paymentField1 = new JTextField(9);
JLabel termLabel2 = new JLabel("Term of loan");
JTextField termField2 = new JTextField(4);
JLabel interestLabel2 = new JLabel("Interest Rate");
JTextField interestField2 = new JTextField(5);
JTextField paymentField2 = new JTextField(9);
JLabel termLabel3 = new JLabel("Term of loan");
JTextField termField3 = new JTextField(4);
JLabel interestLabel3 = new JLabel("Interest Rate");
JTextField interestField3 = new JTextField(5);
JTextField paymentField3 = new JTextField(9);
JButton calculate = new JButton("Calculate Payment");
JButton myButton = new JButton("Clear");
JButton myButton1 = new JButton("Exit");

public wk3_jrs()
{
super("Joe's Mortgage Calculator");
setSize(700, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container pane = getContentPane();

FlowLayout layout = new FlowLayout();

pane.add(amountLabel);
pane.add(amountField);
pane.add(termLabel1);
pane.add(termField1);
pane.add(interestLabel1);
pane.add(interestField1);
pane.add(paymentField1);
System.out.println ("\n");
pane.add(termLabel2);
pane.add(termField2);
pane.add(interestLabel2);
pane.add(interestField2);
pane.add(paymentField2);
System.out.println ("\n");
pane.add(termLabel3);
pane.add(termField3);
pane.add(interestLabel3);
pane.add(interestField3);
pane.add(paymentField3);
pane.add(calculate);
pane.add(myButton);
pane.add(myButton1);
pane.add(myButton);
pane.add(myButton1);

pane.setLayout(layout);
setContentPane(pane);
pane.setBackground(Color.red);


amountField.addActionListener(this);
termField1.addActionListener(this);
interestField1.addActionListener(this);
paymentField1.addActionListener(this);
termField2.addActionListener(this);
interestField2.addActionListener(this);
paymentField2.addActionListener(this);
termField3.addActionListener(this);
interestField3.addActionListener(this);
paymentField3.addActionListener(this);
calculate.addActionListener(this);
myButton.addActionListener(this);
myButton1.addActionListener(this);

}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();

if(source == calculate)

{
double a = 200000;
double[] t = {30, 15, 7};
double[] i = {0.0575, 0.0550, 0.0575};
double payment1, payment2, payment3;

payment1 = (a*((i[0]/12)/(1-Math.pow((1+(i[0]/12)), -(t[0]*12)))));
payment2 = (a*((i[1]/12)/(1-Math.pow((1+(i[1]/12)), -(t[1]*12)))));
payment3 = (a*((i[2]/12)/(1-Math.pow((1+(i[2]/12)), -(t[2]*12)))));
DecimalFormat decimalpoint = new DecimalFormat("0,000.00");

paymentField1.setText("$ "+decimalpoint.format (payment1));
paymentField2.setText("$ "+decimalpoint.format (payment2));
paymentField3.setText("$ "+decimalpoint.format (payment3));
}
if(source == myButton)
{
amountField.setText(null);
termField1.setText(null);
interestField1.setText(null);
paymentField1.setText(null);
termField2.setText(null);
interestField2.setText(null);
paymentField2.setText(null);
termField3.setText(null);
interestField3.setText(null);
paymentField3.setText(null);
}

if(source == myButton1)
{
System.exit(0);
}
}
public static void main(String[]args)
{
wk3_jrs calc = new wk3_jrs();
}
}

User is offlineProfile CardPM
+Quote Post

keems21
RE: Need Help
26 Mar, 2007 - 02:36 PM
Post #2

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
The spacing actually doesn't look all that bad. Next time, make sure you put code tags around your code. It really makes everything look nice.

If you are coding for yourself, then you shouldn't have to worry about any of these issues. Following coding standards like those set forth by sun are there to make your code readable for others.
Here are a few suggestions that should keep your code looking clean enough:
-indent each line one time for each unclosed bracket ({) at that given place
-indent under loops and if/else statements, if the code is to be run only under the given conditions
-try not to have a line of code with more than one semicolon in it (semicolons should indicate the end of a line)


There are coding standards out there. Here's what sun has to say on the issue:
http://java.sun.com/docs/codeconv/

In addition, there are also programs that you can run your code through that will automatically format your code nice and neatly. Check out this if your interested:
http://www.tiobe.com/jacobe.htm
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:22PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month