Assignment statements using the same variable in LHS and RHS Previously discussed Assignment statement: The assignment statement in the Java programming language instructs the computer to update the value stored in a variable Syntax of an assignment statement:
VariableName = Expression ;
Meaning of the assignment statement: The expression on the RHS of the "=" operator is first computed The computed value is then assigned to the receiving variable Assignment statements with the same variable on the LHS and RHS Consider the following program:
public class Assign01
{
public static void main(String[] args)
{
double x = 1.0; // x = 1.0
System.out.print("Before: x = ");
System.out.println(x);
x = x + 4.0; // x is used in the LHS and RHS
System.out.print("After: x = ");
System.out.println(x);
}
}
Explanation: You need to realize that the symbol "=" does not represent an equality relationship The symbol "=" denotes an assignment operation: The computer will first evaluate the LHS "x + 4.0":
LHS = x + 4.0 (x contains 1.0)
= 1.0 + 4.0
= 5.0
Then the result 5.0 is assigned to the receiving variable x:
x = 5.0;
Therefore, after executing the statement "x = x + 4.0", the variable x contains the value 5.0 Example Program: (Demo above code) Prog file: click here How to run the program: Right click on link and save in a scratch directory To compile: javac Assign01.java To run: java Assign01 Example program: interest computation Problem statement: Write a Java program that read in the following information: A principle amount principle A interest rate interest_rate (given in percents) The program prints the principle ammount for the next 3 years (with the interest added to the principle) Algorithm: Let p0 = principle and i = interest_rate The principle amount after 1 year is: p1 = (1.0 + i/100.0)×p0 The principle amount after 2 year is: p2 = (1.0 + i/100.0)×p1 The principle amount after 3 year is: p3 = (1.0 + i/100.0)×p2 A preliminary version of the Java program:
import java.util.Scanner;
public class Interest01
{
public static void main(String[] args)
{
double principle, interest_rate;
double p1, p2, p3;
Scanner in = new Scanner(System.in); // Construct a Scanner object
System.out.print("Enter principle = ");
principle = in.nextDouble(); // Read in principle
System.out.print("Enter interest rate = ");
interest_rate = in.nextDouble(); // Read in interest rate
p1 = (1.0 + interest_rate/100.0) * principle;
System.out.print("Principle after 1 year = ");
System.out.println(p1);
p2 = (1.0 + interest_rate/100.0) * p1;
System.out.print("Principle after 2 year = ");
System.out.println(p2);
p3 = (1.0 + interest_rate/100.0) * p2;
System.out.print("Principle after 3 year = ");
System.out.println(p3);
}
}
Comments: This program works, but it uses an unnecessarily large number of variables We can use the variable principle to record the year-to-year principle amount. Example Program: (Demo above code) Prog file: click here How to run the program: Right click on link and save in a scratch directory To compile: javac Interest01.java To run: java Interest01 Advice in writing programs Programming principle: Each variable in the program stores one piece of information A good programming practice is to assign a meaning to each variable in the program Giving a meaning to each variable will help you understand the steps of the algorithm Improved program to compute interest We will re-write the preliminary version of the program using the following meaning of the variables: principle = the current amount of principle interest_rate = interest rate paid Java program:
import java.util.Scanner;
public class Interest01
{
public static void main(String[] args)
{
double principle, interest_rate;
Scanner in = new Scanner(System.in); // Construct a Scanner object
System.out.print("Enter principle = ");
principle = in.nextDouble(); // Read in principle
System.out.print("Enter interest rate = ");
interest_rate = in.nextDouble(); // Read in interest rate
principle = (1.0 + interest_rate/100.0) * principle;
System.out.print("Principle after 1 year = ");
System.out.println(principle);
principle = (1.0 + interest_rate/100.0) * principle;
System.out.print("Principle after 2 year = ");
System.out.println(principle);
principle = (1.0 + interest_rate/100.0) * principle;
System.out.print("Principle after 3 year = ");
System.out.println(principle);
}
}
Explanation: By storing the computed value "(1.0 + interest_rate/100.0) * principle" back into the variable principle, this variable principle will contains the correct value to be used in the next computation !!! Example Program: (Demo above code) Prog file: click here How to run the program: Right click on link and save in a scratch directory To compile: javac Interest02.java To run: java Interest02