Assignment statements using the same variable in LHS and RHS.

Slides:



Advertisements
Similar presentations
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Advertisements

Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
The switch statement: an N-way selection statement.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Using the Java programming language compiler. Review of relevant material from previous lectures From previous lectures: A computer can only execute machine.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
Shorthand operators.
Floating point variables of different lengths. Trade-off: accuracy vs. memory space Recall that the computer can combine adjacent bytes in the RAM memory.
The character data type char
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
Java TA Session 1. Software Java Runtime Environment (JRE) java.exe Java Development Kit (JDK) java.exe, javac.exe Version 1.6 = Version 6, Version 1.7.
Parameter passing mechanism: pass-by-value. Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately)
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
What does a computer program look like: a general overview.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Working with arrays (we will use an array of double as example)
Introduction to programming in the Java programming language.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
Mixing integer and floating point numbers in an arithmetic operation.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
The Bisection Method. Introduction Bisection Method: Bisection Method = a numerical method in Mathematics to find a root of a given function.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
Arithmetic expressions containing Mathematical functions.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Simple algorithms on an array - compute sum and min.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Sophomore Scholars Java
Introduction to Java Import Scanner class to use in our program
Compiling and Running a Java Program
Chapter 2 Elementary Programming
Data types, Expressions and assignment, Input from User
Comp Sci 200 Programming I Jim Williams, PhD.
First Programs CSE 1310 – Introduction to Computers and Programming
Something about Java Introduction to Problem Solving and Programming 1.
While Statement.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
The Boolean (logical) data type boolean
Java Intro.
Introduction to Java Brief history of Java Sample Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
The for-statement.
Consider the following code:
CS Programming I Jim Williams, PhD.
Presentation transcript:

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 (x contains 1.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 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: 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 p 0 = principle and i = interest_rate The principle amount after 1 year is: p 1 = (1.0 + i/100.0)×p 0 The principle amount after 2 year is: p 2 = (1.0 + i/100.0)×p 1 The principle amount after 3 year is: p 3 = (1.0 + i/100.0)×p 2

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: nterest01.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: nterest02.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