Introduction: Java Programming- Compound Interest

Picture of Java Programming- Compound Interest

Hello in this Instructable I will be showing you how to create a Java program that calculates the interest applied to the principal deposited over course of time.  Before you start this Instructable you will need things:

1.  A laptop/desktop computer

2.  A Java Compiler called Dr. Java (search in Google and type in Dr. Java, and download it from a link for free)

3.  The formula of Compound Interest ( A = P(1 + r / n)^ t * n and A = P * e^ r * t)

Step 1: Step 1: Open Dr. Java

Picture of Step 1: Open Dr. Java

Once you have Dr. Java installed onto your computer go to your desktop and double click the Dr. Java icon to open it.  They may be a message asking if you want all your java programs uploaded to Dr. Java.  You can press yes or no, either way it doesn't matter.

Step 2: Step 2: Import Java Utilities Feature

Picture of Step 2: Import Java Utilities Feature

Before we start writing the actual code we need to install a feature in Java called Scanner.  To use this feature at the top of the program we will type in:

import java.util.*;


After that press the Enter key twice.

Step 3: Step 3: Public Class

Picture of Step 3: Public Class

All of Java code (excluding import command) is written in the class method.  We also need a name for the program, in this example we will call it "test".  Now type in:

public class test{ }


After you type in the class, move your cursor between the brackets next to the word "test" and press Enter twice.

Step 4: Step 4: Main Method

Picture of Step 4: Main Method

Now within the class method we will type in the main method.  Since we only need to display our results at the end, we can use the "void" feature. So we type in:

public static void main(String[ ] args){ }


Once again move you between the brackets and press the Enter key twice.

Step 5: Step 5: Call Scanner Feature

Picture of Step 5: Call Scanner Feature

Now in this program we are going to need to input information. like our Initial Amount (P), Years (y), # of times Compounded a year (n) and the rate (r).  To do this we must call a feature called Scanner and we will give it a name as well (console).  To do this type in:

Scanner console = new Scanner(System.in);

After press Enter key twice.

Step 6: Step 6: Display Message

Picture of Step 6: Display Message

We want to input our information into the program, but it helps the user if he/she knows when to enter it.  To do this we will display a message each variable (which I will discuss in the next step).  We will start by displaying the "Enter Initial Amount".  You will type:

System.out.print("Enter Initial Amount");


After press the Enter key once or twice.

Step 7: Step 7: Declaring a Variable

Picture of Step 7: Declaring a Variable

Now to enter our information into the program you will to declare variables.  Variables are like folders in a cabinet; they hold information (or a value, usually a number) and we use that variable in our program.  In this program we will be using an int declaration (for a number that is whole) or a double declaration (for numbers with a decimal place.  There may be a time when you want to enter change into your initial amount, so we will use the double declaration.  We will start with the "initial amount" variable and we will call the variable initialAmount.  we will type in:

double initialAmount = console.nextDouble( );

Then press the Enter key once or twice.


Step 8: Step 8: Repeat

Picture of Step 8: Repeat

You will repeat steps from 6 and 7 for the rest of the variables.

You will System.out.print(""); to display your message before each of your variable declarations.

Then you will declare a variable for # of times compounded a year, rate and years (you can declare years as a int).

Step 9: Step 9: the If Condition

Picture of Step 9: the If Condition

There are two different formulas for calculating Compound Interest, but what if you wanted to use both.  You can by using the "if" condition.  Whenever need the program to choose between multiple options, you can set different conditions for each option so the program picks that specific one.  We will use logic statement to set the condition. Type in:

if(compound > 0){ }

Then move your cursor between the brackets and press Enter key twice.

Within the if condition we will calculate the Compound Interest and initialize it into a variable that we will declare as well.  We will also display a answer.  Will we first type in the variable amount and declare it as a double variable.  We type in:

double amount = initialAmount * Math.pow( 1 + ( rate / compound), years * compound);
System.out.printf("Your Amount is %10.2f\n", amount);


Move the cursor outside of the if condition and press Enter once or twice.

Step 10: Step 10: the Else If Condition

Picture of Step 10: the Else If Condition

Now we want the option to use the Continuous Compound formula.  Just like the if condition we will input the formula and then display the amount.  This second condition is called the else if condition (as in if not the last then this one may work).  We will type in:

else if (compound == 0){ }

Then move your cursor between the brackets and press the Enter key twice.

In the brackets we type in:

double amount = initialAmount * Math.pow( Math.E, rate * years);
System.out.printf ("Your Amount is %10.2f\n", amount);


Move the cursor outside of the else if brackets and press Enter once or twice.

Step 11: Step 11: Else Condition

Picture of Step 11: Else Condition

There is always a change that the information the user enter may be incorrect.  To anticipate this error we will create a condition called the else condition.  In this condition we are just going to display a message saying the information you entered is invalid.  So type in:

else { }

Move your cursor between the brackets and press Enter twice.

Now type in your message:

System.out.println ("Invalid Input");

Move your cursor outside of the else condition and press Enter key twice.

Step 12: Step 12: Compile

Picture of Step 12: Compile

Once you have finished the last step you can now Compile your code.  Compiling is a feature that looks over your code to make sure there isn't any syntax errors you have made during coding.  The errors will be displayed in yellow and the information about the error will be list at the bottom.  You must correct all errors before you can run the program.

Step 13: Step 13: Run Program

Picture of Step 13: Run Program

Once the code is without error go up the top of Dr. Java and press the Run option; this will execute the code.

Enter the information as display.

Then the program will calculate the amount.

Step 14: Step 14: Finished

Picture of Step 14: Finished

Now you have a program that calculates Compound Interest.  Enjoy!

Comments

About This Instructable

30,304views

10favorites

License:

More by hgrad12:Java Programming- Compound Interest
Add instructable to: