SlideShare a Scribd company logo
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
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).
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.
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

What's hot

Tugasmatematikakelompok
TugasmatematikakelompokTugasmatematikakelompok
Tugasmatematikakelompok
gundul28
 
Tugasmatematikakelompok 150715235527-lva1-app6892
Tugasmatematikakelompok 150715235527-lva1-app6892Tugasmatematikakelompok 150715235527-lva1-app6892
Tugasmatematikakelompok 150715235527-lva1-app6892
drayertaurus
 
Tugas matematika kelompok
Tugas matematika kelompokTugas matematika kelompok
Tugas matematika kelompok
achmadtrybuana
 
E029024030
E029024030E029024030
E029024030
researchinventy
 
preparation of a unit "identities"
preparation of a unit "identities"preparation of a unit "identities"
preparation of a unit "identities"
Naseera noushad
 
Hmotif vldb2020 slide
Hmotif vldb2020 slideHmotif vldb2020 slide
Hmotif vldb2020 slide
Geon Lee
 
"SSumM: Sparse Summarization of Massive Graphs", KDD 2020
"SSumM: Sparse Summarization of Massive Graphs", KDD 2020"SSumM: Sparse Summarization of Massive Graphs", KDD 2020
"SSumM: Sparse Summarization of Massive Graphs", KDD 2020
KyuhanLee4
 
Odd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle Square
Odd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle SquareOdd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle Square
Odd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle Square
Alan Dix
 
Secrets in Inequalities
Secrets in InequalitiesSecrets in Inequalities
Secrets in Inequalities
Hung Pham
 
Solutions manual for college algebra 10th edition by larson ibsn 9781337282291z
Solutions manual for college algebra 10th edition by larson ibsn 9781337282291zSolutions manual for college algebra 10th edition by larson ibsn 9781337282291z
Solutions manual for college algebra 10th edition by larson ibsn 9781337282291z
Certo612
 
Lesson 5.3 honors
Lesson 5.3 honorsLesson 5.3 honors
Lesson 5.3 honors
morrobea
 
Pure Mathematics Unit 2 - Textbook
Pure Mathematics Unit 2 - TextbookPure Mathematics Unit 2 - Textbook
Pure Mathematics Unit 2 - Textbook
Rushane Barnes
 
math m1
math m1math m1
math m1
ZoRo Lossless
 
Coordinate Graph1
Coordinate Graph1Coordinate Graph1
Coordinate Graph1
Ruth Pridgeon
 
CAPE PURE MATHEMATICS UNIT 2 MODULE 1 PRACTICE QUESTIONS
CAPE PURE MATHEMATICS UNIT 2 MODULE 1 PRACTICE QUESTIONSCAPE PURE MATHEMATICS UNIT 2 MODULE 1 PRACTICE QUESTIONS
CAPE PURE MATHEMATICS UNIT 2 MODULE 1 PRACTICE QUESTIONS
Carlon Baird
 
Algebra 2 Section 1-8
Algebra 2 Section 1-8Algebra 2 Section 1-8
Algebra 2 Section 1-8
Jimbo Lamb
 
CAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONS
CAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONSCAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONS
CAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONS
Carlon Baird
 
Application of subQuan to Algebra: 3rd-8th grade and beyond...
Application of subQuan to Algebra: 3rd-8th grade and beyond...Application of subQuan to Algebra: 3rd-8th grade and beyond...
Application of subQuan to Algebra: 3rd-8th grade and beyond...
Dream Realizations
 
Lesson 4.3 regular
Lesson 4.3 regularLesson 4.3 regular
Lesson 4.3 regular
morrobea
 
Algebra 2 Section 1-6
Algebra 2 Section 1-6Algebra 2 Section 1-6
Algebra 2 Section 1-6
Jimbo Lamb
 

What's hot (20)

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

Similar to Oct8 - 131 slid

2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
Education Front
 
There are two types of ciphers - Block and Stream. Block is used to .docx
There are two types of ciphers - Block and Stream. Block is used to .docxThere are two types of ciphers - Block and Stream. Block is used to .docx
There are two types of ciphers - Block and Stream. Block is used to .docx
relaine1
 
3D Math Without Presenter Notes
3D Math Without Presenter Notes3D Math Without Presenter Notes
3D Math Without Presenter Notes
Janie Clayton
 
LeetCode Solutions In Java .pdf
LeetCode Solutions In Java .pdfLeetCode Solutions In Java .pdf
LeetCode Solutions In Java .pdf
zupsezekno
 
tick cross game
tick cross gametick cross game
tick cross game
sanobersheir
 
Sorting ppt
Sorting pptSorting ppt
Sorting ppt
Hassan Mustafa
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!
Paige Bailey
 
Efficient realization for geometric transformation of digital images in run l...
Efficient realization for geometric transformation of digital images in run l...Efficient realization for geometric transformation of digital images in run l...
Efficient realization for geometric transformation of digital images in run l...
Shlomo Pongratz
 
7th maths-1.concept , addition and subtraction properties of intergers-sangh
7th maths-1.concept , addition and subtraction properties of intergers-sangh7th maths-1.concept , addition and subtraction properties of intergers-sangh
7th maths-1.concept , addition and subtraction properties of intergers-sangh
LiveOnlineClassesInd
 
7th maths-1.concept , addition and subtraction properties of intergers
7th maths-1.concept , addition and subtraction properties of intergers7th maths-1.concept , addition and subtraction properties of intergers
7th maths-1.concept , addition and subtraction properties of intergers
LiveOnlineClassesInd
 
Rabotna tetratka 5 odd
Rabotna tetratka 5 oddRabotna tetratka 5 odd
Rabotna tetratka 5 odd
Mira Trajkoska
 
Shape logic 1
Shape logic 1Shape logic 1
Shape logic 1
Michael Gordon
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
Dr. Md. Shohel Sayeed
 
UNIT-1_CSA.pdf
UNIT-1_CSA.pdfUNIT-1_CSA.pdf
UNIT-1_CSA.pdf
ShabbeerBasha8
 
Robotic Manipulator with Revolute and Prismatic Joints
Robotic Manipulator with Revolute and Prismatic JointsRobotic Manipulator with Revolute and Prismatic Joints
Robotic Manipulator with Revolute and Prismatic Joints
Travis Heidrich
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
Creating a Custom Serialization Format (Gophercon 2017)
Creating a Custom Serialization Format (Gophercon 2017)Creating a Custom Serialization Format (Gophercon 2017)
Creating a Custom Serialization Format (Gophercon 2017)
Scott Mansfield
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Abhilash Nair
 
Lecture
LectureLecture
Lecture
Ahmed Atallah
 
Basic arithmetic, instruction execution and program
Basic arithmetic, instruction execution and programBasic arithmetic, instruction execution and program
Basic arithmetic, instruction execution and program
JyotiprakashMishra18
 

Similar to Oct8 - 131 slid (20)

2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
There are two types of ciphers - Block and Stream. Block is used to .docx
There are two types of ciphers - Block and Stream. Block is used to .docxThere are two types of ciphers - Block and Stream. Block is used to .docx
There are two types of ciphers - Block and Stream. Block is used to .docx
 
3D Math Without Presenter Notes
3D Math Without Presenter Notes3D Math Without Presenter Notes
3D Math Without Presenter Notes
 
LeetCode Solutions In Java .pdf
LeetCode Solutions In Java .pdfLeetCode Solutions In Java .pdf
LeetCode Solutions In Java .pdf
 
tick cross game
tick cross gametick cross game
tick cross game
 
Sorting ppt
Sorting pptSorting ppt
Sorting ppt
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!
 
Efficient realization for geometric transformation of digital images in run l...
Efficient realization for geometric transformation of digital images in run l...Efficient realization for geometric transformation of digital images in run l...
Efficient realization for geometric transformation of digital images in run l...
 
7th maths-1.concept , addition and subtraction properties of intergers-sangh
7th maths-1.concept , addition and subtraction properties of intergers-sangh7th maths-1.concept , addition and subtraction properties of intergers-sangh
7th maths-1.concept , addition and subtraction properties of intergers-sangh
 
7th maths-1.concept , addition and subtraction properties of intergers
7th maths-1.concept , addition and subtraction properties of intergers7th maths-1.concept , addition and subtraction properties of intergers
7th maths-1.concept , addition and subtraction properties of intergers
 
Rabotna tetratka 5 odd
Rabotna tetratka 5 oddRabotna tetratka 5 odd
Rabotna tetratka 5 odd
 
Shape logic 1
Shape logic 1Shape logic 1
Shape logic 1
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
UNIT-1_CSA.pdf
UNIT-1_CSA.pdfUNIT-1_CSA.pdf
UNIT-1_CSA.pdf
 
Robotic Manipulator with Revolute and Prismatic Joints
Robotic Manipulator with Revolute and Prismatic JointsRobotic Manipulator with Revolute and Prismatic Joints
Robotic Manipulator with Revolute and Prismatic Joints
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Creating a Custom Serialization Format (Gophercon 2017)
Creating a Custom Serialization Format (Gophercon 2017)Creating a Custom Serialization Format (Gophercon 2017)
Creating a Custom Serialization Format (Gophercon 2017)
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Lecture
LectureLecture
Lecture
 
Basic arithmetic, instruction execution and program
Basic arithmetic, instruction execution and programBasic arithmetic, instruction execution and program
Basic arithmetic, instruction execution and program
 

Recently uploaded

basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
yokeleetan1
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
Mukeshwaran Balu
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 

Recently uploaded (20)

basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 

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
  • 16.
  • 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).
  • 24.
  • 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.
  • 28.
  • 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