39
Oct 8 th Lab08. Quiz review Triangle and Stripes http://www.slideshare.net/takyeon

Oct8 - 131 slid

  • Upload
    tak-lee

  • View
    310

  • Download
    6

Embed Size (px)

DESCRIPTION

aaa

Citation preview

Page 1: Oct8 - 131 slid

Oct 8th

Lab08.

Quiz review

Triangle and Stripes

http://www.slideshare.net/takyeon

Page 2: Oct8 - 131 slid

Quiz review

No

i++

++i

Use and then increase

int i = 3;

int a = i++; // a = 3, i = 4

int b = ++a; // b = 4, a = 4

Increase and then use

Page 3: Oct8 - 131 slid

Quiz review

maxCount 100

str

"Hello"

"HELLO"

• 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, …

• Data in HEAP are not immediately deleted but unlinked, and will be garbage-collected.

Page 4: Oct8 - 131 slid

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

}

}

Page 5: Oct8 - 131 slid

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

Page 6: Oct8 - 131 slid

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,4M2. 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

M1. Iterate pixels to paint

Very difficult

Now you want to paint all the pixels except the diagonal line.

Page 7: Oct8 - 131 slid

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

More examples.

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

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

Diagonal shapes require both row

and col

Linear shapes require either row or col.

Page 8: Oct8 - 131 slid

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)

(row+1)-col >= 0

-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,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

To move a shape to left by 1 pixel,

replace "row" with "row+1"

Page 9: Oct8 - 131 slid

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

HorizontalFlip

(row, 4-col)

(row-(4-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)

To flip a shape, multiple row or column by -1, and add size

-col

size-col

col

col

4 := size of the shape – 1Why -1? Because our row and

col index started from 0.

Page 10: Oct8 - 131 slid

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

VerticalFlip

(4-row, col)

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

(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

Transformation > Vertical Flip.

Page 11: Oct8 - 131 slid

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

HorizontalFlip

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

Vertical flip

(4-row)-(4-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

HorizontalFlip

col-row >= 0

Vertical flip

Page 12: Oct8 - 131 slid

Oct 6th

Lab07.

Loop applications

Page 13: Oct8 - 131 slid

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

}

}

}

Finding common factors of two numbers

Common factors can divide both numbers.E.g. Common factors of 9 and 12 1 and 3

Common factors of 24 and 78 1, 2, 3, and 6

Page 14: Oct8 - 131 slid

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

Page 15: Oct8 - 131 slid

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 words3) Print out

Page 16: Oct8 - 131 slid
Page 17: Oct8 - 131 slid

Oct 1st

Lab06.

2D drawing

Page 18: Oct8 - 131 slid

SquareGrid.java

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

Drawing shapes on 2D grid

Page 19: Oct8 - 131 slid

Single loop for drawing a line

1) How can we get the middle row number?0

size : 7

3

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

int size = grid.getHt();int midRow = size / 2;

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

}

Page 20: Oct8 - 131 slid

Single loop for drawing a line

1) How can we get the middle column number?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

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.

Page 21: Oct8 - 131 slid

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

}

Page 22: Oct8 - 131 slid

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

Page 23: Oct8 - 131 slid

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

Page 24: Oct8 - 131 slid
Page 25: Oct8 - 131 slid

Sep 29th

Lab05.1. Recap the quiz #1

2. String Class

3. static vs. instance method

Page 26: Oct8 - 131 slid

Recap quiz #1.

PRINT your names in the grade server. NO nicknames.

Page 27: Oct8 - 131 slid

Recap quiz #1.

Use specific technical keywordse.g. What does CVS “do” for us?

1. Check out / Download the starter files2. Store / Save multiple versions of the source codeshare, deliver, get, access, connected people, ...

Penalties for inaccurate extra infoe.g. CVS runs our code and give us grades. -1 for incorrect extra info.

Page 28: Oct8 - 131 slid
Page 29: Oct8 - 131 slid

String Class

String s = “hello”;

Create a new String object with an initial value “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”

Page 30: Oct8 - 131 slid

int type vs. Integer Class

int i=0;

Primitive data type

Integer i = 17;

Wrapper Class

don’t have much method provide methods

- convert to string

- generate hash codes

Faster A little slower

Page 31: Oct8 - 131 slid

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

Page 32: Oct8 - 131 slid

Sep 24th

Lab04.Loop

Page 33: Oct8 - 131 slid

Flow of Control

1. Top-to-bottom statements

2. Method calls

3. Conditional statements

4. Iteration (loop)for, while, ...

Page 34: Oct8 - 131 slid

Two goals of iteration

1. AutomationReduce 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. AbstractionCode for various situations

System.out.println(“****”);

How can we print n-number of “*”?

Page 35: Oct8 - 131 slid

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

Level 2. Draw 30 by 10 rectangle (single-loop)

Too many copy & paste. Hard to modify.

int row=0;

while(row<30) {

System.out.println(“**********”);

row++;

}

A little more compact. Still too many * for each line.

Page 36: Oct8 - 131 slid

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

Page 37: Oct8 - 131 slid

iterartorline 1

0

valueline 2

0

targetline 4

Page 38: Oct8 - 131 slid

answerline 1

1

iline 1

1

jline 1

0

Page 39: Oct8 - 131 slid