0% found this document useful (0 votes)
17 views35 pages

New - 2-Ing - OOP-S1-Chap 0

This document serves as an introduction to the Java programming language, covering basic concepts, programming paradigms, and essential tools for development. It outlines the course structure, including topics like object-oriented programming principles, memory management, and Java syntax. Additionally, it provides guidance on setting up the Java environment and writing simple Java programs.

Uploaded by

Abdou Abdou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views35 pages

New - 2-Ing - OOP-S1-Chap 0

This document serves as an introduction to the Java programming language, covering basic concepts, programming paradigms, and essential tools for development. It outlines the course structure, including topics like object-oriented programming principles, memory management, and Java syntax. Additionally, it provides guidance on setting up the Java environment and writing simple Java programs.

Uploaded by

Abdou Abdou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Object Oriented Programming 1

Engineering track / 2nd year


2024-2025

◼ 0. Introduction to Java language

Azeddine Chikh

University of Tlemcen
Objective

◼ This chapter introduces the Java language : basic


concepts, conditional structures & loops
Course Outline

0. Introduction to Java language


1. Basic principles
2. Objects and Classes
3. Messages and communication between objects
4. Encapsulation
5. Memory management of objects
6. Inheritance, Polymorphism and Abstract class
Chapter outline
◼ Introduction
◼ Programming Language Paradigms
◼ What’s Java ?
◼ Installing development tools
◼ Text Editor vs. IDE
◼ JRE or JDK
◼ Java files
◼ A first program
◼ Naming conventions
◼ Basic concepts
◼ Comments
◼ Primitive types
◼ Declaration of variable and assignment
◼ Expressions and operators
◼ Entering the value of a variable on the keyboard
◼ Displaying on the screen
◼ Conditional structures & loops
◼ Conditional Instruction
◼ Switch : choice instruction
◼ Unconditional repetition (for)
◼ Conditional repetition (while)
Introduction

Programming Language Paradigms

◼ Imperative (procedural) programming: Pascal,C,… program = algorithm + data


structure.

◼ Functional programming: adopts a much more mathematical approach to programming.


Ex: Lisp.

◼ Logical programming: the description of a program is in the form of predicates. Ex:


Prolog.

◼ Object-oriented programming: Manipulate a set of objects that interact with each other.
Ex: Smalltalk
Introduction
What’s Java ?

◼ Java is a programming language that has advantageous features:

– Simplicity and productivity:


• Full integration of OO
• Memory management

– Independence from platforms (Interoperability)

– Openness:
• Integrated Internet support
• Integrated connection to databases (JDBC)
Introduction

Installing development tools

◼ One of the key principles of Java lies in its virtual machine:


◼ this ensures to all Java developers that a program will be usable with all operating
systems on which a Java virtual machine is installed.

◼ During the source code compilation phase, it takes an intermediate form called
byte code:
◼ interpretable by the Java virtual machine (JRE : Java Runtime Environment).
Introduction
Text Editor vs. IDE

Two ways to write Java programs:

1. Writing the code in a simple text editor and compiling and running the code on the DOS
command line

2. Using an IDE

• Netbeans http://www.netbeans.com–Borland

• JBuilder http://www.borland.com/jbuilder

• IBM WebSphere Studio (http://www.ibm.com/software/awdtools)

• Sun ONE Studio ( http://wwws.sun.com/software/sundev

• Microsoft.Net Studio ( http://msdn.microsoft.com/vstudio)

• Eclipse – The Leading Open Platform for Professional Developers (https://eclipseide.org/)

• BlueJ : IDE used in our course labs (https://www.bluej.org/about.html)


Introduction
JRE or JDK

Start by downloading the Java environment from the Oracle website. We


choose the latest stable version. There are 2 environments:

1. JRE (Java Runtime Environment) contains everything needed for our Java programs
to run on your computer;

2. JDK (Java Development Kit), in addition to containing the JRE, contains everything
needed to develop, compile…

There are several categories of JDK:

1. J2SE (Java 2 Standard Edition): to develop applications.

2. J2EE (Java 2 Enterprise Edition): for web applications.

3. J2ME (Java 2 Micro Edition): for portable devices, such as mobile phones.
Introduction
JRE or JDK

Start by downloading the Java environment from the Oracle website. Choose
the latest stable version. There are 2 environments:

1. JRE (Java Runtime Environment) contains everything needed for your Java programs
to run on your computer;

2. JDK (Java Development Kit), in addition to containing the JRE, contains everything
needed to develop, compile…

There are several categories of JDK:

1. J2SE (Java 2 Standard Edition): to develop applications.

2. J2EE (Java 2 Enterprise Edition): for web applications.

3. J2ME (Java 2 Micro Edition): for portable devices, such as mobile phones.
Introduction
Java files

• Files containing the source code of Java programs have the extension .java.

• Precompiled files corresponding to Java source codes have the extension


.class.

• A Java program, coded under Windows, can be precompiled under Mac and
finally executed under Linux.

• The machine cannot understand the byte code, it needs the virtual machine
(JRE).
Introduction
A first program

◼ A minimal Java application must contain a class:


◼ Having the same name as the file with the extension ".java" in which it is saved.
◼ Containing (at least) one method called main, of type public and static, having an argument of
type String[ ]

public class WelcomeToOOP1


{
public static void main (String[]args)
{System.out.println(“Welcome to OOP1 course");}
}

◼ The word class means that we are going to define a new Java class, followed by the
name of this class.
◼ In Java, uppercase and lowercase letters are considered different characters :
WelcomeToOOP1 is not the same as WELCOMEtoOOP1.
◼ The characters "{" and "}" mark the beginning and end of the block of instructions to
be performed by the class.
Introduction
A first program

◼ main() { : means that we are going to define the main method of the class.

◼ A Java interpreter has the function of executing the instructions contained in the
instruction block of the main method “main”, of the program that is submitted to it.

◼ A method is a kind of procedure (set of instructions) belonging to a class.


◼ The word void means that the main method does not return any value.
◼ args[ ] is the String type input parameter of the method.
◼ The words public and static each describe a characteristic of the method.

public class WelcomeToOOP1


{
public static void main (String[]args)
{System.out.println(“Welcome to OOP1 course");}
}
Introduction

A first program

◼ Create a text file: WelcomeToOOP1.java

◼ Good practice: 1 class per file and 1 file per class

◼ The first line of the program must be


the declaration of the class
public class WelcomeToOOP1
◼ Every program must contain a main
{
method that has the signature
public static void main (String[]args)
{System.out.println(“Welcome to OOP1 …");}
◼ Write on the screen “Welcome to OOP1
}
course”

◼ Close the braces

◼ Compile the program: WelcomeToOOP1.java

• The compiler generates the bytecode in the file: WelcomeToOOP1.class


• Run the application: java WelcomeToOOP1

◼ “WelcomeToOOP1” is displayed on the screen


Introduction

Naming conventions

◼ • File organization
◼ The .java file must have the same name as the public class it describes
◼ 1 .java file per class, even for the one containing the main(), exception for inner classes

◼ • Basic elements
◼ Packages: lowercase
◼ Classes: Uppercase For The First Letter Of Each Word
◼ Interface: Uppercase For The First Letter Of Each Word
◼ Methods: lowercase For The First Letter
◼ Constants: UPPERCASE
◼ Variables: lowercase For The First Letter
Chapter outline
◼ Introduction
◼ Programming Language Paradigms
◼ What’s Java ?
◼ Installing development tools
◼ Text Editor vs. IDE
◼ JRE or JDK
◼ Java files
◼ A first program
◼ Naming conventions
◼ Basic concepts
◼ Comments
◼ Primitive types
◼ Declaration of variable and assignment
◼ Expressions and operators
◼ Entering the value of a variable on the keyboard
◼ Displaying on the screen
◼ Conditional structures & loops
◼ Conditional Instruction
◼ Switch : choice instruction
◼ Unconditional repetition (for)
◼ Conditional repetition (while)
Basic concepts

Comments

◼ Making program readable to oneself and especially to others.

◼ 3 types of comments in Java :

◼ 1) Multiline comments: /* A multiline comment*/

◼ 2) Line comments: // A line comments

◼ 3) Documentation comments (javadoc comments): /** Class documentation*/


Basic concepts
◼ In Java, we have 2 types of variables:
1. simple or "primitive" type variables;
2. complex type variables or "objects". (Next lectures)

Primitive types

Type Nature

◼ int Integer between -231 and 231-1

◼ float Real number

◼ double Double precision real number

◼ boolean True or False

◼ char String character (letter, digit, punctuation ...)

◼ String String
Basic concepts

Primitive types (Numeric type)

• byte (1byte)

byte temp= 50;

• short (2 bytes)

short maxSpeed= 80;

• int (4 bytes)

int sunTemp= 15600000; // Unit is kelvins

• long (8 bytes)

long lightyear=9460700000000000L;

• float (4 bytes)

float pi = 3.141592653f;

• double (8 bytes) is identic to float, except that it contains more digits after the decimal point

double result = 0.555555555555555555555555555555555555555555559d;


Basic concepts

Primitive types (Non numeric type)

• Character type
char v='A’ ;

• Logical type
boolean result=true;

• String type
//1st method
String phrase;
phrase = "First method";

//2nd method
String phrase = new String();
phrase = " Second method ";

//3rd method
String phrase = "Third method ";

//4th method
String phrase = new String(" Fourth method ");
Basic concepts

Declaration of variable and assignment

◼ Single declaration:
type identifier;
Ex:
• int var1; // declares an integer variable var1
• double var2; // declares a real variable var2

◼ Multiple declaration (N variables with the same type)


type identif1, identif2, ..., identifN;
Ex:
• int num1, num2;
• double x, y;
Basic concepts

Declaration of variable and assignment


◼ Syntax:
• identifier=literal value of type

• type identifier=literal value type ; (simultaneous declaration and assignment)

• identifier2= identifier1

◼ Ex : ◼ Ex:
• int num1,num2 • num1 += 1; // num1 = num1 + 1;
• num1=46; • num2=num1;
• double num3;
• num2=5; num3=4.5; char c=‘g';
• boolean free; The set of real numbers "contains" the set of integers
• num2=2; num4=-3.98e-14;
• free=false; • double sum; int val=4;
• int num1,num2, val_entiere=-65; • sum=21; // int to double: ok
• num1=8762; • sum=val; // int to double: ok
• double num2=2; • val= sum; /* double to int: forbidden*/
• num2=5; valeur=2.7; char c='b’;
Basic concepts

Declaration of variable and assignment


◼ Syntax:
• <type of array> <name of array> [] = { <content of array>};

Ex:
◼ Unidimensional array

• int intArray[] = {0,1,2,3,4,5,6,7,8,9};

• double doubleArray[] = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};

• char carArray[] = {'a','b','c','d','e','f','g’};

• String stringArray[] = {" word1", "word2", "word3" , "word4"};

An empty array of six integers :

• int intArray[] = new int[6];

• int[] intArray2 = new int[6];

◼ Multidimensional array • int intTwoDimArray[][] = { {0,2,4,6,8},{1,3,5,7,9} };


Basic concepts

Declaration of variable and assignment


Casting : Java is very typical, we can't use an operator on two different types.

double n1 = 7, n2 = 2;
int result = n1 / n2; // impossible
int result = (int) (n1 / n2);
System.out.println (" The result is " + result); // displays 3
System.out.println (" The result is " + n1/n2); // displays 3

int i = 123; int i = 456;


float j = (float) i; double j = (double)i;

double i = 1.23;
double j = 2.9999999;
int k = (int)i; // k is 1
k = (int)j; // k is 2

int n1 = 3, n2 = 2;
double result1 = n1 / n2; // Result is 1 instead of 1,5
double result2 = (double) (n1 / n2); // Result is 1 instead of 1,5
double result3 = (double)(n1) / (double)(n2); // Result is 1,5

To get a correct result, we would have to cast each number before doing the operation.
Basic concepts

Expressions and operators

< Less than 23<35

<= Less than or equal to 5<=5

> Greater than 14>-45

>= Greater than or equal to 6>=-3

== Équal 6==(4+2)

! Negation
|| OR (34<23) || (456 != 654)

&& AND true && false


Basic concepts
Expressions and operators (Operators' priority)

❑ Operators with the same priority: evaluated from left to right.

❑ With ( ) we evaluate the expressions between ( ) from the innermost to the outermost.

❑ Without ( ) we evaluate according to the following decreasing priorities:

1. unary operators : ! and - ◼ Ex :

2. multiplicative operators : * / % 21.4+63.2*9 // is equivalent to 21.4+(63.2*9)


3. additive operators : + -
true && 6>=4 || 2+48<62
4. comparison operators : < <= >= >
// (true && (6>=4)) || ((2+48)<62)
5. equality operators : == !=

6. logical and : &&

7. logical or : ||
Basic concepts
Expressions and operators

• Increment: ++ (ex: i=i+1)

• Decrement: -- (ex: i=i-1)

• Multiplication: *

• Decimal division: /

• Modulo: %

• Addition: +

• Subtraction: -
Basic concepts
Expressions and operators

• Increment: ++ (ex: i=i+1)

• Decrement: -- (ex: i=i-1)

• Multiplication: *

• Decimal division: /

• Modulo: %

• Addition: +

• Subtraction: -
Basic concepts

Entering the value of a variable on the keyboard

◼ Input-output mechanisms allow the program to communicate with the outside world.

◼ Import the Scanner class and create a Scanner variable:

import java.util.Scanner; // At the begining of .java file


Scanner sc = new Scanner(System.in);

◼ Enter the value


int v; double v; String v;
v =sc.nextInt(); v=sc.nextDouble(); v=sc.next();
// integer // real // String without space
Basic concepts
Displaying on the screen

• Displaying text on the screen:

System.out.println(" Welcome"); // displays Welcome

• Displaying a value, variable, expression:

System.out.print(expression); // without line return

System.out.println(expression) // with line return

Ex:
System.out.println(3.6/4); // displays 0.9

double val1=7.2,val2=4.5;
System.out.print(val1>val2); // displays true without line return

int number=21,divisor=5;
System.out.println((number%divisor)==1); // displays true
Chapter outline
◼ Introduction
◼ Programming Language Paradigms
◼ What’s Java ?
◼ Installing development tools
◼ Text Editor vs. IDE
◼ JRE or JDK
◼ Java files
◼ A first program
◼ Naming conventions
◼ Basic concepts
◼ Comments
◼ Primitive types
◼ Declaration of variable and assignment
◼ Expressions and operators
◼ Entering the value of a variable on the keyboard
◼ Displaying on the screen
◼ Conditional structures & loops
◼ Conditional Instruction
◼ Switch : choice instruction
◼ Unconditional repetition (for)
◼ Conditional repetition (while)
Conditional Structures & loops
Conditional Instruction

public class avgtest


{
public static void main(String[] args)
{
double avg;
double n1=11, n2= 9;
avg=(2*n1+ n2)/3;
if ( avg >10)
{System.out.println("You are passed";)
else
{System.out.println("You are not passed";}
}
}
Conditional Structures & loops
Switch : choice instruction

public class switchtest{


public static void main(String[] args) {
System.out.println("Choose an option 1 2 or 3");
Scanner sc=new Scanner(System.in);
int choice=sc.nextInt();
switch(choice) {
case 1:
System.out.println("Choice 1");
break;
case 2:
System.out.println("Choice 2");
break;
case 3:
System.out.println("Choice 3");
break;
default:
System.out.println("Choice not recognized");
break;}
}
}
Conditional Structures & loops
Unconditional repetition (for)

public class forlooptest


{
public static void main(String[] args)
{
for (int i=0; i<=5; i++)
{System.out.println(i); // 0,1,2,3,4,5}
}
}

In the parenthesis following the « for » we distinguish 3 parts separated by ‘;’

• int i= 0 declaration and initialization of the loop counter

• i<= 5 condition

• i++ manner in which the counter evolves


Conditional Structures & loops
Conditional repetition (while)

import java.util.Scanner;
public class whilelooptest
{
public static void main(String[] args)
{
System.out.println("Enter the student age");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
while (a<18)
{
System.out.println(" Age = " +a+ ", error : the student is too young !!!");
System.out.println("Enter the student age again");
a=sc.nextInt();
}
System.out.println("the student is "+a+" years old");
}
}

You might also like