0% found this document useful (0 votes)
7 views

TP0

Java

Uploaded by

saadia.kedjar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

TP0

Java

Uploaded by

saadia.kedjar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

UMBB / Faculty of Science / Dept.

Computer Science
2nd A. Ing.

TP OBJECT-ORIENTED PROGRAMMING WITH THE JAVA LANGUAGE

INSTALLATION & GETTING STARTED


Java Language – Eclipse IDE

Objectives: First Java program and getting started with Eclipse


- Installing Eclipse
- Configuring Eclipse and the Workspace
- Eclipse Interface
- Using Eclipse (entering and running a Java program)

1. Go to the Eclipse Foundation website https://www.eclipse.org/ then click the “Download”


button
2. Download ECLIPSE IDE FOR JAVA DEVELOPERS
3. Unzip the downloaded file into the same directory.
4. To start the Eclipse IDE, double-click on the "eclipse.exe" file. This will open an Eclipse
startup window, followed by a window asking you to specify the "workspace" (the working
directory). This is the folder where Eclipse will save your project files, such as ".java"
program files and ".xml", "*.properties" configuration files, etc.
5. When eclipse is launched, create a new Project that you will call TP01
6. Next, to create a main class, right-click on the package called "TP01" and choose the
"New/Class" option. A new window will appear where you can enter the name of the class,
for example ClassTP1. Make sure to check the "public static void main(...)" box so that the
class embeds the main function of your program, equivalent to the "main()" function in the C
language.
7. In the left menu, click on your ClassTP1 class and navigate to the main function and enter
your main code, which displays a “hello world!” message as shown below:

It should be noted that the instruction "System.out.println ("Hello world!");" allows you to display a
message on the screen followed by a line break, which is similar to "printf" in C language. The
function "System.out.print("msg")" allows you to display a message without a line break.
Exercise 1: Input/output, interaction with the user in console mode

1- Displaying a personalized message

Write a program that asks the user for their name and displays a personalized greeting.

2- Calculating the sum of two numbers

Write a program that prompts the user for two numbers and displays the sum of those numbers.

Exercise 2:
Deduce theoretically (on paper) the result of the following code. Then compare your answer to
the result from its compilation and execution.

public classComplex Assignments{


public static void main(String[]args) {
int x= 10;
inty= 5;

x+= 3;// Equivalent to x = x + 3;


y*= 2;// Equivalent to y = y * 2;

int z=x+y;
z-= 5;// Equivalent to z = z - 5;

double d= 4.5;
int i= 2;
d/=i;// Equivalent to d = d / i;
String text1="Hello";
String text2="World";

String result=text1+text2;// String concatenation

System.out.println("x = "+x); System.out.println("y = "+y);


System.out.println("z = "+z); System.out.println("d = "+d);
System.out.println("result = "+result);
}
}

Exercise 3: same question as exercise 2:

public classExampleOperations{

public static voidmain(String[]args) {


int a= 10,b= 5,c= 10,d;

d= (int) (b+=a);
System.out.println("A : a="+a+" b="+b+" c="+c+" d="+d);

a=b=c= 5;
a*=b+=c;
System.out.println("B: a="+a+" b="+b+" c="+c);

c=a<b?a++ :b--;
System.out.println("C: a="+a+" b="+b+" c="+c);

c=a>b?a-- :b++;
System.out.println("D: a="+a+" b="+b+" c="+c);
}
}

You might also like