02-JavaFundamentals

String s abc int n 0 double x 00 a b c d s n x s n x

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: + (0.06 * bill); What is value for bill now? ________ Which letters represent valid assignments given these 3 variable initializations? String s = "abc"; int n = 0; double x = 0.0; a) b) c) d) s n x s = = = = n; x; n; 1; e) f) g) h) n x s n = = = = 1.0; 999; "abc" + 1; 1 + 1.5; Arithmetic Expressions Arithmetic expressions consist of operators such as + - / * and operands such as 40, 1.5, payRate, and hoursWorked Example expression used in an assignment: grossPay = payRate * hoursWorked; Another example expression: 5 / 9 * (fahrenheit - 32); For the previous expression, Which are the operators?_____ Which are the operands?_____ Arithmetic Expressions Arithmetic expressions take many forms or or or or or or a numeric variable a numeric constant expression + expression expression - expression expression * expression expression / expression (expression ) double x = 1.2; 100 or 99.5 1.0 + x 2.5 - x 2*x x / 2.0 (1 + 2.0) Precedence of Arithmetic Operators Expressions with more than one operator require some sort of precedence rules: evaluated in a left to right order evaluated in a left to right order in the absence of parentheses Evaluate 2.0 + 4.0 - 6.0 * 8.0 / 6.0 * - / + Use (parentheses) for readability or to intentionally alter an expression: double C, F; F = 212.0; C = 5.0 / 9.0 * (F - 32); What is the current value of C ____? int Arithmetic variables are similar to double, except they can only store whole numbers (integers) int int int int anInt = another noCanDo 0; = 123; = 1.99; // ERROR Division with integers is also different • Performs quotient remainder whole numbers only anInt = 9 / 2; // anInt = 4, not 4.5 anInt = anInt / 5; What is anInt now? ___ What is anInt now? ___ anInt = 5 / 2; The integer % operation The Java % operator returns the remainder anInt = 9 % 2; // anInt ___1___ anInt = 101 % 2; What is anInt now? ___ anInt = 5 % 11; What is anInt now? ___ anInt = 361 % 60; What is anInt now? ___ int quarter; quarter = 79 % 50 / 25; What is quarter? ___ quarter = 57 % 50 / 25; What is quarter now? ___ What is C now? int C, F; F = 212; C = 5 / 9 * (F - 32); What is the current value of C ____?...
View Full Document

This note was uploaded on 01/25/2014 for the course CSC 227 taught by Professor Mercer during the Fall '12 term at University of Arizona- Tucson.

Ask a homework question - tutors are online