Hello Daniweb,
I have a program due this evening and am ready to give up. I can't figure out my compile problems:
C:\Users\Chris\Desktop\My Java Programs\Chris_Y_Week_5_IA.java:85: call to super must be first statement in constructor
super("mortgagecalcsr7");
^
C:\Users\Chris\Desktop\My Java Programs\Chris_Y_Week_5_IA.java:573: inner classes cannot have static declarations
public static void main(String[] args)
^
2 errors
Tool completed with exit code 1
which came from the following code:
import java.awt.*;
import java.text.NumberFormat;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
public class Chris_Y_Week_5_IA extends JFrame implements ActionListener
{
// Declare Labels
JLabel AmountLabel = new JLabel( "Principal Amount:$ " );
JLabel PaymentLabel = new JLabel( "Monthly Payment: " );
JLabel InterestLabel = new JLabel( "Interest Rate %: " );
JLabel TermLabel = new JLabel( "Length of Loan of Loan in Years: " );
// Declare Text Fields
JTextField mortgageAmount = new JTextField(7);
JTextField Payment = new JTextField(7);
JTextField InterestRate = new JTextField(3);
JTextField Term = new JTextField(3);
// Declare Buttons
JButton Loan7 = new JButton( "7 years at 5.35%" );
JButton Loan15 = new JButton( "15 years at 5.50%" );
JButton Loan30 = new JButton( "30 years at 5.75%" );
JButton ExitButton = new JButton( "Exit" );
JButton ClearButton = new JButton( "Clear All" );
JButton CalculateButton = new JButton( "Calculate Loan" );
// Declares Text Area and ScrollPane
JTextArea LoanPayments = new JTextArea(20,50);
JTextArea GraphArea = new JTextArea(19,50);
JScrollPane scroll = new JScrollPane(LoanPayments);
{
//Frame, Panel, and Layout set up
super("mortgagecalcsr7");
setSize(900, 800);
setLocation(200, 0);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel(new GridLayout(3,1));
//Setup container and contents
Container grid = getContentPane();
grid.setLayout(new GridLayout(4,0,4,4));
pane.add(grid);
pane.add(scroll);
grid.add(AmountLabel);
grid.add(mortgageAmount);
grid.add(InterestLabel);
grid.add(InterestRate);
grid.add(TermLabel);
grid.add(Term);
grid.add(PaymentLabel);
grid.add(Payment);
grid.add(Loan7);
grid.add(Loan15);
grid.add(Loan30);
grid.add(CalculateButton);
grid.add(ClearButton);
grid.add(ExitButton);
Payment.setEditable(false);
setContentPane(pane);
setContentPane(pane);
setVisible(true);
//Adds Action Listeners
ExitButton.addActionListener(this);
ClearButton.addActionListener(this);
Loan7.addActionListener(this);
Loan15.addActionListener(this);
Loan30.addActionListener(this);
mortgageAmount.addActionListener(this);
InterestRate.addActionListener(this);
Term.addActionListener(this);
Payment.addActionListener(this);
CalculateButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object command = e.getSource();
if (command == ExitButton) {
System.exit(0); }
else if (command == Loan7) {
calcLoan(7, 5.35); }
else if (command == Loan15) {
calcLoan(15, 5.50); }
else if (command == Loan30) {
calcLoan(30, 5.75); }
else if (command == CalculateButton ) {
double terms = 0;
double rates = 0;
try {
terms = Double.parseDouble(Term.getText());
rates = Double.parseDouble(InterestRate.getText());
} catch (Exception ex) {
LoanPayments.setText("Invalid term or rate Amount");
return;
}
calcLoan(terms, rates); }
else if (command == ClearButton) {
mortgageAmount.setText("");
Payment.setText("");
InterestRate.setText("");
Term.setText("");
LoanPayments.setText("");
}
}
private void calcLoan(double terms, double rates) {
Term.setText(String.valueOf(terms) );
InterestRate.setText(String.valueOf(rates));
double amount = 0;
try {
amount = Double.parseDouble(mortgageAmount.getText());
//Parse text to
} catch (Exception ex) { LoanPayments.setText
("Invalid mortgage Amount");
return; }
double interestRate = rates;
// Sets interestRate amount
double intRate = (interestRate / 100) / 12;
// Calculates Interest Rate
// double loanTermMonths = terms;
// Calculates Loan Term in Months
int months = (int)terms * 12;
// Converts Loan Term to Months
double rate = (intRate / 12);
// Converts Annual interest rate to monthly interest rate
double payment = amount * intRate / (1 - (Math.pow(1/(1 + intRate), months)));
// Calculation for Monthly payment
double remainingPrincipal = amount;
// Sets Remaining Balance
double MonthlyInterest = 0;
// saves interest payment
double MonthlyAmt = 0;
// saves principal payment
//double x[] = new double[1]; x[0] = 0;
// Number formatter to format output in table
NumberFormat CurrencyFormatter = NumberFormat.getCurrencyInstance();
Payment.setText(CurrencyFormatter.format(payment));
LoanPayments.setText("Month\tPrincipal\tInterest\tEnding Balance\n");
// double[] values = new double[months];
// counting the month backward is confusing
// for(;months > 0; months -- ) {
int currentMonth = 0; while(currentMonth < months) {
//Append loop for mortgage detail in the text area
MonthlyInterest = (remainingPrincipal * intRate);
//Monthly Payment Toward Interest
MonthlyAmt = (payment - MonthlyInterest);
//Monthly Payment Toward Principal
remainingPrincipal = (remainingPrincipal - MonthlyAmt);
//Remaining loan Balance
// values[months-1] = remainingPrincipal;
LoanPayments.append((++currentMonth) + "\t" +
CurrencyFormatter.format(MonthlyAmt) + "\t" +
CurrencyFormatter.format(MonthlyInterest) + "\t" +
CurrencyFormatter.format(remainingPrincipal) + "\n");
GraphArea.append("" + remainingPrincipal);
}
}
public class GraphProgram extends JPanel
{
GraphCanvas graph = new GraphCanvas();
public GraphProgram()
{
setLayout(new BorderLayout());
setSize(300, 300);
add(graph, BorderLayout.CENTER);
}
public void setValues(double[] values) {
graph.setValues(values);
}
public class CoordCanvas extends Canvas
{
protected float viewLeft;
protected float viewRight;
protected float viewTop;
protected float viewBottom;
public void setXRange(float left, float right)
{
viewLeft = left;
viewRight = right;
}
public void setYRange(float top, float bottom)
{
viewTop = top;
viewBottom = bottom;
}
public float toX(float x)
{
return (x - viewLeft) * getSize().width / (viewRight - viewLeft);
}
public float toY(float y)
{
return (y - viewTop) * getSize().height / (viewBottom - viewTop);
}
public float toWidth(float w)
{
return w * getSize().width / Math.abs(viewRight - viewLeft);
}
public float toHeight(float h)
{
return h * getSize().height / Math.abs(viewBottom - viewTop);
}
}
// subclass to draw a bar graph
class GraphCanvas extends CoordCanvas
{
private double[] vals = null;
public void setValues(double[] vals)
{
this.vals = vals;
repaint();
}
public void paint(Graphics comp)
{
if (vals == null || vals.length < 1)
return;
// find maximum value
double max = vals[0];
for (int i = 0; i < vals.length; i++)
if (vals[i] > max)
max = vals[i];
// set coordinate ranges
this.setXRange(0F, (float)vals.length);
this.setYRange((float)max, 0F);
// draw the graph
Graphics2D comp2D = (Graphics2D)comp;
for (int i = 0; i < vals.length; i++)
{
Rectangle2D.Float remainingPrincipal = new Rectangle2D.Float(toX((float)i),
toY((float)vals[i]), toWidth(1F), toHeight((float)vals[i]));
comp2D.fill(remainingPrincipal);
}
}
public static void main(String[] args)
{
Chris_Y_Week_5_IA frame= new Chris_Y_Week_5_IA();
}
}
}
}