COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Size: px
Start display at page:

Download "COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand"

Transcription

1 COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

2 COSC 236 Web Site You will always find the course material at: From this site you can click on the COSC-236 tab to download the PowerPoint lectures, the Quiz solutions and the Laboratory assignments. 2

3 Lecture 05 2/10/2014 3

4 Review of Quiz 5 The following is the class Quiz5, the program that calls the five methods that solve the five problems: 4

5 Review of Quiz 5 Problem 1 Write a Java method problem1 that prints out on the console (8 characters per line each separated by a space) the Unicode special characters for HEX 20 through HEX 47. Method problem1 should initialize the char variable x to a space (i.e.: char x = ) and then print five lines to the console containing the special characters (Hint: use the ++ operator no loops are allowed!). The point of this problem was to demonstrate use of the x++ in a print statement and the location of the Unicode characters. 5

6 Review of Quiz 5 Problem 1 OUTPUT: The point of this problem was to demonstrate use of the x++ in a print statement and the location of the Unicode characters. 6

7 Review of Quiz 5 Problem 2 The mod function, %, for integers is fairly easy to understand. It is the remainder when the numerator n is divided by the denominator d. As noted in class, the general form of the mod function, %, is: n%d = (n/d (int)(n/d))*d. In a single println statement, calculate 128 % 33 as integers and compare it to the definition (128.0/33-128/33)*33. In a single println statement, calculate 12.8 % 3.3 and compare it to the definition. In a single println statement, calculate 11.4 % 3 and compare it to the definition. NOTE: The first two lines of your code should print out as follows: n = 128, d = 33, n % d = 29( ) n = 12.8, d = 3.3, n % d = ( ) Why is there a difference between n % d and the definition? The point of this problem was to demonstrate use of the mod function % and the problem with exact numbers in type double. 7

8 Review of Quiz 5 Problem 2 Solution: The simplest solution is to use values, not variables, in the arithmetic operations directly in the print statement. This way you do not need to define any variables or do anything complicated. The point of this problem was to demonstrate use of the mod function % and the problem with exact numbers in type double. 8

9 Review of Quiz 5 Problem 2 Solution: The point of this problem was to demonstrate use of the mod function % and the problem with exact numbers in type double. 9

10 Review of Quiz 5 Problem 3 The point of this problem was to demonstrate use of the pre-increment (++x) and post increment (x++). 10

11 Review of Quiz 5 Problem 4 Write a single println statement that prints the following line (no loops allowed): 3, 9, 27, 81, 243 The point of this problem was to demonstrate use of the *= operator. 11

12 Review of Quiz 5 Problem 5 Set an integer x equal to (int x = 12345;) and then use a single println statement to print each digit out in order with a space in between them ( ). The point of this problem was to demonstrate use of the integer divide by powers of 10 combined with the mod 10 (% 10) operators to isolate individual digits from a decimal integer. 12

13 Review of Loops First standard technique to design a for loop that executes ntimes: for (int i = 1; i <= ntimes; i++) { statements here execute ntimes; { Second standard technique to design a for loop that executes ntimes: for (int i = 0; i < ntimes; i++) { statements here execute ntimes; { 13

14 Review of Loops First nested for loop that prints nlines and ncolumns : for (int i = 1; i <= nlines; i++) { for (int j = 1; j <= ncolumns; j++) { System.out.print("*"); System.out.println(); *********** In this example, *********** *********** nlines = 3 and ncolumns = 10 14

15 Review of Loops Second nested for loop that prints nlines and ncolumns : for (int i = 0; i < nlines; i++) { for (int j = 0; j < ncolumns; j++) { System.out.print("*"); System.out.println(); *********** In this example, *********** *********** nlines = 3 and ncolumns = 10 15

16 Review of Loops First nested for loop that prints nlines triangle: for (int i = 1; i <= nlines; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); System.out.println(); * In this example, ** *** nlines = 3 16

17 Review of Loops Second nested for loop that prints nlines triangle: for (int i = 0; i < nlines; i++) { for (int j = 0; j <= i; j++) { System.out.print("*"); System.out.println(); * In this example, ** *** nlines = 3 17

18 Review of Loops First nested for loop that prints nlines triangle * extended to square using!: for (int i = 1; i <= nlines; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); for (int j = i+1; j <= nlines; j++) { System.out.print("!"); System.out.println(); *!! In this example, **! *** nlines = 3 18

19 Review of Loops Second nested for loop that prints nlines triangle * extended to square using!: for (int i = 0; i < nlines; i++) { for (int j = 0; j <= i; j++) { System.out.print("*"); for (int j = i+1; j < nlines; j++) { System.out.print("!"); System.out.println(); *!! In this example, **! *** nlines = 3 19

20 Review of Loops We can double the width of any of these figures by printing two character: for (int i = 0; i < nlines; i++) { for (int j = 0; j <= i; j++) { System.out.print("**"); for (int j = i+1; j < nlines; j++) { System.out.print("!!"); System.out.println(); **!!!! In this example, ****!! ****** nlines = 3 20

21 2.4 Managing Complexity (pp ) Drawing complex figures (pp ) Use nested for loops to produce the following output: Why draw ASCII art? Real graphics require a lot of finesse ASCII art has complex patterns Can focus on the algorithms Closer relationship between computer code and output #================# <><> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <><> #================# 21

22 2.4 Managing Complexity (pp ) Development strategy (pp ) Recommendations for managing complexity: 1. Design the program (think about steps or methods needed). write an English description of steps required use this description to decide the algorithm 2. Create a table of patterns of characters use table to write your for loops #================# <><> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <><> #================# 22

23 2.4 Managing Complexity (pp ) 1. Pseudo-code (pp ) pseudo-code: An English description of an algorithm. Example: Drawing a 12 wide by 7 tall box of stars print 12 stars. for (each of 5 lines) { print a star. print 10 spaces. print a star. print 12 stars. ************ * * * * * * * * * * ************ 23

24 2.4 Managing Complexity (pp ) Pseudo-code algorithm for our original figure 1. Line #, 16 =, # 2. Top half spaces (decreasing) <> dots (increasing) <> spaces (same as above) 3. Bottom half (top half upside-down) 4. Line #, 16 =, # #================# <><> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <><> #================# 24

25 2.4 Managing Complexity (pp ) Methods from pseudocode (pp ) public class Mirror { public static void main(string[] args) { line(); tophalf(); bottomhalf(); line(); public static void tophalf() { for (int line = 1; line <= 4; line++) { // contents of each line public static void bottomhalf() { for (int line = 1; line <= 4; line++) { // contents of each line public static void line() { //... 25

26 2.4 Managing Complexity (pp ) 2. Tables (pp ) A table for the top half: Compute spaces and dots expressions from line number line spaces 8 2 * line dots 4 * line #================# <><> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <><> #================# 26

27 2.4 Managing Complexity (pp ) 3. Writing the code (pp ) Useful questions about the top half: What methods? (think structure and redundancy) Number of (nested) loops per line? #================# <><> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <><> #================# 27

28 2.4 Managing Complexity (pp ) Partial solution (pp ) // Prints the expanding pattern of <> for the top half of the figure. public static void tophalf() { for (int line = 1; line <= 4; line++) { System.out.print(" "); for (int space = 1; space <= (8 2 * line); space++) { System.out.print(" "); System.out.print("<>"); for (int dot = 1; dot <= (line * 4-4); dot++) { System.out.print("."); System.out.print("<>"); for (int space = 1; space <= (8 2 * line); space++) { System.out.print(" "); System.out.println(" "); 28

29 2.4 Managing Complexity (pp ) Class constants and scope (pp ) Class constants appear at the beginning of the class and are available for all methods in the class. 29

30 2.4 Managing Complexity (pp ) Scaling the mirror using class constants (pp ) Let's modify our Mirror program so that it can scale. The current mirror (left) is at size 4; the right is at size 3. We'd like to structure the code so we can scale the figure by changing the code in just one place. #================# #============# <><> <><> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <><> <>...<> #============# <><> #================# 30

31 2.4 Managing Complexity (pp ) Limitations of variables (pp ) Idea: Make a variable to represent the size. Use the variable's value in the methods. Problem: A variable in one method can't be seen in others. public static void main(string[] args) { int size = 4; tophalf(); printbottom(); public static void tophalf() { for (int i = 1; i <= size; i++) {... public static void bottomhalf() { for (int i = size; i >= 1; i--) {... // ERROR: size not found // ERROR: size not found 31

32 i's scope 2.4 Managing Complexity (pp ) Scope (pp ) scope: The part of a program where a variable exists. From its declaration to the end of the { braces A variable declared in a for loop exists only in that loop. A variable declared in a method exists only in that method. public static void example() { int x = 3; for (int i = 1; i <= 10; i++) { System.out.println(x); // i no longer exists here // x ceases to exist here x's scope 32

33 2.4 Managing Complexity (pp ) Scope implications (pp ) Variables without overlapping scope can have same name. for (int i = 1; i <= 100; i++) { System.out.print("/"); for (int i = 1; i <= 100; i++) { System.out.print("\\"); int i = 5; // OK // OK: outside of loop's scope A variable can't be declared twice or used out of its scope. for (int i = 1; i <= 100 * line; i++) { int i = 2; // ERROR: overlapping scope System.out.print("/"); i = 4; // ERROR: outside scope 33

34 2.4 Managing Complexity (pp ) Class constants (pp ) class constant: A fixed value visible to the whole program. value can be set only at declaration; cannot be reassigned Syntax: public static final type name = value; name is usually in ALL_UPPER_CASE Examples: public static final int DAYS_IN_WEEK = 7; public static final double INTEREST_RATE = 3.5; public static final int SSN = ; 34

35 2.4 Managing Complexity (pp ) Constants and figures (pp ) Consider the task of drawing the following scalable figure: +/\/\/\/\/\/\/\/\/\/\+ Multiples of 5 occur many times +/\/\/\/\/\/\/\/\/\/\+ +/\/\/\/\+ The same figure at size 2 +/\/\/\/\+ 35

36 2.4 Managing Complexity (pp ) Repetitive figure code (pp ) public class Sign { public static void main(string[] args) { drawline(); drawbody(); drawline(); public static void drawline() { System.out.print("+"); for (int i = 1; i <= 10; i++) { System.out.print("/\\"); System.out.println("+"); public static void drawbody() { for (int line = 1; line <= 5; line++) { System.out.print(" "); for (int spaces = 1; spaces <= 20; spaces++) { System.out.print(" "); System.out.println(" "); 36

37 2.4 Managing Complexity (pp ) Adding a constant (pp ) public class Sign { public static final int HEIGHT = 5; public static void main(string[] args) { drawline(); drawbody(); drawline(); public static void drawline() { System.out.print("+"); for (int i = 1; i <= HEIGHT * 2; i++) { System.out.print("/\\"); System.out.println("+"); public static void drawbody() { for (int line = 1; line <= HEIGHT; line++) { System.out.print(" "); for (int spaces = 1; spaces <= HEIGHT * 4; spaces++) { System.out.print(" "); System.out.println(" "); 37

38 2.4 Managing Complexity (pp ) Complex figure w/ constant (pp ) Modify the Mirror code to be resizable using a constant. A mirror of size 4: #================# <><> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <><> #================# A mirror of size 3: #============# <><> <>...<> <>...<> <>...<> <>...<> <><> #============# 38

39 2.4 Managing Complexity (pp ) Using a constant (pp ) Constant allows many methods to refer to same value: public static final int SIZE = 4; public static void main(string[] args) { tophalf(); printbottom(); public static void tophalf() { for (int i = 1; i <= SIZE; i++) {... public static void bottomhalf() { for (int i = SIZE; i >= 1; i--) {... // OK // OK 39

40 2.4 Managing Complexity (pp ) Loop tables and constant (pp ) Let's modify our loop table to use SIZE This can change the amount added in the loop expression SIZE line spaces -2*line + (2*SIZE) dots 4*line ,2,3,4 6,4,2,0-2*line + 8 0,4,8,12 4*line ,2,3 4,2,0-2*line + 6 0,4,8 4*line - 4 #================# #============# <><> <><> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <><> <>...<> #============# <><> #================# 40

41 2.4 Managing Complexity (pp ) Partial solution (pp ) public static final int SIZE = 4; // Prints the expanding pattern of <> for the top half of the figure. public static void tophalf() { for (int line = 1; line <= SIZE; line++) { System.out.print(" "); for (int space = 1; space <= (line * -2 + (2*SIZE)); space++) { System.out.print(" "); System.out.print("<>"); for (int dot = 1; dot <= (line * 4-4); dot++) { System.out.print("."); System.out.print("<>"); for (int space = 1; space <= (line * -2 + (2*SIZE)); space++) { System.out.print(" "); System.out.println(" "); 41

42 2.4 Managing Complexity (pp ) Observations about constant (pp ) The constant can change the "intercept" in an expression. Usually the "slope" is unchanged. public static final int SIZE = 4; for (int space = 1; space <= (line * -2 + (2 * SIZE)); space++) { System.out.print(" "); It doesn't replace every occurrence of the original value. for (int dot = 1; dot <= (line * 4-4); dot++) { System.out.print("."); 42

43 Assignments for this week 1. Laboratory for Chapter 2 due in one week (Monday 9/22) NOTE: Due date on handout is incorrect IMPORTANT: When you me your laboratory Word Document, be sure it is all in one file 2. Read first part of Chapter 3 for Monday (pp ) 3. Be sure to complete Quiz 6 before leaving class tonight This is another program to write You must demonstrate the program to me before you leave lab 43

Topic 6 loops, figures, constants

Topic 6 loops, figures, constants Topic 6 loops, figures, constants "Complexity has and will maintain a strong fascination for many people. It is true that we live in a complex world and strive to solve inherently complex problems, which

More information

Building Java Programs Chapter 2

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

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

Building Java Programs Chapter 2

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 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 or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Chapter Legal intliterals: 22, 1, and d Results of intexpressions:

Chapter Legal intliterals: 22, 1, and d Results of intexpressions: Chapter 2 1. Legal intliterals: 22, 1, and 6875309 2. d. 11 3. Results of intexpressions: a. 8 b. 11 c. 6 d. 4 e. 33 f. 16 g. 6.4 h. 6 i. 30 j. 1 k. 7 l. 5 m. 2 n. 18 o. 3 p. 4 q. 4 r. 15 s. 8 t. 1 4.

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 2: Primitive Data and Definite Loops These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2007. They may not be rehosted, sold, or modified without expressed

More information

Primitive data, expressions, and variables

Primitive data, expressions, and variables How the computer sees the world Primitive data, expressions, and variables Readings:.. Internally, the computer stores everything in terms of s and 0 s Example: h 0000 "hi" 0000000 0 0000 How can the computer

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

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

Repetition with for loops

Repetition with for loops Repetition with for loops So far, when we wanted to perform a task multiple times, we have written redundant code: System.out.println( Building Java Programs ); // print 5 blank lines System.out.println(

More information

Topic 6 Nested for Loops

Topic 6 Nested for Loops Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It is true that we live in a complex world and strive to solve inherently complex problems, which often

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 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 or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

10/9/07. < Moo? > \ ^ ^ \ (oo)\ ( )\ )\/\ ----w

10/9/07. < Moo? > \ ^ ^ \ (oo)\ ( )\ )\/\ ----w 10/9/07 < Moo? > ------ \ ^ ^ \ (oo)\ ( )\ )\/\ ----w >>> Reminders * Code feedback for Pythony things only. * NOT logic errors that will also be in Java code. / Could someone help me reboot my \ \ spaceship?

More information

CIS 110: Introduction to Computer Programming

CIS 110: Introduction to Computer Programming CIS 110: Introduction to Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline 1. For-loops! 2. Algorithm Design and Pseudocode 9/21/2011

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

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

Admin. CS 112 Introduction to Programming. Counting Down: Code Puzzle. Counting Down: Code Puzzle

Admin. CS 112 Introduction to Programming. Counting Down: Code Puzzle. Counting Down: Code Puzzle Admin CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

Building Java Programs

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

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 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 or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Building Java Programs

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

Building Java Programs

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

Outline. CIS 110: Introduction to Computer Programming. Announcements. For Loops. Redundancy in Patterns. A Solution with Our Current Tools

Outline. CIS 110: Introduction to Computer Programming. Announcements. For Loops. Redundancy in Patterns. A Solution with Our Current Tools Outline CIS 110: Introduction to Computer Programming 1. For-loops! 2. Algorithm Design and Pseudocode Lecture 5 The Loop-the-Loop ( 2.3-2.4) 1 2 Announcements Date of the final is tentatively: MONDAY,

More information

Software Practice 1 Basic Grammar

Software Practice 1 Basic Grammar Software Practice 1 Basic Grammar Basic Syntax Data Type Loop Control Making Decision Prof. Joonwon Lee T.A. Jaehyun Song Jongseok Kim (42) T.A. Sujin Oh Junseong Lee (43) 1 2 Java Program //package details

More information

CIS 110: Introduction to Computer Programming

CIS 110: Introduction to Computer Programming CIS 110: Introduction to Computer Programming Lecture 6 Flexible Methods ( 3.1-3.2) 9/27/2011 CIS 110 (11fa) - University of Pennsylvania 1 Announcements Homework 2 due at 23:59:59 tonight! Watch Piazza

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 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 or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 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 or http://www.class-notes.info or http://www.lecture-notes.tripod.com

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 3 Lecture 5: Parameters and Scope reading: 3.1 i's scope Scope scope: The part of a program where a variable exists. From its declaration to the end of the { braces A variable

More information

McGill University School of Computer Science COMP-202A Introduction to Computing 1

McGill University School of Computer Science COMP-202A Introduction to Computing 1 McGill University School of Computer Science COMP-202A Introduction to Computing 1 Midterm Exam Thursday, October 26, 2006, 18:00-20:00 (6:00 8:00 PM) Instructors: Mathieu Petitpas, Shah Asaduzzaman, Sherif

More information

Lab # 2. For today s lab:

Lab # 2. For today s lab: 1 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot 1 For today s lab: Go the course webpage Follow the links to the lab notes for Lab 2. Save all the java programs you

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 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 or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

AP Computer Science Unit 1. Programs

AP Computer Science Unit 1. Programs AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated

More information

Mr. Monroe s Guide to Mastering Java Syntax

Mr. Monroe s Guide to Mastering Java Syntax Mr. Monroe s Guide to Mastering Java Syntax Getting Started with Java 1. Download and install the official JDK (Java Development Kit). 2. Download an IDE (Integrated Development Environment), like BlueJ.

More information

Object-Oriented Programming

Object-Oriented Programming Object-Oriented Programming Java Syntax Program Structure Variables and basic data types. Industry standard naming conventions. Java syntax and coding conventions If Then Else Case statements Looping (for,

More information

AP Computer Science A Summer Assignment 2017

AP Computer Science A Summer Assignment 2017 AP Computer Science A Summer Assignment 2017 The objective of this summer assignment is to ensure that each student has the ability to compile and run code on a computer system at home. We will be doing

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 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 hope to have a course web site up on Blackboard soon However, I am using the following site all semester to allow

More information

Building Java Programs

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 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 or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 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 or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Course Outline. Introduction to java

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

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 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 or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Computer Science is...

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

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation. Java: Classes Introduction A class defines the abstract characteristics of a thing (object), including its attributes and what it can do. Every Java program is composed of at least one class. From a programming

More information

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

CS 302: INTRODUCTION TO PROGRAMMING IN JAVA. Chapter 5: Methods. Lecture 10

CS 302: INTRODUCTION TO PROGRAMMING IN JAVA. Chapter 5: Methods. Lecture 10 CS 302: INTRODUCTION TO PROGRAMMING IN JAVA Chapter 5: Methods Lecture 10 1 PROBLEM What if I was using a lot of different arrays and often wanted to print out their contents? I would have to have that

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

4 Programming Fundamentals. Introduction to Programming 1 1

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG 1 Notice Prepare the Weekly Quiz The weekly quiz is for the knowledge we learned in the previous week (both the

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

CSE 8A Lecture 21. SMarx extra final exam office hours Fri 3/15 11:15am-1pm EBU-3B #2206

CSE 8A Lecture 21. SMarx extra final exam office hours Fri 3/15 11:15am-1pm EBU-3B #2206 CSE 8A Lecture 21 LAST lecture on Friday! PSA 9 interviews due by Thursday SMarx extra final exam office hours Fri 3/15 11:15am-1pm EBU-3B #2206 Review Exam #4 Statistics Median: 72.5% (14.5/20) High:

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

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision Prof. Hwansoo Han T.A. Minseop Jeong T.A. Wonseok Choi 1 Java Program //package details public class ClassName {

More information

Logical Operators and switch

Logical Operators and switch Lecture 5 Relational and Equivalence Operators SYS-1S22 / MTH-1A66 Logical Operators and switch Stuart Gibson sg@sys.uea.ac.uk S01.09A 1 Relational Operator Meaning < Less than > Greater than

More information

AP CS Unit 3: Control Structures Notes

AP CS Unit 3: Control Structures Notes AP CS Unit 3: Control Structures Notes The if and if-else Statements. These statements are called control statements because they control whether a particular block of code is executed or not. Some texts

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 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 or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

CS 302: Introduction to Programming in Java. Lecture 11 Yinggang Huang. CS302 Summer 2012

CS 302: Introduction to Programming in Java. Lecture 11 Yinggang Huang. CS302 Summer 2012 CS 302: Introduction to Programming in Java Lecture 11 Yinggang Huang 1 Review How do we call a method? What are method inputs called? How many values can be returned from a method? Write a method header

More information

Java Coding 3. Over & over again!

Java Coding 3. Over & over again! Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java

More information

Exercise (Revisited)

Exercise (Revisited) Exercise (Revisited) Redo the cashier problem by using an infinite loop with a break statement. 1... 2 while (true) { 3 System.out.println("Enter price?"); 4 price = input.nextint(); 5 if (price

More information

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Java Programming Fundamentals - Day Instructor: Jason Yoon Website: Java Programming Fundamentals - Day 1 07.09.2016 Instructor: Jason Yoon Website: http://mryoon.weebly.com Quick Advice Before We Get Started Java is not the same as javascript! Don t get them confused

More information

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming s SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177 Programming Time allowed: THREE hours: Answer: ALL questions Items permitted: Items supplied: There is

More information

An overview of Java, Data types and variables

An overview of Java, Data types and variables An overview of Java, Data types and variables Lecture 2 from (UNIT IV) Prepared by Mrs. K.M. Sanghavi 1 2 Hello World // HelloWorld.java: Hello World program import java.lang.*; class HelloWorld { public

More information

Chapter 4: Control structures. Repetition

Chapter 4: Control structures. Repetition Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

More information

Nested Loops. A loop can be nested inside another loop.

Nested Loops. A loop can be nested inside another loop. Nested Loops A loop can be nested inside another loop. Nested loops consist of an outer loop and one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, and started

More information

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops Introductory Programming Imperative Programming III, sections 3.6-3.8, 3.0, 3.9 Anne Haxthausen a IMM, DTU 1. Loops (while, do, for) (sections 3.6 3.8) 2. Overview of Java s (learnt so far) 3. Program

More information

CISC 1600 Lecture 3.1 Introduction to Processing

CISC 1600 Lecture 3.1 Introduction to Processing CISC 1600 Lecture 3.1 Introduction to Processing Topics: Example sketches Drawing functions in Processing Colors in Processing General Processing syntax Processing is for sketching Designed to allow artists

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

Topic 4 Expressions and variables

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

Introduction to the Java Basics: Control Flow Statements

Introduction to the Java Basics: Control Flow Statements Lesson 3: Introduction to the Java Basics: Control Flow Statements Repetition Structures THEORY Variable Assignment You can only assign a value to a variable that is consistent with the variable s declared

More information

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes: 1 INSTRUCTOR: FAZLI CAN TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN Week 12/02/2007-16/02/2007 Lecture Notes: When we write a program we must design our programs to take correct output. For correct output,

More information

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School CSE 201 JAVA PROGRAMMING I Primitive Data Type Primitive Data Type 8-bit signed Two s complement Integer -128 ~ 127 Primitive Data Type 16-bit signed Two s complement Integer -32768 ~ 32767 Primitive Data

More information

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Outline. 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 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

Lecture 2: Operations and Data Types

Lecture 2: Operations and Data Types Lecture 2: Operations and Data Types Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Data types type: A category or set

More information

Java Programming. Computer Science 112

Java Programming. Computer Science 112 Java Programming Computer Science 112 Review: Problem solving Class 4 is the Whole Point of Programming. Is there any particular one you'd like to go through? If you are desperately confused still, now

More information

Chapter 4: Control structures

Chapter 4: Control structures Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

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

CP122 CS I. Iteration

CP122 CS I. Iteration CP122 CS I Iteration Tech News! Pix2Pix: machine learning translation of images https://affinelayer.com/pixsrv/ Tech News! Pix2Pix: machine learning translation of images https://affinelayer.com/pixsrv/

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

CSE 142, Summer 2014

CSE 142, Summer 2014 CSE 142, Summer 2014 Lecture 2: Static Methods Expressions reading: 1.4 2.1 Algorithms algorithm: A list of steps for solving a problem. Example algorithm: "Bake sugar cookies" Mix the dry ingredients.

More information

COMP 202 Java in one week

COMP 202 Java in one week COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

More information

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 1 Lecture 2: Static Methods reading: 1.4-1.5 (Slides adapted from Stuart Reges, Hélène Martin, and Marty Stepp) 2 Recall: structure, syntax class: a program public class

More information

Week 2. expressions, variables, for loops

Week 2. expressions, variables, for loops Week expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty Stepp for their work on these slides. Except where otherwise

More information

Summer Assignment for the School Year

Summer Assignment for the School Year Summer Assignment for the 2018-2019 School Year Course: AP Computer Science A Instructor: Mr. Rivera Welcome to AP Computer Science A. I am looking forward to an exciting school year of teaching Computer

More information

CIS 110: Introduction to Computer Programming

CIS 110: Introduction to Computer Programming CIS 110: Introduction to Computer Programming Lecture 8 Hey (Objects), Listen! ( 3.2-3.3) 10/2/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline Review: what is a library? The Math class The String

More information