Review for Exam 1Today’s classChapter 1IntroComputer organizationSoftwareCompilationTerminologyOOP TerminologyProblem solving stepsChapter 2Readable programsIdentifiersComputer bugsJava classesJava methodsProgram executionMisc stuffEscape sequencesPrimitive variable typesSymbolic names vs. literal valuesReferences and variablesMathExpressionsOverflowOperatorsCastingScanner classReferencesStringsString methodsRectangle classChapter 3Logical expressionsLogical operatorsEqualityOrderingIf statementsSwitchesMiscReview for Exam 1Spring 2007CS 101/CS 101-E2Today’s classAn overview of chapters 1-3Stop me if you want me to go over something in more detail!33Chapter 1Chapter 14IntroComputers think in bits (1 or 0)00101001 = 81Eight bits per byte1024 bytes = 1 Kb1024 Kb = 1 Mb1024 Mb = 1 Gb5Computer organizationCPU = brainMicroprocessor: computer on a single chipNetwork: when two or more computers are plugged into one another6SoftwareOS: the program that manages a computer’s resourcesProgram: a sequence of instructions that performs some taskPerforming an instruction is called “executing” an instruction7CompilationTranslator: translates a program from one language to anotherMachine language: the ones and zeros that a computer understandsA low level language!Compiler: a translator which typically translates a high-level language into a low-level oneJava is a high-level languageJava’s compiler translates Java code into bytecodeBytecode is like machine language, but not tied to a specific machineA Java bytecode interpreter is used to execute the bytecodeCalled a Java Virtual Machine (JVM)8TerminologyAbstractionSimilar objects exhibit similar behaviorThe ability to do the same “thing” on many objectsEncapsulationNot revealing how the method does it’s workConsider String.substring()ModularityDividing code into smaller pieces (modules), each one of which is easier to code9OOP TerminologyOOP (Object-Oriented Programming) languages:Abstract things into the class’ methodsEncapsulate code inside the class’ methodsUse additional methods for modularityA (primitive) type is the basic unit of storage in JavaA type is a template for a variableA class is composed of types (or other classes) as well as methodsA class is a template for an objectCreating a variable/object from a type/class is called instantiating the type/class10Problem solving stepsAnalysisWhat needs to be done?DesignHow is it going to be done?ImplementationMake it so!TestingDoes it work correctly?1111Chapter 2Chapter 212Readable programsComments are English textHave a // before them in a Java file/* *//** */Blank lines make a program easier to readIndentation helps humans identify which code is within the methodKeywords have special meanings in JavaExamples: int, double, class, static, public13IdentifiersIdentifiers: programmer-defined namesFor classes, variables, methods, etc.Cannot be a keywordMust start with a letter (or _ or $)Can contain numbers also (but not as the first character)Good identifiers: radius, width, positionBad identifiers: x, y, q, the_really_really_long_variable_name_hi_mom14Computer bugsA bug is an error in the programTo debug is to remove bugs15Java classesThe class keyword is used to start a class declarationCan be made public (for this course, always do so)A class is a “template” for an objectJust as a type is a “template” for a variable16Java methodsAll methods have the following syntax:modifers type name ( parameters ) { statements }Propertiesof themethodTypethat itreturnsA namefor themethodAny number(including zero)of parametersThe body ofthe method(can be empty)public static void main (String[] args) { ... }17Program executionJava starts executing a program at the beginning of the main() methodBraces { } are used to specify where a method begins and endsA statement ends when a semicolon is encounteredA statement can span multiple lines18Misc stufA literal character string is a sequence of characters enclosed by double quotesSystem is the Java class that allows you to access parts of the computer systemSystem.in: access to the keyboardSystem.out: access to the monitorPeriod is used for selection: Math.roundGiven String s, select a method via: s.substring()An exception is when Java “panics”It means something is wrong19Escape sequencesJava provides escape sequences for printing special characters\b backspace\n newline\t tab\r carriage return\\backslash\" double quote\‘ single quote20Primitive variable typesJava has 8 (or so) primitive types:floatdoublebooleancharbyteshortintlongreal numbersinteger numberstwo values: true and falsea single characterAlso the void “type”Can make a variable final21Symbolic names vs. literal valuesWhich is easier to enter:Math.PI3.141592653589793Entering a symbolic name (i.e. a constant) reduces chances of errorsIt allows for changing the constant later onAre usually final22References and variablesA variable is an actual spot in memory that holds a (primitive type) valueA reference is a memory address that points to another spot in memory where the object isVariables defined in a class are initialized to a default valueVariables defined in a method are not initialized to a default value23MathStandard operators: + - * /Note that / can be either integer division or floating-point division% computes the remainder (aka modulus)Can provide numbers in decimal or scientific notation24ExpressionsEvaluating an expression yields a result and a typeExample: 4/3 yields 1 of type intExample: 3.5*2.0 yields 7.0 of type doubleBinary operator has two operandsExample: 3+4, 6*3, etc.Left one is evaluated firstUnary operator has one operandExample: -3, etc.Operators have precedenceFor example, * and / are evaluated before + and -25OverflowConsider:byte b = 100;b = b * 100;A byte can only hold up to +127This is called overflowJava does not tell you that this happened!Underflow: b -= b*100;26OperatorsAssignment: =Increment (++) and decrement
View Full Document