Combo Lecture Lecture 14/15. Instructor: Craig Duckett
|
|
- Mavis Johns
- 5 years ago
- Views:
Transcription
1 Combo Lecture Lecture 14/15 Instructor: Craig Duckett
2 Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22 nd Assignment 2 (LECTURE 8) GRADED! Section 1: Wednesday, January 31 st Assignment 1 Revision (LECTURE 10) GRADED! Section 1: Wednesday, February 7 th Assignment 2 Revision (LECTURE 12) GRADED! Section 1: Wednesday, February 14 th Assignment 3 (LECTURE 13) GRADED! Section 1: Wednesday, February 21 st Assignment 3 Revision (LECTURE 16) Section 1: Monday, March 5 th The Fickle Finger of Fate Assignment 4 (LECTURE 18) NO REVISION AVAILABLE! Section 1: Monday, March 12 th 2
3 ANNOUNCEMENTS For various reasons, the Cascadia Faculty web server occasionally goes down, making it impossible to get to my BIT web sites to check files, download assignments, use Student Tracker, etc. As such I have mirrored my BIT web sites on my personal web server at programajama.com. To access courses hosted on programajama.com: To access StudentTracker on homeworkhandin.com: If you find that the Cascadia is temporarily down (like it was this past weekend), then you can me at clduckett@gmail.com until the Cascadia servers are back up and running. 3
4 A Quick Note About: Assignment 4 (BASIC or ADVANCED) I will officially be going over Assignment 4 (Basic or Advanced) on LECTURE 17 There is still way too much that we need to look at before we look at Assignment 4 (especially Arrays) so hold tight for another week. Please Note: Even though we ll be looking at Assignment 4 on LECTURE 17 you re still may need information from LECTURE 18 to be successful at it. If you are itching to get started on Assignment 4 you want to look through all the material one your own through Lecture 17 and Lecture 18. On LECTURE 15 I'll be giving you a Gentle Introduction to Arrays, so make sure to be here for that
5 Lecture 14/15 Topics Assignment Operators for statement (loop) do-while loops Nested loops cascading-ifs switch statements Today Next Lecture CHECK OUT PROBLEM/SOLUTIONS IN LECTURE 15 (WHEN UNLOCKED) REGARDING USE OF INSTANCE VARIABLE FROM ONE CLASS IN ANOTHER CLASS
6 And now... The Quiz
7 Revisit Instance Variables with Examples See: Lecture 15 Solutions (When Made Available) IVTest1.java Problem: Instance Variable not working in Second Class IVTest2.java Solution 1: Change to One Class Way of Doing Things IVtest3.java Solution 2: Assign Instance Variable to Local Variable IVTest4.java Solution 3: Return Instance Variable to Local Variable Public Private - Protected
8 More Assignment Operators int A = 8; int B = 4; int C = 12; int D = 9; = Simple assignment operator. Assigns values from right side operands to left side operand. C = A + B will assign value of A + B into C. 12 = = Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A 20 = = Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand. C -= A is equivalent to C = C A 4 = 12-8 *= Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand. /= Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand. B *= A is equivalent to B = B * A 32 = 4 * 8 C /= B is equivalent to C = C / B 3 = 12 / 4 % Modulus operator. Divides left-hand operand by right-hand operand and returns remainder. D%B is 1. 4 goes into 9 twice with 1 remaining.
9 Modulus Example class Remainder public static void main (String args[]) int i = 10; int j = 3; System.out.println("i is " + i); System.out.println("j is " + j); int k = i % j; System.out.println("i%j is " + k); // Here's the output: i is 10 j is 3 i%j is 1 // The modulus is a good way to determine whether a number is even or odd. X%2 will always return a 0 if X is an even number.
10 A Quick Word About Static No, not these kinds of static
11 public class FileName4 extends Object Class public static void main(string[] args) System.out.println( I printed!"); but no Object! What the hay? The main method is going to run whether it contains an instance (object) of a class or not. It is declared static for just such this reason static means that main does not need an instance (object) of the class that contains it to be created in order for main to run, because main is set up to be its own instance. In this way, main acts like the starter on a car, it doesn t need a starter to start the starter it is the starter. Interesting note: you can compile your Java program without a main method, you just can t run it! (just like you can build a car without a starter, but you can t start it without the starter). See FileName5.java for example. Now, if you are wanting to use other classes (their actions and attributes ) down in main, then you do need to create an instance of those classes ( a named object) that can actually do the something (whatever that something is) that you want done. For instance (pun intended!) when we are using the Becker Robot class, then we need to create a named instance of the Robot class (e.g., a Robot object called Karel or Jo or Mary ) if we want to see any activities and actions done (move, pickthing, putthing, etc). Without an Object calling the actions, these methods only remain unused and potential
12 This will not compile (there is no object to do the action) public class StaticDemo int my_member_variable = 99999; public static void main (String args[]) // Access a non-static member from static method System.out.println ("This generates a compiler error :-( " + my_member_variable); This will compile (there is an object called demo to do the action) public class NonStaticDemo int my_member_variable = 99999; public static void main (String args[]) NonStaticDemo demo = new NonStaticDemo(); // Access member variable of demo System.out.println ("This WON'T generate an error! --> " + demo.my_member_variable );
13 Static Defined: In Java, a static member is a member of a class that is not associated with an instance (object) of a class. Instead, the member belongs to the class itself. As a result, you can access the static member without first creating a class instance (object).
14 Static Method A static method does not need an object to call it. It can call itself! You there? I m here! One of the basic rules of working with static methods is that you can t access a nonstatic method or field from a static method because the static method doesn t have an instance of the class to use to reference instance methods or fields.
15 public class FileName3 extends Object public static int printnum() System.out.println("Going to print, some number of times!"); Class Static Method int howmanyprints = 0; while(howmanyprints < 2) System.out.println("Printing!"); howmanyprints++; // This is a basic counter return howmanyprints; public static void main(string[] args) int num = 0; num = printnum(); // <-- Notice this method call has no object System.out.println( "The method printed " + num + " times!"); Method is called in main and a value is returned and put into num Example: FileName3.java
16 Some Additional Static Examples StaticDemo1.java Does Not Compile StaticDemo2.java Does Compile StaticDemo3.java A Static Method
17 Public (or Instance ) Method A public method does need an object to call it. It can not call itself! Therefore, in order to use a public method down in main you need to create an instance of an object from the class that contains the method. I m an Object! I m a Method! Glad you Called!
18 The for statement (loop) F o r a s l o n g a s t h i s c o n d i t i o n i s t r u e... do s o m e t h i n g.
19 The for statement (loop) The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows: Semi-colon Semi-colon for(initiating statement; conditional statement; next statement) // body statement(s); F o r a s l o n g a s t h i s c o n d i t i o n i s t r u e... do s o m e t h i n g.
20 The for statement (loop) for (initial statement; conditional; next statement // usually incremental statement(s); F o r a s l o n g a s t h i s i s t r u e... do s o m e t h i n g. class ForLoopDemo public static void main(string[] args) for(int count = 1; count < 11; count++) System.out.println("Count is: " + count); The output of this program is: Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6 Count is: 7 Count is: 8 Count is: 9 Count is: 10
21 The for statement (loop) There are three clauses in the for statement. The init-stmt statement is done before the loop is started, usually to initialize an iteration variable ( counter ). After initialization this part of the loop is no longer touched. The condition expression is tested before each time the loop is done. The loop isn't executed if the Boolean expression is false (the same as the while loop). The next-stmt statement is done after the body is executed. It typically increments an iteration variable ( adds 1 to the counter ). for(int count = 1; count < 11; count++) System.out.println("Count is: " + count);
22 The for Loop
23 initialization; while(condition) statement; for(initialization; condition; increment) statement; The for loop is shorter, and combining the intialization, test, and increment in one statement makes it easier to read and verify that it's doing what you expect. The for loop is better when you are counting something. If you are doing something an indefinite number of times, the while loop may be the better choice.
24 while loop class WhileDemo public static void main(string[] args) int count = 1; while (count < 11) System.out.println("Count is: " + count); count++; for loop class ForLoopDemo public static void main(string[] args) for(int count = 1; count < 11; count++) System.out.println("Count is: " + count);
25 while loop class WhileDemo public static void main(string[] args) int count = 1; while (count < 11) System.out.println("Count is: " + count); count++; for loop class ForLoopDemo public static void main(string[] args) for(int count = 1; count < 11; count++) System.out.println("Count is: " + count); SEE: for_while_test.java
26 The Do-While Loop
27 do-while loops The Java programming language also provides a do-while statement, which can be expressed as follows: do statement(s) while (expression); The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once, as shown in the following DoWhileDemo program: class DoWhileDemo public static void main(string[] args) int count = 1; do System.out.println("Count is: " + count); count++; while (count < 11);
28
29 while loop EVALUATES AT THE TOP class WhileDemo public static void main(string[] args) int count = 1; while (count < 11) System.out.println("Count is: " + count); count++; for do-while loop EVALUATES AT THE BOTTOM class DoWhileDemo public static void main(string[] args) int count = 1; do System.out.println("Count is: " + count); count++; while (count < 11); SEE: for_while_do_while _test.java
30 Nested Loops We will look at nested loops today and again next lecture.
31 A Box of Stars and More Let s imagine we want to make a box of stars (asterisks). How might we go about doing that? ******* ******* ******* ******* ******* rows_of_stars_01 rows_of_stars_02 rows_of_stars_03 rows_of_stars_04 rows_of_stars_05 NestWhileTest.java NestForWhileTest.java NestedForsClock.java
32 REFRESHER: The if-else Statement if(boolean_expression) statement 1 //Executes when true else //<-- No Conditional statement 2 //Executes when false public class IfElseTest public static void main(string args[]) int x = 30; if( x < 20 ) System.out.print( The number is less than 20."); else System.out.print( The number is NOT less than 20!");
33 The if-else Statement if(boolean_expression) statement 1 //Executes when true else // <--No Conditional statement 2 //Executes when false Now, this works great if you re only testing two conditions, but what do you do if you have more than two conditions? What if you have three conditions, or four, or five?
34 Several if Statements You could create a whole bunch of if statements to look for and test a particular condition, and this is perfectly acceptable, although this might get unwieldy if you have several conditions, say ten or more to look through. if(boolean_expression_01) statement//executes when true if(boolean_expression_02) statement//executes when true if(boolean_expression_03) statement//executes when true if(boolean_expression_04) statement//executes when true if(boolean_expression_05) statement//executes when true
35 Several if Statements int grade = 98; if(grade >=0 && grade < 60) System.out.println( Sorry. You did not pass. ); if(grade >= 60 && grade < 70) System.out.println( You just squeaked by. ); if(grade >= 70 && grade < 80) System.out.println( You are doing better. ); if(grade >= 80 && grade < 90) System.out.println( Not too bad a job! ); if(grade >= 90 && grade <= 100) System.out.println( You are at the top of your class! );
36 Nested if-elses" It is common to make a series of tests on a value, where the else part contains only another nested if statement. If you use indentation for the else part, it isn't easy to see that these are really a series of tests which are similar. This is traditional syntax, but there s actually a cleaner way to do this in Java. int grade = 68; if(grade >=0 && grade < 60) System.out.println("Sorry. You did not pass."); else if(grade >= 60 && grade < 70) System.out.println("You just squeaked by."); else if(grade >= 70 && grade < 80) System.out.println("You are doing better."); else if(grade >= 80 && grade < 90) System.out.println("Not too bad a job!"); else //(grade >= 90 && grade <= 100) System.out.println("You are at the top of your class!"); And how about all those squiggles!
37 "Cascading-if" if else if else if(boolean_expression 1) //Executes when the Boolean expression 1 is true else if(boolean_expression 2) //Executes when the Boolean expression 2 is true else // <-- No Conditional //Executes when none of the above conditions are true. if(boolean_expression 1) //Executes when the Boolean expression 1 is true else if(boolean_expression 2) //Executes when the Boolean expression 2 is true else if(boolean_expression 3) //Executes when the Boolean expression 3 is true else // <-- No Conditional //Executes when none of the above conditions are true. Text
38 "Cascading-if" if else if else So, contrary to our typical formatting style of indenting to the braces, it is better to write them at the same indentation level by writing the if on the same line as the else. int grade = 68; if(grade >=0 && grade < 60) System.out.println("Sorry. You did not pass."); else if(grade >= 60 && grade < 70) System.out.println("You just squeaked by."); else if(grade >= 70 && grade < 80) System.out.println("You are doing better."); else if(grade >= 80 && grade < 90) System.out.println("Not too bad a job!"); else // <-- No conditional System.out.println("You are at the top of your class!");
39 if (this.frontisblocked()) this.turnaround(); else if (this.canpickthing()) this.turnright(); else if (this.leftisblocked()) this.turnleft(); else this.move();
40 The Switch Statement
41
42 switch statement The switch statement is similar to the cascading-if statement in that both are designed to choose one of several alternatives. The switch statement is more restrictive in its use, however, because it uses a single value to choose the alternative to execute. The cascading-if can use any expressions desired. This restriction is also the switch statement s strength: the coder knows that the decision is based on a single value. switch ( expression ) case value1 : //statement(s) when expression == value1; break; case value2 : //statement(s) when expression == value2; break; case value3 : //statement(s) when expression == value3; break; default : //statement(s) if no previous match;
43
44 switch statement int score = 8; switch (score) case 10: System.out.println ("Excellent."); case 9: System.out.println ( Well above average."); break; case 8: System.out.println ( Above average."); break; case 7: case 6: System.out.print ("Average. You should seek help."); break; default : System.out.println ("Not passing."); Now, this switch written as is will work provided the user enters the correct data and in the correct range, but it will not properly catch improper data or data that is outside the range. What might you do to properly handle values that are outside of the range? Add so if statements to the default perhaps!
45 switch statement int score = 8; switch (score) case 10: System.out.println ("Excellent."); case 9: System.out.println ( Well above average."); break; case 8: System.out.println ( Above average."); break; case 7: case 6: System.out.print ("Average. You should seek help."); break; default : if(score > 11 score < 0) System.out.println ("Score entered is not in range!"); else System.out.println ( Less than 6. Not passing.");
46 break and continue Using break will break you out of a loop or switch, and often stop the program. Sometimes though, you do not want to stop the program, you just want to continue where you left off, or else try going back through the loop again (for example, if the user entered an incorrect value). In that case, you might use continue instead of break, although this might have some unexpected strangeness that goes along with it. Let s have a look Demo Examples: break_continue.java break_continue2.java
47 Switch INSTRUCTOR NOTE: Show Examples SwitchExample.java SwitchExample2.java
48 ICE: Switch
49 Introduction to Arrays What is an Array? So far, you have been working with variables that hold only one value. The integer variables you have set up have held only one number (and later in the quarter we will see how string variables will hold one long string of text). An array is a collection to hold more than one value at a time. It's like a list of items. Think of an array as like the columns in a spreadsheet. You can have a spreadsheet with only one column, or several columns. The data held in a single-list (one-dimensional) array might look like this: grades
50 Introduction to Arrays grades Now, the way we might have declared data like this up until now is to do something along these lines: int value1 = 100; int value2 = 89; int value3 = 96; int value4 = 100; int value5 = 98; However, if we knew before hand that we were going to be declaring six int integers (or ten, or fifteen, etc), we could accomplish the same type of declaration by using an array. To set up an array of numbers like that in the table above, you have to tell Java what type of data is going into the array, then how many positions the array has. You d set it up like this: int[ ] grades; NOTE: Arrays must be of the same data type, i.e., all integers (whole numbers) or all doubles (floating-point numbers) or all strings (text characters) you cannot mix-and-match data types in an array.
51 Introduction to Arrays grades int[ ] grades; The only difference between setting up a normal integer variable and an array is a pair of square brackets after the data type. The square brackets are enough to tell Java that you want to set up an array. The name of the array above is grades. Just like normal variables, you can call them almost anything you like (except Java defined keywords). The square brackets tells Java that you want to set up an integer array. It doesn't say how many positions the array should hold. To do that, you have to set up a new array object: grades = new int[5]; In between the square brackets you need the pre-defined size of the array. The size is how many positions the array should hold. If you prefer, you can put all that on one line: int[ ] grades = new int[5];
52 int[ ] grades = 100, 89, 96, 100, 98 ; // Java treats as a // new instance Introduction to Arrays grades You can also declare the int separately and call it by its given name, like this: int summer2012 = 5; int[ ] grades = new int[summer2012]; (Whether you call the number inside the brackets or a named variable is up to your particular style of coding and preference.) So we are telling Java to set up an array with 5 positions in it. After this line is executed, Java will assign default values for the array. Because we've set up an integer array, the default values for all 5 positions will be zero ( 0 ). To assign values to the various positions in an array, you do it in the normal way: grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; grades [0] = 100; Length of the grades [1] = 89; array is equal to grades [2] = 96; the number of grades [3] = 100; slots declared grades [4] = 98; This is called the index If you know what values are going to be in the array, you can set them up like this instead:
53 Introduction to Arrays grades When you declare an array with a given data type, name and number, like grades = new int[5]; You are reserving a collection space in memory by that name, sized according to data type, and the large enough to separately contain the data for the declared number. array element index space reserved for data value stored in element grades (a named reserved space set aside to hold exactly five 32-bit elements all initializing to zero) bits 32-bits 32-bit 32-bits 32-bits grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98;
54 Introduction to Arrays: Example import java.util.*; public class Array_Demo extends Object public static void main(string[] args) // Setting up the integer 5-element array: int [] grades = new int[5]; grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; // Of course you could have done it this way: // int [] grades = 100, 89, 96, 100, 98; int i; for(i = 0; i < grades.length; i++) System.out.println("Grade " + (i + 1) + " is: " + grades[i]);
Lecture 14. 'for' loops and Arrays
Lecture 14 'for' loops and Arrays For Loops for (initiating statement; conditional statement; next statement) // usually incremental body statement(s); The for statement provides a compact way to iterate
More informationLecture 8 " INPUT " Instructor: Craig Duckett
Lecture 8 " INPUT " Instructor: Craig Duckett Assignments Assignment 2 Due TONIGHT Lecture 8 Assignment 1 Revision due Lecture 10 Assignment 2 Revision Due Lecture 12 We'll Have a closer look at Assignment
More informationLecture 6. Instructor: Craig Duckett
Lecture 6 Instructor: Craig Duckett Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment 1 submissions. If you did not submit an Assignment
More informationLecture 15. Arrays (and For Loops)
Lecture 15 Arrays (and For Loops) For Loops for (initiating statement; conditional statement; next statement) // usually incremental { body statement(s); The for statement provides a compact way to iterate
More informationLecture 10. Instructor: Craig Duckett
Lecture 10 Instructor: Craig Duckett Announcements Assignment 1 Revision DUE TONIGHT in StudentTracker by midnight If you have not yet submitted an Assignment 1, this is your last chance to do so to earn
More informationLecture 13. Instructor: Craig Duckett. Boolean Expressions, Logical Operators, and Return Values
Lecture 13 Instructor: Craig Duckett Boolean Expressions, Logical Operators, and Return Values Assignment 3: The Maze DUE TONIGHT! Uploaded to StudentTracker by midnight If you are submitting as part of
More informationLecture 17. Instructor: Craig Duckett. Passing & Returning Arrays
Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22 nd Assignment 2 (LECTURE 8) GRADED! Section
More informationSequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;
Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The
More informationJava provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:
JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators
More informationBIT 115: Introduction To Programming LECTURE 3. Instructor: Craig Duckett
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu Lecture 3 Announcements By now everyone should be up and running with Java, jgrasp, and the Becker Robots
More informationLecture 17. For Array Class Shenanigans
Lecture 17 For Array Class Shenanigans For or While? class WhileDemo { public static void main(string[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; Note:
More informationLecture 7. Instructor: Craig Duckett OUTPUT
Lecture 7 Instructor: Craig Duckett OUTPUT Lecture 7 Announcements ASSIGNMENT 2 is due LECTURE 8 NEXT LECTURE uploaded to StudentTracker by midnight Assignment 2!!! Assignment Dates (By Due Date) Assignment
More informationThe Java language has a wide variety of modifiers, including the following:
PART 5 5. Modifier Types The Java language has a wide variety of modifiers, including the following: Java Access Modifiers Non Access Modifiers 5.1 Access Control Modifiers Java provides a number of access
More informationAdministration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP
Administration Conditional Statements CS 99 Summer 2000 Michael Clarkson Lecture 4 Lab 2 due now on floppy Lab 3 due tomorrow via FTP need Instruct account password Lab 4 posted this afternoon Prelim 1
More informationCS 177 Week 5 Recitation Slides. Loops
CS 177 Week 5 Recitation Slides Loops 1 Announcements Project 1 due tonight at 9PM because of evac. Project 2 is posted and is due on Oct. 8th 9pm Exam 1 Wednesday Sept 30 6:30 7:20 PM Please see class
More informationLecture 11. Instructor: Craig Duckett. Instance Variables
Lecture 11 Instructor: Craig Duckett Instance Variables Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22 nd Assignment 2 (LECTURE 8) GRADED! Section 1: Wednesday,
More informationInstructor: Craig Duckett
Instructor: Craig Duckett cduckett@cascadia.edu Announcements Assignment 1 Due Lecture 5 by MIDNIGHT I will double dog dare try to have Assignment 1 graded and back to you by Wednesday, January 24 th hopefully
More informationLoops. CSE 114, Computer Science 1 Stony Brook University
Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?
More informationFollowing is the general form of a typical decision making structure found in most of the programming languages:
Decision Making Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined
More informationAccelerating Information Technology Innovation
Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lección 03 Control Structures Agenda 1. Block Statements 2. Decision Statements 3. Loops 2 What are Control
More informationBuilding Java Programs Chapter 2
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. Data types type: A category or set of data values. Constrains the operations that can
More informationDefinite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting
Unit 2, Part 2 Definite Loops Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting Let's say that we're using a variable i to count the number of times that
More informationBuilding Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.
Building Java Programs Chapter 2 bug Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. 2 An Insect Software Flaw 3 4 Bug, Kentucky Bug Eyed 5 Cheesy Movie 6 Punch Buggy
More informationBuilding Java Programs Chapter 2
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. bug 2 An Insect 3 Software Flaw 4 Bug, Kentucky 5 Bug Eyed 6 Cheesy Movie 7 Punch Buggy
More informationControl Structures in Java if-else and switch
Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification
More informationRepe$$on CSC 121 Spring 2017 Howard Rosenthal
Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when
More informationBasic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java
Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection
More informationBjarne Stroustrup. creator of C++
We Continue GEEN163 I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone. Bjarne Stroustrup creator
More informationControl Flow Statements
Control Flow Statements The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution
More informationL o o p s. for(initializing expression; control expression; step expression) { one or more statements }
L o o p s Objective #1: Explain the importance of loops in programs. In order to write a non trivial computer program, you almost always need to use one or more loops. Loops allow your program to repeat
More informationCOMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015 Announcements Check the calendar on the course webpage regularly for updates on tutorials and office hours.
More informationBuilding Java Programs
Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 2 Data types type: A category or set of data
More informationControl Structures: if and while A C S L E C T U R E 4
Control Structures: if and while A C S - 1903 L E C T U R E 4 Control structures 3 constructs are essential building blocks for programs Sequences compound statement Decisions if, switch, conditional operator
More informationLecture 12. Instructor: Craig Duckett
Lecture 12 Instructor: Craig Duckett Assignment 2 Revision DUE TONIGHT! Uploaded to StudentTracker by midnight YOUR LAST CHANCE TO EARN POINTS! Assignment 3 Maze DUE Lecture 13 Uploaded to StudentTracker
More informationRepe$$on CSC 121 Fall 2015 Howard Rosenthal
Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while
More informationECE 122. Engineering Problem Solving with Java
ECE 122 Engineering Problem Solving with Java Lecture 10 For Loops and Arrays Outline Problem: How can I perform the same operations a fixed number of times? Considering for loops Performs same operations
More informationScope of this lecture. Repetition For loops While loops
REPETITION CITS1001 2 Scope of this lecture Repetition For loops While loops Repetition Computers are good at repetition We have already seen the for each loop The for loop is a more general loop form
More informationJava I/O and Control Structures
Java I/O and Control Structures CSC 2014 Java Bootcamp Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Some slides in this presentation are adapted from the slides accompanying
More informationBuilding Java Programs
Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 Copyright 2009 by Pearson Education Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 Copyright
More informationPrimitive Data, Variables, and Expressions; Simple Conditional Execution
Unit 2, Part 1 Primitive Data, Variables, and Expressions; Simple Conditional Execution Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Overview of the Programming Process Analysis/Specification
More informationCourse Outline. Introduction to java
Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.
More informationProgram Development. Java Program Statements. Design. Requirements. Testing. Implementation
Program Development Java Program Statements Selim Aksoy Bilkent University Department of Computer Engineering saksoy@cs.bilkent.edu.tr The creation of software involves four basic activities: establishing
More informationJava Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.
Loop Control There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first,
More information4 Programming Fundamentals. Introduction to Programming 1 1
4 Programming Fundamentals Introduction to Programming 1 1 Objectives At the end of the lesson, the student should be able to: Identify the basic parts of a Java program Differentiate among Java literals,
More informationSchool of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn
CPS109 Course Notes 6 Alexander Ferworn Unrelated Facts Worth Remembering Use metaphors to understand issues and explain them to others. Look up what metaphor means. Table of Contents Contents 1 ITERATION...
More informationCOSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand
COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site I have decided to keep this site for the whole semester I still hope to have blackboard up and running, but you
More informationIntroduction to Java & Fundamental Data Types
Introduction to Java & Fundamental Data Types LECTURER: ATHENA TOUMBOURI How to Create a New Java Project in Eclipse Eclipse is one of the most popular development environments for Java, as it contains
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationTopic 4 Expressions and variables
Topic 4 Expressions and variables "Once a person has understood the way variables are used in programming, he has understood the quintessence of programming." -Professor Edsger W. Dijkstra Based on slides
More informationBuilding Java Programs
Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 Variables reading: 2.2 self-check: 1-15 exercises: 1-4 videos: Ch. 2 #2 2 Receipt example What's bad about the
More informationJava I/O and Control Structures Algorithms in everyday life
Introduction Java I/O and Control Structures Algorithms in everyday life CSC 2014 Java Bootcamp Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Source: http://xkcd.com/627/
More informationCONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction
CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics COMP-202 Unit 1: Introduction Announcements Did you miss the first lecture? Come talk to me after class. If you want
More informationPROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS Q1. Name any two Object Oriented Programming languages? Q2. Why is java called a platform independent language? Q3. Elaborate the java Compilation process. Q4. Why do we write
More informationBasics of Java Programming
Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement
More informationExercise 4: Loops, Arrays and Files
Exercise 4: Loops, Arrays and Files worth 24% of the final mark November 4, 2004 Instructions Submit your programs in a floppy disk. Deliver the disk to Michele Zito at the 12noon lecture on Tuesday November
More informationCMPT 125: Lecture 4 Conditionals and Loops
CMPT 125: Lecture 4 Conditionals and Loops Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 17, 2009 1 Flow of Control The order in which statements are executed
More informationCS 106 Introduction to Computer Science I
CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)
More informationComputer Science is...
Computer Science is... Automated Software Verification Using mathematical logic, computer scientists try to design tools to automatically detect runtime and logical errors in huge, complex programs. Right:
More informationCOSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand
COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236
More informationBuilding Java Programs
Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 2 Data and expressions reading: 2.1 3 The computer s view Internally, computers store everything as 1 s and 0
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationKey Points. COSC 123 Computer Creativity. Java Decisions and Loops. Making Decisions Performing Comparisons. Making Decisions
COSC 123 Computer Creativity Java Decisions and Loops Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) A decision is made by evaluating a condition in an if/
More informationCONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops
COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables
More informationSelection and Repetition Revisited
Selection and Repetition Revisited CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/
More informationControl Structures in Java if-else and switch
Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2016 February 2, 2016 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions
More informationCS111: PROGRAMMING LANGUAGE II
CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A
More informationCS111: PROGRAMMING LANGUAGE II
1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables
More informationChapter 3. Selections
Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of
More informationBuilding Java Programs. Chapter 2: Primitive Data and Definite Loops
Building Java Programs Chapter 2: Primitive Data and Definite Loops Copyright 2008 2006 by Pearson Education 1 Lecture outline data concepts Primitive types: int, double, char (for now) Expressions: operators,
More informationAP COMPUTER SCIENCE A
AP COMPUTER SCIENCE A CONTROL FLOW Aug 28 2017 Week 2 http://apcs.cold.rocks 1 More operators! not!= not equals to % remainder! Goes ahead of boolean!= is used just like == % is used just like / http://apcs.cold.rocks
More informationint j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.
11-1 One of the most important structures in Java is the -loop. A loop is basically a block of code that is with certain rules about how to start and how to end the process. Suppose we want to sum up all
More informationCOMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016
COMP-202: Foundations of Programming Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016 Review What is the difference between a while loop and an if statement? What is an off-by-one
More informationCOSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan
COSC 123 Computer Creativity Java Decisions and Loops Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) A decision is made by evaluating a condition in an if/else
More informationCOSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand
COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236
More informationCSE 114 Computer Science I
CSE 114 Computer Science I Iteration Cape Breton, Nova Scotia What is Iteration? Repeating a set of instructions a specified number of times or until a specific result is achieved How do we repeat steps?
More informationIntro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming
Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals
More informationCSE 113 A. Announcements - Lab
CSE 113 A February 21-25, 2011 Announcements - Lab Lab 1, 2, 3, 4; Practice Assignment 1, 2, 3, 4 grades are available in Web-CAT look under Results -> Past Results and if looking for Lab 1, make sure
More informationCSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:
CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Tuesday 10:00 AM 12:00 PM * Wednesday 4:00 PM 5:00 PM Friday 11:00 AM 12:00 PM OR
More informationCS112 Lecture: Repetition Statements
CS112 Lecture: Repetition Statements Objectives: Last revised 2/18/05 1. To explain the general form of the java while loop 2. To introduce and motivate the java do.. while loop 3. To explain the general
More informationCS112 Lecture: Working with Numbers
CS112 Lecture: Working with Numbers Last revised January 30, 2008 Objectives: 1. To introduce arithmetic operators and expressions 2. To expand on accessor methods 3. To expand on variables, declarations
More informationGet JAVA. I will just tell you what I did (on January 10, 2017). I went to:
Get JAVA To compile programs you need the JDK (Java Development Kit). To RUN programs you need the JRE (Java Runtime Environment). This download will get BOTH of them, so that you will be able to both
More informationCOMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015 Announcements Slides will be posted before the class. There might be few
More informationCompSci 125 Lecture 11
CompSci 125 Lecture 11 switch case The? conditional operator do while for Announcements hw5 Due 10/4 p2 Due 10/5 switch case! The switch case Statement Consider a simple four-function calculator 16 buttons:
More informationCS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG 1 Notice Reading Assignment Chapter 1: Introduction to Java Programming Homework 1 It is due this coming Sunday
More informationTOPIC 2 INTRODUCTION TO JAVA AND DR JAVA
1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared
More informationRecap: Assignment as an Operator CS 112 Introduction to Programming
Recap: Assignment as an Operator CS 112 Introduction to Programming q You can consider assignment as an operator, with a (Spring 2012) lower precedence than the arithmetic operators First the expression
More informationLesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014
Lesson 6A Loops By John B. Owen All rights reserved 2011, revised 2014 Topic List Objectives Loop structure 4 parts Three loop styles Example of a while loop Example of a do while loop Comparison while
More informationcis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1
topics: introduction to java, part 1 cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 cis20.1-fall2007-sklar-leci.2 1 Java. Java is an object-oriented language: it is
More informationRobots. Byron Weber Becker. chapter 6
Using Variables Robots Learning to Program with Java Byron Weber Becker chapter 6 Announcements (Oct 3) Reminder: Reading for today y( (Oct 3 rd ) Ch 6.1-6.4 Reading for Wednesday (Oct 3 rd ) The rest
More informationComputer Science is...
Computer Science is... Machine Learning Machine learning is the study of computer algorithms that improve automatically through experience. Example: develop adaptive strategies for the control of epileptic
More informationCS112 Lecture: Loops
CS112 Lecture: Loops Objectives: Last revised 3/11/08 1. To introduce some while loop patterns 2. To introduce and motivate the java do.. while loop 3. To review the general form of the java for loop.
More information1007 Imperative Programming Part II
Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be
More informationConditionals, Loops, and Style
Conditionals, Loops, and Style yes x > y? no max = x; max = y; http://xkcd.com/292/ Fundamentals of Computer Science Keith Vertanen Copyright 2013 Control flow thus far public class ArgsExample public
More informationCS 231 Data Structures and Algorithms, Fall 2016
CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard
More informationReview Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:
More informationprintf( Please enter another number: ); scanf( %d, &num2);
CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful
More informationCONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays
CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types COMP-202 Unit 6: Arrays Introduction (1) Suppose you want to write a program that asks the user to enter the numeric final grades of 350 COMP-202
More informationObject-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018
Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative
More informationOutline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.
OOP in Java 1 Outline 1. Getting started, primitive data types and control structures 2. Classes and objects 3. Extending classes 4. Using some standard packages 5. OOP revisited Parts 1 to 3 introduce
More informationCS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals
CS125 : Introduction to Computer Science Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals c 2005, 2004, 2003, 2002, 2001, 2000 Jason Zych 1 Lecture 6 : Compound Statements, Scope,
More information