Help with Java Pie Chart

Cyprusice
Cyprusice used Ask the Experts™
on
I am in need of help with the Java pie chart I have built in my mortgage calculator. I am trying to have the chart represent the total principle paid over the term of the loan and the total interest paid over the term of the loan. I have a basic pie chart built with 40% of it green the the rest is red. I also have a formual that I believe will produce the principle paid over the term of the loan (amnt * ((1 - Math.pow((1 + mApr))) / Math.pow(( 1 + mApr ), (mTerm - 1 )))). Problem is I am not sure how I am suppose to get the value of this formula and convert it into a percentage that can then be sent to my graphpanel and used to build the pie chart. Can anyone help shed some light on this?

Also I get this error when I compile with the principle paid line not commented out (compiles fine with it commented out)

 pow(double,double) in java.lang.Math cannot be applied to (double)
                   princPaid = amnt * ((1 - Math.pow((1 + mApr))) / Math.pow(( 1 + mApr ), (mTerm - 1 ))); // sets the princ paid over the term of the loan
                   
import java.text.DecimalFormat; // Import used to format the decimal place
import java.text.NumberFormat; // Import used to format numerical into dollar format
import java.awt.*; // Import Layout
import java.awt.Color; //Sets Color
import java.awt.event.*;
import java.awt.event.ActionEvent; // Import for ActionEvent for the buttons
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.awt.event.ActionListener; // Import for ActionListener for the buttons
import javax.swing.*; // Import GUI Components
import javax.swing.event.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;


public class Mortgage extends JApplet implements ActionListener //Sets the Class and type of program along w/ implementing Action Listener
{
	 //Declares and builds the variables, labels, fields, buttons, and combo boxes needed for the program
	 private double apr;//Creates variable for the APR
	 private double term;//Creates variable for the length of the loan in years
	 private double amnt;//Creates variable for the total amount of the loan
	 private double pay;//Creates variable for what the monthly payment is
	 private double mTerm;//Creates variable for what the length of the loan is in months
	 private double mApr;//Creates variable for what the monthly intrests is
	 private double princPaid;
	 double [] termArray;
	 double [] aprArray;
	 double prncpl;
     JTextArea amntFld; //Text field that you can enter your loans amount into; all varible, labels, fields, buttons, and combo boxes are using camelCase
     JLabel title; //Label used to title the applet
     JLabel amntLbl; //Label for the loan amount text field
     JComboBox loanSlctBx; //Combo box used to allow the users to select from the 3 different loans
     JLabel loanSlctLbl; //Label for the combo box
     JLabel payLbl; // label for payFld
     JTextArea payFld; //Text field used to display the output
     JButton calculateBtn; //Button used to trigger the calculations
     JButton resetBtn; //Button used to trigger reset of all feilds
     JButton graphBtn;//Button used to trigger the graph to appear
     JScrollPane payFldScrl ; //Pane used to attach a scroll bar to text area
     JLabel resetLbl; //Label for reset instructions


     public void init()//Contains all other methods that need to run right away
     {
		 getContentPane().setLayout(null);
		 setupGUI();
		 load();
	 }
	 void setupGUI()//sets up the GUI used to operate the program
	 {
		 //Builds all labels, text feilds, buttons, combo boxes, and applet required throughout the applet
	     //This includes placing them in the correct location of the applet
	     //and making them the correct size with the appropreate display text and color.
	     //all content is added to the Content Pane
         amntFld = new JTextArea();//Builds a text area for the amntFld
	     amntFld.setLocation(300,47);//defines the loaction for the amntFld
	     amntFld.setSize(125,16);//defines the size of the amntFld
	     getContentPane().add(amntFld);//Sets the contentPane properties and addes the amntFld to it

	     title = new JLabel();
	     title.setLocation(173,4);
	     title.setSize(175,32);
	     title.setText("Preset Mortgage Calculator");//Sets the test for the title label
	     getContentPane().add(title);

	     amntLbl = new JLabel();
	     amntLbl.setLocation(63,47);
	     amntLbl.setSize(200,16);
	     amntLbl.setText("Enter Loan Amount");
	     getContentPane().add(amntLbl);

	     String loanSlctB_tmp[]={"7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};//set up string containing actionevent triggers for the Array
	     loanSlctBx = new JComboBox(loanSlctB_tmp);//Creats the Combo Box and points to String above
	     loanSlctBx.setLocation(300,75);
	     loanSlctBx.setSize(125,32);
	     loanSlctBx.setEditable(false);//disables the ability to edit the Combo Box
	     getContentPane().add(loanSlctBx);

	     loanSlctLbl = new JLabel();
	     loanSlctLbl.setLocation(63,75);
	     loanSlctLbl.setSize(200,32);
	     loanSlctLbl.setText("Select Loan");
	     getContentPane().add(loanSlctLbl);

	     payLbl = new JLabel();
		 payLbl.setLocation(0,140);
		 payLbl.setSize(500,32);
		 payLbl.setText("Payment #     Monthly Payment     Interest Paid         Principal Paid      Amount Left");
         getContentPane().add(payLbl);

	     payFld = new JTextArea();
		 JScrollPane payFldScrl = new JScrollPane();//Creates and defines the scroll pane
		 payFldScrl.setBounds(2, 162, 470, 300);//Sets location and size of the scroll pane
		 payFldScrl.getViewport().add(payFld);//sets what field to view with the scroll pane
         getContentPane().add(payFldScrl);


	     calculateBtn = new JButton("Calculate");
	     calculateBtn.setLocation(300,114);
	     calculateBtn.setSize(102,32);
	     calculateBtn.setBackground (Color.lightGray);//sets color of the button
	     calculateBtn.setText("Calculate");
	     getContentPane().add(calculateBtn);
	     calculateBtn.addActionListener(this);//adds the actionlistener

	     resetBtn = new JButton("Reset");
		 resetBtn.setLocation(250,600);
		 resetBtn.setSize(102,32);
		 resetBtn.setBackground (Color.lightGray);//sets color of the button
		 resetBtn.setText("Reset");
		 getContentPane().add(resetBtn);
	     resetBtn.addActionListener(this);//adds the actionlistener

	     graphBtn = new JButton("Graph");
		 graphBtn.setLocation(140,600);
		 graphBtn.setSize(102,32);
		 graphBtn.setBackground (Color.lightGray);//sets color of the button
		 graphBtn.setText("Graph");
		 getContentPane().add(graphBtn);
	     graphBtn.addActionListener(this);//adds the actionlistener

	     setSize(475,650);//sets size of the applet
	     setVisible(true);//allows you to see the applet


	 }
//reads the file
public void load()

     {
          Reader fis;
               try
               {

                    fis = new FileReader("termapr.txt"); //reads from the file in the same directory

                    BufferedReader b = new BufferedReader( fis );
                    String inputholder = ""; //creates appropreate strings
                    String values = "";
                    while ((inputholder = b.readLine()) != null)//set conditions for the values string
                    {
                    values = values + inputholder + ","; //sets value to the values string
                    }
                    String[] line = values.split(",");
                    termArray = new double[3];
                    termArray[0] = Double.parseDouble(line[0].trim());//adds values from file to arrays
                    termArray[1] = Double.parseDouble(line[2].trim());
                    termArray[2] = Double.parseDouble(line[4].trim());

                    aprArray = new double[3];
                    aprArray[0] = Double.parseDouble(line[1].trim());
                    aprArray[1] = Double.parseDouble(line[3].trim());
                    aprArray[2] = Double.parseDouble(line[5].trim());

               b.close();
               fis.close();
               }

               catch ( Exception e1 )
               {
                    e1.printStackTrace(  );
               }
     }

	 public void actionPerformed(ActionEvent e)//builds the  the action events needed for the buttons
	 {System.out.println();
		 Object source = e.getSource();
	           if (source==calculateBtn)//sets the action for the calucate button
	           {
	                calculate();
	           }

	           if (source==resetBtn)//sets the action for the reset button
	           {
	                reset();
	           }


	           if (source == graphBtn)//sets the action for the graph button
	           {
	               mFrame = new JFrame("Mortgage Graph");
	               mFrame.getContentPane().add(new GraphPanel());
	               mFrame.setSize(800,600);
	               mFrame.setLocation(200,100);
			       mFrame.setVisible(true);


			   }
		   }
          public JFrame mFrame = new JFrame();



        void calculate()
        {
			if (loanSlctBx.getSelectedItem().equals("7 years at 5.35%"))//array used to selcted term and APR bassed on the Combo Box
							 {
								 apr=aprArray[0];//tells to store the 1st value to the varible
								 term=termArray[0];
							 }
							 else if (loanSlctBx.getSelectedItem().equals("15 years at 5.5%"))
							 {
								 apr=aprArray[1];
								 term=termArray[1];//tells to store the 2nd value to the varible
							 }
							 else if (loanSlctBx.getSelectedItem().equals("30 years at 5.75%"))
							 {
								 apr=aprArray[2];//tells to store the 3rd value to the varible
								 term=termArray[2];
				 }
			 // Makes all the calculation needed for the output
	 	     amnt = Double.parseDouble(amntFld.getText());
	 	     amntFld.setText(currencyComposition(amnt)); // Formats the value to use a US currencey composistion
	 	     mTerm = term*12; //sets the mTerm variable
	 	     mApr = (apr / 100) / 12; //sets the mApr with the monthly interest
	  	     pay = amnt * (mApr / (1 - Math.pow((1 + mApr), -mTerm)));//sets the pay varible with the results
	  	     princPaid = amnt * ((1 - Math.pow((1 + mApr))) / Math.pow(( 1 + mApr ), (mTerm - 1 ))); // sets the princ paid over the term of the loan
	 	     double newAmnt = amnt;//sets new variable for new loan amount
	  	     //Loop to run the calulations thorughout the life of the loan
	 	     for (int i=0; i<mTerm;i++)
	 	     {
	 			 if (i==36||i==72||i==108||i==144||i==180||i==216||i==252||i==288||i==326||i==360)
	 			 {
	 				 try
	 				 {
	 					 Thread.sleep(500);//sleep timer
	 				 }
	 				 catch (InterruptedException e1)
	 				 {
	 				 }
	 			 }
	 			 //Prints the output for the calculations in the JTextAera one line at a time
	 			 payFld.append(i+1 +"                        "+currencyComposition(pay)+"                       "+currencyComposition(newAmnt*mApr)+"                    "+currencyComposition(pay-(newAmnt*(mApr))) +"         "+currencyComposition(newAmnt)+"\n");
	 			 newAmnt -= pay-(newAmnt*(mApr));




	 		 }
	 	 }

	 	      //resets all fields
		      void reset()
		      {
		           amntFld.setText(null);
		           payFld.setText(null);
		           loanSlctBx.setSelectedIndex(0);
     }





			 // Formats the numeric values to readable currency
			 private String currencyComposition(double input)
			 {
				 DecimalFormat df = new DecimalFormat("$###,###,###.00");
	 			 df.setMaximumFractionDigits(2);
	 			 String formattedString = df.format(input);
	 			 return formattedString;
			 }
		 }

class GraphPanel extends JPanel
{
  private static final long serialVersionUID = 1L;
	BufferedImage backBuffer;  //  @jve:decl-index=0:
	double pricePay = .40;	// I want to send the pricePay calculations here and concert it to a percentage

	JLabel amntLbl;


	/**
	 * This is the default constructor
	 */
	public void GraphPanel() {
		init();
	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	void init() {
		this.setSize(500, 500);
	}

	public void paintComponent(Graphics g){
		if(backBuffer == null){
			backBuffer = new BufferedImage(1024,1024,BufferedImage.TYPE_INT_ARGB);
		}

		int width = backBuffer.getWidth();
		int height = backBuffer.getHeight();

		Graphics2D g2 = (Graphics2D)backBuffer.getGraphics();
		g2.setColor(Color.black);
		g2.fillRect(0, 0, width, height);

		//Adds the Legend
		Font font = new Font("Calibri", Font.BOLD, 30);
		g2.setFont(font);
		g2.setColor(Color.white);
		g2.drawString("Green is the Principal Paid", 20, 125);
		g2.drawString("Red is the Interest Paid", 20, 160);

		// Draw the Pie chart
		// Percentage of 360 degrees
		double princ = 360.0 * this.pricePay;
		g2.setColor(Color.green);
		g2.fillArc(width/4, height/4, width/2, width/2, 0, (int) princ);

		// This is the red portion, we add 1 since the algorithm needs the exta pixels for the chart
		g2.setColor(Color.red);
		g2.fillArc(width/4, height/4, width/2, width/2, (int)princ ,(int)(360-princ)+1);


		// Draw the back buffer to the component
		g.drawImage(backBuffer, 0, 0, getWidth(), getHeight(), null);
	}



}

Open in new window

termapr.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
This error:
 pow(double,double) in java.lang.Math cannot be applied to (double)
                   princPaid = amnt * ((1 - Math.pow((1 + mApr))) / Math.pow(( 1 + mApr ), (mTerm - 1 ))); // sets the princ paid over the term of the loan

seems to be that you have Math.pow((1+mApr)))
it should be Math.pow((1+mApr),([something here])))

Author

Commented:
Thanks robthewolf,

I now have the formula fixed but how do I get the value of that formula from one class to the other so that I can use it in my graph?
Two options for sharing data between classes:

1) Store the data in a class attribute and make it accessible to the other class. For classes in different packages this is achieved by declaring it 'public'.   E.g.

public double princPaid;

Then access the value using the familiar association/'dot' operator. E.g.

myObject.princPaid

2) Store the date in a class attribute but keep it private to that class, and instead declare a method in your class that provides other classes with access to the data.  E.g.

public double getPrincPaid () {
  return princPaid;
}

Accessing the data in a similar way to option 1, but by method call instead of direct access.  E.g.

myObject.getPrincPaid()

The second option is the recommended option, as it does not leave the attribute vulnerable to inappropriate changes.

Either way, the class that wants to access your principle paid value, must create or have access to an instance of the other class.

Hope this helps.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

firstly i notice that your constructor for GraphPanel is incorrect.
      public void GraphPanel() {
            init();
      }

it should not contain void.

secondly you should probably break your code down a little.  Mostly this is not to get it to work just make it easier to follow.
create a new class called MortgageData
move you loading and calculation methods to MortgageData
I would do something like this

public class MortgageData {
  [all your doubles and related data goes here]
eg. double princPaid;

  public MortgageData(){
    load("termapr.txt");
  }

  private load(String fileName){
  [the load method goes here]
  }

  public calculate(){
    [your calculate function goes here]
  }

 [for all your fields above you need getters (if you want them visible)]
 
 public double getPrincPaid(){
    return princPaid;
  }
}

in your Mortgage class you need
MortgageData mortgageData = new MortgageData();

then in init instead of load()
mortgageData.load();

in your action listener:
instead of calculate
mortgageData.calculate();

I would create a separate class for GraphPanel, instead of having it at the bottom of the Mortgage code.
add a field in GraphPanel
MortgageData mortgageData;

Change the constructor to take and argument
public GraphPanel(MortgageData mortgageData){
  this.mortgageData = mortgageData;
  init();
}

where ever you need princPaid in GraphPanel you now have access to it by doing
this.mortgageData.getPrincPaid();

one final thing remember to change the line
                   mFrame.getContentPane().add(new GraphPanel());
to

                   mFrame.getContentPane().add(new GraphPanel(mortgageData));

hope this helps

Author

Commented:
Everyone,

I have decided to go with using public double variables even if it is not he preferred method due to time restraints on the project. I seem to have gotten the princPaid variable through to the other class (yet I am not sure if its contents are correct) and now my only issue is turning it into a percentage so that I can use it in my pie graph correctly. Here is the code I have to do that but its does not seem to work with my graph even though it compiles. I think I have either my formulas wrong or how I am getting my variables there are wrong can some one shed some light on this issue.
import java.text.DecimalFormat; // Import used to format the decimal place
import java.text.NumberFormat; // Import used to format numerical into dollar format
import java.awt.*; // Import Layout
import java.awt.Color; //Sets Color
import java.awt.event.*;
import java.awt.event.ActionEvent; // Import for ActionEvent for the buttons
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.awt.event.ActionListener; // Import for ActionListener for the buttons
import javax.swing.*; // Import GUI Components
import javax.swing.event.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;


public class Mortgage extends JApplet implements ActionListener //Sets the Class and type of program along w/ implementing Action Listener
{
	 //Declares and builds the variables, labels, fields, buttons, and combo boxes needed for the program
	 private double apr;//Creates variable for the APR
	 private double term;//Creates variable for the length of the loan in years
	 public double amnt;//Creates variable for the total amount of the loan
	 private double pay;//Creates variable for what the monthly payment is
	 private double mTerm;//Creates variable for what the length of the loan is in months
	 private double mApr;//Creates variable for what the monthly intrests is
	 double [] termArray;
	 double [] aprArray;
	 double prncpl;
     JTextArea amntFld; //Text field that you can enter your loans amount into; all varible, labels, fields, buttons, and combo boxes are using camelCase
     JLabel title; //Label used to title the applet
     JLabel amntLbl; //Label for the loan amount text field
     JComboBox loanSlctBx; //Combo box used to allow the users to select from the 3 different loans
     JLabel loanSlctLbl; //Label for the combo box
     JLabel payLbl; // label for payFld
     JTextArea payFld; //Text field used to display the output
     JButton calculateBtn; //Button used to trigger the calculations
     JButton resetBtn; //Button used to trigger reset of all feilds
     JButton graphBtn;//Button used to trigger the graph to appear
     JScrollPane payFldScrl ; //Pane used to attach a scroll bar to text area
     JLabel resetLbl; //Label for reset instructions


     public void init()//Contains all other methods that need to run right away
     {
		 getContentPane().setLayout(null);
		 setupGUI();
		 load();
	 }
	 void setupGUI()//sets up the GUI used to operate the program
	 {
		 //Builds all labels, text feilds, buttons, combo boxes, and applet required throughout the applet
	     //This includes placing them in the correct location of the applet
	     //and making them the correct size with the appropreate display text and color.
	     //all content is added to the Content Pane
         amntFld = new JTextArea();//Builds a text area for the amntFld
	     amntFld.setLocation(300,47);//defines the loaction for the amntFld
	     amntFld.setSize(125,16);//defines the size of the amntFld
	     getContentPane().add(amntFld);//Sets the contentPane properties and addes the amntFld to it

	     title = new JLabel();
	     title.setLocation(173,4);
	     title.setSize(175,32);
	     title.setText("Preset Mortgage Calculator");//Sets the test for the title label
	     getContentPane().add(title);

	     amntLbl = new JLabel();
	     amntLbl.setLocation(63,47);
	     amntLbl.setSize(200,16);
	     amntLbl.setText("Enter Loan Amount");
	     getContentPane().add(amntLbl);

	     String loanSlctB_tmp[]={"7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};//set up string containing actionevent triggers for the Array
	     loanSlctBx = new JComboBox(loanSlctB_tmp);//Creats the Combo Box and points to String above
	     loanSlctBx.setLocation(300,75);
	     loanSlctBx.setSize(125,32);
	     loanSlctBx.setEditable(false);//disables the ability to edit the Combo Box
	     getContentPane().add(loanSlctBx);

	     loanSlctLbl = new JLabel();
	     loanSlctLbl.setLocation(63,75);
	     loanSlctLbl.setSize(200,32);
	     loanSlctLbl.setText("Select Loan");
	     getContentPane().add(loanSlctLbl);

	     payLbl = new JLabel();
		 payLbl.setLocation(0,140);
		 payLbl.setSize(500,32);
		 payLbl.setText("Payment #     Monthly Payment     Interest Paid         Principal Paid      Amount Left");
         getContentPane().add(payLbl);

	     payFld = new JTextArea();
		 JScrollPane payFldScrl = new JScrollPane();//Creates and defines the scroll pane
		 payFldScrl.setBounds(2, 162, 470, 300);//Sets location and size of the scroll pane
		 payFldScrl.getViewport().add(payFld);//sets what field to view with the scroll pane
         getContentPane().add(payFldScrl);


	     calculateBtn = new JButton("Calculate");
	     calculateBtn.setLocation(300,114);
	     calculateBtn.setSize(102,32);
	     calculateBtn.setBackground (Color.lightGray);//sets color of the button
	     calculateBtn.setText("Calculate");
	     getContentPane().add(calculateBtn);
	     calculateBtn.addActionListener(this);//adds the actionlistener

	     resetBtn = new JButton("Reset");
		 resetBtn.setLocation(250,600);
		 resetBtn.setSize(102,32);
		 resetBtn.setBackground (Color.lightGray);//sets color of the button
		 resetBtn.setText("Reset");
		 getContentPane().add(resetBtn);
	     resetBtn.addActionListener(this);//adds the actionlistener

	     graphBtn = new JButton("Graph");
		 graphBtn.setLocation(140,600);
		 graphBtn.setSize(102,32);
		 graphBtn.setBackground (Color.lightGray);//sets color of the button
		 graphBtn.setText("Graph");
		 getContentPane().add(graphBtn);
	     graphBtn.addActionListener(this);//adds the actionlistener

	     setSize(475,650);//sets size of the applet
	     setVisible(true);//allows you to see the applet


	 }
//reads the file
public void load()

     {
          Reader fis;
               try
               {

                    fis = new FileReader("termapr.txt"); //reads from the file in the same directory

                    BufferedReader b = new BufferedReader( fis );
                    String inputholder = ""; //creates appropreate strings
                    String values = "";
                    while ((inputholder = b.readLine()) != null)//set conditions for the values string
                    {
                    values = values + inputholder + ","; //sets value to the values string
                    }
                    String[] line = values.split(",");
                    termArray = new double[3];
                    termArray[0] = Double.parseDouble(line[0].trim());//adds values from file to arrays
                    termArray[1] = Double.parseDouble(line[2].trim());
                    termArray[2] = Double.parseDouble(line[4].trim());

                    aprArray = new double[3];
                    aprArray[0] = Double.parseDouble(line[1].trim());
                    aprArray[1] = Double.parseDouble(line[3].trim());
                    aprArray[2] = Double.parseDouble(line[5].trim());

               b.close();
               fis.close();
               }

               catch ( Exception e1 )
               {
                    e1.printStackTrace(  );
               }
     }

	 public void actionPerformed(ActionEvent e)//builds the  the action events needed for the buttons
	 {System.out.println();
		 Object source = e.getSource();
	           if (source==calculateBtn)//sets the action for the calucate button
	           {
	                calculate();
	           }

	           if (source==resetBtn)//sets the action for the reset button
	           {
	                reset();
	           }


	           if (source == graphBtn)//sets the action for the graph button
	           {
	               mFrame = new JFrame("Mortgage Graph");
	               mFrame.getContentPane().add(new GraphPanel());
	               mFrame.setSize(800,600);
	               mFrame.setLocation(200,100);
			       mFrame.setVisible(true);


			   }
		   }
          public JFrame mFrame = new JFrame();
          public double princPaid;



        void calculate()
        {
			if (loanSlctBx.getSelectedItem().equals("7 years at 5.35%"))//array used to selcted term and APR bassed on the Combo Box
							 {
								 apr=aprArray[0];//tells to store the 1st value to the varible
								 term=termArray[0];
							 }
							 else if (loanSlctBx.getSelectedItem().equals("15 years at 5.5%"))
							 {
								 apr=aprArray[1];
								 term=termArray[1];//tells to store the 2nd value to the varible
							 }
							 else if (loanSlctBx.getSelectedItem().equals("30 years at 5.75%"))
							 {
								 apr=aprArray[2];//tells to store the 3rd value to the varible
								 term=termArray[2];
				 }
			 // Makes all the calculation needed for the output\

	 	     amnt = Double.parseDouble(amntFld.getText());
	 	     amntFld.setText(currencyComposition(amnt)); // Formats the value to use a US currencey composistion
	 	     mTerm = term*12; //sets the mTerm variable
	 	     mApr = (apr / 100) / 12; //sets the mApr with the monthly interest
	  	     pay = amnt * (mApr / (1 - Math.pow((1 + mApr), -mTerm)));//sets the pay varible with the results
	  	     princPaid = amnt * ((1 - Math.pow((1 + mApr), (-mTerm))) / Math.pow(( 1 + mApr ), (mTerm - 1 ))); // sets the princ paid over the term of the loan
	 	     double newAmnt = amnt;//sets new variable for new loan amount
	  	     //Loop to run the calulations thorughout the life of the loan
	 	     for (int i=0; i<mTerm;i++)
	 	     {
	 			 if (i==36||i==72||i==108||i==144||i==180||i==216||i==252||i==288||i==326||i==360)
	 			 {
	 				 try
	 				 {
	 					 Thread.sleep(500);//sleep timer
	 				 }
	 				 catch (InterruptedException e1)
	 				 {
	 				 }
	 			 }
	 			 //Prints the output for the calculations in the JTextAera one line at a time
	 			 payFld.append(i+1 +"                        "+currencyComposition(pay)+"                       "+currencyComposition(newAmnt*mApr)+"                    "+currencyComposition(pay-(newAmnt*(mApr))) +"         "+currencyComposition(newAmnt)+"\n");
	 			 newAmnt -= pay-(newAmnt*(mApr));




	 		 }
	 	 }

	 	      //resets all fields
		      void reset()
		      {
		           amntFld.setText(null);
		           payFld.setText(null);
		           loanSlctBx.setSelectedIndex(0);
     }





			 // Formats the numeric values to readable currency
			 private String currencyComposition(double input)
			 {
				 DecimalFormat df = new DecimalFormat("$###,###,###.00");
	 			 df.setMaximumFractionDigits(2);
	 			 String formattedString = df.format(input);
	 			 return formattedString;
			 }
		 }

class GraphPanel extends JPanel
{
  private static final long serialVersionUID = 1L;
	BufferedImage backBuffer;  //  @jve:decl-index=0:
	double princ;
	static public double amnt;
	static public double princPaid;
	static double pricePaidPerct = princPaid/amnt;

	JLabel amntLbl;


	/**
	 * This is the default constructor
	 */
	public GraphPanel() {
		init();

	}


	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	void init() {
		this.setSize(500, 500);

	}

	public void paintComponent(Graphics g){
		if(backBuffer == null){
			backBuffer = new BufferedImage(1024,1024,BufferedImage.TYPE_INT_ARGB);
		}

		int width = backBuffer.getWidth();
		int height = backBuffer.getHeight();

		Graphics2D g2 = (Graphics2D)backBuffer.getGraphics();
		g2.setColor(Color.black);
		g2.fillRect(0, 0, width, height);

		//Adds the Legend
		Font font = new Font("Calibri", Font.BOLD, 30);
		g2.setFont(font);
		g2.setColor(Color.white);
		g2.drawString("Green is the Principal Paid", 20, 125);
		g2.drawString("Red is the Interest Paid", 20, 160);

		// Draw the Pie chart
		// Percentage of 360 degrees
		double princ = 360.0 * GraphPanel.pricePaidPerct;
		g2.setColor(Color.green);
		g2.fillArc(width/4, height/4, width/2, width/2, 0, (int) princ);

		// This is the red portion, we add 1 since the algorithm needs the exta pixels for the chart
		g2.setColor(Color.red);
		g2.fillArc(width/4, height/4, width/2, width/2, (int)princ ,(int)(360-princ)+1);


		// Draw the back buffer to the component
		g.drawImage(backBuffer, 0, 0, getWidth(), getHeight(), null);
	}



}

Open in new window

Author

Commented:
If you haven't noticed I am a complete beginner with Java so please bear with me; I do appreciate any and all suggestion.
Sorry I am not clear on what you want us to help you with.  It would be easier if you stated you mathematical formulas that you wish to code and the code that you are using to represent them only.  That way we do not have to search through all your code to find it and we understand what you are trying to do.
Additionally, if you are a beginner at Java you should be trying to learn how to do things properly in Java.  It is a powerful Object Oriented language.  So use it the way it should be used.  I can promise you that in the long run, and even the short run it is best to take advantage of the power that a language has than hacking around cos you think it will be faster.
Have a look at my suggestion again, most of it is just copy and paste.  With about 5 lines of new code.
I concur with Rob, his original advice was sound and detailed. However I'm sure this is difficult to appreciate when you're first learning Java.

With respect to the percentage formula, if I wanted to calculate the percentage of x in y, where x and y are both doubles and y > x. Then I would simply divide x by y. E.g.

double percentage = x/y;  

This of course gives me a real number between 0 and 1. If I wanted an integer number between 0 and 100 (and precision isn't important) then I would do something like this:

int percentage = (int)((x * 100)/y);

This is called a type-cast and converts a double precision number to an integer by trunkating it (removing the decimal places without performing any rounding).

Having said all this, as Rob mentioned, if you provide more specific detail you'll get a more helpful answer.

Have fun.

Author

Commented:
Ok,

I will try to be more specific. Your right this is difficult for me to appreciate the power of objects but I did take your advice to heart and tried to apply my code that wasn't using this technique to what you suggested. Problem is that I get a few errors now, which I am sure are because I am doing something wrong, I just cant seem to figure out what it is they are looking for. I do know that I want the first example of how to get a percentage, because I want a .XX number. I have included my entire code plus the problem areas  are included in replies below.

Here is the error I get when compiling the new layout.

C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:220: invalid method declaration; return type required
  private load(String termapr.txt)
          ^
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:220: ')' expected
  private load(String termapr.txt)
                             ^
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:220:  expected
  private load(String termapr.txt)
                                 ^
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:256: invalid method declaration; return type required
public calculate()
       ^
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:319: illegal start of type
      this.mortgageData.getpricePaidPerct();
      ^
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:319: ';' expected
      this.mortgageData.getpricePaidPerct();
          ^
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:319:  expected
      this.mortgageData.getpricePaidPerct();
                                         ^
7 errors

Tool completed with exit code 1

I am sure I put the this.mortgageData.getpricePaidPerct(); line in the wrong place, the others I am not sure why they want a return type.. I will continue to work on this in the mean time your assistance is as always welcome. I hope that I am more detailed this time.
/*
Week 5: Mortgage Calc + Change Request #7
PRG 421
Programmer: Dale Tweten
Date: Jan 27, 2010
Filename: Mortgage.java
V. 1

Requestor:  Ninfa Pendleton - Rapid City, SD
Write the program in Java (with a graphical user interface)
and have it calculate and display the mortgage payment amount
from user input of the amount of the mortgage and the user's
selection from a menu of available mortgage loans:

- 7 years at 5.35%
- 15 years at 5.5%
- 30 years at 5.75%

Use an array for the mortgage data for the different loans.
Read the interest rates to fill the array from a sequential
file. Display the mortgage payment amount followed by the loan
balance and interest paid for each payment over the term of the
loan. Add graphics in the form of a chart. Allow the user to
loop back and enter a new amount and make a new selection or
quit. Please insert comments in the program to document the program.

*/
import java.text.DecimalFormat; // Import used to format the decimal place
import java.text.NumberFormat; // Import used to format numerical into dollar format
import java.awt.*; // Import Layout
import java.awt.Color; //Sets Color
import java.awt.event.*;
import java.awt.event.ActionEvent; // Import for ActionEvent for the buttons
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.awt.event.ActionListener; // Import for ActionListener for the buttons
import javax.swing.*; // Import GUI Components
import javax.swing.event.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;


public class Mortgage extends JApplet implements ActionListener //Sets the Class and type of program along w/ implementing Action Listener
{
	 //Declares and builds the labels, fields, buttons, and combo boxes needed for the program
     JTextArea amntFld; //Text field that you can enter your loans amount into; all varible, labels, fields, buttons, and combo boxes are using camelCase
     JLabel title; //Label used to title the applet
     JLabel amntLbl; //Label for the loan amount text field
     JComboBox loanSlctBx; //Combo box used to allow the users to select from the 3 different loans
     JLabel loanSlctLbl; //Label for the combo box
     JLabel payLbl; // label for payFld
     JTextArea payFld; //Text field used to display the output
     JButton calculateBtn; //Button used to trigger the calculations
     JButton resetBtn; //Button used to trigger reset of all feilds
     JButton graphBtn;//Button used to trigger the graph to appear
     JScrollPane payFldScrl ; //Pane used to attach a scroll bar to text area
     JLabel resetLbl; //Label for reset instructions

     MortgageData mortgageData = new MortgageData();

     public void init()//Contains all other methods that need to run right away
     {
		 getContentPane().setLayout(null);
		 setupGUI();
		 mortgageData.load();
	 }
	 void setupGUI()//sets up the GUI used to operate the program
	 {
		 //Builds all labels, text feilds, buttons, combo boxes, and applet required throughout the applet
	     //This includes placing them in the correct location of the applet
	     //and making them the correct size with the appropreate display text and color.
	     //all content is added to the Content Pane
         amntFld = new JTextArea();//Builds a text area for the amntFld
	     amntFld.setLocation(300,47);//defines the loaction for the amntFld
	     amntFld.setSize(125,16);//defines the size of the amntFld
	     getContentPane().add(amntFld);//Sets the contentPane properties and addes the amntFld to it

	     title = new JLabel();
	     title.setLocation(173,4);
	     title.setSize(175,32);
	     title.setText("Preset Mortgage Calculator");//Sets the test for the title label
	     getContentPane().add(title);

	     amntLbl = new JLabel();
	     amntLbl.setLocation(63,47);
	     amntLbl.setSize(200,16);
	     amntLbl.setText("Enter Loan Amount");
	     getContentPane().add(amntLbl);

	     String loanSlctB_tmp[]={"7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};//set up string containing actionevent triggers for the Array
	     loanSlctBx = new JComboBox(loanSlctB_tmp);//Creats the Combo Box and points to String above
	     loanSlctBx.setLocation(300,75);
	     loanSlctBx.setSize(125,32);
	     loanSlctBx.setEditable(false);//disables the ability to edit the Combo Box
	     getContentPane().add(loanSlctBx);

	     loanSlctLbl = new JLabel();
	     loanSlctLbl.setLocation(63,75);
	     loanSlctLbl.setSize(200,32);
	     loanSlctLbl.setText("Select Loan");
	     getContentPane().add(loanSlctLbl);

	     payLbl = new JLabel();
		 payLbl.setLocation(0,140);
		 payLbl.setSize(500,32);
		 payLbl.setText("Payment #     Monthly Payment     Interest Paid         Principal Paid      Amount Left");
         getContentPane().add(payLbl);

	     payFld = new JTextArea();
		 JScrollPane payFldScrl = new JScrollPane();//Creates and defines the scroll pane
		 payFldScrl.setBounds(2, 162, 470, 300);//Sets location and size of the scroll pane
		 payFldScrl.getViewport().add(payFld);//sets what field to view with the scroll pane
         getContentPane().add(payFldScrl);


	     calculateBtn = new JButton("Calculate");
	     calculateBtn.setLocation(300,114);
	     calculateBtn.setSize(102,32);
	     calculateBtn.setBackground (Color.lightGray);//sets color of the button
	     calculateBtn.setText("Calculate");
	     getContentPane().add(calculateBtn);
	     calculateBtn.addActionListener(mortgageData.calculate());//adds the actionlistener

	     resetBtn = new JButton("Reset");
		 resetBtn.setLocation(250,600);
		 resetBtn.setSize(102,32);
		 resetBtn.setBackground (Color.lightGray);//sets color of the button
		 resetBtn.setText("Reset");
		 getContentPane().add(resetBtn);
	     resetBtn.addActionListener(this);//adds the actionlistener

	     graphBtn = new JButton("Graph");
		 graphBtn.setLocation(140,600);
		 graphBtn.setSize(102,32);
		 graphBtn.setBackground (Color.lightGray);//sets color of the button
		 graphBtn.setText("Graph");
		 getContentPane().add(graphBtn);
	     graphBtn.addActionListener(this);//adds the actionlistener

	     setSize(475,650);//sets size of the applet
	     setVisible(true);//allows you to see the applet


	 }


	 public void actionPerformed(ActionEvent e)//builds the  the action events needed for the buttons
	 {
		 Object source = e.getSource();
	           if (source==calculateBtn)//sets the action for the calucate button
	           {
	                calculate();
	           }

	           if (source==resetBtn)//sets the action for the reset button
	           {
	                reset();
	           }


	           if (source == graphBtn)//sets the action for the graph button
	           {
	               mFrame = new JFrame("Mortgage Graph");
	               mFrame.getContentPane().add(new GraphPanel(mortgageData));
	               mFrame.setSize(800,600);
	               mFrame.setLocation(200,100);
			       mFrame.setVisible(true);


			   }
		   }
          public JFrame mFrame = new JFrame();


	 	      //resets all fields
		      void reset()
		      {
		           amntFld.setText(null);
		           payFld.setText(null);
		           loanSlctBx.setSelectedIndex(0);
			   }

			 // Formats the numeric values to readable currency
			 private String currencyComposition(double input)
			 {
				 DecimalFormat df = new DecimalFormat("$###,###,###.00");
	 			 df.setMaximumFractionDigits(2);
	 			 String formattedString = df.format(input);
	 			 return formattedString;
			 }
		 }


public class MortgageData
{
     double apr;//Creates variable for the APR
  	 double term;//Creates variable for the length of the loan in years
  	 double amnt;//Creates variable for the total amount of the loan
  	 double pay;//Creates variable for what the monthly payment is
  	 double mTerm;//Creates variable for what the length of the loan is in months
  	 double mApr;//Creates variable for what the monthly intrests is
  	 double [] termArray;//Creates variable for the term array
  	 double [] aprArray;//Creates variable for the apr array
  	 double prncpl;
  	 double princPaid;
  	 double pricePaidPerct;

  public MortgageData()
  {
    load("termapr.txt");
  }

  private load(String termapr.txt)
  {
	  //reads the file
	  Reader fis;
	  try
	  {
		  fis = new FileReader("termapr.txt"); //reads from the file in the same directory

	       BufferedReader b = new BufferedReader( fis );
	       String inputholder = ""; //creates appropreate strings
	       String values = "";
	       while ((inputholder = b.readLine()) != null)//set conditions for the values string
	       {
	       values = values + inputholder + ","; //sets value to the values string
	       }
	       String[] line = values.split(",");
	       termArray = new double[3];
	       termArray[0] = Double.parseDouble(line[0].trim());//adds values from file to arrays
	       termArray[1] = Double.parseDouble(line[2].trim());
	       termArray[2] = Double.parseDouble(line[4].trim());

	       aprArray = new double[3];
	       aprArray[0] = Double.parseDouble(line[1].trim());
	       aprArray[1] = Double.parseDouble(line[3].trim());
	       aprArray[2] = Double.parseDouble(line[5].trim());

	       b.close();
	       fis.close();
	   }
	   catch ( Exception e1 )
	   {
		   e1.printStackTrace(  );
	   }
   }


public calculate()
{
    if (loanSlctBx.getSelectedItem().equals("7 years at 5.35%"))//array used to selcted term and APR bassed on the Combo Box
								 {
									 apr=aprArray[0];//tells to store the 1st value to the varible
									 term=termArray[0];
								 }
								 else if (loanSlctBx.getSelectedItem().equals("15 years at 5.5%"))
								 {
									 apr=aprArray[1];
									 term=termArray[1];//tells to store the 2nd value to the varible
								 }
								 else if (loanSlctBx.getSelectedItem().equals("30 years at 5.75%"))
								 {
									 apr=aprArray[2];//tells to store the 3rd value to the varible
									 term=termArray[2];
								 }

				 // Makes all the calculation needed for the output\

		 	     amnt = Double.parseDouble(amntFld.getText());
		 	     amntFld.setText(currencyComposition(amnt)); // Formats the value to use a US currencey composistion
		 	     mTerm = term*12; //sets the mTerm variable
		 	     mApr = (apr / 100) / 12; //sets the mApr with the monthly interest
		  	     pay = amnt * (mApr / (1 - Math.pow((1 + mApr), -mTerm)));//sets the pay varible with the results
		  	     princPaid = amnt * ((1 - Math.pow((1 + mApr), (-mTerm))) / Math.pow(( 1 + mApr ), (mTerm - 1 ))); // sets the princ paid over the term of the loan
		 	     pricePaidPerct = princPaid / amnt;
		 	     double newAmnt = amnt;//sets new variable for new loan amount
		  	     //Loop to run the calulations thorughout the life of the loan
		 	     for (int i=0; i<mTerm;i++)
		 	     {
		 			 if (i==36||i==72||i==108||i==144||i==180||i==216||i==252||i==288||i==326||i==360)
		 			 {
		 				 try
		 				 {
		 					 Thread.sleep(500);//sleep timer
		 				 }
		 				 catch (InterruptedException e1)
		 				 {
		 				 }
		 			 }
		 			 //Prints the output for the calculations in the JTextAera one line at a time
		 			 payFld.append(i+1 +"                        "+currencyComposition(pay)+"                       "+currencyComposition(newAmnt*mApr)+"                    "+currencyComposition(pay-(newAmnt*(mApr))) +"         "+currencyComposition(newAmnt)+"\n");
		 			 newAmnt -= pay-(newAmnt*(mApr));
				 }
			 }


 //[for all your fields above you need getters (if you want them visible)]

 public double getpricePaidPerct()
 {
    return pricePaidPerct;
  }
}



class GraphPanel extends JPanel
{
  private static final long serialVersionUID = 1L;
	BufferedImage backBuffer;  //  @jve:decl-index=0:

	this.mortgageData.getpricePaidPerct();

	JLabel amntLbl;


	/**
	 * This is the default constructor
	 */
	public GraphPanel(MortgageData mortgageData)
	{
	  this.mortgageData = mortgageData;
	  init();
  }


	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	public void init()
	{
		this.setSize(500, 500);
	}

	public void paintComponent(Graphics g)
	{
		if(backBuffer == null)
		{
			backBuffer = new BufferedImage(1024,1024,BufferedImage.TYPE_INT_ARGB);
		}

		int width = backBuffer.getWidth();
		int height = backBuffer.getHeight();

		Graphics2D g2 = (Graphics2D)backBuffer.getGraphics();
		g2.setColor(Color.black);
		g2.fillRect(0, 0, width, height);

		//Adds the Legend
		Font font = new Font("Calibri", Font.BOLD, 30);
		g2.setFont(font);
		g2.setColor(Color.white);
		g2.drawString("Green is the Principal Paid", 20, 125);
		g2.drawString("Red is the Interest Paid", 20, 160);

		// Draw the Pie chart
		// Percentage of 360 degrees
		double princ = 360.0 * GraphPanel.pricePaidPerct;
		g2.setColor(Color.green);
		g2.fillArc(width/4, height/4, width/2, width/2, 0, (int) princ);

		// This is the red portion, we add 1 since the algorithm needs the exta pixels for the chart
		g2.setColor(Color.red);
		g2.fillArc(width/4, height/4, width/2, width/2, (int)princ ,(int)(360-princ)+1);


		// Draw the back buffer to the component
		g.drawImage(backBuffer, 0, 0, getWidth(), getHeight(), null);
	}
}

Open in new window

Author

Commented:
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:220: invalid method declaration; return type required
  private load(String termapr.txt)
          ^
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:220: ')' expected
  private load(String termapr.txt)
                             ^
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:220:  expected
  private load(String termapr.txt)
public MortgageData()
  {
    load("termapr.txt");
  }

  private load(String termapr.txt)
  {
	  //reads the file
	  Reader fis;
	  try
	  {
		  fis = new FileReader("termapr.txt"); //reads from the file in the same directory

	       BufferedReader b = new BufferedReader( fis );
	       String inputholder = ""; //creates appropreate strings
	       String values = "";
	       while ((inputholder = b.readLine()) != null)//set conditions for the values string
	       {
	       values = values + inputholder + ","; //sets value to the values string
	       }
	       String[] line = values.split(",");
	       termArray = new double[3];
	       termArray[0] = Double.parseDouble(line[0].trim());//adds values from file to arrays
	       termArray[1] = Double.parseDouble(line[2].trim());
	       termArray[2] = Double.parseDouble(line[4].trim());

	       aprArray = new double[3];
	       aprArray[0] = Double.parseDouble(line[1].trim());
	       aprArray[1] = Double.parseDouble(line[3].trim());
	       aprArray[2] = Double.parseDouble(line[5].trim());

	       b.close();
	       fis.close();
	   }
	   catch ( Exception e1 )
	   {
		   e1.printStackTrace(  );
	   }
   }

Open in new window

Author

Commented:
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:256: invalid method declaration; return type required
public calculate()
public calculate()
{
    if (loanSlctBx.getSelectedItem().equals("7 years at 5.35%"))//array used to selcted term and APR bassed on the Combo Box
								 {
									 apr=aprArray[0];//tells to store the 1st value to the varible
									 term=termArray[0];
								 }
								 else if (loanSlctBx.getSelectedItem().equals("15 years at 5.5%"))
								 {
									 apr=aprArray[1];
									 term=termArray[1];//tells to store the 2nd value to the varible
								 }
								 else if (loanSlctBx.getSelectedItem().equals("30 years at 5.75%"))
								 {
									 apr=aprArray[2];//tells to store the 3rd value to the varible
									 term=termArray[2];
								 }

				 // Makes all the calculation needed for the output\

		 	     amnt = Double.parseDouble(amntFld.getText());
		 	     amntFld.setText(currencyComposition(amnt)); // Formats the value to use a US currencey composistion
		 	     mTerm = term*12; //sets the mTerm variable
		 	     mApr = (apr / 100) / 12; //sets the mApr with the monthly interest
		  	     pay = amnt * (mApr / (1 - Math.pow((1 + mApr), -mTerm)));//sets the pay varible with the results
		  	     princPaid = amnt * ((1 - Math.pow((1 + mApr), (-mTerm))) / Math.pow(( 1 + mApr ), (mTerm - 1 ))); // sets the princ paid over the term of the loan
		 	     pricePaidPerct = princPaid / amnt;
		 	     double newAmnt = amnt;//sets new variable for new loan amount
		  	     //Loop to run the calulations thorughout the life of the loan
		 	     for (int i=0; i<mTerm;i++)
		 	     {
		 			 if (i==36||i==72||i==108||i==144||i==180||i==216||i==252||i==288||i==326||i==360)
		 			 {
		 				 try
		 				 {
		 					 Thread.sleep(500);//sleep timer
		 				 }
		 				 catch (InterruptedException e1)
		 				 {
		 				 }
		 			 }
		 			 //Prints the output for the calculations in the JTextAera one line at a time
		 			 payFld.append(i+1 +"                        "+currencyComposition(pay)+"                       "+currencyComposition(newAmnt*mApr)+"                    "+currencyComposition(pay-(newAmnt*(mApr))) +"         "+currencyComposition(newAmnt)+"\n");
		 			 newAmnt -= pay-(newAmnt*(mApr));
				 }
			 }

Open in new window

Author

Commented:
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:319: illegal start of type
      this.mortgageData.getpricePaidPerct();
      ^
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:319: ';' expected
      this.mortgageData.getpricePaidPerct();
          ^
C:\Users\Dale\Documents\Downloads\Downloads\Downloads\week 5\V2\Mortgage.java:319:  expected
      this.mortgageData.getpricePaidPerct();
                                         ^
class GraphPanel extends JPanel
{
  private static final long serialVersionUID = 1L;
	BufferedImage backBuffer;  //  @jve:decl-index=0:

	this.mortgageData.getpricePaidPerct();

	JLabel amntLbl;


	/**
	 * This is the default constructor
	 */
	public GraphPanel(MortgageData mortgageData)
	{
	  this.mortgageData = mortgageData;
	  init();
  }


	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	public void init()
	{
		this.setSize(500, 500);
	}

	public void paintComponent(Graphics g)
	{
		if(backBuffer == null)
		{
			backBuffer = new BufferedImage(1024,1024,BufferedImage.TYPE_INT_ARGB);
		}

		int width = backBuffer.getWidth();
		int height = backBuffer.getHeight();

		Graphics2D g2 = (Graphics2D)backBuffer.getGraphics();
		g2.setColor(Color.black);
		g2.fillRect(0, 0, width, height);

		//Adds the Legend
		Font font = new Font("Calibri", Font.BOLD, 30);
		g2.setFont(font);
		g2.setColor(Color.white);
		g2.drawString("Green is the Principal Paid", 20, 125);
		g2.drawString("Red is the Interest Paid", 20, 160);

		// Draw the Pie chart
		// Percentage of 360 degrees
		double princ = 360.0 * GraphPanel.pricePaidPerct;
		g2.setColor(Color.green);
		g2.fillArc(width/4, height/4, width/2, width/2, 0, (int) princ);

		// This is the red portion, we add 1 since the algorithm needs the exta pixels for the chart
		g2.setColor(Color.red);
		g2.fillArc(width/4, height/4, width/2, width/2, (int)princ ,(int)(360-princ)+1);


		// Draw the back buffer to the component
		g.drawImage(backBuffer, 0, 0, getWidth(), getHeight(), null);
	}
}

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
> public calculate()


methods need to define a return type
if they don't return anyhting then specify void

public void calculate()

Author

Commented:
Ok, I have errors at lines 319 and 256 solved I still need need to figure out 220. I don't think i should be voiding that line because should return the content from the file.
Correct, as Objects mentioned, every method must have a return type (or void). I don't think your load method does need to return anything.  It does its job, reading data from the file and populating the attributes.

Advice: take some time to read the compiler error messages carefully, they are very informative and will be your best friend once you've become familiar with them.

Have fun.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial