Assignment statements using the same variable in LHS and RHS
-
Upload
elijah-mills -
Category
Documents
-
view
45 -
download
2
description
Transcript of Assignment statements using the same variable in LHS and RHS
![Page 1: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/1.jpg)
Assignment statements using the same variable in LHS and RHS
![Page 2: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/2.jpg)
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 ;
![Page 3: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/3.jpg)
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
![Page 4: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/4.jpg)
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); } }
![Page 5: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/5.jpg)
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
![Page 6: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/6.jpg)
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;
![Page 7: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/7.jpg)
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
![Page 8: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/8.jpg)
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)
![Page 9: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/9.jpg)
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
![Page 10: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/10.jpg)
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
![Page 11: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/11.jpg)
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);
} }
![Page 12: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/12.jpg)
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.
![Page 13: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/13.jpg)
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
![Page 14: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/14.jpg)
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
![Page 15: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/15.jpg)
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
![Page 16: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/16.jpg)
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
![Page 17: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/17.jpg)
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); } }
![Page 18: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/18.jpg)
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 !!!
![Page 19: Assignment statements using the same variable in LHS and RHS](https://reader035.fdocuments.net/reader035/viewer/2022072014/56812cbd550346895d917710/html5/thumbnails/19.jpg)
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