Download presentation
Presentation is loading. Please wait.
1
Lecture 3: Strings, Screen Output, and Debugging Yoni Fridman 7/2/01 7/2/01
2
OutlineOutline ä Methods ä Math class methods ä Screen output ä Escape sequences ä Strings ä String methods ä String concatenation ä Debugging – Types of errors ä Fixing errors ä Methods ä Math class methods ä Screen output ä Escape sequences ä Strings ä String methods ä String concatenation ä Debugging – Types of errors ä Fixing errors
3
MethodsMethods ä A method is a block of related statements that can be executed as a unit. ä A large program is normally made up of many methods, each of which contains one algorithm. ä To execute the block of statements, the method must be called. ä This is done by writing the name of the method followed by a pair of parentheses. ä Any arguments to the method go inside the parentheses. Example: System.out.println(“Hello”); System.out.println is the method name, “Hello” is the argument. ä A method is a block of related statements that can be executed as a unit. ä A large program is normally made up of many methods, each of which contains one algorithm. ä To execute the block of statements, the method must be called. ä This is done by writing the name of the method followed by a pair of parentheses. ä Any arguments to the method go inside the parentheses. Example: System.out.println(“Hello”); System.out.println is the method name, “Hello” is the argument.
4
Math Class Methods ä Some methods return a value after they execute. ä Methods are grouped into classes. ä Let’s look at some methods in the Math class: Math.pow(2.0, 3.0) returns 8.0. Math.sqrt(4.0) returns 2.0. Math.abs(-2.0) returns 2.0. Math.max(1.0, 2.0) returns 2.0. Math.min(1.0, 2.0) returns 1.0. Math.round(1.7) returns 2. ä ä ä Some methods return a value after they execute. ä Methods are grouped into classes. ä Let’s look at some methods in the Math class: Math.pow(2.0, 3.0) returns 8.0. Math.sqrt(4.0) returns 2.0. Math.abs(-2.0) returns 2.0. Math.max(1.0, 2.0) returns 2.0. Math.min(1.0, 2.0) returns 1.0. Math.round(1.7) returns 2. ä ä
5
Screen Output As we’ve seen, println() is a method that outputs its argument to the screen. ä The argument can be any expression of any type. print() is a similar method, but unlike println(), it does not advance to the next line. ä What about printing a blank line? Two ways: Don’t pass any argument to the method – System.out.println(); Use the escape sequence “\n” – System.out.println(“Hello\n”); ä Make sure not to use a forward slash (/). ä ä As we’ve seen, println() is a method that outputs its argument to the screen. ä The argument can be any expression of any type. print() is a similar method, but unlike println(), it does not advance to the next line. ä What about printing a blank line? Two ways: Don’t pass any argument to the method – System.out.println(); Use the escape sequence “\n” – System.out.println(“Hello\n”); ä Make sure not to use a forward slash (/). ä ä
6
Escape Sequences ä An escape sequence is a combination of characters that represent a single character. ä The escape sequence \n represents the new-line character. ä Java escape sequences start with the backslash (\). ä \" represents a double quote ("). ä Why is this necessary? ä \\ represents a backslash (\). ä Why is this necessary? ä ä ä An escape sequence is a combination of characters that represent a single character. ä The escape sequence \n represents the new-line character. ä Java escape sequences start with the backslash (\). ä \" represents a double quote ("). ä Why is this necessary? ä \\ represents a backslash (\). ä Why is this necessary? ä ä
7
StringsStrings Since the argument to println() can be of any type, what type is “Hello”? It’s not an int, a double, or a boolean. “Hello” belongs to a special type called String, which has its own methods. ä Strings are declared and initialized like any other type. Example: String myName = “Yoni”; ä Notice that the value must be in quotes. Why? ä ä Since the argument to println() can be of any type, what type is “Hello”? It’s not an int, a double, or a boolean. “Hello” belongs to a special type called String, which has its own methods. ä Strings are declared and initialized like any other type. Example: String myName = “Yoni”; ä Notice that the value must be in quotes. Why? ä ä
8
String Methods ä A string can be thought of as a numbered series of characters, starting from 0: ä The string methods take advantage of this: Assume myName stores the String “Yoni”. myName.indexOf(“ni”) returns 2. myName.indexOf(“Hi”) returns -1 (not found). myName.length() returns 4. myName.substring(1, 3) returns “on”. myName.toLowerCase() returns “yoni”. myName.toUpperCase() returns “YONI”. ä A string can be thought of as a numbered series of characters, starting from 0: ä The string methods take advantage of this: Assume myName stores the String “Yoni”. myName.indexOf(“ni”) returns 2. myName.indexOf(“Hi”) returns -1 (not found). myName.length() returns 4. myName.substring(1, 3) returns “on”. myName.toLowerCase() returns “yoni”. myName.toUpperCase() returns “YONI”. Yo ni 0123
9
String Concatenation ä Strings can also be concatenated (joined together). ä Strings are concatenated using the + operator: “Yoni” + “Fridman” gives the string “YoniFridman”. “Yoni” + “ ” + “Fridman” gives the string “Yoni Fridman”. myName + “ ” + “Fridman” gives the string “Yoni Fridman”. What does myName += “Fridman” do? ä Other types can also be concatenated to strings: Example: “Two = ” + 2 gives the string “Two = 2”. Beware: 1 + 2 + “Hello” gives the string “3Hello”. Why? What does 2 + “” do? ä ä ä Strings can also be concatenated (joined together). ä Strings are concatenated using the + operator: “Yoni” + “Fridman” gives the string “YoniFridman”. “Yoni” + “ ” + “Fridman” gives the string “Yoni Fridman”. myName + “ ” + “Fridman” gives the string “Yoni Fridman”. What does myName += “Fridman” do? ä Other types can also be concatenated to strings: Example: “Two = ” + 2 gives the string “Two = 2”. Beware: 1 + 2 + “Hello” gives the string “3Hello”. Why? What does 2 + “” do? ä ä
10
Debugging – Types of Errors ä Debugging is the process of finding bugs (errors) in a program and fixing them. ä There are three types of errors: ä Compile-time error: This is an error that the compiler finds and tells you about when you try to compile a program. ä Run-time error: This is an error that the interpreter finds while a program is running, and that makes the program crash. ä A run-time error is also known as an exception. ä Incorrect behavior: This is an error that isn’t found by the compiler or by the interpreter. The program runs, but doesn’t give the results you intended. ä Debugging is the process of finding bugs (errors) in a program and fixing them. ä There are three types of errors: ä Compile-time error: This is an error that the compiler finds and tells you about when you try to compile a program. ä Run-time error: This is an error that the interpreter finds while a program is running, and that makes the program crash. ä A run-time error is also known as an exception. ä Incorrect behavior: This is an error that isn’t found by the compiler or by the interpreter. The program runs, but doesn’t give the results you intended.
11
Fixing Errors ä If the compiler finds a compile-time error: ä It will give you an error message – read the message carefully before trying to fix the error. ä ä ä If the interpreter finds a run-time error: ä It will also give you an error message, but these are less common. ä If you’re getting incorrect behavior: Use println() to track the values of relevant variables. ä ä ä Test your program carefully before turning it in. ä Choose random test data, as well as boundary and special test data. ä If the compiler finds a compile-time error: ä It will give you an error message – read the message carefully before trying to fix the error. ä ä ä If the interpreter finds a run-time error: ä It will also give you an error message, but these are less common. ä If you’re getting incorrect behavior: Use println() to track the values of relevant variables. ä ä ä Test your program carefully before turning it in. ä Choose random test data, as well as boundary and special test data.
12
HomeworkHomework ä Read: 4.1 (except for last two subsections), 4.2, 4.3, 4.4, 4.5, 8.2. ä HW2 out today, due Friday. ä Compatibility Calculator. ä Read: 4.1 (except for last two subsections), 4.2, 4.3, 4.4, 4.5, 8.2. ä HW2 out today, due Friday. ä Compatibility Calculator.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.