java help

KataKoniK|ATI

New member
Hey,

There is this problem that is driving me nuts and I cannot get the program output values to be like this...

help1.jpg


instead when I run the program I get this...

help.jpg


As you can see in the second pic, it just does the same calculation over and over again for each year.

I already tried doing a loop but it still won't do a separate calculation for each year. Like I said, it does the same calculation over and over again for each year.

Therefore,
What would be the proper technique to handle such a calculation?
I tried nested loops already but I still get the same incorrect output.

Here is the part of the code that does the calculation...

Code:
private void processInputs()
	{
		int year;
		double interest, principle, answer, answer1, answer2, answer3, answer4;
		
		year = numberOfYearsInput.getNumber();
		interest = interestRateInput.getNumber();
		principle = initialPrincipleInput.getNumber();

		for(int counter = 1; counter <= year; counter++)
		{
				answer3 = principle;
				answer = interest + 100;
				answer1 = answer / 100;
				answer2 = answer3 * answer1 * answer1 * answer1 * answer1;
				answer4 = answer2 - answer3;
				displayNumbers(counter, answer3, answer4, answer2);
		}
		
		
	}
	private void displayNumbers (int year1, double num1, double num2, double num3)
	{
		String numberLine = Format.justify ('l', year1, 20) +
							Format.justify ('r', num1, 20, 2) +
							Format.justify ('r', num2, 33, 2) +
							Format.justify ('r', num3, 40, 2);
		output.append (numberLine + "\n");
	}

Thanks in advance.


If necessary, here's the whole code for the program...

Code:
import javax.swing.*;
import BreezySwing.*;

public class Project7_5 extends GBFrame
{
	private JLabel 		interestRate;
	private JLabel		initialPrinciple;
	private JLabel		numberOfYears;
	private JLabel		yearNumberOutput;
	private JLabel      principleBeginYearOutput;
	private JLabel      interestEarnedOutput;
	private JLabel		principleEndYearOutput;
	private DoubleField interestRateInput;
	private DoubleField initialPrincipleInput;
	private IntegerField numberOfYearsInput;
	private JButton     compute;
	private JButton     clear;
	private JTextArea   output;
	
	public Project7_5()
	{
		//Define the table's header line
		String header = Format.justify ('l', "YEAR #", 25) +
						Format.justify ('r', "YEAR BEGINNING PRINCIPLE", 24) +
						Format.justify ('r', "INTEREST EARNED", 30) +
						Format.justify ('r', "YEAR ENDING PRINCIPLE", 40) +"\n";
		//Constructors
		interestRate			 = addLabel       ("Enter the interest rate (quarterly)"     ,1,1,1,1);
		initialPrinciple         = addLabel       ("Enter the initial principle" ,1,2,1,1);
		numberOfYears            = addLabel       ("Enter the number of years"   ,1,3,1,1);
		interestRateInput        = addDoubleField (0                             ,2,1,1,1);
		initialPrincipleInput    = addDoubleField (0							 ,2,2,1,1);
		numberOfYearsInput       = addIntegerField(0                             ,2,3,1,1);
		compute                  = addButton      ("Compute"                     ,3,1,1,1);
		clear					 = addButton      ("Clear"						 ,3,3,1,1);
		output                   = addTextArea    (header                        ,4,1,3,4);
		output.setEnabled(false);
	}
	public void buttonClicked (JButton buttonObj)
	{
			if(buttonObj == compute)
			{
				processInputs();
				interestRateInput.requestFocus();
			}
			else if(buttonObj == clear)
			{
				String header = Format.justify ('l', "YEAR #", 25) +
						        Format.justify ('r', "YEAR BEGINNING PRINCIPLE", 24) +
					          	Format.justify ('r', "INTEREST EARNED", 30) +
						        Format.justify ('r', "YEAR ENDING PRINCIPLE", 40) +"\n";
				output.setText(header);
			}
	}
	private void processInputs()
	{
		int year;
		double interest, principle, answer, answer1, answer2, answer3, answer4;
		
		year = numberOfYearsInput.getNumber();
		interest = interestRateInput.getNumber();
		principle = initialPrincipleInput.getNumber();

		for(int counter = 1; counter <= year; counter++)
		{
				answer3 = principle;
				answer = interest + 100;
				answer1 = answer / 100;
				answer2 = answer3 * answer1 * answer1 * answer1 * answer1;
				answer4 = answer2 - answer3;
				displayNumbers(counter, answer3, answer4, answer2);
		}
		
		
	}
	private void displayNumbers (int year1, double num1, double num2, double num3)
	{
		String numberLine = Format.justify ('l', year1, 20) +
							Format.justify ('r', num1, 20, 2) +
							Format.justify ('r', num2, 33, 2) +
							Format.justify ('r', num3, 40, 2);
		output.append (numberLine + "\n");
	}
			
		public static void main(String args [])
	{
		Project7_5 theGUI = new Project7_5();
		theGUI.setSize(900,400);
		theGUI.setVisible(true);
	}
}
 
Don't know if you ever figured out your problem, but if you didn't here ya go. Its quite simple.

You are not updating your principle each time you are calculating your numbers. You are just using the same principle which is 10 for every pass in the loop so of course all your calulations are going to be the same. Try this...

Code:
private void processInputs()
	{
		int year;
		double interest, principle, answer, answer1, answer2, answer3, answer4;
		
		year = numberOfYearsInput.getNumber();
		interest = interestRateInput.getNumber();
		principle = initialPrincipleInput.getNumber();

                answer3 = principle;
		for(int counter = 1; counter <= year; counter++)
		{
				 
				answer = interest + 100;
				answer1 = answer / 100;
				answer2 = answer3 * answer1 * answer1 * answer1 * answer1;
				answer4 = answer2 - answer3;
				displayNumbers(counter, answer3, answer4, answer2);
                                answer3 = answer2;
		}
		
		
	}
This way your intial principle which is 10 is set before it enters the loop. Then when it prints out the calculations, your principle gets updated by "answer3 = answer2;"

Hope this helps!
-Heaton
 
Back
Top