2.2 Information on Program Appearance and Printing.

Slides:



Advertisements
Similar presentations
This is Java Jeopardy.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Begin Java Pepper. Objectives What is a program? Learn the basics of your programming tool: BlueJ Write a first Java program and see it run Make some.
IT151: Introduction to Programming
Your First Java Program: HelloWorld.java
CMT Programming Software Applications
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
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,
CS 106 Introduction to Computer Science I 09 / 11 / 2006 Instructor: Michael Eckmann.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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:
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Unit 3: Java Data Types Math class and String class.
Program Statements Primitive Data Types and Strings.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2: Java Fundamentals
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
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:
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
Getting Started.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
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,
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
By Mr. Muhammad Pervez Akhtar
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CSC 110 – Intro to Computing - Programming
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,
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Lecture 4 – Scanner & Style
Computer Programming Your First Java Program: HelloWorld.java.
Unit 2: Java Introduction to Programming
String class.
Intro to Java.
Introduction to Objects
Statements, Comments & Simple Arithmetic
Chapter 3 Classes and Objects
Fundamentals of Programming
CMSC 202 Java Primer 2.
Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes
Introduction to Java Programming
elementary programming
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Output Manipulation.
Instructor: Alexander Stoytchev
© A+ Computer Science - Basic Java © A+ Computer Science -
How to Run a Java Program
Introduction to Objects
Presentation transcript:

2.2 Information on Program Appearance and Printing

 Comments are explanatory notes that a programmer can include in a program.  They are not code and they do not affect how the program runs.  On any line where “//” appears, whatever follows this pair of symbols is treated as a comment.  Likewise, anything that appears between “/*” and “*/” will be treated as a comment.

 Here is a commented version of the first program: /* This is my first program. */ public class FirstProg { public static void main(String[] args) { // A method is called here. System.out.println("Hello World"); }

 Notice how the examples so far have indents. It is true that the code will run the same without them. However, it’s good practice (and extremely helpful) to indent so that it is clear where a block of code begins and ends.  Eclipse should do this for you automatically.  All examples given will be shown in this form.

 Here is a brief summary of some of the aspects of printing in Java when using println() and related methods.  1. The method println() prints a line of output followed by a new line or carriage return.  You can also make use of the method print(), which prints a line without a carriage return.  The complete call would be of the form: System.out.print("Some Parameter");

 2. You can print arithmetic constants as well as strings of characters.  A call such as this would print out the numeric value 3: System.out.println(3);

 Note that what would appear on the screen for the following call would be exactly the same, but that there is an important distinction in the meaning of the program code.  The example above prints a numeric value, while the example below prints a string containing the symbol “3”: System.out.println("3");

 3. You can print out more than one parameter at a time.  The symbol that accomplishes this is the “+” sign.  When printing strings, if you want blank spaces between things, you need to supply them explicitly. If you did this: System.out.println("Hello" + "World");  You would see this: HelloWorld

 However, you could also do this: System.out.println("Hello" + " World");  Or this: System.out.println("Hello" + " " + "World");  And you would see: Hello World

 4. Using the “+” sign when printing numeric values has a different effect.  It does the arithmetic. If you did this: System.out.println(3 + 4);  You would see this: 7

 5. You can also combine a parameter in quotes with a numerical value without quotes using a “+” sign.  In this case the system automatically converts the numerical value to a string and does concatenation.  If you did this: System.out.println("Hello" + 7);  You would see this: Hello7

 The full explanation of the different ways in which the “+” sign works will be given later.  For the time being you should simply be able to use it correctly with strings and numbers.

 6. Although the dot has a special syntactic meaning in various places in Java code, in places where simple parameters in the form of numbers are allowed, such as when printing, it has its customary meaning—that of a decimal point.  Thus, if you did this: System.out.println(3.4);  You would see this value: 3.4

 7. In Java printing, the backslash “\” is used as the escape sequence.  This means that any symbol immediately following the backslash is simply treated as a printable character.  That character is not treated as having any syntactic meaning.  This allows you to print characters that would otherwise be ambiguous or syntactically incorrect in a printing parameter.

 If you wanted to print a double quote, you could do this: System.out.println("\"");  If you wanted to print the backslash itself, you could do this: System.out.println("\\");

 The backslash can also be used to insert special printing instructions into a printed string. \n represents a new line.  Thus, if you did this: System.out.println("Hello\nWorld");  You would see this: Hello World