Oct 8th 
Lab08. 
Quiz review 
Triangle and Stripes 
http://www.slideshare.net/takyeon
Quiz review 
No 
i++ 
++i 
Use and then increase 
Increase and then use 
int i = 3; 
int a = i++; // a = 3, i = 4 
int b = ++a; // b = 4, a = 4
Quiz review 
str 
maxCount 100 
• Whenever a new variable is declared, it is 
added to STACK. 
• Primitive data types are stored in STACK 
• byte, short, int, long, float, double, boolean, char 
• Other data types are stored in HEAP. 
• String, Integer, Scanner, … 
"Hello" 
"HELLO" 
• Data in HEAP are not immediately 
deleted but unlinked, and will be 
garbage-collected.
public static void main(String[] args) { 
Scanner sc = new Scanner(System.in); 
int size = sc.nextInt(); 
for(int row=1;row<=size;row++) { 
for(int col=1;col<=size;col++) { 
System.out.print(row*col + " "); 
} 
System.out.println(); 
} 
}
Lab – 2D drawing 
Two methods. 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
M1. Iterate pixels to paint 
for (int i=0; i<size; i++) { 
grid.setColor(i, i, Color.BLUE); 
} 
Intuitive and efficient  
M2. Iterate every pixel, use if conditionals to 
check pixels to paint 
for (int row=0; row<size; row++) { 
for (int col=0; col<size; col++) { 
if(row==col) { 
grid.setColor(row, col, Color.BLUE); 
} 
} 
} 
Complex and inefficient  
BUT! More generalizable
Lab – 2D drawing 
Two methods. 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
M1. Iterate pixels to paint 
Very difficult  
4,0 4,1 4,2 4,3 4,4 M2. Iterate every pixel, use if conditionals to 
check pixels to paint 
for (int row=0; row<size; row++) { 
for (int col=0; col<size; col++) { 
if(row!=col) { 
grid.setColor(row, col, Color.BLUE); 
} 
} 
} 
You can simply inverse the conditional logic 
Now you want to 
paint all the pixels 
except the diagonal 
line.
More examples. 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
row>2 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
row%2 == 1 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
col%2 == 1 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
row-col 
0 -1 -2 -3 -4 
1 0 -1 -2 -3 
2 1 0 -1 -2 
3 2 1 0 -1 
4 3 2 1 0 
(row-col)>=0 
Diagonal shapes 
require both row 
and col 
Linear shapes require either row 
or col.
Transformation > Move 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
(row+1, col) 
0,1 0,2 0,3 0,4 0,5 
1,1 1,2 1,3 1,4 1,5 
2,1 2,2 2,3 2,4 2,5 
3,1 3,2 3,3 3,4 3,5 
4,1 4,2 4,3 4,4 4,5 
(row+1)-col 
-1 -2 -3 -4 -5 
0 -1 -2 -3 -4 
1 0 -1 -2 -3 
2 1 0 -1 -2 
3 2 1 0 -1 
(row+1)-col >= 0 
To move a shape to left by 1 
pixel, 
replace "row" with "row+1"
Transformation > Horizontal Flip. 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
0,4 0,3 0,2 0,1 0,0 
1,4 1,3 1,2 1,1 1,0 
2,4 2,3 2,2 2,1 2,0 
3,4 3,3 3,2 3,1 3,0 
4,4 4,3 4,2 4,1 4,0 
Horizontal 
Flip 
(row, 4-col) 
row-(4-col) 
-4 -3 -2 -1 0 
-3 -2 -1 0 1 
-2 -1 0 1 2 
-1 0 1 2 3 
0 1 2 3 4 
(row-(4-col))>=0 
To flip a shape, multiple row or 
column by -1, and add size 
col 
-col 
size-col 
col 
4 := size of the shape – 1 
Why -1? Because our row and 
col index started from 0.
Transformation > Vertical Flip. 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
Vertical 
Flip 
(4-row, col) 
4,0 4,1 4,2 4,3 4,4 
3,0 3,1 3,2 3,3 3,4 
2,0 2,1 2,2 2,3 2,4 
1,0 1,1 1,2 1,3 1,4 
0,0 0,1 0,2 0,3 0,4 
(4-row)-col 
4 3 2 1 0 
3 2 1 0 -1 
2 1 0 -1 -2 
1 0 -1 -2 -3 
0 -1 -2 -3 -4 
(4-row)-col >= 0
0 -1 -2 -3 -4 
1 0 -1 -2 -3 
2 1 0 -1 -2 
3 2 1 0 -1 
4 3 2 1 0 
(row-col)>=0 
-4 -3 -2 -1 0 
-3 -2 -1 0 1 
-2 -1 0 1 2 
-1 0 1 2 3 
0 1 2 3 4 
(row-(4-col))>=0 
Horizontal 
Flip 
Vertical flip 
4 3 2 1 0 
3 2 1 0 -1 
2 1 0 -1 -2 
1 0 -1 -2 -3 
0 -1 -2 -3 -4 
(4-row)-col >= 0 
Vertical flip 
0 1 2 3 4 
-1 0 1 2 3 
-2 -1 0 1 2 
-3 -2 -1 0 1 
-4 -3 -2 -1 0 
(4-row)-(4-col) >= 0 
Horizontal 
Flip 
col-row >= 0
Oct 6th 
Lab07. 
Loop applications
Finding common factors of two numbers 
Common factors can divide both numbers. 
E.g. Common factors of 9 and 12  1 and 3 
public void commonFactor(int n1, int n2) { 
for(int i=1; i<=min(n1,n2); i++) { 
if(n1%i==0 && n2%i==0) { 
System.out.println(i); 
} 
} 
} 
Common factors of 24 and 78  1, 2, 3, and 6
compareTo method 
String s1 = "aaa"; 
String s2 = "aac"; 
int k = s1.compareTo(s2); // k => -2 
Compares s1 and s2 lexicographically. 
Negative if s1 precedes s2 
Positive if s1 follows s2 
Zero if s1 is equal to s2
Get multiple words, find the first and the last words 
1) Using while loop, keep asking words until "STOP" 
2) Using compareTo, update the first and the last words 
3) Print out
Oct8 - 131 slid
Oct 1st 
Lab06. 
2D drawing
SquareGrid.java 
Drawing shapes on 2D grid 
ExampleDriver.java 
• Prompt a shape question 
• Create an empty grid 
• Draw the requested shape 
OperatorMaker.java 
drawOp (SquareGrid grid, int symbol) 
minus, plus, divide, multiply (SquareGrid grid) 
You will change only these methods 
Single loop for drawing a line 
0 1) How can we get the middle row number? 
3 
size : 7 
int size = grid.getHt(); 
int midRow = size / 2; 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
2) How to draw a line? 
• Iterate over columns (0 – 4) 
• Paint the middle cell 
for (int iCol=0; iCol<size; iCol++) { 
grid.setColor(midRow, iCol, Color.BLUE); 
}
Single loop for drawing a line 
0,0 0,1 0,2 0,3 0,4 1) How can we get the middle column number? 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
int size = grid.getWd(); 
int midCol = size / 2; 
2) How to draw a line? 
• Iterate over rows (0 – 4) 
• Paint the middle cell 
for (int iRow=0; iRow<size; iRow++) { 
grid.setColor(iRow, midCol, Color.BLUE); 
} 
Notice that drawing horizontal and vertical lines are quite similar. 
We just switched row and column variables.
Single loop for drawing a line 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
1) How to draw a line? 
• Iterate over rows or columns (0-4) 
• Paint diagonal cells. 
for (int iRow=0; iRow<size; iRow++) { 
grid.setColor(iRow, iRow, Color.BLUE); 
}
for (int iCol=0; iCol<size; iCol++) { 
grid.setColor(midRow, iCol, Color.BLUE); 
} 
for (int iRow=0; iRow<size; iRow++) { 
grid.setColor(iRow, midCol, Color.BLUE); 
} 
for (int iRow=0; iRow<size; iRow++) { 
grid.setColor(iRow, iRow, Color.BLUE); 
} 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
0,0 0,1 0,2 0,3 0,4 
1,0 1,1 1,2 1,3 1,4 
2,0 2,1 2,2 2,3 2,4 
3,0 3,1 3,2 3,3 3,4 
4,0 4,1 4,2 4,3 4,4 
for (int iCol=0; iCol<size; iCol++) { 
grid.setColor(iCol, iCol, Color.BLUE); 
} 
or
Single loop for drawing a line 
Iterating over 
the columns, 
paint the middle 
cell. 
Iterating over 
the columns, 
paint the middle 
cell. 
Iterating over 
the rows, 
paint the center 
cell. 
Iterating over 
the columns, 
paint i-th cell. 
Draw Plus, 
Divide, 
and Divide (rotated).
Oct8 - 131 slid
Sep 29th 
Lab05. 
1. Recap the quiz #1 
2. String Class 
3. static vs. instance method
Recap quiz #1. 
PRINT your names in 
the grade server. 
NO nicknames.
Recap quiz #1. 
Use specific technical keywords 
e.g. What does CVS “do” for us? 
1. Check out / Download the starter files 
2. Store / Save multiple versions of the source code 
share, deliver, get, access, connected people, ... 
Penalties for inaccurate extra info 
e.g. CVS runs our code and give us grades.  -1 for incorrect extra info.
Oct8 - 131 slid
String Class 
Create a new String object with an 
initial value “hello” 
String s = “hello”; 
String objects have many convenient methods, 
upperS = s.toUpperCase(); // will set upperS to “HELLO” 
whereIsl= s.indexOf(‘l’); // will find the position of the first ‘l’ 
newS = s.replace(‘e’,’a’); // will set newS to “hallo”
int type vs. Integer Class 
int i=0; 
Primitive data type 
Integer i = 17; 
Wrapper Class 
Faster A little slower 
don’t have much method provide methods 
- convert to string 
- generate hash codes
Static vs. Instance method 
Intance methods need a sheep as a subject. 
bob.eat(); 
bob.smileTo(clara); 
bob.getPenNumber(); 
Static methods are about all the sheeps. 
Sheep.getTotalSheep(); 
Sheep.removeAll(); 
Sheep.addSheep(‘evan’);
Sep 24th 
Lab04. 
Loop
Flow of Control 
1. Top-to-bottom statements 
2. Method calls 
3. Conditional statements 
4. Iteration (loop) 
for, while, ...
Two goals of iteration 
1. Automation 
Reduce repetition of code 
System.out.println(“****”); 
System.out.println(“****”); 
System.out.println(“****”); 
How can we reduce? 
for(int i=0;i<3;i++) { 
System.out.println(“****”); 
} 
2. Abstraction 
Code for various situations 
System.out.println(“****”); 
How can we print n-number of “*”?
From manual & concrete to automatic & abstract 
Level 1. Draw 30 by 10 rectangle (hard-coded) 
System.out.println(“**********”); 
System.out.println(“**********”); 
System.out.println(“**********”); 
... 27 more lines 
 Too many copy & paste. Hard to modify. 
Level 2. Draw 30 by 10 rectangle (single-loop) 
int row=0; 
while(row<30) { 
System.out.println(“**********”); 
row++; 
} 
 A little more compact. 
 Still too many * for each line.
From manual & concrete to automatic & abstract 
Level 3. Draw 30 by 10 rectangle (nested-loop) 
int row=0, col=0; 
while(row<30) { 
while(col<10) { 
System.out.print(“*”); 
} 
System.out.println(); 
} 
 Much compact. 
 Cannot change # of row and col 
Level 4. Draw height by width (nested-loop, parameterized) 
int row=0, col=0; 
int height=30, width=10; 
while(row<height) { 
while(col<width) { 
System.out.print(“*”); 
} 
System.out.println(); 
} 
 Compact 
 Can draw any sized rectangle
iterartor 
line 1 
0 
value 
line 2 
0 
target 
line 4
answer 
line 1 
1 
i 
line 1 
1 
j 
line 1 
0
Oct8 - 131 slid

More Related Content

PPTX
Oct27
PDF
PDF
1st prep.sheet فى الجبر والهندسة للصف الأول الإعدادى لغات
KEY
9-7 Graphing Points in Coordinate Plane
DOC
Final Revision Math Primary 2 (Part 1)
PDF
Inequations and finding rule
PPSX
Evolutionary Graphics - Class 02 2014
PDF
"Incremental Lossless Graph Summarization", KDD 2020
Oct27
1st prep.sheet فى الجبر والهندسة للصف الأول الإعدادى لغات
9-7 Graphing Points in Coordinate Plane
Final Revision Math Primary 2 (Part 1)
Inequations and finding rule
Evolutionary Graphics - Class 02 2014
"Incremental Lossless Graph Summarization", KDD 2020

What's hot (20)

DOCX
Tugasmatematikakelompok
DOCX
Tugasmatematikakelompok 150715235527-lva1-app6892
DOCX
Tugas matematika kelompok
PDF
E029024030
PPTX
preparation of a unit "identities"
PDF
Hmotif vldb2020 slide
PDF
"SSumM: Sparse Summarization of Massive Graphs", KDD 2020
PPTX
Odd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle Square
PDF
Secrets in Inequalities
PDF
Solutions manual for college algebra 10th edition by larson ibsn 9781337282291z
PPT
Lesson 5.3 honors
PDF
Pure Mathematics Unit 2 - Textbook
PDF
math m1
PPT
Coordinate Graph1
PDF
CAPE PURE MATHEMATICS UNIT 2 MODULE 1 PRACTICE QUESTIONS
PDF
Algebra 2 Section 1-8
PDF
CAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONS
PDF
Application of subQuan to Algebra: 3rd-8th grade and beyond...
PPT
Lesson 4.3 regular
PDF
Algebra 2 Section 1-6
Tugasmatematikakelompok
Tugasmatematikakelompok 150715235527-lva1-app6892
Tugas matematika kelompok
E029024030
preparation of a unit "identities"
Hmotif vldb2020 slide
"SSumM: Sparse Summarization of Massive Graphs", KDD 2020
Odd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle Square
Secrets in Inequalities
Solutions manual for college algebra 10th edition by larson ibsn 9781337282291z
Lesson 5.3 honors
Pure Mathematics Unit 2 - Textbook
math m1
Coordinate Graph1
CAPE PURE MATHEMATICS UNIT 2 MODULE 1 PRACTICE QUESTIONS
Algebra 2 Section 1-8
CAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONS
Application of subQuan to Algebra: 3rd-8th grade and beyond...
Lesson 4.3 regular
Algebra 2 Section 1-6
Ad

Similar to Oct8 - 131 slid (20)

PDF
Introduction to programming - class 3
PDF
Programming methodology lecture17
DOCX
Lecture3 oopj
PPT
13slide graphics
PPT
java graphics
PPT
Jeop game-final-review
PPTX
ATTRIBUTES OF OUTPUT PRIMITIVES IN COMPUTER GRAPHICS
PDF
Computer Graphics in Java and Scala - Part 1
PPT
Unit 2 Graphics and awt.hsjshbshshsbsbsbs.pptx
PDF
Introduction to programming class 11 exercises
PPT
JavaYDL13
PPTX
20.1 Java working with abstraction
PDF
Shape logic 1
PDF
Coding for
PPT
graph coloring.ppt
PDF
NEW AP Computer Science Exam GridWorld Quick Reference Booklet
PPTX
graphics programming in java
PDF
Contest Tips and Tricks
DOCX
Coding io1-materials for students-group3
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Introduction to programming - class 3
Programming methodology lecture17
Lecture3 oopj
13slide graphics
java graphics
Jeop game-final-review
ATTRIBUTES OF OUTPUT PRIMITIVES IN COMPUTER GRAPHICS
Computer Graphics in Java and Scala - Part 1
Unit 2 Graphics and awt.hsjshbshshsbsbsbs.pptx
Introduction to programming class 11 exercises
JavaYDL13
20.1 Java working with abstraction
Shape logic 1
Coding for
graph coloring.ppt
NEW AP Computer Science Exam GridWorld Quick Reference Booklet
graphics programming in java
Contest Tips and Tricks
Coding io1-materials for students-group3
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ad

Recently uploaded (20)

PDF
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
PPTX
ai_satellite_crop_management_20250815030350.pptx
PPTX
Petroleum Refining & Petrochemicals.pptx
PPTX
Information Storage and Retrieval Techniques Unit III
PPTX
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
PDF
Computer System Architecture 3rd Edition-M Morris Mano.pdf
PPTX
wireless networks, mobile computing.pptx
DOC
T Pandian CV Madurai pandi kokkaf illaya
PDF
August -2025_Top10 Read_Articles_ijait.pdf
PPT
Chapter 1 - Introduction to Manufacturing Technology_2.ppt
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
Design of Material Handling Equipment Lecture Note
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PDF
Cryptography and Network Security-Module-I.pdf
PDF
Applications of Equal_Area_Criterion.pdf
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
ai_satellite_crop_management_20250815030350.pptx
Petroleum Refining & Petrochemicals.pptx
Information Storage and Retrieval Techniques Unit III
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
Computer System Architecture 3rd Edition-M Morris Mano.pdf
wireless networks, mobile computing.pptx
T Pandian CV Madurai pandi kokkaf illaya
August -2025_Top10 Read_Articles_ijait.pdf
Chapter 1 - Introduction to Manufacturing Technology_2.ppt
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
distributed database system" (DDBS) is often used to refer to both the distri...
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Exploratory_Data_Analysis_Fundamentals.pdf
Design of Material Handling Equipment Lecture Note
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
Cryptography and Network Security-Module-I.pdf
Applications of Equal_Area_Criterion.pdf
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx

Oct8 - 131 slid

  • 1. Oct 8th Lab08. Quiz review Triangle and Stripes http://www.slideshare.net/takyeon
  • 2. Quiz review No i++ ++i Use and then increase Increase and then use int i = 3; int a = i++; // a = 3, i = 4 int b = ++a; // b = 4, a = 4
  • 3. Quiz review str maxCount 100 • Whenever a new variable is declared, it is added to STACK. • Primitive data types are stored in STACK • byte, short, int, long, float, double, boolean, char • Other data types are stored in HEAP. • String, Integer, Scanner, … "Hello" "HELLO" • Data in HEAP are not immediately deleted but unlinked, and will be garbage-collected.
  • 4. public static void main(String[] args) { Scanner sc = new Scanner(System.in); int size = sc.nextInt(); for(int row=1;row<=size;row++) { for(int col=1;col<=size;col++) { System.out.print(row*col + " "); } System.out.println(); } }
  • 5. Lab – 2D drawing Two methods. 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 M1. Iterate pixels to paint for (int i=0; i<size; i++) { grid.setColor(i, i, Color.BLUE); } Intuitive and efficient  M2. Iterate every pixel, use if conditionals to check pixels to paint for (int row=0; row<size; row++) { for (int col=0; col<size; col++) { if(row==col) { grid.setColor(row, col, Color.BLUE); } } } Complex and inefficient  BUT! More generalizable
  • 6. Lab – 2D drawing Two methods. 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 M1. Iterate pixels to paint Very difficult  4,0 4,1 4,2 4,3 4,4 M2. Iterate every pixel, use if conditionals to check pixels to paint for (int row=0; row<size; row++) { for (int col=0; col<size; col++) { if(row!=col) { grid.setColor(row, col, Color.BLUE); } } } You can simply inverse the conditional logic Now you want to paint all the pixels except the diagonal line.
  • 7. More examples. 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 row>2 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 row%2 == 1 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 col%2 == 1 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 row-col 0 -1 -2 -3 -4 1 0 -1 -2 -3 2 1 0 -1 -2 3 2 1 0 -1 4 3 2 1 0 (row-col)>=0 Diagonal shapes require both row and col Linear shapes require either row or col.
  • 8. Transformation > Move 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 (row+1, col) 0,1 0,2 0,3 0,4 0,5 1,1 1,2 1,3 1,4 1,5 2,1 2,2 2,3 2,4 2,5 3,1 3,2 3,3 3,4 3,5 4,1 4,2 4,3 4,4 4,5 (row+1)-col -1 -2 -3 -4 -5 0 -1 -2 -3 -4 1 0 -1 -2 -3 2 1 0 -1 -2 3 2 1 0 -1 (row+1)-col >= 0 To move a shape to left by 1 pixel, replace "row" with "row+1"
  • 9. Transformation > Horizontal Flip. 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 0,4 0,3 0,2 0,1 0,0 1,4 1,3 1,2 1,1 1,0 2,4 2,3 2,2 2,1 2,0 3,4 3,3 3,2 3,1 3,0 4,4 4,3 4,2 4,1 4,0 Horizontal Flip (row, 4-col) row-(4-col) -4 -3 -2 -1 0 -3 -2 -1 0 1 -2 -1 0 1 2 -1 0 1 2 3 0 1 2 3 4 (row-(4-col))>=0 To flip a shape, multiple row or column by -1, and add size col -col size-col col 4 := size of the shape – 1 Why -1? Because our row and col index started from 0.
  • 10. Transformation > Vertical Flip. 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 Vertical Flip (4-row, col) 4,0 4,1 4,2 4,3 4,4 3,0 3,1 3,2 3,3 3,4 2,0 2,1 2,2 2,3 2,4 1,0 1,1 1,2 1,3 1,4 0,0 0,1 0,2 0,3 0,4 (4-row)-col 4 3 2 1 0 3 2 1 0 -1 2 1 0 -1 -2 1 0 -1 -2 -3 0 -1 -2 -3 -4 (4-row)-col >= 0
  • 11. 0 -1 -2 -3 -4 1 0 -1 -2 -3 2 1 0 -1 -2 3 2 1 0 -1 4 3 2 1 0 (row-col)>=0 -4 -3 -2 -1 0 -3 -2 -1 0 1 -2 -1 0 1 2 -1 0 1 2 3 0 1 2 3 4 (row-(4-col))>=0 Horizontal Flip Vertical flip 4 3 2 1 0 3 2 1 0 -1 2 1 0 -1 -2 1 0 -1 -2 -3 0 -1 -2 -3 -4 (4-row)-col >= 0 Vertical flip 0 1 2 3 4 -1 0 1 2 3 -2 -1 0 1 2 -3 -2 -1 0 1 -4 -3 -2 -1 0 (4-row)-(4-col) >= 0 Horizontal Flip col-row >= 0
  • 12. Oct 6th Lab07. Loop applications
  • 13. Finding common factors of two numbers Common factors can divide both numbers. E.g. Common factors of 9 and 12  1 and 3 public void commonFactor(int n1, int n2) { for(int i=1; i<=min(n1,n2); i++) { if(n1%i==0 && n2%i==0) { System.out.println(i); } } } Common factors of 24 and 78  1, 2, 3, and 6
  • 14. compareTo method String s1 = "aaa"; String s2 = "aac"; int k = s1.compareTo(s2); // k => -2 Compares s1 and s2 lexicographically. Negative if s1 precedes s2 Positive if s1 follows s2 Zero if s1 is equal to s2
  • 15. Get multiple words, find the first and the last words 1) Using while loop, keep asking words until "STOP" 2) Using compareTo, update the first and the last words 3) Print out
  • 17. Oct 1st Lab06. 2D drawing
  • 18. SquareGrid.java Drawing shapes on 2D grid ExampleDriver.java • Prompt a shape question • Create an empty grid • Draw the requested shape OperatorMaker.java drawOp (SquareGrid grid, int symbol) minus, plus, divide, multiply (SquareGrid grid) You will change only these methods 
  • 19. Single loop for drawing a line 0 1) How can we get the middle row number? 3 size : 7 int size = grid.getHt(); int midRow = size / 2; 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 2) How to draw a line? • Iterate over columns (0 – 4) • Paint the middle cell for (int iCol=0; iCol<size; iCol++) { grid.setColor(midRow, iCol, Color.BLUE); }
  • 20. Single loop for drawing a line 0,0 0,1 0,2 0,3 0,4 1) How can we get the middle column number? 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 int size = grid.getWd(); int midCol = size / 2; 2) How to draw a line? • Iterate over rows (0 – 4) • Paint the middle cell for (int iRow=0; iRow<size; iRow++) { grid.setColor(iRow, midCol, Color.BLUE); } Notice that drawing horizontal and vertical lines are quite similar. We just switched row and column variables.
  • 21. Single loop for drawing a line 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 1) How to draw a line? • Iterate over rows or columns (0-4) • Paint diagonal cells. for (int iRow=0; iRow<size; iRow++) { grid.setColor(iRow, iRow, Color.BLUE); }
  • 22. for (int iCol=0; iCol<size; iCol++) { grid.setColor(midRow, iCol, Color.BLUE); } for (int iRow=0; iRow<size; iRow++) { grid.setColor(iRow, midCol, Color.BLUE); } for (int iRow=0; iRow<size; iRow++) { grid.setColor(iRow, iRow, Color.BLUE); } 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 for (int iCol=0; iCol<size; iCol++) { grid.setColor(iCol, iCol, Color.BLUE); } or
  • 23. Single loop for drawing a line Iterating over the columns, paint the middle cell. Iterating over the columns, paint the middle cell. Iterating over the rows, paint the center cell. Iterating over the columns, paint i-th cell. Draw Plus, Divide, and Divide (rotated).
  • 25. Sep 29th Lab05. 1. Recap the quiz #1 2. String Class 3. static vs. instance method
  • 26. Recap quiz #1. PRINT your names in the grade server. NO nicknames.
  • 27. Recap quiz #1. Use specific technical keywords e.g. What does CVS “do” for us? 1. Check out / Download the starter files 2. Store / Save multiple versions of the source code share, deliver, get, access, connected people, ... Penalties for inaccurate extra info e.g. CVS runs our code and give us grades.  -1 for incorrect extra info.
  • 29. String Class Create a new String object with an initial value “hello” String s = “hello”; String objects have many convenient methods, upperS = s.toUpperCase(); // will set upperS to “HELLO” whereIsl= s.indexOf(‘l’); // will find the position of the first ‘l’ newS = s.replace(‘e’,’a’); // will set newS to “hallo”
  • 30. int type vs. Integer Class int i=0; Primitive data type Integer i = 17; Wrapper Class Faster A little slower don’t have much method provide methods - convert to string - generate hash codes
  • 31. Static vs. Instance method Intance methods need a sheep as a subject. bob.eat(); bob.smileTo(clara); bob.getPenNumber(); Static methods are about all the sheeps. Sheep.getTotalSheep(); Sheep.removeAll(); Sheep.addSheep(‘evan’);
  • 33. Flow of Control 1. Top-to-bottom statements 2. Method calls 3. Conditional statements 4. Iteration (loop) for, while, ...
  • 34. Two goals of iteration 1. Automation Reduce repetition of code System.out.println(“****”); System.out.println(“****”); System.out.println(“****”); How can we reduce? for(int i=0;i<3;i++) { System.out.println(“****”); } 2. Abstraction Code for various situations System.out.println(“****”); How can we print n-number of “*”?
  • 35. From manual & concrete to automatic & abstract Level 1. Draw 30 by 10 rectangle (hard-coded) System.out.println(“**********”); System.out.println(“**********”); System.out.println(“**********”); ... 27 more lines  Too many copy & paste. Hard to modify. Level 2. Draw 30 by 10 rectangle (single-loop) int row=0; while(row<30) { System.out.println(“**********”); row++; }  A little more compact.  Still too many * for each line.
  • 36. From manual & concrete to automatic & abstract Level 3. Draw 30 by 10 rectangle (nested-loop) int row=0, col=0; while(row<30) { while(col<10) { System.out.print(“*”); } System.out.println(); }  Much compact.  Cannot change # of row and col Level 4. Draw height by width (nested-loop, parameterized) int row=0, col=0; int height=30, width=10; while(row<height) { while(col<width) { System.out.print(“*”); } System.out.println(); }  Compact  Can draw any sized rectangle
  • 37. iterartor line 1 0 value line 2 0 target line 4
  • 38. answer line 1 1 i line 1 1 j line 1 0