Unformatted text preview:

COMP 14 Introduction to ProgrammingParts of the ComputerQuestions - HardwareQuestions - SoftwareQuestions - BinaryQuestions: Java BasicsQuestions – IdentifiersQuestions - ExpressionsQuestions - AssigningQuestions – Valid StatementsQuestions – Variables and ObjectsDecimal Format ExamplesComparing CharactersComparing Floating-PointComparing StringsBoolean OperatorsQuestionsSlide 18Slide 19Slide 20Selection StatementsLoopsObject-Oriented Design Simplified MethodologyThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian IlieCOMP 14Introduction to ProgrammingAdrian IlieJuly 11, 2005The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie2________- computer components including the CPU, main memory, I/O devices, and secondary storage________ - the brain of the computer, containing the CU, PC, IR, ALU, and ACC________ - computer instructions to solve a problemThe digits 0 and 1 are called _________or the shortened term ________Parts of the ComputerhardwareCPUprogrambinary digitsbitsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie3Questions - Hardware ____________- points to the next instruction to be executed ____- a unique location in memory ____________- stores information permanentlyInstructions executed by the CPU must be first loaded to ________program counter (PC)addresssecondary storagemain memoryThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie4Questions - SoftwareClassify the following pieces of software as operating system or application:1. Microsoft Windows 20002. Microsoft PowerPoint3. Linux4. Your COMP 14 programsOSOSappappThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie5Questions - Binary•What’s the maximum value a 6-bit number can represent?•What’s the decimal representation of 111010?•What’s the binary representation of 35?26-1=6325*1+24*1+23*1+22*0+21*1+20*0=583510=1000112The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie6Questions: Java Basics1. The ________ rules of a language determine which instructions are valid.2. True or False? Hello! is an example of a legal Java identifier. _______3. If an operator has an integer and a floating-point operand, the result of the operation is a ___________ number.4. The expression (int) (9.2) evaluates to ___.syntaxFalsefloating-point9The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie7Questions – IdentifiersClassify the following as legal or illegal identifiers:1. My First Program ____2. my1stProgram ____3. 1stProgram ____4. $money ____5. an_identifier ____6. Jane'sProgram ____illegallegalillegallegallegalillegalThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie8Questions - Expressions 1. (5 + 4) % 62. (5 + 6) % 3.53. (double) (13) / 24. (double) (13 / 2)9 % 6311 % 3.5not possible(double) (6)6.013.0 / 26.5The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie9Questions - AssigningWhat 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 = 5; ______num = 5 + 4 - 2; ______num = num * 3; ______num = 3.4 + 5; ______unknown5721errorwould give an error since 3.4 + 5 wouldnot result in an intThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie10Questions – Valid StatementsWhich of the following are valid Java assignment statements? Assume that i, x, and percent have been declared as double variables and properly initialized.1. i = i + 5; ______2. x + 2 = x; ______3. x = 2.5 * x; ______4. percent = 10%; ______validinvalidvalidinvalidThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie11Questions – Variables and Objects1. True or False. A primitive variable is a variable that stores the address of a memory space. ______2. The operator ____ is used to create a class object.3. In Java, the ________ operator is used to access 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. ______newdot (.)FalseTrueThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie12Decimal Format ExamplesDecimalFormat twoDecimal = new DecimalFormat("0.00");DecimalFormat fmt = new DecimalFormat("0.##");System.out.println (twoDecimal.format(56.379)); _______System.out.println (fmt.format(56.379)); _______System.out.println (twoDecimal.format(.3451)); _______System.out.println (fmt.format(.3451)); _______System.out.println (twoDecimal.format(.3)); _______System.out.println (fmt.format(.3)); _______56.380.350.3056.380.350.3The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie13Comparing Characters•'a' > 'A'•'6' < 7•' ' <= 's'97 > 65 true54 < 7 false32 <= 115 trueThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie14Comparing Floating-Point3.0 / 7.02.0 / 7.0(3.0 / 7.0) + (2.0 / 7.0) + (2.0 / 7.0) == 10.4285714285714285false0.2857142857142857If we did this math with exact arithmetic, this expression would be true0.9999999999999999The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie15Comparing StringsGiven String str = "Homer";str.compareTo("Marge") str.equals("homer")str.compareTo("Bart")negative valuefalsepositive valueThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie16Boolean Operators•NOT ! (unary)!(2+2==5)•AND && (binary)(2+2==5) && (1+1==2)•OR || (binary)(2+2==5) || (1+1==2)truefalsetrueThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie17Questions1. What type of primitive variable can the result of a logical expression be stored in? _______2. Under what conditions would the expression ((ch >= 'A') && (ch <= 'Z')) evaluate to false? ______________________3. What method do you use to compare two Strings? __________4. Why is 'a' greater than 'A'? ______________________________________booleanch < 'A' or ch > 'Z'compareTo'a' comes after 'A' in the ASCII character tableThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie18QuestionsGiven 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)2. ((x > 5) || !(sum == 55))3. !((y > 1.0) && (x < 4))4. ((count != 4) || (sum > 100) && wrong)falsetruetruetrue(false || !(false))!(true && false)(true || false && true)(true && false)(true || false)The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie191. What is printed if movieRating is 1 and movieName is "Gigli"?2. What is printed if movieRating is 5?QuestionsAssume movieRating is an int and movieName is a


View Full Document

UNC-Chapel Hill COMP 14 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?