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