COMP 14 Introduction to Programming Adrian Ilie June 28 2005 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Today Variables and expressions Input Output Writing a whole program 2 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Variables Associated with data Input data Output data Intermediate data We need to define Data type Identifier Values will be assigned in expressions 3 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Variables Steps 1 Identify all data from the algorithm 2 Determine data types based on the range and nature of the values 3 Find meaningful names 4 Example Ch 1 Exercise 10 Compute average score Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Declaration of Variables dataType identifier Must be declared before it can be used Can be but doesn t have to be initialized when declared Identifier should start in lowercase indicate separate words with uppercase good style Example number of students in class int numStudents Multiple variables of the same data type can be declared on a single line int numStudents numGrades total 5 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Assignment variable expresssion Assignment Operator expression can be a value 3 or a mathematical expression 2 1 The expression must evaluate to the same data type as the variable was declared 6 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Assignment The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the operator is evaluated answer 4 sum 4 MAX lowest 2 3 1 Then the result is stored in the variable on the left hand side 7 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Assignment The right and left hand sides of an assignment statement can contain the same variable First one is added to the original value of count count count 1 Then the result is stored back into count overwriting the original value 8 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Example average score Write assignments and expressions Declaration of variables good style a At the beginning b Before being used 9 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Named Constant static final dataType IDENTIFIER value Declared by using the reserved word final Always initialized when it is declared Identifier should be in ALL CAPS separate words with underscore good style Example 1 inch is always 2 54 centimeters final double CM PER INCH 2 54 10 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions What is stored in the memory location referred to by the identifier num after each of the following statements is executed in order int num num num num num 3 5 4 2 num 2 3 4 5 unknown 3 7 14 error would give an error since 3 4 5 would not result in an int 11 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions Which of the following are valid Java assignment statements Assume that i x and percent have been declared as double variables and properly initialized 1 2 3 4 12 i i x 2 x 2 5 percent Adrian Ilie valid 5 x invalid x valid 10 invalid The UNIVERSITY of NORTH CAROLINA at CHAPEL Input 1 Standard input 2 Dialog windows tomorrow 3 File tomorrow 13 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Reading Keyboard Input We can let the user assign values to variables through keyboard input In Java input is accomplished using objects that represent streams of data A stream is an ordered sequence of bytes System in is the standard input stream object by default the keyboard 14 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Reading Keyboard Input The idea is that with System in we have access to a stream of bytes coming from the keyboard In other words we have access to the keys pressed on the keyboard But how do we actually get the values of the keys that are pressed 15 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Reading Keyboard Input The input stream is made up of multiple objects InputStreamReader charReader new InputStreamReader System in BufferedReader keyboard new BufferedReader charReader 1 character at a time Whole line at once OR all in one statement BufferedReader keyboard new BufferedReader new InputStreamReader System in 16 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Reading Keyboard Input The readLine method of the BufferedReader class reads an entire line of input as a String String line keyboard readLine 17 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Example average score Input scores Input weights 18 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL String to Numbers String to int Integer parseInt strExpression Integer parseInt 6723 6723 String to float Float parseFloat strExpression Float parseFloat 345 76 345 76 String to double Double parseDouble strExpression Double parseDouble 1234 56 1234 56 19 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Example average score Convert scores to int Convert weights to double 20 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Output Standard output device is usually the monitor Access the monitor using the standard output object System out Two methods to output a string 1 print 2 println 21 Adrian Ilie Puts the cursor on the next line at the end The UNIVERSITY of NORTH CAROLINA at CHAPEL Examples System out println Hi System out println There Hi There System out print Hi System out println There HiThere int num 5 3 System out println num 8 int num 5 System out println The sum is num 3 The sum is 8 22 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL String Concatenation A string cannot be split between two lines X String greeting How are you doing today Concatenation produces one string where the second string has been appended to the first string String greeting How are you doing today greeting How are you doing today 23 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL String Concatenation Operator can be used to concatenate two strings or a string and a numeric value or character Precedence rules still apply Example String str int num1 12 num2 26 str The sum num1 num2 str The sum 1226 24 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Example average score Output result Formatting numeric strings 25 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Writing a Whole Program Class used to group a set of related operations methods allows users to create their own data types Method set of instructions designed to accomplish a specific task
View Full Document