Unformatted text preview:

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 syntaxWhile statements:While statements:while ( expression ) actionwhile ( expression ) actionAction is executed repeatedly while expression Action is executed repeatedly while expression is trueis trueOnce expression is false, program execution Once expression is false, program execution moves on to next statementmoves on to next statementAction can be a single statement or a blockAction can be a single statement or a blockIf expression is initially false, action is never If expression is initially false, action is never executedexecutedNote 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 syntaxFor statements:For statements:for ( forinit; forexpression; forupdate ) actionfor ( forinit; forexpression; forupdate ) actionforinit 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 trueAfter action is executed at the end of each loop, After action is executed at the end of each loop, forupdate is executedforupdate is executedOnce forexpression is false, program execution Once forexpression is false, program execution moves on to next statementmoves on to next statementAction can be a single statement or a blockAction can be a single statement or a blockIf expression is initially false, action is never If expression is initially false, action is never executedexecuted44Chapter 6: IterationChapter 6: IterationCommon pitfallsCommon pitfallsInfinite loop: a loop whose text Infinite loop: a loop whose text expression never evaluates to falseexpression never evaluates to falseBe 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 toFor 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-1Otherwise, it’s called an “off-by-one” errorOtherwise, it’s called an “off-by-one” errorBe sure your loop variable initialization Be sure your loop variable initialization is correctis correct55Chapter 6: IterationChapter 6: IterationFile accessFile accessJava provides the File class for file I/OJava provides the File class for file I/OConstructor takes in the file name as a Constructor takes in the file name as a StringStringA stream is a name for a input or A stream is a name for a input or output methodoutput methodSystem.out: output streamSystem.out: output streamSystem.err: error output streamSystem.err: error output streamSystem.in: input streamSystem.in: input streamFile: file input or output streamFile: file input or output stream66Chapter 6: IterationChapter 6: IterationScanner methodsScanner methodsThe Scanner class can be initialized The Scanner class can be initialized with an File objectwith an File objectScanner 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 intinthasNextDouble(): 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 methodsmethodsInstance (member) methods modify the Instance (member) methods modify the state of the objectstate of the objectThat state can include instance (member) That state can include instance (member) variables as well as class variablesvariables as well as class variablesClass methods do Class methods do notnot modify the state modify the state of the objectof the objectExamples: Math.sin(), Math.cos(), etc.Examples: Math.sin(), Math.cos(), etc.Can only access class variablesCan only access class variablesThey 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 variablesvariablesInstance (member) variables are one Instance (member) variables are one per objectper objectCan only be accessed by instance Can only be accessed by instance (member) methods(member) methodsClass variables are one for the Class variables are one for the entireentire classclassThe single class variable is common to all The single class variable is common to all the objects of a classthe objects of a classCan 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 classesParametersParametersThe values passed into the method


View Full Document

UVA CS 101 - Review for Midterm 3

Documents in this Course
Classes

Classes

53 pages

Recursion

Recursion

15 pages

Iteration

Iteration

88 pages

PLEDGED

PLEDGED

6 pages

Objects

Objects

33 pages

PLEDGED

PLEDGED

11 pages

CS 101

CS 101

42 pages

Classes

Classes

83 pages

Iteration

Iteration

92 pages

Classes

Classes

186 pages

Classes

Classes

208 pages

Hardware

Hardware

21 pages

Arrays

Arrays

70 pages

Load more
Download Review for Midterm 3
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 Review for Midterm 3 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 Review for Midterm 3 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?