190 likes | 393 Views
Assignment statements using the same variable in LHS and RHS. Previously discussed. Assignment statement: Syntax of an assignment statement :. The assignment statement in the Java programming language instructs the computer to update the value stored in a variable.
E N D
Assignment statements using the same variable in LHS and RHS
Previously discussed • Assignment statement: • Syntax of an assignment statement: • The assignment statement in the Java programming language instructs the computer to update the value stored in a variable VariableName = Expression ;
Previously discussed (cont.) • 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); } }
Assignment statements with the same variable on the LHS and RHS (cont.) • Explanation: • You need to realize that the symbol "=" does not represent an equality relationship • The symbol "=" denotes an assignment operation: • 1. The computer will first evaluate the LHS "x + 4.0": LHS = x + 4.0 (x contains 1.0) = 1.0 + 4.0 = 5.0
Assignment statements with the same variable on the LHS and RHS (cont.) Therefore, after executing the statement "x = x + 4.0", the variable x contains the value 5.0 • 2. Then the result 5.0 is assigned to the receiving variable x: x = 5.0;
Assignment statements with the same variable on the LHS and RHS (cont.) • Example Program: (Demo above code) • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Assign01.java • 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: The program prints the principle amount for the next 3 years (with the interest added to the principle) • A principle amount principle • A interest rate interest_rate (given in percents)
Example program: interest computation (cont.) • 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
Example program: interest computation (cont.) • 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
Example program: interest computation (cont.) 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); } }
Example program: interest computation (cont.) • 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: interest computation (cont.) • Example: • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Interest01.java • 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
Improved program to compute interest (cont.) • 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
Improved program to compute interest (cont.) 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); } }
Improved program to compute interest (cont.) • 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 !!!
Improved program to compute interest (cont.) • Example: • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Interest02.java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Interest02.java • To run: java Interest02