Lecture 3: Strings, Screen Output, and Debugging Yoni Fridman 7/2/01 7/2/01.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Introduction to Python
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Introduction to Methods
Georgia Institute of Technology Introduction to Java part 2 Barb Ericson Georgia Institute of Technology May 2006.
2.2 Information on Program Appearance and Printing.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
The Java Programming Language
Program Statements Primitive Data Types and Strings.
CSC Programming I Lecture 5 August 30, 2002.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
CSE 131 Computer Science 1 Module 1: (basics of Java)
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
CSC Programming I Lecture 6 September 4, 2002.
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
Chapter 2 part #1 C++ Program Structure
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Chapter 2 Variables.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Lab 01-2 Objectives:  Writing a Java program.  How to send output to the command line console.  Learn about escape sequences.  Learn how to compile,
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
Chapter 2 print / println String Literals Escape Characters Variables / data types.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
Controlling Program Flow with Decision Structures.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Chapter 2 1.What is the difference between print / println 2.What are String Literals 3.What are the Escape Characters for backslash, double quotataions,
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CS 106A, Lecture 4 Introduction to Java
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Dept of Computer Science University of Maryland College Park
CSC201: Computer Programming
String class.
Chapter 2, Part I Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Lecture Note Set 1 Thursday 12-May-05
Chapter 2 – Getting Started
Introduction to Java part 2
Java Tutotrial for [NLP-AI] 2
CMSC 202 Java Primer 2.
Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes
Variables, Data Types & Math
Introduction to Java part 2
Variables, Data Types & Math
Unit 3: Variables in Java
Output Manipulation.
Loops CGS3416 Spring 2019 Lecture 7.
Chapter 1 c++ structure C++ Input / Output
Instructor: Alexander Stoytchev
Presentation transcript:

Lecture 3: Strings, Screen Output, and Debugging Yoni Fridman 7/2/01 7/2/01

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

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.

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. ä ä

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 (/). ä ä

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? ä ä

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? ä ä

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

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: “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: “Hello” gives the string “3Hello”. Why?  What does 2 + “” do? ä ä

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.

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.

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.