CS121: Computer Programming I

Size: px
Start display at page:

Download "CS121: Computer Programming I"

Transcription

1 CS121: Computer Programming I A) Practice with Java Control Structures B) Methods Dr Olly Gotel ogotel@pace.edu Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223 Week 4, Slide 1

2 This Week s Agenda (Part A) Lots of practice with conditionals & loops Some loop extras A word on scope & local variables Finishing up with nested loops from week 3 s slides lots of class examples CS121/IS223 Week 4, Slide 2

3 Things to do in-between Classes Code this? * *** ***** *** * Don t forget to do the readings and exercises from the text book these are essential to build on the class work and all outlined in Ex Sheet 1. You should have this all under control before week 5 starts! CS121/IS223 Week 4, Slide 3

4 Changing Between Loops Write the following while loop as a for loop: int count = 1; while (count < 5){ System.out.println( Num = + count); count++; CS121/IS223 Week 4, Slide 4

5 Answer for (int count = 1; count <5; count++) System.out.println( Num = + count); CS121/IS223 Week 4, Slide 5

6 Combining Statements int x = 1; while (x++ < 10){ if (x == 5){ System.out.println( Number is 5 ); In real programs, expect to combine loops & conditionals Beware of ++ try putting this before and after x and see what happens ++x??? x++??? CS121/IS223 Week 4, Slide 6

7 Exercise Write a Java program that accepts student marks from a user (from 0 to 100) until the user enters a bogus value (like 999 or 3): print out the highest mark entered print out the lowest mark entered (use if statements within a while loop) Call your file StudentMarks.java This is called a sentinel value CS121/IS223 Week 4, Slide 7

8 An Answer Uh oh the most common mistake! What is wrong with this? Hint look at the initial values of max & min Also try using a do-while loop import java.util.scanner; Scanner scan = new Scanner(System.in); next = scan.nextint(); next = scan.nextint(); CS121/IS223 Week 4, Slide 8

9 Nesting Write a nested for loop to print the following on the screen: cols = 5 ***** rows = 3 ***** ***** Use the outer loop for rows Use the inner loop for columns Extend this program to take input from user so the user can enter the number columns & number rows they want then make it print any symbol CS121/IS223 Week 4, Slide 9

10 Working With Strings A string in Java is a sequence of characters Sometimes useful to identify separate elements within a string (called tokens): token token token token This is a string delimiter delimiter delimiter Extracting these elements is tokenising the string CS121/IS223 Week 4, Slide 10

11 StringTokenizer Class Class from the Java Standard Class Library to work with string elements (part of java.util package) Use as is for now you can do some interesting loops by processing strings Try this out CS121/IS223 Week 4, Slide 11

12 Example From [Lewis & Loftus 2003] -- Update to use Scanner for reading input Where the methods are kept Uses classes, objects & methods OO again! CS121/IS223 Week 4, Slide 12

13 Want to Know What s Going On? Loop doesn t do what you expect need some diagnostic tools? Tracing variables watching variables change when the program is running helps with debugging Insert some temporary println() statements in your code to print the value of changing variables trace statements Remove the trace statements after debugging CS121/IS223 Week 4, Slide 13

14 Example int count, sum = 0; System.out.println( Sum is + sum); for (count = 1; count < 10; count++){ sum = sum + count; System.out.println( Count is + count); System.out.println( Sum is + sum); System.out.println( Sum = + sum); CS121/IS223 Week 4, Slide 14

15 Loops Use the break Statement Too The break statement can exit a loop from inside a loop body any sort of loop while (true){ x++; if (x > 10){ break; break exits the loop when x is greater than 10 Program execution then continues after the loop CS121/IS223 Week 4, Slide 15

16 Example From [Savitch 2004] ignore the old ways of reading input (superseded by Scanner in Java 5.0) CS121/IS223 Week 4, Slide 16

17 When To Use a break Statement When a condition changes inside a loop & you don t want to carry out the remaining loop statements break statements can make your code hard to read Avoid or use sparingly CS121/IS223 Week 4, Slide 17

18 The continue Statement The continue statement jumps to the next iteration of the loop when x is greater than 10 while (true){ x++ if (x>10){ continue; Avoid or use sparingly CS121/IS223 Week 4, Slide 18

19 Compound Statements A Bit Xtra Can declare variables inside compound statements Called local variables used for temporary/transient matters A local variable is only valid inside its local scope // counter does not exist out here { int counter = 10; // Can use counter in here // Can t use counter here counter is a local variable A compound statement defines local scope CS121/IS223 Week 4, Slide 19

20 Scope & Local Variables while (condition){ int x = 10; // can use x here // can t use x here x is a local variable it has local scope within the enclosing braces (i.e. ONLY exists and can be used inside here { **here** ) CS121/IS223 Week 4, Slide 20

21 Disjoint Scopes while (condition){ int x = 10; // can use x here while (condition){ int x = 10; // can use x here BUT it is a // different x from the x above CS121/IS223 Week 4, Slide 21

22 Lifetime & Visibility Variables have 2 characteristics lifetime & visibility: lifetime when & for how long it exists visibility what parts of the program can access the variable x only exists when the scope is active (i.e. the program is executing inside a compound statement outer braces) x is created anew & initialised every time the compound statement is executed (i.e. every time the loop body is executed) CS121/IS223 Week 4, Slide 22

23 What Happens? (1) for (int i=0;i<10;i++){ System.out.println(i); System.out.println(i); Make sure variable in scope (2) int i=10; for (int i=0; i<10;i++){ System.out.println(i); System.out.println(i); Don t re-declare the same variable (3) int i=20; for (i=0;i<10;i++){ System.out.println(i); System.out.println(i); i in scope outside loop Type in and try out figure out what is going on CS121/IS223 Week 4, Slide 23

24 Key Points (Part A) Conditionals & loops are fundamental to controlling the control of execution through a program: you need to practice & practice with loops you will have LOTS of these in the exam! Variables can have restricted scope - local variables are only temporary, so memory is freed after their use CS121/IS223 Week 4, Slide 24

25 This Week s Agenda (Part B) Methods ONE main method but you can have plenty of others! CS121/IS223 Week 4, Slide 25

26 Writing Java Programs Only written very small programs to date using one class (the name of your program) OO, but not really OO How do your programs work? How do you write really BIG programs? How do you write OO ones? One big class, with lots of statements, loops & selections inside? repeats bits of code jumps all over the place It would just be a mess needs structure CS121/IS223 Week 4, Slide 26

27 How Do You Eat an Elephant? One bite at a time! DON T! What did you do between waking up & getting to university today? CS121/IS223 Week 4, Slide 27

28 How Do You Tie Shoe Laces? Complex - so we refer to it as a whole and blackbox the details CS121/IS223 Week 4, Slide 28

29 Helping With Digestion Which is easier to talk about or refer to: a sandwich, a packet of chips, a cookie, some fruit, a carton of juice? a box lunch? Package stuff up and give it a label to refer to! CS121/IS223 Week 4, Slide 29

30 Organising Code Managing complexity We group programming statements to make the task of programming more comprehensible Statements are generally grouped when, together, they perform some task A name is assigned to such blocks of code (functions, procedures, subroutines, modules, methods) so this name can be referred to in place of all the statements Every programming language has its own way for doing these things CS121/IS223 Week 4, Slide 30

31 Chunky Variable a chunk of data with a name Method a chunk of code with a name The foundations of reuse CS121/IS223 Week 4, Slide 31

32 Packaging Code printsquare: Refer to printsquare() whenever we want to use the sequence of statements (this is abstraction) Write once, debug once, use many times, update in one place In Java, we call this a method & declare it inside a class CS121/IS223 Week 4, Slide 32

33 Methods in Java A method is a named sequence of programming statements To use a method, you call it but you have to state where the method can be found (i.e. which class the code is inside) You do this by calling the method on an object of the class: General - objectname.methodname(); Specific - myobject.printsquare(); Static methods are called on a class no objects needed CS121/IS223 Week 4, Slide 33

34 Clarification Some programming languages have functions or procedures Python has functions for e.g. Java has object methods a bit like a function that an object can perform terry.forwardmarch(); object of class turtle method Variable of a class type names an object (e.g. terry) this code tells Terry to move forward CS121/IS223 Week 4, Slide 34

35 More Clarification This turtle is called Tiffany To get her to move forward: tiffany.forwardmarch(); terry.forwardmarch(); CS121/IS223 Week 4, Slide 35

36 Code Structure Source file (ClassName.java) Method 1 Statement Statement Method 2 Statement Class CS121/IS223 Week 4, Slide 36

37 Classes & Methods public class ExampleClass{ public void methodone(){ Statements public void methodtwo(){ Statements Instance object created - one particular ExampleClass ExampleClass myobject = new ExampleClass(); myobj.methodone(); myobj.methodtwo(); A class can declare a number of methods (responsibilities, services, behaviours) Constructor - a method with same name as class to make an instance object of this class Methods are called on instance objects of the class (dot notation) CS121/IS223 Week 4, Slide 37

38 Classes & Methods - Example public class SquareClass{ Class name Method name public void printsquare(){ for (int i = 0; i < 5; i++) System.out.println( ); Method code in braces Methods are just a nice way of packaging your code now just call the method rather than repeat the code CS121/IS223 Week 4, Slide 38

39 What Do These Mean? public class SquareClass{ public void printsquare(){ for (int i = 0; i < 5; i++) System.out.println( ); public can call the method from anywhere in a program (if object is accessible) a visibility/access modifier void method returns no value back to the place of the method call, just does something handy CS121/IS223 Week 4, Slide 39

40 Written a Method, How Do You Use It? Methods are called on objects but when you start a program running, you have no objects Remember the main method: public static void main(string[] args){ Means method does NOT need to be called on an object main creates object(s) & calls their methods the objects then take over the show Only one main method in a program, even if lots of java files this is where your program starts CS121/IS223 Week 4, Slide 40

41 Hybrid OO - meaning I am making an instance of a class in the class itself better to have a separate DRIVER class that does this but easier for small things for now Using Our Example public class SquareClass{ public void printsquare(){ for (int i = 0; i < 5; i++) System.out.println( ); public static void main(string[] args){ SquareClass myobj = new SquareClass(); myobj.printsquare(); Constructor CS121/IS223 Week 4, Slide 41

42 Classes, Objects & Methods Not the best way Future > class types & driver classes Instance variable - gpa - each student object has own value for gpa (Student.java) public class Student { private int gpa=0; public int getgpa() { return this.gpa; public void setgpa(int newgpa) { this.gpa=newgpa; public static void main(string[] args) { Student s1 = new Student(); System.out.println(s1.getGpa()): Other methods here CS121/IS223 Week 4, Slide 42

43 Program Flow Like being interrupted by a phone call but you carry on doing what you were previously doing when it is over A method call changes the flow of a program The program jumps & control passes to the method (it may be in an external class/file) Control returns to the point it left when the method has finished With conditionals (branches), loops (repeats) & methods (side-tracks) we can get quite interesting program flow CS121/IS223 Week 4, Slide 43

44 Built-in Methods Black-box programming Methods provided by the programming language to carry out routine or complex things: System.out.println( bla ); We know nothing about how built-in methods like println are coded We don t need to know anything, other than: how to use the method (i.e. how to call it & whether to pass it any arguments - we are passing println a string here, bla ) the type of value the method returns println returns nothing - it is void) A sort of protocol CS121/IS223 Week 4, Slide 44

45 How Many Built-in Methods? There are an enormous number of built-in methods to take advantage of (see the API)! However, you are not expected to know about or use all of these you will find things as & when you need them They can save you some work! CS121/IS223 Week 4, Slide 45

46 User-Defined Methods When there is no built-in method to do what you want to do, you have to roll your own It is exactly like a built-in method, but YOU write the code Your method may do one specific thing, but it is often smarter to make it general purpose CS121/IS223 Week 4, Slide 46

47 Example We want to write a program to print out this sign ******************************************************* ******************************************************* Welcome ******************************************************* ******************************************************* ******************************************************* ******************************************************* Example from Dr Courtney CS121/IS223 Week 4, Slide 47

48 A Class & Its Methods - Declaring public class SignClass{ public void printtwolines(){ System.out.println(" *******************************"); System.out.println(" *******************************"); public void printwelcome(){ System.out.println(" Welcome"); public void printfourlines(){ for(int i=1; i <=4; i++) System.out.println("********************************"); i is local / temporary variable with restricted scope Why use methods? CS121/IS223 Week 4, Slide 48

49 Driver Class Making an Object; Calling its Methods public class SignDemo{ public static void main(string[] args){ SignClass welcomesign = new SignClass(); //instantiating an object welcomesign.printtwolines(); //invocation of method in object s class definition welcomesign.printwelcome(); //invocation of method welcomesign.printfourlines(); //invocation of method Why do this? CS121/IS223 Week 4, Slide 49

50 Try it! Go back 2 slides make a SignClass class exactly as shown Go back 1 slide make a SignDemo class exactly as shown 2 java files in the same source folder - compile and run see what happens Then, look at the code changes to these 2 classes in the following slides, make the changes and keep re -running your code CS121/IS223 Week 4, Slide 50

51 Making Methods More General (i) Parameter public void printlines(int number){ for(int i=1; i <=number; i++) System.out.println("*********************"); Create the above method in the SignClass public class SignDemo{ public static void main (String[] args){ SignClass welcomesign = new SignClass(); welcomesign.printlines(2); welcomesign.printwelcome(); welcomesign.printlines(4); Values for parameters to pass into methods should agree in type, quantity, and position CS121/IS223 Week 4, Slide 51

52 Aside This is called the method SIGNATURE or method declaration public void printlines(int number) Method name Parameter - variable name and its type The return type Visibility/access modifier CS121/IS223 Week 4, Slide 52

53 Making Methods More General (ii) public class SignDemo{ public static void main(string[] args){ SignClass forsalesign = new SignClass(); forsalesign.printlines(2); forsalesign.decideonmessage(); forsalesign.printmessage(); forsalesign.printlines(4); Need to declare some instance data for the SignClass, so each actual sign object we make can store its own unique message CS121/IS223 Week 4, Slide 53

54 Making Methods More General (iii) public class SignClass{ private String message= ; public void printlines(int number){ for (int i=1; i<=number; i++) System.out.println(" ******************"); public void printmessage(){ System.out.println(" " + message"); public void decideonmessage(){ System.out.println( What message do you want?"); message = scan.nextline(); Make your Scanner object the usual way CS121/IS223 Week 4, Slide 54

55 Making a General Purpose Method Try the next example too CS121/IS223 Week 4, Slide 55

56 Returning to our Example Hybrid OO public class SquareClass{ public void printsquare(){ for (int i = 0; i < 5; i++) System.out.println( ); public static void main(string[] args){ SquareClass myobj = new SquareClass(); myobj.printsquare(); Constructor Change the method to print a square of ANY size CS121/IS223 Week 4, Slide 56

57 Squares of Any Size? Revisiting a few concepts here Varied behaviour a better abstraction Parameter to method public void printsquare(int side){ for (int row = 0; row < side; row++){ for (int col = 0; col < side; col++){ System.out.print( + ); Scope of side? System.out.println( \n ); To call: myobj.printsquare(8); Change the method to print a rectangle of ANY size Parameter variable New line character Argument to method initialises side (the parameter variable) CS121/IS223 Week 4, Slide 57

58 Rectangles of Any Size? An even better abstraction 2 parameters to method public void printrect(int rows, int cols){ for (int row = 0; row < rows; row++){ for (int col = 0; col < cols; col++){ System.out.print( + ); System.out.println( \n ); 2 arguments to method types MUST match, so order is important, the value of 2 goes into rows and the value of 4 goes into cols To call: myobj.printrect(2, 4); Change the method to print a rectangle of ANY size using any character! CS121/IS223 Week 4, Slide 58

59 Rectangles Using Any Character? Better abstraction yet! 3 parameters to method public void printrect(int rows, int cols, char ch){ for (int row = 0; row < rows; row++){ for (int col = 0; col < cols; col++){ System.out.print(ch); System.out.println( \n ); 3 arguments to method types MUST match, so order is important, this time * goes into the variable ch To call: myobj.printrect(2, 4, * ); CS121/IS223 Week 4, Slide 59

60 Methods That Return Values Does a calculation & returns a value you must do something with this return value! Type of the return value declared public int square(int number){ number = number * number; return number; void methods return no value Parameter is number to square, with its type declared return statement returns a value & ends the method MUST be present if method declaration/signature returns a type, and here it must return an int value To call & use: int x = myobj.square(3); CS121/IS223 Week 4, Slide 60

61 The return Statement in Java Returns a value & ends a method jumps back to where the method was called & inserts its value Methods only return 1 value, but this could be a collection of values (next week s topic)! public int square(int number){ return number * number; statement; This statement is NEVER executed because it is AFTER the return statement Note, constructors have no return type, not even void! CS121/IS223 Week 4, Slide 61

62 What s the Difference? Arguments & parameters: two sides of the same coin -- it is just terminology (often used interchangeably) You specify a method signature using parameters You call a method using arguments (i.e. you give actual values for the parameters) When a method is called, arguments are passed into (substituted for) the parameters Be careful of scope - where do these variables exist? CS121/IS223 Week 4, Slide 62

63 More Terminology Encapsulation taking some code statements, turning them into a method, then using them as a whole: simpler to refer to & remember as a whole can be used many times, in many places (reuse) other people can use your code & you can use other people s (a clear contract of use) Generalisation writing methods that can do more than one particular thing: a method that can find the factorial of any number is more useful than a method that only finds 5! CS121/IS223 Week 4, Slide 63

64 We Want Methods to be Good Tools! Good to make methods as general -purpose & reusable as possible CS121/IS223 Week 4, Slide 64

65 Writing Java Programs Using Methods No longer a long sequential list of statements Instead, loosely coupled methods that call each other Create small cohesive methods that do a single thing: if it does many things, split into multiple methods if it is long, split into multiple methods Parameterise your methods to make them general -purpose CS121/IS223 Week 4, Slide 65

66 Key Points (Part B) Methods in Java: parameters specify the type & number of variables used by a method (can have any number) sometimes called formal parameters arguments are the actual values of these variables & they are passed to the method when it is called sometimes called actual parameters scope determines the lifetime of a variable (often delimited by the braces of compound statements & sometimes nested) local / temporary variables are variables declared in a method, & have restricted scope created when scope entered, destroyed when scope exited (names can be reused if scopes disjoint) if your method specifies a non-void return type, it needs a return statement CS121/IS223 Week 4, Slide 66

67 Before Next Time Reading: if 3 rd -6 th edition read the relevant sections in Chapter 4 on methods PRACTICE: Variables, types, conditionals, loops. Etc. (Not methods!) exercise sheet 1 should be completed! Start working on Part A of exercise sheet 2 Start developing your project! NOTE: This is your FIRST exposure to these concepts and they take time and practice to learn. But you HAVE to try things or you will get lost! We will keep on revisiting things and building on basics CS121/IS223 Week 4, Slide 67

68 Coming Up Next TEST ON CONDITIONALS, LOOPS, ETC (not methods) We will keep on looking at methods We will also look at a new data type when we next meet the array, it is used for storing collections of data. We will look at arrays containing data of primitive types (like int, double, char ) A few loose ends that some of you may find helpful for project work.. I ll show you some sample projects too CS121/IS223 Week 4, Slide 68

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements CS121: Computer Programming I A) Practice with Java Control Structures B) Methods Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours

More information

CS121: Computer Programming I

CS121: Computer Programming I CS121: Computer Programming I Object-Oriented Primer Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming

More information

Brain Re-Wiring Time. CS121: Computer Programming I. How to Tackle a Problem? Agenda. Making Changes - which localises change?

Brain Re-Wiring Time. CS121: Computer Programming I. How to Tackle a Problem? Agenda. Making Changes - which localises change? CS121: Computer Programming I Brain Re-Wiring Time Object-Oriented Primer Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Repe$$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 information

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

This Week s Agenda (Part A) CS121: Computer Programming I. The Games People Play. Data Types & Structures. The Array in Java.

This Week s Agenda (Part A) CS121: Computer Programming I. The Games People Play. Data Types & Structures. The Array in Java. CS121: Computer Programming I A) Collections B) File I/O & Error Handling Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use

More information

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$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 information

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS121/IS223. Object Reference Variables. Dr Olly Gotel CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223

More information

CS111: PROGRAMMING LANGUAGE II

CS111: 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 information

STUDENT LESSON A12 Iterations

STUDENT LESSON A12 Iterations STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of

More information

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x ); Chapter 5 Methods Sections Pages Review Questions Programming Exercises 5.1 5.11 142 166 1 18 2 22 (evens), 30 Method Example 1. This is of a main() method using a another method, f. public class FirstMethod

More information

Java I/O and Control Structures

Java 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 information

Loops. CSE 114, Computer Science 1 Stony Brook University

Loops. 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 information

Java I/O and Control Structures Algorithms in everyday life

Java 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 information

CS111: PROGRAMMING LANGUAGE II

CS111: 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 information

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

CONTENTS: 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 information

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

School 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 information

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I Repetition 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/ Some slides in this

More information

Introduction to Software Development (ISD) Week 3

Introduction to Software Development (ISD) Week 3 Introduction to Software Development (ISD) Week 3 Autumn term 2012 Aims of Week 3 To learn about while, for, and do loops To understand and use nested loops To implement programs that read and process

More information

Chapter 3. Selections

Chapter 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 information

CS 231 Data Structures and Algorithms, Fall 2016

CS 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 information

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Writing a Simple Java Program Intro to Variables Readings Your textbook is Big Java (3rd Ed). This Week s Reading: Ch 2.1-2.5, Ch

More information

CONTENTS: 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 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 information

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website: Repetition 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/ Some slides in this

More information

Repetition CSC 121 Fall 2014 Howard Rosenthal

Repetition CSC 121 Fall 2014 Howard Rosenthal Repetition CSC 121 Fall 2014 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 information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods Encapsulation You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods external - the interaction of the object with other objects in the program

More information

Midterms Save the Dates!

Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu (Using the Scanner and String Classes) Anatomy of a Java Program Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual jump

More information

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I Repetition 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/ Some slides in this

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

Recap: Assignment as an Operator CS 112 Introduction to Programming

Recap: 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 information

Computer Science II (20082) Week 1: Review and Inheritance

Computer Science II (20082) Week 1: Review and Inheritance Computer Science II 4003-232-08 (20082) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Syntax and Semantics of Formal (e.g. Programming) Languages Syntax

More information

Lecture 5: Methods CS2301

Lecture 5: Methods CS2301 Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int

More information

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software. Introduction to Netbeans This document is a brief introduction to writing and compiling a program using the NetBeans Integrated Development Environment (IDE). An IDE is a program that automates and makes

More information

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014 1 COE 212 Engineering Programming Welcome to Exam I Tuesday November 11, 2014 Instructors: Dr. Bachir Habib Dr. George Sakr Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam

More information

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I Repetition 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/ Some slides in this

More information

Chapter 4: Writing Classes

Chapter 4: Writing Classes Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own

More information

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

L 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 information

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm CIS 110 Introduction To Computer Programming February 29, 2012 Midterm Name: Recitation # (e.g. 201): Pennkey (e.g. bjbrown): My signature below certifies that I have complied with the University of Pennsylvania

More information

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Announcements PS 3 is due Thursday, 10/6 Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Room TBD Scope: Lecture 1 to Lecture 9 (Chapters 1 to 6 of text) You may bring a sheet of paper (A4, both sides) Tutoring

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming (Spring 2012) Lecture #7: Variable Scope, Constants, and Loops Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112

More information

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CS 152: Data Structures with Java Hello World with the IntelliJ IDE CS 152: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Electrical and Computer Engineering building

More information

Claremont McKenna College Computer Science

Claremont McKenna College Computer Science Claremont McKenna College Computer Science CS 51 Handout 4: Problem Set 4 February 10, 2011 This problem set is due 11:50pm on Wednesday, February 16. As usual, you may hand in yours until I make my solutions

More information

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.

More information

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited Table of Contents Date(s) Title/Topic Page #s 11/6 Chapter 3 Reflection/Corrections 56 Chapter 4: Writing Classes 4.1 Objects Revisited 57 58-59 look over your Ch 3 Tests and write down comments/ reflections/corrections

More information

4. Java Project Design, Input Methods

4. Java Project Design, Input Methods 4-1 4. Java Project Design, Input Methods Review and Preview You should now be fairly comfortable with creating, compiling and running simple Java projects. In this class, we continue learning new Java

More information

Static Methods. Why use methods?

Static Methods. Why use methods? Static Methods A method is just a collection of code. They are also called functions or procedures. It provides a way to break a larger program up into smaller, reusable chunks. This also has the benefit

More information

Bjarne Stroustrup. creator of C++

Bjarne 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 information

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements COMP-202 Unit 4: Programming With Iterations CONTENTS: The while and for statements Introduction (1) Suppose we want to write a program to be used in cash registers in stores to compute the amount of money

More information

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 - Week 7 1 Static member variables So far: Member variables

More information

Ch. 6. User-Defined Methods

Ch. 6. User-Defined Methods Ch. 6 User-Defined Methods Func5onal Abstrac5on Func5onal regarding func5ons/methods Abstrac5on solving a problem in a crea5ve way Stepwise refinement breaking down large problems into small problems The

More information

Last Class. While loops Infinite loops Loop counters Iterations

Last Class. While loops Infinite loops Loop counters Iterations Last Class While loops Infinite loops Loop counters Iterations public class January31{ public static void main(string[] args) { while (true) { forloops(); if (checkclassunderstands() ) { break; } teacharrays();

More information

QUIZ Friends class Y;

QUIZ Friends class Y; QUIZ Friends class Y; Is a forward declaration neeed here? QUIZ Friends QUIZ Friends - CONCLUSION Forward (a.k.a. incomplete) declarations are needed only when we declare member functions as friends. They

More information

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos. Lecture 05: Methods AITI Nigeria Summer 2012 University of Lagos. Agenda What a method is Why we use methods How to declare a method The four parts of a method How to use (invoke) a method The purpose

More information

CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 CS1004: Intro to CS in Java, Spring 2005 Lecture #23: OO Design, cont d. Janak J Parekh janak@cs.columbia.edu Administrivia HW#5 due Tuesday And if you re cheating on (or letting others see your) HW#5

More information

CS113: Lecture 4. Topics: Functions. Function Activation Records

CS113: Lecture 4. Topics: Functions. Function Activation Records CS113: Lecture 4 Topics: Functions Function Activation Records 1 Why functions? Functions add no expressive power to the C language in a formal sense. Why have them? Breaking tasks into smaller ones make

More information

Object-oriented programming in...

Object-oriented programming in... Programming Languages Week 12 Object-oriented programming in... College of Information Science and Engineering Ritsumeikan University plan this week intro to Java advantages and disadvantages language

More information

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition Chapter 4: Writing Classes Objects An object has: Presentation slides for state - descriptive characteristics Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus,

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by: Menu Driven Systems While loops, menus and the switch statement Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list while loops recap

More information

Selec%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 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 information

Top-Down Program Development

Top-Down Program Development Top-Down Program Development Top-down development is a way of thinking when you try to solve a programming problem It involves starting with the entire problem, and breaking it down into more manageable

More information

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char Review Primitive Data Types & Variables int, long float, double boolean char String Mathematical operators: + - * / % Comparison: < > = == 1 1.3 Conditionals and Loops Introduction to Programming in

More information

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur Lecture 04 Demonstration 1 So, we have learned about how to run Java programs

More information

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Definite 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 information

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Class Encapsulation Anatomy of a Method Writing Classes Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: class definitions instance data encapsulation and

More information

CSE 114 Computer Science I

CSE 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 information

CS 106 Introduction to Computer Science I

CS 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 information

(Refer Slide Time: 1:27)

(Refer Slide Time: 1:27) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 1 Introduction to Data Structures and Algorithms Welcome to data

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

Selection and Repetition Revisited

Selection 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 information

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8 CS 302: INTRODUCTION TO PROGRAMMING Lectures 7&8 Hopefully the Programming Assignment #1 released by tomorrow REVIEW The switch statement is an alternative way of writing what? How do you end a case in

More information

Inheritance and Interfaces

Inheritance and Interfaces Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical

More information

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4) Loops! Flow of Control: Loops (Savitch, Chapter 4) TOPICS while Loops do while Loops for Loops break Statement continue Statement CS 160, Fall Semester 2014 2 An Example while Loop Step- by- step int count

More information

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

1007 Imperative Programming Part II

1007 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 information

Selec%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 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 information

COMP-202 Unit 4: Programming with Iterations

COMP-202 Unit 4: Programming with Iterations 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 information

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments

More information

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15 Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL

More information

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

More information

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Things to Review Review the Class Slides: Key Things to Take Away Do you understand

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 10 Exercise

More information

First Java Program - Output to the Screen

First Java Program - Output to the Screen First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

More information

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file? QUIZ on Ch.5 Why is it sometimes not a good idea to place the private part of the interface in a header file? Example projects where we don t want the implementation visible to the client programmer: The

More information

Algorithms and Conditionals

Algorithms and Conditionals Algorithms and Conditionals 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 information

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Sequence 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 information

Midterms Save the Dates!

Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Instance Variables if Statements Readings This Week s Reading: Review Ch 1-4 (that were previously assigned) (Reminder: Readings

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill CSCI 136 Data Structures & Advanced Programming Lecture 3 Fall 2017 Instructors: Bill & Bill Administrative Details Lab today in TCL 216 (217a is available, too) Lab is due by 11pm Sunday Copy your folder

More information

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class CS0007: Introduction to Computer Programming Review General Form of a switch statement: switch (SwitchExpression) { case CaseExpression1:

More information

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017 Java Methods Lecture 8 COP 3252 Summer 2017 May 23, 2017 Java Methods In Java, the word method refers to the same kind of thing that the word function is used for in other languages. Specifically, a method

More information

Java Bytecode (binary file)

Java Bytecode (binary file) Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-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 information

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

Get 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 information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to define and invoke void and return java methods JAVA

More information

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7) Array Basics: Outline Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and Constants

More information

CS121/IS223. Collections Revisited. Dr Olly Gotel

CS121/IS223. Collections Revisited. Dr Olly Gotel CS121/IS223 Collections Revisited Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223

More information