Review for Midterm 3Chapter 6: Iteration while loop syntaxChapter 6: Iteration for loop syntaxChapter 6: Iteration Common pitfallsChapter 6: Iteration File accessChapter 6: Iteration Scanner methodsChapter 7: Methods and classes Instance methods vs. class methodsChapter 7: Methods and classes Instance variables vs. class variablesChapter 7: Methods and classes ParametersChapter 7: Methods and classes Instance variablesChapter 7: Methods and classes Blocks and scopingChapter 7: Methods and classes OverloadingChapter 7: Methods and classes Constructors and thisChapter 7: Methods and classes Specific methods and instancesChapter 7: Methods and classes equals()Chapter 7: Methods and classes Not on the examToday’s demotivatorsChapter 8: Arrays and collections Array basicsChapter 8: Arrays and collections Array declarationsChapter 8: Arrays and collections Array accessChapter 8: Arrays and collections Array sizeChapter 8: Arrays and collections Array miscellaneousChapter 8: Arrays and collections Sorting and suchChapter 8: Arrays and collections Not on the exam11Review for Midterm 3Review for Midterm 322Chapter 6: IterationChapter 6: Iterationwhile loop syntaxwhile loop syntaxWhile statements:While statements:while ( expression ) actionwhile ( expression ) actionAction is executed repeatedly while expression Action is executed repeatedly while expression is trueis trueOnce expression is false, program execution Once expression is false, program execution moves on to next statementmoves on to next statementAction can be a single statement or a blockAction can be a single statement or a blockIf expression is initially false, action is never If expression is initially false, action is never executedexecutedNote that do-while statements will not be Note that do-while statements will not be on the examon the exam33Chapter 6: IterationChapter 6: Iterationfor loop syntaxfor loop syntaxFor statements:For statements:for ( forinit; forexpression; forupdate ) actionfor ( forinit; forexpression; forupdate ) actionforinit is executed once only (before the loop forinit is executed once only (before the loop starts the first time)starts the first time)Action is executed repeatedly while Action is executed repeatedly while forexpression is trueforexpression is trueAfter action is executed at the end of each loop, After action is executed at the end of each loop, forupdate is executedforupdate is executedOnce forexpression is false, program execution Once forexpression is false, program execution moves on to next statementmoves on to next statementAction can be a single statement or a blockAction can be a single statement or a blockIf expression is initially false, action is never If expression is initially false, action is never executedexecuted44Chapter 6: IterationChapter 6: IterationCommon pitfallsCommon pitfallsInfinite loop: a loop whose text Infinite loop: a loop whose text expression never evaluates to falseexpression never evaluates to falseBe sure that your for loop starts and Be sure that your for loop starts and ends where you want it toends where you want it toFor example, in an array of size For example, in an array of size nn, it , it needs to start at 0 and end at needs to start at 0 and end at nn-1-1Otherwise, it’s called an “off-by-one” errorOtherwise, it’s called an “off-by-one” errorBe sure your loop variable initialization Be sure your loop variable initialization is correctis correct55Chapter 6: IterationChapter 6: IterationFile accessFile accessJava provides the File class for file I/OJava provides the File class for file I/OConstructor takes in the file name as a Constructor takes in the file name as a StringStringA stream is a name for a input or A stream is a name for a input or output methodoutput methodSystem.out: output streamSystem.out: output streamSystem.err: error output streamSystem.err: error output streamSystem.in: input streamSystem.in: input streamFile: file input or output streamFile: file input or output stream66Chapter 6: IterationChapter 6: IterationScanner methodsScanner methodsThe Scanner class can be initialized The Scanner class can be initialized with an File objectwith an File objectScanner filein = new Scanner (new File Scanner filein = new Scanner (new File (filename)); (filename)); The Scanner class has a bunch of The Scanner class has a bunch of methods useful in loops:methods useful in loops:hasNextInt(): tells whether there is a next hasNextInt(): tells whether there is a next intinthasNextDouble(): same idea, but with hasNextDouble(): same idea, but with doublesdoubles77Chapter 7: Methods and classesChapter 7: Methods and classesInstance methods vs. class Instance methods vs. class methodsmethodsInstance (member) methods modify the Instance (member) methods modify the state of the objectstate of the objectThat state can include instance (member) That state can include instance (member) variables as well as class variablesvariables as well as class variablesClass methods do Class methods do notnot modify the state modify the state of the objectof the objectExamples: Math.sin(), Math.cos(), etc.Examples: Math.sin(), Math.cos(), etc.Can only access class variablesCan only access class variablesThey are declared with the keyword staticThey are declared with the keyword static88Chapter 7: Methods and classesChapter 7: Methods and classesInstance variables vs. class Instance variables vs. class variablesvariablesInstance (member) variables are one Instance (member) variables are one per objectper objectCan only be accessed by instance Can only be accessed by instance (member) methods(member) methodsClass variables are one for the Class variables are one for the entireentire classclassThe single class variable is common to all The single class variable is common to all the objects of a classthe objects of a classCan be accessed by both instance Can be accessed by both instance (member) methods and class methods(member) methods and class methods99Chapter 7: Methods and classesChapter 7: Methods and classesParametersParametersThe values passed into the method
View Full Document