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

Similar documents
Topic 6 loops, figures, constants

Building Java Programs Chapter 2

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

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

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

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

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

Building Java Programs

Primitive data, expressions, and variables

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

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

Repetition with for loops

Topic 6 Nested for Loops

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

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

CIS 110: Introduction to Computer Programming

Recap: Assignment as an Operator CS 112 Introduction to Programming

CS 112 Introduction to Programming

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

Building Java Programs

CS 112 Introduction to Programming

Getting started with Java

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

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

Building Java Programs

Building Java Programs

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

Software Practice 1 Basic Grammar

CIS 110: Introduction to Computer Programming

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

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

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

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

CS111: PROGRAMMING LANGUAGE II

Building Java Programs

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

Lab # 2. For today s lab:

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

AP Computer Science Unit 1. Programs

Mr. Monroe s Guide to Mastering Java Syntax

Object-Oriented Programming

AP Computer Science A Summer Assignment 2017

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

Building Java Programs

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

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

Course Outline. Introduction to java

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

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

Computer Science is...

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

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

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

Program Fundamentals

4 Programming Fundamentals. Introduction to Programming 1 1

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

STUDENT LESSON A12 Iterations

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

CS111: PROGRAMMING LANGUAGE II

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

Logical Operators and switch

AP CS Unit 3: Control Structures Notes

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

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

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

Java Coding 3. Over & over again!

Exercise (Revisited)

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

An overview of Java, Data types and variables

Chapter 4: Control structures. Repetition

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

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

CISC 1600 Lecture 3.1 Introduction to Processing

Introduction to Software Development (ISD) Week 3

Topic 4 Expressions and variables

Introduction to the Java Basics: Control Flow Statements

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

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

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

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

Lecture 2: Operations and Data Types

Java Programming. Computer Science 112

Chapter 4: Control structures

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 );

CP122 CS I. Iteration

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CSE 142, Summer 2014

COMP 202 Java in one week

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Building Java Programs

Week 2. expressions, variables, for loops

Summer Assignment for the School Year

CIS 110: Introduction to Computer Programming

Transcription:

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 tab to download the PowerPoint lectures, the Quiz solutions and the Laboratory assignments. 2

Lecture 05 2/10/2014 3

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

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

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

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(29.000000000000004) n = 12.8, d = 3.3, n % d = 2.9000000000000012(2.9000000000000017) 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

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

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

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

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

Review of Quiz 5 Problem 5 Set an integer x equal to 12345 (int x = 12345;) and then use a single println statement to print each digit out in order with a space in between them (1 2 3 4 5). 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

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

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

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

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

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

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

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

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

2.4 Managing Complexity (pp. 99-110) Drawing complex figures (pp. 99-105) 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

2.4 Managing Complexity (pp. 99-110) Development strategy (pp. 99-105) 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

2.4 Managing Complexity (pp. 99-110) 1. Pseudo-code (pp. 105-108) 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

2.4 Managing Complexity (pp. 99-110) 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

2.4 Managing Complexity (pp. 99-110) Methods from pseudocode (pp. 105-108) 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

2.4 Managing Complexity (pp. 99-110) 2. Tables (pp. 105-108) A table for the top half: Compute spaces and dots expressions from line number line spaces 8 2 * line dots 4 * line - 4 1 6 6 0 0 2 4 4 4 4 3 2 2 8 8 4 0 0 12 12 #================# <><> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <><> #================# 26

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

2.4 Managing Complexity (pp. 99-110) Partial solution (pp. 105-108) // 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

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

2.4 Managing Complexity (pp. 99-110) Scaling the mirror using class constants (pp. 108-110) 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

2.4 Managing Complexity (pp. 99-110) Limitations of variables (pp. 108-110) 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

i's scope 2.4 Managing Complexity (pp. 99-110) Scope (pp. 108-110) 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

2.4 Managing Complexity (pp. 99-110) Scope implications (pp. 108-110) 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

2.4 Managing Complexity (pp. 99-110) Class constants (pp. 108-110) 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 = 658234569; 34

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

2.4 Managing Complexity (pp. 99-110) Repetitive figure code (pp. 108-110) 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

2.4 Managing Complexity (pp. 99-110) Adding a constant (pp. 108-110) 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

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

2.4 Managing Complexity (pp. 99-110) Using a constant (pp. 108-110) 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

2.4 Managing Complexity (pp. 99-110) Loop tables and constant (pp. 108-110) 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 - 4 4 1,2,3,4 6,4,2,0-2*line + 8 0,4,8,12 4*line - 4 3 1,2,3 4,2,0-2*line + 6 0,4,8 4*line - 4 #================# #============# <><> <><> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <>...<> <><> <>...<> #============# <><> #================# 40

2.4 Managing Complexity (pp. 99-110) Partial solution (pp. 108-110) 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

2.4 Managing Complexity (pp. 99-110) Observations about constant (pp. 108-110) 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

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 email me your laboratory Word Document, be sure it is all in one file 2. Read first part of Chapter 3 for Monday (pp. 136-150) 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