I am having problems with the following code. Here is my assignment for this week, Write the program in Java (with a GUI) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit. Please insert comments in the program to document the program.
So the first code that I will post in the code I turned in for my last assignment in Java I, and I did get an "A" on it. But there are somethings I cannot figure out with it too. Such as, why I can't get the math formula to round my numbers. This is the code that is supposed to be used with the GUI code that I will post second.
/*Java Programming I - PRG/420
Instructor: Richard Xue
Student: Debora Beeman
Assignment Due: Week 5, Day 7*/
import java.io.*;
import java.text.DecimalFormat;
public class MortgageCalculator
{
public static void main(String[] args) throws Exception
{
//Assign variable for financial and term information
double LoanAmt = 200000.00;
double[] IntRate = {.0535, .0550, .0575};
double Int = 0;
int[] termMonths = {84, 180, 360};
double Months = 360;
double monthlyPayment = 0;
double interestPaid = 0;
double loanBal = 200000;
double Temp = 0;
int IntCounter = 0;
DecimalFormat money = new DecimalFormat("$0.00");
int a;
int b;
int c;
int d;
int e;
int f;
//Loop for going through array and displaying payment amount.
for (a = 0; a <= 2; a++){
IntCounter = IntCounter +1;
for (b = 0; b <= termMonths [b]; b++){
LoanAmt = 200000;
loanBal = 200000;
//formula to calculate monthly payment amount
monthlyPayment = (LoanAmt*(IntRate[a]/12))/(1-1/Math.pow((1+IntRate[a]/12), termMonths[a]));
//Print monthly payment, over term of loan, years with interest rate of .0535.
System.out.println("Monthly Payment for " + (money.format(LoanAmt))+ " and a term of ");
System.out.println((termMonths[a]/12)+ " years with interest rate of " + (IntRate[a]*100)+ "% will be " +
(money.format(monthlyPayment))+ " a month.");
System.out.println();
System.out.println("Press the Enter key to continue");
System.in.read();
//Loop statement - formula for loan balance and interest paid
while(termMonths[a] > 0){
//Reduce payments one month at a time
Months = (termMonths[a] --);
//Calculate Interest paid and gives loan balance
Temp = (1 - 1 /Math.pow((1 + IntRate[a]/12), Months));
interestPaid = (monthlyPayment * Temp);
loanBal = (LoanAmt - monthlyPayment + interestPaid);
//Prints out the payment amount, interest amount paid & loan balance
System.out.println("Payment " + (IntCounter ++));
System.out.println("After payment of " + (money.format(monthlyPayment)));
System.out.println("Interest paid on loan is " + (money.format(interestPaid)));
System.out.println("The current balance is " + (money.format(loanBal)));
System.out.println();
//Shows new loan amount after receiving payment.
LoanAmt = loanBal;}
//Pause the screen
if (IntCounter > 0){
try {
Thread.sleep(2500);
//Loop statement - forumla for loan balance and interest paid
while(termMonths[b] > 0){
//Reduce payments one month at a time
Months = (termMonths[b] --);
//Calculate Interest paid and gives loan balance
Temp = (1 - 1 /Math.pow(1 + IntRate[b]/12, Months));
interestPaid = (monthlyPayment * Temp);
loanBal = (LoanAmt - monthlyPayment + interestPaid);
//Prints out the payment amount, interest amount paid and loan balance
System.out.println("Payment " + (IntCounter ++));
System.out.println("After payment of " + (money.format(monthlyPayment)));
System.out.println("Interest paid on loan is " + (money.format(interestPaid)));
System.out.println("The current balance is " + (money.format(loanBal)));
System.out.println();
//Shows new loan amount after receiving payment
LoanAmt = loanBal;}
//Pause the screen
if (IntCounter > 0){
try {
Thread.sleep(2500);
//Stop loop
if (termMonths[b] <=0){
System.out.println("Your Loan is paid off.");}}
catch (InterruptedException e1) {
//Stop loop
if (termMonths[b] <=0){
System.out.println("Your Loan is paid off.");
//}
The GUI code is as follows: The main problem with it right now is that I cannot get anything but a blank pink screen to come up when I have told it to add the Labels. Can you tell me what I'm doing wrong?
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.*;
public class BeemanGUI1 extends JFrame{
//The window object
static JFrame aWindow = new JFrame("Beeman's Mortgage Calculator");
public static void main(String[] args)
{
Toolkit theKit = aWindow.getToolkit(); //Get window toolkit
Dimension wndSize = theKit.getScreenSize(); //Get screen size
//Set position of screen in center & size to half screen size
aWindow.setBounds(wndSize.width/4, wndSize.height/4, //Position of window
wndSize.width/2, wndSize.height/2); //Size of Window
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set exit method
aWindow.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); //Set cursor
aWindow.getContentPane().setBackground(Color.PINK); //Set color of screen background
aWindow.setVisible(true); //Display to screen
}
//Set up variables
private javax.swing.JLabel AddPanel;
private javax.swing.JLabel PrincipalLabel;
private javax.swing.JLabel YearLabel;
private javax.swing.JLabel IntLabel;
private javax.swing.JTextField PrincipalTextField;
private javax.swing.JTextField YearTextField;
private javax.swing.JTextField IntTextField;
private javax.swing.JTextField ResultsTextField;
private javax.swing.JButton CalculateButton;
public void initializeComponents(){
//Run Components
AddPanel = new JLabel();
PrincipalLabel = new JLabel("Amount");
PrincipalTextField = new JTextField();
YearLabel = new JLabel("Term");
PrincipalTextField = new JTextField();
IntLabel = new JLabel("Rate");
PrincipalTextField = new JTextField();
ResultsTextField = new JTextField();
CalculateButton = new JButton("Calculate");
//Set the attributes
PrincipalTextField.setColumns(10);
YearTextField.setColumns(4);
IntTextField.setColumns(5);
ResultsTextField.setColumns(10);
ResultsTextField.setEditable(false);}
public BeemanGUI1(){
AddPanel.add(AddPanel);
AddPanel.add(PrincipalTextField);
AddPanel.add(PrincipalLabel);
AddPanel.add(YearTextField);
AddPanel.add(YearLabel);
AddPanel.add(IntTextField);
AddPanel.add(IntLabel);
AddPanel.add(ResultsTextField);
AddPanel.add(CalculateButton);
setVisible(true);
}
public static void main1(String[] args) {
new MortgageCalculator();
}
private void setResultValue(){
double Principal = Double.parseDouble(PrincipalTextField.getText());
double Term = Integer.parseInt(YearTextField.getText());
double Rate = Double.parseDouble(IntTextField.getText())/100;
double Results = (Principal * Rate / 12)/(1-1/Math.pow((1+Rate/ 12), Term));
ResultsTextField.setText(Double.toString(Results));}
public void actionPerformed(ActionEvent event){
System.out.println("Results Button");
event.getActionCommand();
if("Calculate".equals(CalculateButton)){
setResultValue();
}}}