CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051//f13/

Slides:



Advertisements
Similar presentations
CSC 1051 – Algorithms and Data Structures I
Advertisements

Basic Syntax: Data & Expressions
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
© 2004 Pearson Addison-Wesley. All rights reserved1-1 Intermediate Java Programming Lory Al Moakar.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Program Statements Primitive Data Types and Strings.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2: Java Fundamentals
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Chapter 2 Data and Expressions Part One. © 2004 Pearson Addison-Wesley. All rights reserved2-2/29 Data and Expressions Let's explore some other fundamental.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
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.
Chapter 2 Data and Expressions Java Software Solutions Foundations of Program Design 1.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Object Oriented Programming Idea Computer program may be seen as comprising a collection of objects Object Fundamental entity in a JAVA program Used to.
Intermediate Java Programming
CSC 1051 – Data Structures and Algorithms I
CSC 1051 – Data Structures and Algorithms I
Chapter 2 Data and Expressions
Introduction to Objects
Escape Sequences What if we wanted to print the quote character?
Data and Expressions Part One
Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes
Module 2 - Part 1 Variables, Assignment, and Data Types
Module 2 - Part 1 Variables, Assignment, and Data Types
CSC 1051 – Data Structures and Algorithms I
Instructor: Alexander Stoytchev
Java Programming Presented by Dr. K. SATISH KUMAR,
© A+ Computer Science - Basic Java © A+ Computer Science -
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Module 2 - Part 1 Variables, Assignment, and Data Types
Introduction to Objects
Presentation transcript:

CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus and from Daniel Joyce’s slides for CSC1051. CSC 1051 M.A. Papalaskari, Villanova University 2: Basics of Java Programming - Strings and Printing

Lab 1: Learn about jGrasp - the programming environment that we will be using in this class –Compile …. and run a java program Understand the relationship between a Java class name and the name of the.java file where the class is defined Practice using basic Java output statements and adding comments Learn about variables, string literals, concatenation. E.g., System.out.println ("Howdy " + name); System.out.println ("The answer is " + x); System.out.print ("Counting... up: " + (count + 1)); System.out.println ("... and\n... down: " + (count - 1)); Explore Java syntax Experience some errors! CSC 1051 M.A. Papalaskari, Villanova University

Character Strings A string literal is represented by putting double quotes around the text Examples: "This is a string literal." "123 Main Street" "X" CSC 1051 M.A. Papalaskari, Villanova University

Character Strings A string literal is represented by putting double quotes around the text Examples: "This is a string literal." "123 Main Street" "X" Every character string is an object in Java, defined by the String class CSC 1051 M.A. Papalaskari, Villanova University spaces matter in here!

The println Method In the Lincoln program we invoked the println method to print a character string The System.out object represents a destination (the monitor screen) to which we can send output System.out.println ("Whatever you are, be a good one."); object method name information provided to the method (parameters) CSC 1051 M.A. Papalaskari, Villanova University

The print Method In the Lincoln program we invoked the println method to print a character string The System.out object represents a destination (the monitor screen) to which we can send output print is similar to the println except that it does not advance to the next line System.out.print ("Whatever you are, be a good one."); object method name information provided to the method (parameters) CSC 1051 M.A. Papalaskari, Villanova University

String Concatenation The string concatenation operator (+) is used to append one string to the end of another "And one more " + "thing" CSC 1051 M.A. Papalaskari, Villanova University

Hands on: Use MyQuote.java as a starting point (program from Lab 1), focus on this part of the code: System.out.println ("Howdy " + name); System.out.println ("The answer is " + x); System.out.print ("Counting... up: " + (count + 1)); System.out.println ("... and\n... down: " + (count - 1)); Try the following: 1)What if you remove the parentheses around (count + 1)? 2)What happens if we try this way of breaking a line: System.out.print ("Counting... up: " + (count + 1)); 3) How can we get all this output to print all in one line? Other examples (textbook): Countdown.java Facts.java Countdown.java Facts.java CSC 1051 M.A. Papalaskari, Villanova University

Escape Sequences What if we wanted to print the quote character? Let’s try something like this… System.out.println ("I said "Hello" to you."); An escape sequence is a series of characters that represents a special character An escape sequence begins with a backslash character ( \ ) System.out.println ("I said \"Hello\" to you."); CSC 1051 M.A. Papalaskari, Villanova University

Escape Sequences Some Java escape sequences: Escape Sequence \b \t \n \r \" \' \\ Meaning backspace tab newline carriage return double quote single quote backslash CSC 1051 M.A. Papalaskari, Villanova University

//******************************************************************** // Roses.java Author: Lewis/Loftus // // Demonstrates the use of escape sequences. //******************************************************************** public class Roses { // // Prints a poem (of sorts) on multiple lines. // public static void main (String[] args) { System.out.println ("Roses are red,\n\tViolets are blue,\n" + "Sugar is sweet,\n\tBut I have \"commitment issues\",\n\t" + "So I'd rather just be friends\n\tAt this point in our " + "relationship."); } Output Roses are red, Violets are blue, Sugar is sweet, But I have "commitment issues", So I'd rather just be friends At this point in our relationship. Example from textbook: Roses.java Roses.java

Quick Check CSC 1051 M.A. Papalaskari, Villanova University Write a single println statement that produces the following output: "Thank you all for coming to my home tonight," he said mysteriously.