prof. rommel anthony palomino department of computer science and information technology

43
Introduction to Programming <Lecture 3> Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011

Upload: irina

Post on 06-Feb-2016

22 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Programming . Spring 2011. Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology. Programming Fundamentals. Understanding Java Code. public class Hello { /** * My first Java program */ - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Introduction to Programming<Lecture 3>

Prof. Rommel Anthony PalominoDepartment of Computer Science and

Information Technology

Spring 2011

Page 2: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Rommel AB Palomino - UDC Spring 2011

2

Programming Fundamentals

Page 3: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Rommel AB Palomino - UDC Spring 2011

3

Understanding Java Code1 public class Hello2 {3 /**4 * My first Java program5 */6 public static void main( String[ ] args ){7 //prints the string Hello world on

screen8 System.out.println(“Hello world”);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

Page 4: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Rommel AB Palomino - UDC Spring 2011

4

Understanding Java Code1 public class Hello2 {3 /**4 * My first Java program5 */6 public static void main( String[ ] args ){7 //prints the string Hello world on

screen8 System.out.println(“Hello world”);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

Page 5: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Rommel AB Palomino - UDC Spring 2011

5

Java Comments 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.

Page 6: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Rommel AB Palomino - UDC Spring 2011

6

Java Comments3 styles of comments: C++ Style:

// Your Comment C Style:

/* Your Comment */

Javadoc Style: /**

Your Comment @author James Bond@version 0.07 */

Page 7: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Rommel AB Palomino - UDC Spring 2011

7

Java Statements One or more lines of code terminated by a

semicolon. System.out.println(“Hello World”);

System.out.println(“Hello World”);

Page 8: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Rommel AB Palomino - UDC Spring 2011

8

Java Blocks 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”);

}

Page 9: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Rommel AB Palomino - UDC Spring 2011

9

Guidelines 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){

Page 10: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Rommel AB Palomino - UDC Spring 2011

10

Guidelines 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”);

}

Page 11: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Rommel AB Palomino - UDC Spring 2011

11

Java Identifiers 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

Page 12: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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.

Page 13: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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 ( )

Page 14: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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.

Page 15: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Java Keywords Keywords are reserved words by Java

You cannot use keywords as names for your identifiers: variables, classes, methods ... etc.

Page 16: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Java Keywords

Page 17: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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

Page 18: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Java Literals: Integer Integer literals come in different formats:

decimal (base 10) hexadecimal (base 16) octal (base 8).

Page 19: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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

Page 20: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Java Literals: Floating Point

Represents decimals with fractional parts Example: 3.1416

Can be expressed in standard or scientific notation Standard:

583.45 Scientific:

5.8345e2

Page 21: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Java Literals: Boolean

Boolean literals have only two values: true false.

Page 22: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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?

Page 23: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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.

Page 24: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Java Literals: String String literals represent multiple characters

and are enclosed by double quotes.

“Hello World”

Page 25: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Primitive Data Types

The Java programming language defines eight primitive data types. boolean (for logical)char (for textual)byteshortint longdoublefloat

integers

floating point

Page 26: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Primitive Data Types: Logical-boolean A boolean data type represents two states:

true and false.

boolean isPurple = false; boolean isYummy = true;

Page 27: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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

Page 28: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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!”;

Page 29: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Integral: byte, short, int & long

Can be expressed in: decimal, octal or hexadecimal.

int myMemoryAddress = 0xFF;int mySavings = 255;int anOctalNumber = 0377;

Page 30: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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

ranges:

Page 31: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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

Page 32: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Floating Point: float and double

Floating-point data types have the following ranges:

Page 33: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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.

Page 34: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Declaring and Initializing Variables Declare a variable as follows:<data type> <name> [=initial value];

Note: Values enclosed in <> are required values, while those values in [] are optional.

Page 35: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Declaring and Initializing Variables

1 public class VariableSamples { 2 public static void main( String[] args ){ 3 //declare a data type with variable name result and boolean

data type 4 boolean result;

5 //declare a data type with variable name 6 // option and char data type 7 char option; 8 option = 'C'; //assign 'C' to option

9 //declare a data type with variable name 10 //grade, double data type and initialized 11 //to 0.0 12 double grade = 0.0; 13 }14 }

Page 36: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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”;

Page 37: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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;

Page 38: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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()

Page 39: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Outputting Variable Data: Sample Program

1 public 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:

10The value of x=A

Page 40: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

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

Page 41: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

System.out.println() vs. System.out.print() Examples

Program 1:

Output:HelloWorld

Program 2:

Output:HelloWorld

System.out.print(“Hello”);System.out.print(“World”);

System.out.println(“Hello”);System.out.println(“World”);

Page 42: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Rommel AB Palomino - UDC Spring 2011

42

Questions?

Page 43: Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology

Rommel AB Palomino - UDC Spring 2011

43

For Next Class 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.