midterm common mistakes

11
Review CST200 – Week 7: Common Mistakes in the First two Midterms Instructor: Andreea Molnar

Upload: asu-online

Post on 17-May-2015

968 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Midterm common mistakes

Review

CST200 – Week 7: Common Mistakes in the First two Midterms

Instructor: Andreea Molnar

Page 2: Midterm common mistakes

Outline

•Division

•Escape Sequences

•String Methods

Page 3: Midterm common mistakes

Division

% (remainder operator, modulus operator) - returns the remainder after dividing the second operand to the first

19 % 4 = 3

4 % 9 = 4

Page 4: Midterm common mistakes

Division

% - the sign of the result is the sign of the numerator (first operand)

-10 % 3 = -1

10 % -3 = 1

Page 5: Midterm common mistakes

Division

/ - results depend on the type of the operand

• if both operands are integers (byte, short, int, long) the result is integer, any fractional part of the result is discarded

• if either or both operands are floating point (float, double) the result is floating point

Page 6: Midterm common mistakes

Division14 / 5 = 2

14.0 / 5 = 2.8

14 / 5.0 = 2.8

14.0 / 5.0 = 2.8

(double) 14 / 5 = 2.8

(double) (14 / 5) = 2.0 // in this case the integer division is performed first and afterwards the type casting

Page 7: Midterm common mistakes

Escape SequencesEscape sequence Meaning

\b backspace

\t tab

\n newline

\r return

\’’ double quote

\’ single quote

\\ backslash

Page 8: Midterm common mistakes

Escape SequencesString firstName = "Mary";

String lastName = "Smith";

System.out.println(firstName + "\t" + lastName);

Mary Smith

System.out.println(firstName + "\n" + lastName);

Mary

Smith

System.out.println(firstName + "\"" + lastName);

Mary"Smith

System.out.println(firstName + "\\" + lastName);

Mary\Smith

Page 9: Midterm common mistakes

String Methods

String substring(int beginIndex, int endIndex)

returns a substring that begins at the beginIndex and ends at the endIndex -1

String str = “Arizona State University”;

str.substring (3, 5); //substring that begins at the index 3 (z) and ends at the 5-1 (o) zo

Page 10: Midterm common mistakes

String Methods

String indexOf (String str)

returns the index of the first occurrence of str

if multiple occurrences returns just the first one

String str = “Arizona State University”;

str.substring (“i”); //2

Page 11: Midterm common mistakes

Best of luck with the exam!