Basic Programming Elements
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
1
Content 1. Stand alone applications and mini-applications 2. Name and identifiers 3. Java program structure 4. Data types 5. Operators 6. Flow execution control
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
2
1. Stand alone applications and mini-applications
Mini- applications = applets stand alone application) = program that can be used independently. They have main() method public static void main (String[] args)
launching the execution (run program) Java interpreter java numeProgram
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
3
Applet A Java program that extends Applet class. This class doesn’t contain main method the program cannot be launched using Java interpreter. They are used in Web pages (via HTML file). Web server browser (ex. Internet Explorer, Mozilla Firefox).
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
4
2. Names and Identifiers
An identifier is a sequence of unlimited length of letters and digits where the first is compulsory a letter. They are string of characters representing names given to variable, classes or methods. They should differ from keywords (including null, false and true). Names are used for referring entities like packages, class types, interfaces, type members, parameters, local variables etc. The names can be: simple - ex.: xxx composed – sequences of identifiers separated by ".". ex.: xxx.yyy.zzz
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
5
abstract do import public try boolean double instanceof return void break else int short volatile byte extends interface static while case final long super catch finally native switch char float new synchronized class for null this const goto package throw continue if private throws default implements protected transient Keywords
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
6
Ex.: java.awt.BorderLayout java.lang.System.out.println()
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
7
3. Java Program Structure
Lexical entities: comments – keywords identifiers literals separators – signs ({, }, [, ], ;, . and ,) used to separate lexical entities operators Variables Constants Expressions
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
8
Instructions – lexical units that describe what the program has to perform – the instructions are separated by ”;”. Code block { instructions; }. packages subpackages
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
9
Source files Code source filename.java Compiler javac filename.class Package declaration: package NumePachet;
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
10
Program example:
/** Program name * Comments */ public class NumeClasaPrincipala { /** * Metoda principala a aplicatiei * * Se pot transmite parametrii prin tabelul argumente */ public static void main (String[] args) { // Aici se adauga codul programului System.out.println("Salut! Merge programul!"); }//terminare metoda main }
de
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
11
Compilation javac Running java NumeClasaprincipala
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
12
4. Data Types
Type variabileName; Variable types in Java: reference - classes, interfaces, arrays (or null) and primitive data. Where are they used?
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
13
Primitive data types
Type byte short int long char Significance Integer Integer cu semn Integer cu semn Dimension Minim 8 biţi -128 16 biţi -32768 -2147483648 63 -2 Characters 38 -3.40282347*10 Maxim +127 32767 2147483648 63 2 -1 Characters 38 3.40282347*10
32 biţi Integer cu semn 64 biţi Integer fără semn 16 biţi Float point 32 biţi float Virgulă mobilă în simplă precizie double Virgulă mobilă în 64 biţi dublă precizie boolean Val. booleană 32 biţi
1.79769313486231 1.7976931348623157 570*10308 308 0*10 false true
14
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
Data can be: 1. numerical - integers: byte, short, int, long, char; 2. numerical - real: float, double. 3. boolean - logical 4. character - char. In Java are used for characters the standard Unicode (Wide Character) with 38.885 characters.
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
15
String data1; String data2=”sir de caractere”; String data3=new String("alt sir");
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
16
Data types declarations: byte varByte; // variabila de 8 biti short varShort; // variabila de 16 biti int varInt; // variabila de 32 biti long varLong; // variabila de 64 biti long varLong=1L; //atribuire long varLong=2l; float varFloat; // variabila de 32 biti double varDouble; // variabila de 64 biti char caracter; // variabila de un caracter char caracter = ‘c’; //declara variabila caracter si ii atribuie //valoarea c boolean val=true;
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
17
Arrays: int Matrice1[]; // float Matrice2[][]; // Matrice2 mat=new float[3][4]; int[] matrice3=new int[5]; mat şi matrice3 sunt date de tip referinţă (reference type). String[] str= {"unu", "doi", "trei"}; int[] tab= {1, 3, 5, 2, 34};
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
18
5. Operators
1. Assign operator - "=". add (+=) subtract (-=) multiplication (*=) divide (/=) modulo (%=) AND (&=) OR (|=) EXCLUSIVE OR (^=) int x=y=z=25; a = 3; // atribuie variabilei a valoarea 3
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
19
a - = 5; // atribuie variabilei a rezultatul operaţiei a – 5. (scădere şi atribuire) a * = 2; // atribuie variabilei a rezultatul operaţiei a * 2. (înmulţire şi atribuire) a / = 5; // atribuie variabilei a rezultatul operaţiei a / 5 2. Binary arithmetic operators: add (+) – ex.: a+b subtract (-) – ex.: a-b multiply (*) – ex.: a*b divide (/) – ex.: a/b modulo (%) – ex.: a%b 3. Unary arithmetic operators: decrement (-) – ex.: a-Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
20
increment (+) – ex.: a++ minus sign (-) plus sign (+) int x=3, y, z; y=++x; // x este incrementat inainte de atribuire z=y--; //intai se face atribuirea si apoi decrementarea z=-y; //atribuie valoarea lui y cu semn schimbat x=~y; //atribuie valoarea lui y cu complementare pe biti float f1=3.15, f2; f2=f1++; f3=++f1; f1=5.15, f2=3.15 şi f3=5.15.
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
21
Order: int < long < float < double 4. Relational operators: equal (==) different (!=) greater (>) smaller (=) smaller or equal ( < = == != ! ~ ?: && || Operation Atribuire Mai mare decât Mai mic decât Mai mic sau egal cu Mai mare sau egal cu Egal cu Nu este egal cu Negaţie logică Negaţie logică pe biţi Operatori condiţional ŞI (conditional AND) SAU (conditional OR) Example a=b a>b a >>> +=
Incrementare Decrementare Adunare Scădere Multiplicare Împărţire Modulo ŞI pe biţi (bitwise AND) SAU pe biţi (bitwise OR) SAU EXCLUSIV pe biţi (XOR) Deplasare stânga Deplasare dreapta Deplasare dreapta cu umplere cu zero Atribuie rezultatul adunării
a++ or ++a a-- or --a a+b a - b or -b a*b a/b a%b a&b a|b a^b a > b a >>> b a += b
25
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
-= *= /= &= |= ^= %= = >>>=
Atribuie rezultatul scăderii Atribuie rezultatul înmulţirii Atribuie rezultatul împărţirii Atribuie rezultatul lui ŞI pe biţi Atribuie rezultatul lui SAU pe biţi Atribuie rezultatul lui SAU EXCLUSIV pe biţi Atribuie rezultatul lui modulo Atribuie rezultatul deplasării la stânga Atribuie rezultatul deplasării la dreapta Atribuie rezultatul deplasării la dreapta cu umplere cu zero
a -= b a *= b a /= b a &= b a |= b a ^= b a %= b a = b a >>>= b
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
26
7. Concatenation operator for string of characters “+”. String s1=”qwe”; String s2=”M”; String s3; s3=s1+s2;
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
27
6. Instructions for the control of execution if, for, while, do … while, switch, break and continue.
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
28
if instructions
1) if (expresie_conditie) instructiune; 2) if (expresie) { instructiuni; } 3) if (expresie) instructiune else instructiune; 4) if (expresie) { instructiuni; } else { instructiuni; }
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
29
5) test ? rezultat_adevarat : rezultat_false;
z = (x < y) ? x : y; if ((a==true) || (b==false)) { ....}
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
30
switch switch (test) { case valoareUnu : rezultatUnu; break: case valoareDoi : rezultatDoi; break; case valoareTrei : rezultatTrei; break; ………….. default : rezultatImplicit; }
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
31
for loop for (initialization; test; increment) instruction; or for (initialization; test; increment) { instructions; }
Tiberiu Leţia: Software Engineering –Java Programming Language – Programming Basic Elements
32
for (int i=0; i
You May Also Find These Documents Helpful
-
Based on the Greiner (2009) article, define an applet and distinguish one from a servlet.…
- 693 Words
- 4 Pages
Satisfactory Essays -
A1: Applet is a type of Java program that runs on web browser. It can be a fully functional Java application because it has full Java API…
- 559 Words
- 3 Pages
Good Essays -
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Chapter 19 Chapter 20 Chapter 21 Chapter 22 Chapter 23 Chapter 24 Chapter 25 Chapter 26 Chapter 27 An Introduction to Hardware, Software, and the Internet An Introduction to Software Development Objects and Classes Algorithms Java Syntax and Style Data Types, Variables, and Arithmetic Boolean Expressions and if-else Statements Iterative Statements: while, for, do–while Implementing Classes and Using Objects Strings Class Hierarchies and Interfaces Arrays…
- 3908 Words
- 16 Pages
Powerful Essays -
i. Application – Defines the applications used to process requests and what ports and sockets are used.…
- 890 Words
- 4 Pages
Good Essays -
3. What is the purpose of each of the following components of the X Window system?…
- 415 Words
- 2 Pages
Satisfactory Essays -
ii. The optional keyword static is used to declare a special kind of variable called a class variable. Class variables will be discussed in another lecture.…
- 939 Words
- 4 Pages
Better Essays -
double mph = ( (14 * (60/45.5))/1.6); // That is, (km / hr) / (1.6 km / mile)…
- 305 Words
- 4 Pages
Good Essays -
A Java applet is a small program designed to run on a web page or in a Java virtual machine. The small size of the applet allows it to be executed in most browsers quickly. Applets can be used for various jobs from displaying images to custom navigation menus and even in-browser applications like calculators.…
- 263 Words
- 2 Pages
Satisfactory Essays -
Application 1 Panels Utils Application 2 Help Printing Applications Helper Code/Libs Application Frame Basic Libraries Foundation…
- 2893 Words
- 12 Pages
Powerful Essays -
3. To make profit, a local store marks up the prices of its items by a certain percentage. Write a Java program that reads the original price of the item sold, the percentage of the marked-up price, and sales tax rate. The program then outputs the original price of the item, the mark-up percentage of the item, the store's selling price of the item, the sales tax rate, the sales tax, and the final price of the item. ( the final price of the item is the selling price plus the sales tax.)…
- 416 Words
- 2 Pages
Satisfactory Essays -
It is composed of not only an operating system, but also middleware, user interface (UI), browser, and application. It also includes C/C++ libraries that are used in components of various Android systems.…
- 834 Words
- 4 Pages
Good Essays -
One of the most fundamental concepts of modern operating systems is the distinction between a program and the activity of executing a program. The former is a static set of directions, whereas the latter is a dynamic activity whose properties change as time progresses. The activity of executing a program under the control of the operating system is known as a process.…
- 572 Words
- 3 Pages
Good Essays -
In this section, you will write a simple IDL interface for the Hello World program. The IDL interface defines the contract between the client and server parts of your application, specifying what operations and attributes are available. OMG IDL is programming-language independent. You must map it to Java before writing any of the implementation code. (Running idltojava on the IDL file does this for you automatically.) Here's the complete Hello.idl file:…
- 4045 Words
- 17 Pages
Powerful Essays -
1) It is a plain Java technology interface that is designated with the MappedSuperclass annotation.…
- 5205 Words
- 21 Pages
Good Essays -
A Python identifier is a name used to identify a variable, function, class, module, or other object. An identifier…
- 2043 Words
- 9 Pages
Powerful Essays