COMP 14 Introduction to Programming Adrian Ilie July 11 2005 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Parts of the Computer hardware computer components including the CPU main memory I O devices and secondary storage CPU the brain of the computer containing the CU PC IR ALU and ACC program computer instructions to solve a problem binary digits The digits 0 and 1 are called bits or the shortened term 2 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions Hardware program counter points PC to the next instruction to be executed a unique location in memory address stores information secondary storage permanently Instructions executed by the CPU must be first loaded tomain memory 3 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions Software Classify the following pieces of software as operating system or application 1 2 3 4 4 Microsoft Windows 2000 OS app Microsoft PowerPoint Linux OS Your COMP 14 programs app Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions Binary What s the maximum value a 6bit number can represent 26 1 63 What s the decimal representation of 111010 25 1 24 1 23 1 22 0 21 1 20 0 5 What s the binary representation 3510 1000112 of 35 5 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions Java Basics syntax rules of a language 1 The determine which instructions are valid 2 True or False Hello is an example False of a legal Java identifier 3 If an operator has an integer and a floating point operand the result of the operation isfloating point a number 4 The expression int 9 2 evaluates to 9 6 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions Identifiers 1 2 3 4 5 6 7 Classify the following as legal or illegal identifiers My First Program illegal legal my1stProgram 1stProgram illegal legal money an identifier legal illegal Jane sProgram Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions Expressions 9 6 1 5 4 6 3 11 3 5 2 5 6 3 5not possible 13 0 2 3 double 13 2 6 5 4 double 13 2 double 6 6 0 8 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions Assigning 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 5 5 4 2 num 3 3 4 5 unknown 5 7 21 error would give an error since 3 4 5 would not result in an int 9 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions Valid Statements 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 10 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 Questions Variables and Objects 1 True or False A primitive variable is a variable that stores the address of a memory space False 2 The operator new is used to create a class object 3 In Java the operator is used to access dot members of a class It separates the class or object name from the method name 4 True or False Class objects are instances of that class True 11 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Decimal Format Examples DecimalFormat twoDecimal new DecimalFormat 0 00 DecimalFormat fmt new DecimalFormat 0 56 38 System out println twoDecimal format 56 379 System out println fmt format 56 379 56 38 12 System out println twoDecimal format 3451 System out println fmt format 3451 0 35 0 35 System out println twoDecimal format 3 System out println fmt format 3 0 30 0 3 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Comparing Characters 13 a A 97 65 true 6 7 54 7 false s 32 115 true Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Comparing FloatingPoint 3 0 7 00 4285714285714285 2 0 7 00 2857142857142857 3 0 7 0 2 0 7 0 2 0 7 0 1 0 9999999999999999 false If we did this math with exact arithmetic this expression would be 14 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Comparing Strings Given String str Homer str compareTo Marge negative value str equals homer str compareTo Bart 15 Adrian Ilie false positive value The UNIVERSITY of NORTH CAROLINA at CHAPEL Boolean Operators NOT unary true binary false 2 2 5 AND 2 2 5 1 1 2 OR binary 2 2 5 1 1 2 16 Adrian Ilie true The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions 1 What type of primitive variable can the result of a logical expression be stored in boolean 2 Under what conditions would the expression ch A ch Z evaluate to false ch A or ch Z 3 What method do you use to compare two Strings compareTo 4 Why is a greater than A a comes after A in the ASCII character table 17 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions Given the following declarations evaluate each Boolean expression int count 0 sum 54 double x 4 3 y 1 2 boolean wrong true 1 wrong sum 60 true false false true 2 x 5 sum 55 false false 3 y true 1 0 false x 4 true 4 count 4 false sum 100 true wrong true true false 18 Adrian Ilie true The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions Assume movieRating is an int and movieName is a String switch movieRating case 1 System out println Run away if movieName equals Gigli System out println Quickly case 2 System out println Save your money break case 3 System out println It s OK break 1 What is printed if movieRating is 1 and movieName is Gigli Run away Quickly Save your money 2 What is printed if movieRating is 5 19 Adrian Ilie Nothing The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions if x 0 y 0 z x y System out println z z System out print The sum is if x y 0 System out println negative else System out println positive What is printed with the following values of x and y 1 x 5 y 4 z 1 The sum is positive The sum is negative 2 x 9 y 5 The sum is positive 3 x 5 y 5 20 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Selection Statements What type of selection statement should be used 1 Print Male if gender is M and Female if gender is F if else 2 Print the state associated with areaCode where areaCode switch is an int 3 Print the maximum of three integers nested if else 21 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Loops final int MAX 20 int i for i 0 i MAX i 3 System out println i 22 Translate this to a while loop Adrian Ilie final int MAX 20 …
View Full Document