Module: POO Second Year, Computer Science, USTHB Ameur Imane
TP. Java Syntax
Exercise 1
a. Give the correct order of the code lines of the following program, then enter it and run it on Eclipse.
b. Check the validity of the operations found in the following program. Correct each invalid instruction
then give the value obtained for each instruction.
Exercise 2
Write a program in Java that allows you to test the age of a person entered on the screen, it displays minor if the
age is less than 18 years old and major otherwise.
Page | 1
Module: POO Second Year, Computer Science, USTHB Ameur Imane
Exercise 3
Write a Java program that asks the user to enter an integer n and display whether the typed number is prime or
not?
Exercise 4
Write a java program that asks the user to enter their name and displays their name with the welcome message.
Exercise 5
Write a java program that asks the user to enter an integer and displays that the number is even or odd
depending on the value entered.
Exercise 6
Write a Java program that calculates the sum of the first 100 integers.
Exercise 7
Write a Java program that asks the user to enter an integer n and displays the sum of the first n integers.
Exercise 8
Write a Java program that asks the user to enter an integer n and successively display all even numbers that are
less than or equal to n.
Improve the program so that it also displays the number of even integers less than or equal to n.
Exercise 9
Write a Java program that asks the user to enter an integer n less than or equal to 9 and display the
multiplication table for this number.
Exercise 10
Write a Java program allowing you to:
a. Create an array A of N randomly generated integers.
Use the [Link] method to generate random real numbers then use casting to transform them into integers
between 0 and 100.
b. Display table A as well as the average of all its elements.
c. Sort array A and display its sorted contents (use the sort method of the Arrays class).
d. Copy table A into another table of integers B of size M > N. Randomly fill the empty boxes of B then
display its content and its average.
Exercise 11
Write a Java program allowing you to:
a. Read a character string s1 then convert it to uppercase.
b. Show the string s1 and its length.
c. Replace all spaces with periods and display the string s1.
d. Read a second s2 channel and display the answer to the following questions:
i. Are the two channels identical?
ii. Do the two strings start with the same character?
iii. Does s1 start with s2?
iv. Does s1 end with s2?
v. Does s1 contain s2? if yes, determine then display the private string s1 of s2.
Page | 2