CSE 110 - 4th set of Notes
CSE 110 - 4th set of Notes CSE 110
Popular in Prin of Programming with Java Lecture
Popular in ComputerScienence
This 4 page Class Notes was uploaded by RianMartins on Thursday October 15, 2015. The Class Notes belongs to CSE 110 at Arizona State University taught by Justin Selgrad in Fall 2015. Since its upload, it has received 98 views. For similar materials see Prin of Programming with Java Lecture in ComputerScienence at Arizona State University.
Reviews for CSE 110 - 4th set of Notes
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 10/15/15
CSE 110 PRINCIPLES OF PROGRAMMING IN JAVA WEEKLY NOTES 4 Loops A loop is a programming construct that allows us to repeat code While Loop Pretest while ltexpressiongt statements When you don39t know how many times to repeat but know the stopend condition well we use whileloop The statements are executed so long as expression is true Logical error leads to an infinite loop and crash the program Examples Display a sequence of numbers eg display the numbers from 1 to 100 1 2 3 4 5 int a 0 while a lt 100 a Systemoutprintlna Get the user input repeatedly until it is invalid as long as it is valid eg calculate the average of user inputs Scanner in new Scanner Systemin int sum 0 int count 0 while inhasNextInt sum innextInt count SystemoutprintlnquotAve quot doublesumcount DoWhile Loop Posttest do statements while ltexpressiongt It is similar to whileloop The statements are executed first and then the expression is checked to repeat or stop It is useful when executing at least once Examples Whileloop and doloop are usually used to read a text file until a Reader object hits some keywords or the end of file Chapter 7 Scanner in new Scanner Systemin int age 0 do Systemoutprintln quotGuess my agequot age innextInt whileage 46 For Loop Pretest for ltinitializegt ltexpressiongt ltincrementgt statements ltnitiaizegt A statement to initialize values ltexpressiongt A test expression The statements in the block is executed repeatedly so long as expression is true ltncrementgt An operation to perform after every iteration Most important item in computer programming It is used to repeat the statement when you know the number of repeats or number of elements instead of whiledoloops Examples This program is to print out numbers from 0 to 9 for i O ilt 10 i i 1 Systemoutprintlni Functions i 0 9 Initialization i is set as 0 2 i lt 10 9 Expression check whether i is less than 10 3 Do the statements in the block 4 i i 1 9 Increment i is updated 0 9 192 5 repeat 23 and 4 until 2 is resolved as false Methods public Monster public void damaged public String getName A function is called method in Java A method is also a member of a class Constructors vs Regular Methods Constructor void function to return nothing a function to return something Definition declare make ltfunctionReturnTypegt ltfuctionNamegt ltparameterTypegt ltparameterNamegt void kill Monster x kill the Monster xm int getHealth return this Monster39s HP nvocationcall use ltobjectNamegtltfunctionNamegt ltparameter if anygt Monster ml new Monster Creates a new object of type Monster mlkill orga int ourrentHP II mlgetHealth An accessor method does not change the object on which it operates getter method such as getXXX A mutator method change the values of instance variables on which it operates setter method such as setXXX Void voidtype method returns nothing public void kill Monster a list of statements Return returntype method returns a data of primitive or objecttype It must have a returnstatement inside of the block and it should return a variable of the same type specified in the header of the method public boolean isDead l boolean output false list of statements return output Scope Variables can be declared Inside a method Known as local variables Only available inside this method Parameter variables are like local variables Outside of a method Sometimes called global scope They can be used and changed in all methods of same scope Example public static void mainString args int sum O for int i l i lt 10 i int square i i sum sum square Systemoutprintlnsum sum is a local variable in main square is only visible inside the for loop block i is only visible inside the for loop