midterm common mistakes
TRANSCRIPT
Review
CST200 – Week 7: Common Mistakes in the First two Midterms
Instructor: Andreea Molnar
Outline
•Division
•Escape Sequences
•String Methods
Division
% (remainder operator, modulus operator) - returns the remainder after dividing the second operand to the first
19 % 4 = 3
4 % 9 = 4
Division
% - the sign of the result is the sign of the numerator (first operand)
-10 % 3 = -1
10 % -3 = 1
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
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
Escape SequencesEscape sequence Meaning
\b backspace
\t tab
\n newline
\r return
\’’ double quote
\’ single quote
\\ backslash
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
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
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
Best of luck with the exam!