Top Banner
Oct 27 th Lab12. String methods Debugging http://www.slideshare.net/takyeon
57

Oct27

Jul 02, 2015

Download

Engineering

Tak Lee

lab slide Oct27
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Oct27

Oct 27th

Lab12.String methods

Debugging

http://www.slideshare.net/takyeon

Page 2: Oct27

String methods

String str = "studytonight";

System.out.println(str.charAt(2)); // => u

charAt(int i) returns the character at the index i position

String a = "a";

String b = "b";

System.out.println(a.compareTo(b)); // => -1

System.out.println(b.compareTo(a)); // => 1

System.out.println(a.compareTo(a)); // => 0

a.compareTo(b) compares a and b lexicographically.

Page 3: Oct27

String methods

String str = "The quick brown fox jumps over the lazy dog";

System.out.println(str.indexOf('u')); // => 5

System.out.println(str.indexOf('&')); // => -1

System.out.println(str.indexOf(' ')); // => 3

System.out.println(str.indexOf(' ',3)); // => 3

System.out.println(str.indexOf(' ',4)); // => 9

a.indexOf(int ch) returns the first index of ch within a

a.indexOf(String s) returns the first index of s within a

a.indexOf(int ch, int fromIndex)

returns the first index of ch within a, starting search at the fromIndex

Page 4: Oct27

String methods

String str = "1-2-3-4-5";

System.out.println(str.replace('-',' ')); // => "1 2 3 4 5"

a.replace(char oldCh, char newCh)

returns a new string resulting from replacing all oldCh in a with newCh

String str = "aaaaa";

System.out.println(str.replace('a','b')); //=> "bbbbb"

System.out.println(str.replace("aa","b")); //=> "bba"

System.out.println(str.replace("aaa","a")); //=> "aaa"

a.replace(CharSequence o, CharSequence n)

returns a new string resulting from replacing all o in a with n

Can be CharBuffer, Segment, String, StringBuffer, StringBuilder

Newly added string will not be evaluated.

Page 5: Oct27

String methods

String str = "0-2-4-6-8";

System.out.println(str.substring(4)); // => "4-6-8"

a.substring(int beginIndex)

returns a new string from beginIndex till the end

String str = "0-2-4-6-8";

System.out.println(str.substring(4,8)); // => "4-6-"

a.substring(int beginIndex, int endIndex)

returns a new string from beginIndex (inclusive) till endIndex (exclusive)

Page 6: Oct27

http://codingbat.com/java

Page 7: Oct27

DebuggingTrace program's behavior line-by-line

Step 1. Set "break point" at the suspicious line

Step 2. Run the debugger

next line into

Step 3.

- Examine the contents of memory- Decide how to proceed with running/debugging the program by either stepping "over", "into", or "out of" code segments

out of

Page 8: Oct27

Debugging

Conditional breakpoint

Page 9: Oct27

Oct 20th

Lab11.Mid-term #1 review

Javadoc

http://www.slideshare.net/takyeon

Page 10: Oct27

Midterm review

Regrading request

- Write it on a separate sheet of paper

- Hand it in to professor with your exam.

- The entire exam will be regraded.

Page 11: Oct27

- Will our culture really be defined by interfaces?

- What "uses" are there for information visualization interms of explaining things to the general public.

- Mechanical Turk* ethics (pay is below US minimum wage)* what would they want to do using Mechanical Turk* origins of the name (interesting con around a chess playing

machine around 200 years ago)

- Art and Computer Science* can CS people create art? can Art people create CS?* what ideas do they have in terms of CS changing the Art world* ask if any of them have been to the 3rd floor of CSIC and seen

the Treemap art gallery

Page 12: Oct27

Javadoc: Java Documentation Comments

Javadoc is a tool which comes with JDK and it is used for generating Java code documentation in HTML format from Java source code which has required documentation in a predefined format.

source code

generated documentation web page

block comment that starts with /**

@tagName to add special tags

Page 13: Oct27

Javadoc: Java Documentation Comments

@param Adds a parameter with the specified parameter-name followed by the specified description to the "Parameters" section.@return Adds a "Returns" section with the description text. @return description@see Adds a "See Also" heading with a link or text entry that points to reference.@throws Adds a Throws subheading to the generated documentation, with the class-name and description text..

Page 14: Oct27

Oct 13th

Lab09.Stack, Heap, and Metaspace

http://www.slideshare.net/takyeon

Page 15: Oct27

public class Student {

public String name;

public int tokenLevel;

private static int currentCount = 0;

private static final int DEFAULT_TOKENS = 3;

public Student() {

name = "unknown";

tokenLevel = DEFAULT_TOKENS;

currentCount++;

}

}

Student s1 = new Student();

s1.name = "Tak";

STACK HEAP META SPACE

DEFAULT_TOKENS

0

3

currentCount 1 Xs1

name

tokenLevel 3

"unknown"

"Tak"

constructor

All static fields of a class live in Metaspace.

All local variables (both primitives and references) live on the stack. Non-primitive data objects are stored in the heap, and referenced by variables on the stack.

All non-primitive objects live on the heap.Primitive instance variables of those objects are stored inside the object. Non-primitive variables are stored outside of the object, and referenced by the instance variable.

Page 16: Oct27

Size? 5

*

***

*****

*******

*********

Scanner sc = new Scanner(System.in);

int size = sc.nextInt();

for(int row=0; row<size; row++) {

for(int col=0;col<4-row;col++) {

System.out.print(" ");

}

for(int col=0;col<row*2+1;col++) {

System.out.print("*");

}

System.out.println();

}

sc.close();

row col : 1st

spaces before *

col : 2nd

number of *

0 4 1

1 3 3

2 2 5

3 1 7

4 0 9

Page 17: Oct27

int x,y;

x=2; y=5;

System.out.println(x++ * y++);

System.out.println(++x * ++y);

System.out.println(++x * y++);

System.out.println(x++ * ++y);

System.out.println("1 + 2 = " + 1 + 2);System.out.println("1 + 2 = " + (1 + 2));

1 + 2 = 121 + 2 = 3

year % 4 == 0 && year % 100 != 0 || year % 400 == 0

((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)

Page 18: Oct27

Any question?

Page 19: Oct27

Oct 8th

Lab08.

Quiz review

Triangle and Stripes

http://www.slideshare.net/takyeon

Page 20: Oct27

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 21: Oct27

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 22: Oct27

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 23: Oct27

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 24: Oct27

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 25: Oct27

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 26: Oct27

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 27: Oct27

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 28: Oct27

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 29: Oct27

(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 30: Oct27

Oct 6th

Lab07.

Loop applications

Page 31: Oct27

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 32: Oct27

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 33: Oct27

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 34: Oct27
Page 35: Oct27

Oct 1st

Lab06.

2D drawing

Page 36: Oct27

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 37: Oct27

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 38: Oct27

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 39: Oct27

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 40: Oct27

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 41: Oct27

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 42: Oct27
Page 43: Oct27

Sep 29th

Lab05.1. Recap the quiz #1

2. String Class

3. static vs. instance method

Page 44: Oct27

Recap quiz #1.

PRINT your names in the grade server. NO nicknames.

Page 45: Oct27

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 46: Oct27
Page 47: Oct27

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 48: Oct27

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 49: Oct27

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 50: Oct27

Sep 24th

Lab04.Loop

Page 51: Oct27

Flow of Control

1. Top-to-bottom statements

2. Method calls

3. Conditional statements

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

Page 52: Oct27

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 53: Oct27

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 54: Oct27

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 55: Oct27

iterartorline 1

0

valueline 2

0

targetline 4

Page 56: Oct27

answerline 1

1

iline 1

1

jline 1

0

Page 57: Oct27