Yes this is homework, having cleared that up what I want to do is compile,
and run these 2 programs simultaneously. Any help would be appreciated.
*/
import java.awt.*; /* Import statements */
import javax.swing.*;
import java.awt.event.*;
class Assignmentweek2
{
public static void main(String args[])
{
Interest i=new Interest();
i.init();
i.setVisible(true);
}
}
class Interest extends JFrame implements ActionListener
{
JPanel a,i,y,r,b,all; /*GUI */
JLabel am,it,yr,rt;
JTextField amt,itr,yrs,rate;
JButton cal,clr;
float amount,years,rate_in,interest;
public Interest()
{
setTitle("Find Payment"); /*GUI specs */
setSize(500,400);
setLocation(200,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
a=new JPanel();
i=new JPanel();
y=new JPanel();
r=new JPanel();
b=new JPanel();
all=new JPanel(new FlowLayout(FlowLayout.CENTER,1000,20));
am=new JLabel("Enter Amount");
it=new JLabel("Total Monthly Payment is");
yr=new JLabel("Enter Years");
rt=new JLabel("Enter Interest rate");
amt=new JTextField(10);
itr=new JTextField(10);
itr.setEditable(false);
yrs=new JTextField(10);
rate=new JTextField(10);
cal=new JButton("Calculate");
clr=new JButton("New Amount");
}
public void init()
{
setLayout(new BorderLayout());
a.add(am);
a.add(amt);
y.add(yr);
y.add(yrs);
r.add(rt);
r.add(rate);
i.add(it);
i.add(itr);
b.add(cal);
b.add(clr);
all.add(a);
all.add(y);
all.add(r);
all.add(i);
add(b,BorderLayout.SOUTH);
add(all,BorderLayout.CENTER);
cal.addActionListener(this);
clr.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
try
{
if(cal==e.getSource())
{
amount=Float.parseFloat(amt.getText());
/*Program computations */
years=Float.parseFloat(yrs.getText());
rate_in=Float.parseFloat(rate.getText());
interest=amount/years*rate_in/100+amount/years/12;
itr.setText(Float.toString(interest));
}
else if(clr==e.getSource())
{
amt.setText("");
yrs.setText("");
rate.setText("");
itr.setText("");
}
}
catch(NumberFormatException n) /*Resets GUI
to 0's if wrong # format entry is attempted */
{
amt.setText("0");
yrs.setText("0");
rate.setText("0");
itr.setText("0");
}
}
}/* END*/
-------------------------------------Along with this program------------------
----------------------------------------------------------
*/
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
public class Week3 extends JFrame implements ActionListener
{
//Components
JPanel lpanel = new JPanel();
JPanel tpanel = new JPanel();
JPanel p1 = new JPanel();
JPanel pAmount = new JPanel();
JPanel pInterest = new JPanel();
JPanel pTermlngth = new JPanel();
JPanel pPymtresult = new JPanel();
JPanel pButton = new JPanel();
JLabel lAmount = new JLabel();
JLabel lTermlngth = new JLabel();
JLabel lRate = new JLabel();
JLabel lTitle = new JLabel();
JLabel lCombo = new JLabel();
JLabel lPymtresult = new JLabel();
JLabel lMain = new JLabel();
JLabel lMonthlypymt = new JLabel();
JLabel lBalance = new JLabel();
JLabel lInterest = new JLabel();
JButton bCalc = new JButton();
JButton bMonthlypymt = new JButton();
JButton bQuit = new JButton();
JTextField tAmount = new JTextField(8);
JTextField tTermlngth = new JTextField();
JTextField tRate = new JTextField();
JTextField tMonthlypymt = new JTextField(8);
JTextField tPymtresult = new JTextField(8);
JTextField tCombo = new JTextField();
JTextField tBalance = new JTextField();
JTextField tInterest = new JTextField();
String sErrorMessage = "";
boolean bError = false;
String sPymtnumber = "######";
String sPercent = "###.###%";
String sAreaheader = " Payment\tPrinciple\tInterest\tBalance\n";
NumberFormat nfCurrency = NumberFormat.getCurrencyInstance();
NumberFormat nfDecimals = new DecimalFormat(sPymtnumber);
NumberFormat nfPercent = new DecimalFormat(sPercent);
DecimalFormat decimalPlaces=new DecimalFormat("0.00");
JComboBox cbSetup = new JComboBox();
JTextArea taArea = new JTextArea(20,40);
JScrollPane spScrollpanel = new JScrollPane(taArea);
FlowLayout lFlow = new FlowLayout(FlowLayout.LEFT,5,5);
//Variables
double dIntpaid = 0;
double dAmountpaid = 0;
double dTotaldue = 0;
double dMonthlyapr = 0;
double dAmount = 0;
double dApr = 0;
double dAprconv = 0;
double dPymt = 0;
double dTotinterest = 0;
double dTotbalance = 0;
double dMonthlypymt = 0;
double dYearlyrate = 0;
double dTermlngthconv = 0;
double dTermlngth = 0;
int iCounter = 0;
int iTermlngth = 0;
int iTotmonths = 0;
//Initialize array
double[] xRate = {.0535,.0550,.0575};
int[] xTerm = {7,15,30};
public static void main(String[] args)
{
new Week3();
}
public Week3()
{
Container main = getContentPane();
for(iCounter = 0; iCounter <= 2; iCounter++)
{
cbSetup.addItem(xTerm[iCounter] + " years - " + nfPercent.format(xRate
[iCounter]));
}
//Set editables
tMonthlypymt.setEditable(false);
tMonthlypymt.setBackground(Color.white);
tBalance.setEditable(false);
tBalance.setBackground(Color.white);
tInterest.setEditable(false);
tInterest.setBackground(Color.white);
tRate.setEditable(false);
tRate.setBackground(Color.white);
tTermlngth.setEditable(false);
tTermlngth.setBackground(Color.white);
taArea.setEditable(false);
taArea.setBackground(Color.white);
tAmount.setEditable(true);
tAmount.setBackground(Color.white);
//Read text panel
lpanel.setLayout(new GridLayout(8,1));
lpanel.setMinimumSize(new java.awt.Dimension(130,150));
lpanel.setPreferredSize(new java.awt.Dimension(130,150));
lpanel.setMaximumSize(new java.awt.Dimension(Short.MAX_VALUE,Short.MAX_VALUE))
;
lpanel.add(lCombo);
lpanel.add(lAmount);
lpanel.add(lRate);
lpanel.add(lTermlngth);
lpanel.add(lMonthlypymt);
lpanel.add(lBalance);
lpanel.add(lInterest);
lCombo.setText("Payback Option");
lAmount.setText("Amount=150000 etc.");
lRate.setText("APR");
lTermlngth.setText("Length of Loan");
lMonthlypymt.setText("Monthly Payment");
lBalance.setText("Existing Balance");
lInterest.setText("Existing Interest");
//Write text panel
tpanel.setLayout(new GridLayout(8,1));
tpanel.setMinimumSize(new java.awt.Dimension(100, 150));
tpanel.setPreferredSize(new java.awt.Dimension(100, 150));
tpanel.setMaximumSize(new java.awt.Dimension(Short.MAX_VALUE,Short.MAX_VALUE))
;
tpanel.add(cbSetup);
tpanel.add(tAmount);
tpanel.add(tRate);
tpanel.add(tInterest);
tpanel.add(tTermlngth);
cbSetup.addActionListener(this);
tpanel.add(tMonthlypymt);
tpanel.add(tBalance);
tpanel.add(tInterest);
//Create border layout
p1.setBorder(BorderFactory.createEmptyBorder(10, 10, 2, 10));
p1.setLayout(new BorderLayout());
p1.add(lpanel, "West");
p1.add(tpanel, "East");
main.add(p1, "West");
//Buttons
pButton.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
pButton.setLayout(new BorderLayout());
bCalc.setText("Calculate Mortgage");
main.add(bCalc);
bMonthlypymt.setText("See all Payments");
main.add(bMonthlypymt);
bQuit.setText("Quit");
main.add(bQuit);
main.add(pButton);
//Scrollable
spScrollpanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
main.add(spScrollpanel);
main.setLayout(lFlow);
//Listeners
bCalc.addActionListener(this);
bMonthlypymt.addActionListener(this);
bQuit.addActionListener(this);
//Main title
this.setSize(650,550);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Mortgage Week 3!");
this.setVisible(true);
}
public void actionPerformed(ActionEvent thisEvent)
{
Object source = thisEvent.getSource();
//Declare exceptions
if(source == bCalc)
{
try
{
iTermlngth = Integer.parseInt(tTermlngth.getText());
dAmount = Double.parseDouble(tAmount.getText());
dApr = Double.parseDouble(tRate.getText());
}
catch(NumberFormatException e)
{
sErrorMessage = sErrorMessage + "Invalid";
bError = true;
}
if (iTermlngth <= 0 || dAmount <= 0 || dApr <= 0)
{
sErrorMessage = sErrorMessage + "Invalid";
bError = true;
}
if(bError == true)
{
JOptionPane.showMessageDialog(this, sErrorMessage, "Invalid", JOptionPane.
ERROR_MESSAGE);
tMonthlypymt.setText("");
}
//Mortgage math
else
{
dTermlngthconv = iTermlngth / 12;
dAprconv = dApr / 100;
dMonthlypymt = (((dAmount * dAprconv) * dTermlngthconv) + dAmount) /
iTermlngth;
dTotbalance = dMonthlypymt * iTermlngth;
dTotinterest = dTotbalance - dAmount;
tMonthlypymt.setText(nfCurrency.format(dMonthlypymt));
tBalance.setText(nfCurrency.format(dTotbalance));
tInterest.setText(nfCurrency.format(dTotinterest));
bMonthlypymt.setEnabled(true);
}
}
if(source == bMonthlypymt)
{
dMonthlyapr = (dApr / 100) / 12;
dTotaldue = dAmount;
taArea.setText(sAreaheader);
for(int iCounter = 0; iCounter <= iTermlngth - 1; iCounter++)
{
dIntpaid = dTotaldue * dMonthlyapr;
if(dIntpaid <= 0)
dIntpaid = 0;
dAmountpaid = dMonthlypymt - dIntpaid;
dTotaldue = dTotaldue - dAmountpaid;
taArea.append(" "
+nfDecimals.format(iCounter + 1) + "\t"
+nfCurrency.format(dAmountpaid) + "\t"
+nfCurrency.format(dIntpaid) + "\t"
+nfCurrency.format(dTotaldue) + "\n");
taArea.setCaretPosition(0);
}
}
//Display all payments
if(source == cbSetup)
{
int cbStart = cbSetup.getSelectedIndex();
iTotmonths = xTerm[cbStart]* 12;
dYearlyrate = xRate[cbStart]* 100;
tTermlngth.setText(Integer.toString(iTotmonths));
tRate.setText(Double.toString(dYearlyrate));
tAmount.requestFocus();
}
//exit program
if(source == bQuit)
{
System.exit(0);
}
}
} /*END*/
-----------------------------------------------------any help would be
appriciated---------------------------------------------------
Fred Kleinschmidt - 25 Apr 2007 22:27 GMT
> Yes this is homework, having cleared that up what I want to do is compile,
> and run these 2 programs simultaneously. Any help would be appreciated.
[quoted text clipped - 28 lines]
> <snip>
> }
New class Both.java:
public static void main(String[] args)
{
Week3 w3 = new Week3();
Interest i=new Interest();
i.init();
i.setVisible(true);
}

Signature
Fred L. Kleinschmidt
Willliwill - 27 Apr 2007 16:10 GMT
Thanks I will give it a try, is there any way to make both programs open up
in their own respective windows as individual Java apps ?
>> Yes this is homework, having cleared that up what I want to do is compile,
>> and run these 2 programs simultaneously. Any help would be appreciated.
[quoted text clipped - 10 lines]
> i.setVisible(true);
>}
Thomas A. Russ - 26 Apr 2007 19:05 GMT
> Yes this is homework, having cleared that up what I want to do is compile,
> and run these 2 programs simultaneously. Any help would be appreciated.
OK. Here's a general plan of attack. For your week2 assignment, split
the program into two parts: One is the Interest JFrame and the other is
either a class or else a static method that just builds the "all" panel,
and one that builds the button JPanel.
In other words, move the construction of the "all" and "b" panels out of
the Frame construction and make them separately callable.
This will make your Frame code constructor very simple, since it will
pretty much just call
setTitle("Find Payment"); /*GUI specs */
setSize(500,400);
setLocation(200,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(Interest.makeButtonPanel(),BorderLayout.SOUTH);
add(Interest.makeInputPanel(),BorderLayout.CENTER);
That way, you can use the makeButtonPanel and makeInputPanel methods to
create the components and add them to the interface for your second week
project. This will require some reworking of the action handler for the
week1 project, since it will no longer have direct access to the fields
in the InputPanel, but rather will have to get them indirectly. But
that gives you the needed flexibility.
[Snip long code listing]

Signature
Thomas A. Russ, USC/Information Sciences Institute
Willliwill - 27 Apr 2007 16:08 GMT
Thanks I will give it a try, is there any way to make both programs open up
in their own respective windows as individual Java apps ?
>Yes this is homework, having cleared that up what I want to do is compile,
>and run these 2 programs simultaneously. Any help would be appreciated.
[quoted text clipped - 416 lines]
>-----------------------------------------------------any help would be
>appriciated---------------------------------------------------