Jump to content

How to rewrite this as innerclass/ anonymous inner class?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
sam199006

sam199006

    Newbie

  • Members
  • Pip
  • 5 posts
I need to rewrite my two code "chunks". The "simple" chunk needs to be a non-anonymous inner class and the "compound" chunk needs to be an annymous inner class. I wrote this class before noticing that the prof wanted it to be written a certain way. It is due tonight but I can't rewrite these two parts without a bunch of errors popping up (never used inner classes before). Can someone help me with this?


the simple chunk is the second last portion and the compound chunk is te last portion. I appreciate any help yu guys can lend!


public class InterestCalculator extends JPanel {


	public JTextField principal, rate, years;

	public JLabel display, displayVal;

	public double princ, perc, time, power, finAmount;

	public String princ1, perc1, time1, curValue;

	public InterestCalculator(int width, int height) {

		display = new JLabel();

		displayVal = new JLabel();


		setLayout(new GridLayout(0,6));

		

		JLabel prin = new JLabel("Principal: ");

		add(prin);

		int fileNameTextFieldLength = 10;

		principal = new JTextField(fileNameTextFieldLength);

		add(principal);

		

		JLabel prin1 = new JLabel("Rate(Percentage): ");

		add(prin1);

		int fileNameTextFieldLength1 = 5;

		rate = new JTextField(fileNameTextFieldLength1);

		add(rate);


		JLabel prin2 = new JLabel("Years: ");

		add(prin2);

		int fileNameTextFieldLength2 = 3;

		years = new JTextField(fileNameTextFieldLength2);

		add(years);

		

		JLabel spacer1 = new JLabel("");

		JLabel spacer2 = new JLabel("");

		JLabel spacer3 = new JLabel("");

		JLabel spacer4 = new JLabel("");

		JLabel spacer5 = new JLabel("");

		JLabel spacer6 = new JLabel("");

		add(spacer1);

		

		JButton simple = new JButton("Compute Simple Interest");

		JButton compound = new JButton("Compute Compound Interest");

		add(simple);

		add(spacer2);

		add(compound);

		add(spacer3);

		add(spacer4);

		add(spacer5);

		add(spacer6);

		

		simple.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				time1 = years.getText();

				time = Double.parseDouble(time1);

				perc1 = rate.getText();

				perc = Double.parseDouble(perc1);

				princ1 = principal.getText();

				princ = Double.parseDouble(princ1);

				finAmount = (princ + (princ * (perc/100) * time));

				curValue = NumberFormat.getCurrencyInstance().format(finAmount);

				add(display);

				display.setText("Computed Simple Interest is: " + curValue);

				add(displayVal);

				displayVal.setText("" + curValue);

			}

		});

		

		compound.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				time1 = years.getText();

				time = Double.parseDouble(time1);

				perc1 = rate.getText();

				perc = Double.parseDouble(perc1);

				princ1 = principal.getText();

				princ = Double.parseDouble(princ1);

				power = Math.pow((1+(perc/100)), time);

				finAmount = princ*power;

				curValue = NumberFormat.getCurrencyInstance().format(finAmount);

				add(display);

				display.setText("Computed Compound Interest is: ");

				add(displayVal);

				displayVal.setText("" + curValue);

			}

		});

		

		

	}

	

	

	

}

Edited by Roger, 19 October 2011 - 03:59 PM.
added [code] tags


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
It's reallllyyy hard reading your code without surrounding it with the
 tags.


An anonymous-inner class looks something like this:

[code]

button.addActionListener( new ActionListener() {

    public void actionPerformed(ActionEvent e) {

        doSomeWork();

    }

});


I'm guessing the non anonymous inner class would look something like this:

class A {

   someMethod() {

        button.addActionListener( new MyActionListener() );

   }

   private class MyActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            doSomeWork();

        }

    }

}



#3
sam199006

sam199006

    Newbie

  • Members
  • Pip
  • 5 posts
I changed my class to what you see below, but I get nothing showing when I click the simple button. Any idea why?


class simpleStuff {

		  void simpleMethod() {

		        simple.addActionListener( new MyActionListener() );

		   }

			   class MyActionListener implements ActionListener {

			        public void actionPerformed(ActionEvent e) {

			        	time1 = years.getText();

				time = Double.parseDouble(time1);

				perc1 = rate.getText();

				perc = Double.parseDouble(perc1);

				princ1 = principal.getText();

				princ = Double.parseDouble(princ1);

				finAmount = (princ + (princ * (perc/100) * time));

				curValue = NumberFormat.getCurrencyInstance().format(finAmount);

				add(display);

				display.setText("Computed Simple Interest is: " + curValue);

				add(displayVal);

				displayVal.setText("" + curValue);

			        }

			    }

			}

Edited by Roger, 19 October 2011 - 03:59 PM.
added [code] tags


#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Again, when posting code wrap [code] tags around it. It keeps the formatting and makes it more readable.

When you add all of your components onto a frame then make the frame visible, any additional components(like a JLabel) that are added make the frame dirty and the frame has to be repainted.
This could be the reason you don't see anything. So...
Try adding all of your JLabels (display, displayVal, etc) near the beginning of execution.

Then later call the setText(...) methods.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users