02 Programming Concepts

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Department of Computer Engineering Department of Computer Engineering

Objectives

Students should:
• Know the steps required to create programs
using a programming language and related
terminology.
Programming Concepts
• Be familiar with the basic structure of a Java
program.
• Be able to modify simple java to obtain desired
2140101 Computer Programming for International Engineers results

2140101 Computer Programming for International Engineers 2


INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY

Department of Computer Engineering Department of Computer Engineering

Programming Languages Categories


• Program – a set or sequence of instructions
that tell a computer what to do

• Instructions – described using


programming languages High-level Languages
Assembly Language
Machine Language
Hardware

2140101 Computer Programming for International Engineers 3 2140101 Computer Programming for International Engineers 4
INTERNATIONAL SCHOOL OF ENGINEERING INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY CHULALONGKORN UNIVERSITY
Department of Computer Engineering Department of Computer Engineering

Traditional Compiled Programs Running a Java Program

Compiler executable file


(Windows)
Your Code

Compiler executable file


(Linux)

Compiler executable file


(Mac OS)

2140101 Computer Programming for International Engineers 5 2140101 Computer Programming for International Engineers 6
INTERNATIONAL SCHOOL OF ENGINEERING INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY CHULALONGKORN UNIVERSITY

Department of Computer Engineering Department of Computer Engineering

Write once, run everywhere Programming Cycle

2140101 Computer Programming for International Engineers 7 2140101 Computer Programming for International Engineers 8
INTERNATIONAL SCHOOL OF ENGINEERING INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY CHULALONGKORN UNIVERSITY
Department of Computer Engineering Department of Computer Engineering

First Java Program Look Inside a Program

MyFirstProgram.java

class

method

2140101 Computer Programming for International Engineers 9 2140101 Computer Programming for International Engineers 10
INTERNATIONAL SCHOOL OF ENGINEERING INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY CHULALONGKORN UNIVERSITY

Department of Computer Engineering Department of Computer Engineering

Syntax, Keywords, & Identifiers Java Keywords


• Syntax – rules of the language, very strict abstract continue for new switch
synchroniz
assert default goto* package
ed
boolean do if private this
• Keywords – words that reserved for some
bit double implements protected throw
purpose. You cannot use keywords as an
byte else import public throws
identifier.
case enum instanceof return transient

catch extends int short try


• Identifiers – names that given to classes, char final interface static void
methods, and variables class finally long strictfp volatide

const* float native super while

2140101 Computer Programming for International Engineers 11 2140101 Computer Programming for International Engineers 12
INTERNATIONAL SCHOOL OF ENGINEERING * not used INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY CHULALONGKORN UNIVERSITY
Department of Computer Engineering Department of Computer Engineering

Comments Readability

2140101 Computer Programming for International Engineers 13 2140101 Computer Programming for International Engineers 14
INTERNATIONAL SCHOOL OF ENGINEERING INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY CHULALONGKORN UNIVERSITY

Department of Computer Engineering Department of Computer Engineering

println() println() and print()

2140101 Computer Programming for International Engineers 15 2140101 Computer Programming for International Engineers 16
INTERNATIONAL SCHOOL OF ENGINEERING INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY CHULALONGKORN UNIVERSITY
Department of Computer Engineering Department of Computer Engineering

Escape Sequences Variables


• \t – Tab
• Symbolic names of memory locations
• \n – Newline
• \” – double quote • A variable must have name and data type
• \’ – single quote associated with it.
• \\ – backslash
• Must be declared before using it.
int x;
double y;
String myText;

2140101 Computer Programming for International Engineers 17 2140101 Computer Programming for International Engineers 18
INTERNATIONAL SCHOOL OF ENGINEERING INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY CHULALONGKORN UNIVERSITY

Department of Computer Engineering Department of Computer Engineering

Assign value to a variable Naming Rules for Java Identifier


Assignment operator (=) • Cannot be a Java reserved word.
• Assign the value on the right to the • Case-sensitive
variable on the left • Unlimited length of sequence of Unicode
letters and digits
x = 3; • Must begin with a letter, underscore (_), or
Y = 6.5;
a dollar sign ($).
myText = “Java Programming”;
• White space not allowed
int z;
z = x;
2140101 Computer Programming for International Engineers 19 2140101 Computer Programming for International Engineers 20
INTERNATIONAL SCHOOL OF ENGINEERING INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY CHULALONGKORN UNIVERSITY
Department of Computer Engineering Department of Computer Engineering

Naming Convention Expressions


• Use meaningful names • An expression is a value, a variable, a
• For compound words use camelCase. method, or one of their combinations that
• Class names begin with an Uppercase letter.
Account, DictionaryItem, FileUtility, Article
can be evaluated to a value.
3.875
• Variable names and method names begin with a
lowercase letter: a + b – 10
Height, speed, filename, tempInCelcius, 8 >= x
imcomingMsg, textToShow. p || q
(method names usually are verbs) locate, sortItem,
findMinValue, checkForError “go”
• For a constant use all uppercase letters and underscore System.out.print(“go”)
(_) to separate words in compound names. Math.sqrt(2)
SOUND_SPEED, KM_PER_MILE, BLOCK_SIZE (x + 3 > y) && (x – 3 < z)

2140101 Computer Programming for International Engineers 21 2140101 Computer Programming for International Engineers 22
INTERNATIONAL SCHOOL OF ENGINEERING INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY CHULALONGKORN UNIVERSITY

Department of Computer Engineering Department of Computer Engineering

Statements Simple calculation


• Complete sentence that causes some action to occur.
• ends with a semicolon (;) • Arithmetic operators
int k;
int j = 10;
– add (+)
double d1, d2, d3;
k = a + b – 10;
– subtract (-)
boolean p = (a >= b); – multiply (*)
System.out.println(“go”);
sqareRootTwo = Math.sqrt(2); – divide (/)
• block of statements are the statements withing a block of curly
– modulo (%) : remainder after divide
braces ({ … })
{ • Assignment operator (=) is used to assign
int m, n;
m = 5; a value or the result from the calculation
}
n = m * m;
to a variable
2140101 Computer Programming for International Engineers 23 2140101 Computer Programming for International Engineers 24
INTERNATIONAL SCHOOL OF ENGINEERING INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY CHULALONGKORN UNIVERSITY
Department of Computer Engineering Department of Computer Engineering

A Simple Calculation Program Another Calculation

2140101 Computer Programming for International Engineers 25 2140101 Computer Programming for International Engineers 26
INTERNATIONAL SCHOOL OF ENGINEERING INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY CHULALONGKORN UNIVERSITY

You might also like