lecture 3 strings screen output and debugging l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Lecture 3: Strings, Screen Output, and Debugging PowerPoint Presentation
Download Presentation
Lecture 3: Strings, Screen Output, and Debugging

Loading in 2 Seconds...

play fullscreen
1 / 12

Lecture 3: Strings, Screen Output, and Debugging - PowerPoint PPT Presentation


  • 87 Views
  • Uploaded on

Lecture 3: Strings, Screen Output, and Debugging. Yoni Fridman 7/2/01. Outline. Methods Math class methods Screen output Escape sequences Strings String methods String concatenation Debugging – Types of errors Fixing errors. Methods.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Lecture 3: Strings, Screen Output, and Debugging' - hop


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
outline
Outline
  • Methods
    • Math class methods
  • Screen output
    • Escape sequences
  • Strings
    • String methods
    • String concatenation
  • Debugging – Types of errors
    • Fixing errors
methods
Methods
  • 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.
math class methods
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.
  • <Code Example>
screen output
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 (/).
  • <Code Example>
escape sequences
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?
  • <Code Example>
strings
Strings
  • 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?
  • <Code example>
string methods

n

i

Y

o

0

1

2

3

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”.
string concatenation
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?
  • <Code Example>
debugging types of errors
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.
fixing errors
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.
    • <Code Example>
  • 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.
    • <Code Example>
  • Test your program carefully before turning it in.
    • Choose random test data, as well as boundary and special test data.
homework
Homework
  • 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.