Topics Discussed: Using java to compute simple calculations, such as bank interest.

Source Code for Lesson 3:

import java.util.Scanner;
class Jtutorial1 {
    public static void main(String args[]){
       Scanner input = new Scanner(;
       //Asks a user for how much money they want to invest in a bank
       //What is the APR of that bank. Annual percentage rate.
       //How long do you want to invest the money for.
       //i = p*r*t.  interest = principle (starting investment)*(rate)*(time--in years)
       System.out.println("How much money are you interested in investing: ");
       double principle = input.nextDouble();
       System.out.println("\nWhat is the APR offered at this bank? (5.9% = 5.9):  ");
       double rate = input.nextDouble();
       //rate = rate /100;
       rate /= 100; //Same as above.
       System.out.println("\nHow long do you want to invest this money for? ");
       int Time = input.nextInt();
       double total = principle * (1+rate*Time);
      // System.out.println("\nThe total amount of money in the account after: " + Time + " years is: " + total); // Same as below but without using total variable.
       System.out.println("\nThe total amount of money in the account after: " + Time + " years is: " + (principle * (1+rate*Time)));
    } //End main
} //End class

Homework: Write a ‘financial goal’ calculator, this calculator can take in a variety of inputs so long as it provides the unknown inputs via equation / logic. Example: allow input for Time invested / rate / how much money you want in the end — determine starting amount of money. Or Starting money, rate, and goal money, determine how long it would take. Etc. If you need help with this formula, or with posting your code to ideone, please let me know.

Compound operations
A compound operation is any time where we’re doing in one statement what might have otherwise taken more than one operation to state. There are actually 3 operations in this lesson that I would consider to be compound operations for varying reasons, lets take a look at each of them.

double principle = input.nextDouble();

The reason that I consider this to be a compound operation is because it would be more basic to have this as two statements, stated something like this:

double principle;  
principle = input.nextDouble();

But as you can see, we are able to declare this in one statement.

rate /= 100;

The reason this is a compound operator is because of the special /= arithmetic operator. There are actually a few of those as follows:

+=, -=, /=, *=,

All of these are handled in the same way. When you append them to a variable they have this basic syntax: “variable =variable(operation) number;

System.out.println("\nThe total amount of money in the account after: " + Time + " years is: " + (principle * (1+rate*Time)));

The reason why this is a compound operation should be pretty apparent. Rather than using a variable named “total” to store the total amount of money, we’re actually computing it using math in the println() statement.

Last modified: April 7, 2019