Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.

Slides:



Advertisements
Similar presentations
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
CMT Programming Software Applications
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
String Escape Sequences
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
C-Language Keywords(C99)
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 2: Java Fundamentals
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Primitive Data Types. Identifiers What word does it sound like?
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Chapter 2 Variables.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Java Variables and Types
Variables and Primative Types
OUTPUT STATEMENTS GC 201.
Introduction to Programming in Java
Escape Sequences What if we wanted to print the quote character?
An overview of Java, Data types and variables
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
In this class, we will cover:
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011

Programming Fundamentals Rommel AB Palomino - UDC Spring 20112

Understanding Java Code Rommel AB Palomino - UDC Spring public class Hello 2{2{ 3 /** 4 * My first Java program 5 */ 6 public static void main( String[ ] args ){ 7 //prints the string Hello world on screen 8 System.out.println(“Hello world”); 9}9} 10 } Indicates class name That is a Java comment That is the main method, where Java programs start!!! Another comment Prints the quoted text Indicates class starts Indicates class ends

Understanding Java Code Rommel AB Palomino - UDC Spring public class Hello 2{2{ 3 /** 4 * My first Java program 5 */ 6 public static void main( String[ ] args ){ 7 //prints the string Hello world on screen 8 System.out.println(“Hello world”); 9}9} 10 } Indicates class name That is a Java comment That is the main method, where Java programs start!!! Another comment Prints the quoted text Indicates class starts Indicates class ends

Java Comments Rommel AB Palomino - UDC Spring  The main purpose for comments is for documentation purposes.  You ALWAYS have to comment your code.  Commenting makes other people have an idea what you want to do even if it is not working properly.

Java Comments Rommel AB Palomino - UDC Spring styles of comments:  C++ Style:  // Your Comment  C Style:  /* Your Comment */  Javadoc Style:  /** Your James */

Java Statements Rommel AB Palomino - UDC Spring  One or more lines of code terminated by a semicolon.  System.out.println(“Hello World”);  System. out. println(“Hello World”);

Java Blocks Rommel AB Palomino - UDC Spring  They are one or more statements bounded by an opening and closing curly braces that groups the statements as one unit.  They can be nested  public static void main( String[] args ){ System.out.println("Hello"); System.out.println("world”); }

Guidelines Rommel AB Palomino - UDC Spring  Java programs always end with the.java extension  Your filename should match the name of your public class  When creating blocks “{“ can be placed immediately after the statement:  public static void main(String[ ] args) {  Or in the next line:  public static void main(String[ ] args) {

Guidelines Rommel AB Palomino - UDC Spring  Indent your code properly:  Public static void main(String[ ] args) { System.out.println(“Hello World”); }  Public static void main(String[ ] args) { System.out.println(“Hello World”); }

Java Identifiers Rommel AB Palomino - UDC Spring Identifiers  are tokens that represent names of variables, methods, classes, etc.  Examples of identifiers are: Hello, main, System, out. Java identifiers are case-sensitive. Banana banana

Java Identifiers Must begin with either: a letter an underscore “_” a dollar sign “$”. Letters may be lower or upper case. Subsequent characters may use numbers 0 to 9. Cannot use Java keywords.

Best Practices  1. For names of classes, capitalize the first letter of the class name. i.e.:  class Animal  2. For names of methods and variables, the first letter of the word should start with a small letter. For example:  public void myMethod ( )

Coding Guidelines  3. In case of multi-word identifiers, use capital letters to indicate the start of the word except the first word. For example:  getSoldBananas()  myCheckingAccountNumber  4. Avoid using underscores at the start of the identifier such as _read or _write.

Java Keywords Keywords are reserved words by Java You cannot use keywords as names for your identifiers: variables, classes, methods... etc.

Java Keywords

Java Literals Literals are tokens that do not change or are constant. The different types of literals in Java are:  Integer Literals  Floating-Point Literals  Boolean Literals  Character Literals  String Literals

Java Literals: Integer Integer literals come in different formats:  decimal (base 10)  hexadecimal (base 16)  octal (base 8).

Java Literals: Integer Special Notations in using integer literals in our programs:  Decimal No special notation example: 12  Hexadecimal Precede by 0x or 0X example: 0xC  Octal Precede by 0 example: 014

Java Literals: Floating Point Represents decimals with fractional parts  Example: Can be expressed in standard or scientific notation  Standard:   Scientific:  e2

Java Literals: Boolean Boolean literals have only two values: true false.

Java Literals: Character Character Literals represent single Unicode characters. Unicode character  a 16-bit character set that replaces the 8-bit ASCII character set.  Unicode allows the inclusion of symbols and special characters from other languages. How many elements could you represent with just 8 bits?

Java Literals: Character To use a character literal, enclose the character in single quote delimiters. For example  the letter a, is represented as ‘a’.  special characters such as a newline character, a backslash is used followed by the character code.  ‘\n’ for the newline character,  ‘\r’ for the carriage return  ‘\b’ for backspace.

Java Literals: String String literals represent multiple characters and are enclosed by double quotes. “Hello World”

Primitive Data Types The Java programming language defines eight primitive data types. boolean (for logical) char (for textual) byte short int long double float integers floating point

Primitive Data Types: Logical-boolean A boolean data type represents two states: true and false. boolean isPurple = false; boolean isYummy = true;

Primitive Data Types: char A character data type (char), represents a single Unicode character. It must have its literal enclosed in single quotes(’ ’). For example, ‘a’ //The letter a ‘\t’ //A tab To represent special characters like ' (single quotes) or " (double quotes), use the escape character \. For example, '\'' //for single quotes '\"' //for double quotes

Primitive Data Types: char A String represents a data type that contains multiple characters. It is not a primitive data type, it is a class. It has its literal enclosed in double quotes (“ ”). String message=“Hello world!”;

Integral: byte, short, int & long  Can be expressed in: decimal, octal or hexadecimal. int myMemoryAddress = 0xFF; int mySavings = 255; int anOctalNumber = 0377;

Integral: byte, short, int & long Integral data type have the following ranges:

Floating Point: float and double Floating point types has double as default data type. Floating-point literal includes either a decimal point or one of the following, E or e //(add exponential value) F or f //(float) D or d //(double) Examples are, 3.14 //A simple floating-point value (a double) 6.02E23 //A large floating-point value 2.718F //A simple float size value 123.4E+306D//A large double value with redundant D

Floating Point: float and double Floating-point data types have the following ranges:

Variables A variable is an item of data used to store the state of objects. A variable has a:  data type The data type indicates the type of value that the variable can hold.  name The variable name must follow rules for identifiers.

Declaring and Initializing Variables Declare a variable as follows: [=initial value]; Note: Values enclosed in <> are required values, while those values in [] are optional.

Declaring and Initializing Variables 1public class VariableSamples { 2public static void main( String[] args ){ 3//declare a data type with variable name result and boolean data type 4boolean result; 5//declare a data type with variable name 6// option and char data type 7char option; 8option = 'C'; //assign 'C' to option 9//declare a data type with variable name 10//grade, double data type and initialized 11//to double grade = 0.0; 13} 14}

Guidelines  1. Initialize your variables as you declare them.  2. Use descriptive names for your variables.  Need a variable to store a client’s last name:  String x = “Smith”;  String lastName = “Smith”;

Declaring and Initializing Variables: Coding Guidelines  3. Declare one variable per line of code. For example, the variable declarations,  double exam=0; double quiz=10; double grade = 0;  is preferred over the declaration,  double exam=0, quiz=10, grade=0;

Outputting Variable Data In order to output the value of a certain variable, we can use the following commands: System.out.println() System.out.print()

Outputting Variable Data: Sample Program 1public class OutputVariable { 2 public static void main( String[] args ){ 3 int value = 10; 4 char x; 5 x = ‘A’; 6 System.out.println( value ); 7 System.out.println( “The value of x=“ + x ); 8 } 9} The program will output the following text on screen: 10 The value of x=A

System.out.println() vs. System.out.print() System.out.println()  Appends a newline at the end of the data output System.out.print()  Does not append newline at the end of the data output

System.out.println() vs. System.out.print() Examples Program 1: Output: HelloWorld Program 2: Output: Hello World System.out.print(“Hello”); System.out.print(“World”); System.out.println(“Hello”); System.out.println(“World”);

Questions? Rommel AB Palomino - UDC Spring

For Next Class Rommel AB Palomino - UDC Spring  Quiz:  Life cycle of a program: (Three first stages)  Problem definition: Identifying the problem. What is the real problem to solve?  Problem Analysis: Analyzing the problem as inputs and outputs. What are the inputs and outputs? (Requirements of the problem)  Algorithm Representation: Representing your algorithm using pseudocode and/or flowchart. Check when and how to use correctly the flowchart blocks, and/or pseudocode wording/numbering.