Starting JavaProgramming

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,
Outline Java program structure Basic program elements
JavaScript, Third Edition
JAVA PROGRAMMING PART II.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
By Mr. Muhammad Pervez Akhtar
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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: รัฐภูมิ เถื่อนถนอม
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Key Words / Reserved Words
Java Language Basics.
Working with Java.
Chapter 4 Assignment Statement
Data types Data types Basic types
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Yanal Alahmad Java Workshop Yanal Alahmad
Lecture 2: Data Types, Variables, Operators, and Expressions
CSE 190D, Winter 2013 Building Java Programs Chapter 1
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Variables and Arithmetic Operators in JavaScript
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment Statement
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Computing with C# and the .NET Framework
Java Programming: From Problem Analysis to Program Design, 4e
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
null, true, and false are also reserved.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to Java Programming
Basics of ‘C’.
An overview of Java, Data types and variables
Chapter 2: Basic Elements of Java
Chapter 1: Computer Systems
Fundamentals 2.
Units with – James tedder
Units with – James tedder
JavaScript Reserved Words
elementary programming
Focus of the Course Object-Oriented Software Development
Module 2 - Part 1 Variables, Assignment, and Data Types
Introduction to Java Applications
CSE 142, Spring 2012 Building Java Programs Chapter 1
Chap 2. Identifiers, Keywords, and Types
Module 2 Variables, Data Types and Arithmetic
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

Starting JavaProgramming http://docs.oracle.com/javase/

Content Java Structure Compile & Running Program Escape Sequence Character Method printf() Language Basics Variables Operators Control Flow Statements http://docs.oracle.com/javase/

Java Program Structure Java must have a public class Start with ‘public static void main(String[]args) ’ There are two kinds of java programming: GUI-based Text-based Syntax to declare class: [modifier] [class] class_name{ ……. }

Example modifier class_name main program

Details Halo.java Define class and modifier That can be compiled and executed by the JVM

Details (2) Halo.java Main program and first time it is run by JVM Public : one of modifier Static : type of method Void : no return value Main : main method String : type of argumen Args : Array of argumen which can be added while running

Details (3) Halo.java Function to display text in console “println”  after display text produce a new line “print”  just display text

Compiling Program Compile  with command “javac name_file.java”

Compiling Program Compile will produce class file

Running Program Running  with command “java class_file” without .class

Escape Sequence Character A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:

Ex: Escape Sequence Character

Method printf() The printf( ) method automatically uses Formatter to create a formatted string. String format Object … args

Method printf() Result

Another Ex: printf()

Printf() to command line summary Source : http://www.java2s.com/Tutorial/Java/0120__Development/printftocommandlinesummary.htm

Variables The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. The Java programming language defines the following kinds of variables: Instance Variables (Non-Static Fields) Class Variables (Static Fields) Local Variables Parameters

Naming Variables Variable names are case-sensitive Must start with a letter (a-z, A-Z), the dollar sign "$", or the underscore character “_“, after the first character, can be followed by numbers(0-9). Variable names can’t contain dash (-) or space (“ “). Beginning with lowercase on the first word and uppercase letters in the second and subsequent words. Also keep in mind that the variable names you choose must not be a keyword or reserved word.

Java Language Keywords abstract continue for new switch assert*** default goto* package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum**** instanceof return transient catch extends int short try char final interface static void class finally long strictfp** volatile const* float native super while * not used ** added in 1.2 *** added in 1.4 **** added in 5.0

Instance Variables Can be access with instance class

Class/Static Variables Can be access with static class

Local Variables declare local variable within method

Parameters

Primitive Data Types Data type Length range of values Example boolean 1 bit 0 and 1 0; 1 byte 8 bit -128 to 127 (-27 to 27) -5; 10 short 2 byte / 16 bit -32,768 to 32,767 (-215 to 215) -12,777; 31,578 int 4 byte / 32 bit -2,147,483,648 to 2,147,483,647 (-221 to 221) -2,107,483,448 ; 2,145,483,638 long 8 byte / 64 bit -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. (-263 to 263) 9,103,372,036,854,775,807 float 1.40129846432481707e-45 to 3.40282346638528860e+38 double 4.94065645841246544e-324d to 1.79769313486231570e+308d char 0 to 65,535 (unsigned)

Default Values Data Type Default Value (for fields) byte short int short int long 0L float 0.0f double 0.0d char '\u0000' String (or any object)   null boolean false

Integer Literals

Floating-Point Literals

Arrays An array is a container object that holds a fixed number of values of a single type.

Creating and Initializing an Array

Creating and Initializing Array (2)

Accessing an Array

Multi Dimesion Array 1 2 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2

Ex: Multi Dimesion Array

Copying Arrays

Operators Operators are symbols and special characters (mathematics) used in an expression Example: int x = 3; int y = x; int z = x * y; boolean status = true;

Operators (2)

Operators (3) Arithmetic Operators Unary Operators perform addition, subtraction, multiplication, division, and modulo. Unary Operators require only one operand perform incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean. The Equality and Relational Operators determine if one operand is greater than, less than, equal to, or not equal to another operand.

Operators (4) Conditional Operators Bitwise and Bit Shift Operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. Bitwise and Bit Shift Operators To manipulated bit pattern less commonly used.

Ex: Unary Operator

Ex: Bitwise and Bit Shift Operators

Ex: Bitwise and Bit Shift Operators(2)

Operator Priority Operator in bracket or parentheses "(...)" Increment and decrement operators Multiplication and division operators Addition and subtraction operators Bitwise operators

Try Out Operator Change the following program to use assignments operator!!!

Try Out Operator answer

Control Flow Statements Control Flow Statements consist of: Decision making statements, if-then  executed only if a particular test evaluates to true if-then-else Switch  the switch statement can have a number of possible execution paths. Looping statements, and For While do-while Branching statements Break Continue return

Ex: IF ELSE

Ex: Switch

Try Out Decision Making Statements Calculates the number of days in a particular month: Number of Days in February 2012= 29 Number of Days in February 2011= 28 Number of Days in January, March, May, July, August, October, December = 31 Number of Days in April, June, September, November = 30 Clue: use if else and switch statement

While Statement The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:

Ex: While Statement

Do-While Statement The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.

Ex: Do-While Statement

The for Statement The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop“. The general form of the for statement can be expressed as follows:

Ex: For Statement

Ex (2): For Statement

The break Statement The break statement has two forms: labeled and unlabeled. You can use unlabeled in switch statement, or to terminate a for, while, or do-while loop. You can use labeled for loops to search for a value in a two-dimensional array.

Ex: unlabeled break Statement

Ex: labeled break Statement

The continue Statement The continue statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop A labeled continue statement skips the current iteration of an outer loop marked with the given label.

Ex: unlabeled continue Statement

Ex: labeled continue Statement

The return Statement Return value

Class Scanner Import: java.util.Scanner; A simple text scanner which can parse primitive types and strings using regular expressions. For example, this code allows a user to read a number from System.in:

Class Scanner (2) nextInt(): to receive integer data type Import: java.util.Scanner; nextInt(): to receive integer data type nextShort(): to receive short data type nextLong(): to receive long data type nextDouble(): to receive double data type nextFloat(): to receive float data type nextLine(): to receive string data type nextBoolean(): to receive boolean data type

Ex: Class Scanner

Result Ex: Class Scanner

Ex: Class Scanner (2)

Result Ex: Class Scanner (2) Any Question?

Assignment Make a simple calculator to: addition, substraction, multiplication, division Use Class Scanner

THANKS