240 likes | 246 Views
CS110- Lecture 4 Feb 9, 2005. Announcements Send me email (ASAP) if you don’t have an OWL id. Default password is “cs110”. Lab 2 is next week. Questions?. Agenda. Expressions Arithmetic Operators Operator Precedence Increment and decrement Operators Assignment Operators
E N D
CS110- Lecture 4Feb 9, 2005 • Announcements • Send me email (ASAP) if you don’t have an OWL id. • Default password is “cs110”. • Lab 2 is next week. • Questions? CS110-Spring 2005, Lecture 4
Agenda • Expressions • Arithmetic Operators • Operator Precedence • Increment and decrement Operators • Assignment Operators • Data Conversions and Conversion Techniques • Interactive Programs • Scanner Class • Programs to Demonstrate above concepts CS110-Spring 2005, Lecture 4
Expressions • An Expression is a combination of one or more operators that usually perform a calculation. • The operands might be literals, constants variables or other sources of data. E.g.: int result; // declaration result = 20/4; //Expression result = result + 4; //Expression CS110-Spring 2005, Lecture 4
Arithmetic Operators • +, - , * do what you expect them to • %(Modulus or remainder operator) gives remainder • 20 % 6 = 2 • 2000 % 4 (might be helpful to find if year is a leap year) • 11 % 2 (if 1 then number is odd otherwise even) CS110-Spring 2005, Lecture 4
Arithmetic Operators • Division Operator (/) • Integer Arithmetic: / truncates • 20/6 is 3 • Decimal Arithmetic: works properly • 20.0/6, 20/6.0, 20.0/6.0 are all 3.33333… • Unary Operators (only one operand) • -3, +4(rarely used) • No built in operator for exponentiation. • But Math class in java class library has methods that perform mathematical functions. • Math.pow(2,3) = 8.0, Math.sqrt(25) = 5.0 CS110-Spring 2005, Lecture 4
Operator Precedence • Operators can be combined to create more complex expressions. • E.g.: result = 14 + 8/2; • The right hand expression is evaluated and the result is stored in result variable. • Order of operator evaluation makes a big difference. CS110-Spring 2005, Lecture 4
Operator Precedence • All expressions are evaluated according to an operator precedence hierarchy. • E.g.: result = 14 + 8/2; • However precedence can be forced by using parentheses. • E.g.: result = (14 + 8)/2; • Parentheses can be nested and the innermost nested expressions are evaluated first. • E.g.: result = 3 * ((18-4) /2 ); 18 result 11 result 21 result CS110-Spring 2005, Lecture 4
Operator Precedence • +, -, *, /, % : Left to right associative – meaning that arithmetic operators at the same level of precedence are performed left to right. • + and - have same precedence • *, /, % have same precedence (higher than + and -) • E.g.: result = 3 * (18-4)/2 ; • E.g.: result = 3 + 16 /4 - 2 ; 21 result 5 result CS110-Spring 2005, Lecture 4
Operator Precedence • A syntactically correct expression has matching left and right parentheses. • When a variable is referenced in an expression, its current value is used to perform the calculation. • E.g.: int count = 4, total = 5, sum; sum = count + total; sum = sum + 1; 9 sum 10 sum CS110-Spring 2005, Lecture 4
Increment and Decrement operators • Increment operator (++) adds 1 to the value and decrement operator (--) subtracts from the value • sum++; //this will increment value of sum by 1 and result is // stored back into the variable sum • sum++ (postfix increment operator) • ++sum (prefix increment operator) CS110-Spring 2005, Lecture 4
Increment and Decrement operators • sum++ and ++sum are functionally equivalent. • When these operators are used in a larger expression they can yield different results. If sum is 15 then • total = sum++; //total is 15 and sum is 16 • total = ++sum; //total is 16 and sum is 16 CS110-Spring 2005, Lecture 4
Assignment Operators • += can be used as follows • sum += 5; // sum = sum +5; • // following statement is equivalent to // sum = sum+total+12/count sum += total + 12/count ; Likewise we can use -=, *=, /=, %= CS110-Spring 2005, Lecture 4
Data Conversion • It is sometimes helpful and necessary to convert data value of one type to another type. • Careful about losing information. • Suppose a variable of type long is converted to int value (narrowing conversion)- Loss of information. • Suppose a variable of type int is converted to long value (widening conversion)- Generally no loss of information as more storage space available. CS110-Spring 2005, Lecture 4
Conversion Techniques • Assignment Conversion long money = 24567890987L; int dollar = 342567; money = dollar; //int value is converted // to long variable If we try to do dollar = money; //compile time error But we can do this by: dollar = (int) money; //Casting CS110-Spring 2005, Lecture 4
Conversion Techniques • Promotion- This conversion occurs automatically when certain operators need to modify their operands in order to perform the operation. • float sum = 25.0F; int count = 3; float result = sum /count;//count is promoted // to float automatically String totalStudents= “Total:”+ 24; CS110-Spring 2005, Lecture 4
Conversion Techniques • Casting- The most general form of conversion in Java. A cast is a java operator that is specified by a type name in parenthesis. • dollar = (int) money; //Casting • int total = 10; int count = 4; float result = total /count; //result = 2.0 float result = (float) total /count; //2.5 CS110-Spring 2005, Lecture 4
Interactive programs • Good programs read data from the user interactively during execution. • This way new results can be computed each time the program is run. • The Scanner class which is part of the standard Java class library(1.5) provide convenient methods for reading input values of various types. CS110-Spring 2005, Lecture 4
Scanner class • Before sending requests to (or calling methods of) any class we must create its instance (object). • Mostly use new reserved word to create a new object. • Note that String is an exceptional case: String greeting = “Hello! World”; String greeting = new String (“Hello! World”); CS110-Spring 2005, Lecture 4
Scanner class • Scanner keyboard = new Scanner(System.in); • Message invoking a method • int i = keyboard.nextInt() nextInt() { get number user types return that number } Somewhere in Scanner.java i = that number CS110-Spring 2005, Lecture 4
Add two numbers public class SumTwoNumbers { //Execution always starts from main public static void main(String[] args) { System.out.println(“Enter two numbers on line”); Scanner keyboard = new Scanner(System.in); int num1 = keyboard.nextInt(); int num2 = keyboard.nextInt(); System.out.println(“Sum is:”); System.out.println(num1+num2); } } CS110-Spring 2005, Lecture 4
More programs…. • Write a class AppleBasket. • It should have only one method main. • It asks the user for no. of baskets and no. of apples in each basket. It then prints the total no. of apples. • Output should be something likethis: Enter the number of apples in each basket: 6 Enter the number of baskets: 10 The total number of apples is: 60 CS110-Spring 2005, Lecture 4
More programs…. • Write a class ChangeMaker. • It should have only one method main. • It asks the user for a no. between 1 and 99. It tells the user one combination of coins that equals that amount of change. • Output should be something likethis: Enter a number between 1 and 99: 87 87 cents in coins can be given as: 3 quarters 1 dimes 0 nickels 2 pennies CS110-Spring 2005, Lecture 4
Pseudo code for ChangeMaker • Declare originalAmount, amount, quarters, dimes, nickels, pennies of type int • Read the amount from user into the variable amount • Assign amount to originalAmount • Set the variable quarters equal to the maximum number of quarters in amount. • Reset amount to the change left after giving that many quarters. • Repeat steps 4 and 5 for dimes and nickels. • Assign amount left to pennies • Output originalAmount and the numbers of each coin. CS110-Spring 2005, Lecture 4
Exercise • Programming Projects • 2.2 • 2.8 • Reading Exercise • Section 2.7 – 2.9 CS110-Spring 2005, Lecture 4