Professional Documents
Culture Documents
Homework
Page 1 of 20
Page 2 of 20
Page 3 of 20
To take benefit from these exercises, students must resolve them after the lecture and before
the tutorial session. This will help him to review the course practically and be prepared for the
tutorial and lab sessions.
No solution to the homework exercises is provided, so student may ask course or lab instructor
to explain the questions or even to resolve some of them if the time allow during the lecture or
during the office hours.
We are happy to have you among our students and we wish you a successful course with an
excellent level of learning and practicing.
Page 4 of 20
Exercise 1
Write an application that displays the numbers 1 to 4 on the same line, with each pair of
adjacent numbers separated by one space. Use the following techniques:
a) Use one System.out.println statement.
b) Use four System.out.print statements.
c) Use one System.out.printf statement.
What does the following code print?
Exercise 2
Write a Java program that calculates and prints the subtraction of two integers: write
declarations, statements or comments that accomplish each of the following tasks:
Page 5 of 20
Exercise 4
State the order of evaluation of the operators in each of the following Java statements, and
show the value of x after each statement is performed:
a) x = b * ( c / b ) + c % b;
b) x = a - b / c * 10;
Exercise 5
Write a Java program that computes the sum of two rational numbers given by the user.
𝑎 𝑏
[Note: If two numbers are rational, then we can write them in the form 𝑥 and 𝑦, for some
integer values of a, b, x and y, and x and y cannot be zero.
𝑎 𝑏
Then their sum is: 𝑥
+𝑦
𝑎𝑦 𝑏𝑥
= 𝑥𝑦 + 𝑦𝑥
(𝑎𝑦+𝑏𝑥)
=
𝑥𝑦
Page 6 of 20
Exercise 1
Write Java program that asks the user to enter an integer number than accomplish each of the
following tasks:
a) If the number is equal to 7, display "The variable number is equal to 7".
b) If the number is not equal to 7, display "The variable number is not equal to 7".
You have to resolve the exercise using if statement only.
Exercise 2
Write a Java program that determines whether a number n given by the user is a multiple of 5
or not.
Exercise 3
Write a Java program that determines whether a number n given by the user is outside the
range ]a, b[ or not where a and b are also given by the user in good order (a <= b).
Page 7 of 20
Part 1:
• Write a Java program that presents to the users a choice of your 4 favorite beverages
(Water, Milk, Juice, and Coffee).
• Then allow the user to choose a beverage by entering a number from 1 to 4.
• Output the name of which beverage they chose. Use switch to display the choice.
• Modify the program so that if the user enters a choice other than 1,2,3, and 4 then it will
output "Invalid choice."
Part 2:
• Modify your program to make the user enter a quantity of the chosen drink. If the user
enters quantity outside of the range [1,5] then the program will output "Invalid
quantity."
• Knowing that drink prices are respectively: 1 SAR, 1.5 SAR, 2 SAR, and 1.75 SAR,
print the total amount that the user should pay.
Part 3:
• Modify your program to ask the user to enter the amount of money first.
• Then he makes his choice and quantity.
• If the money entered is less than the total amount, print “You cannot buy this quantity
of the chosen drink” otherwise, print the total amount and the remaining money.
Page 8 of 20
Exercise 1
Write a Java program that computes the sum of even numbers less than or equal to n where n
is given by the user (without nesting the control statements).
Exercise 2
Write a Java application that computes the sum of even numbers and the product of odd
numbers that are in the range [a, b] where a and b are given by the user (without nesting the
control statements, to do so you have to use 2 separate loops).
Example 2:
Number 1 = 1
Number 2 = 2
Number 3 = 3
Number 4 = 4
Page 9 of 20
Page 10 of 20
Exercise 1
Write a Java program that computes the maximum of n numbers given by the user (use counter
and sentinel approaches).
Exercise 2
Write a Java program that counts the number of divisors of a number n given by the user.
Exercise 3
Write a Java program that determines whether a number n given by the user is prime or not.
(use: do/while)
Exercise 4
Write a Java program that prints the list of characters between two characters a and b given
by the user and supposed ordered.
Page 11 of 20
Sum = 10
The number of duplicates is: 6
Example 2:
Number 1 = 10
Number 2 = 20
Number 3 = 30
Number 4 = 40
Number 5 = 50
Number 6 = 60
Number 7 = 70
Number 8 = 80
Number 9 = 90
Number 10 = 100
Sum = 550
No duplicate numbers were entered.
Page 12 of 20
Exercise 1
Create a method that takes a value and indicate whether it is positive or negative by a return a
Boolean value.
Declared as: boolean isPositive (float a)
Exercise 2
Create a method that takes a value and tell whether it is null or not.
Declared as: boolean isZero (float a)
Exercise 3
Create a method delta() that takes three values, a, b and c, which are the coefficients of a
quadratic equation and returns the value of the delta, which is given by b ² - 4ac
Exercise 4
Using the 3 methods above, create an application that calculates the roots of an equation of
second degree:
ax ² + bx + c = 0
For it exists, the coefficient a must be nonzero.
If delta is positive, then there are two distinct roots:
Page 13 of 20
Page 14 of 20
Exercise 1
Create a method that randomly select a letter in the alphabet.
Exercise 2
Complete this code fragment which is supposed to count the number of alphanumeric
characters in a string s (you may use Character.isDigit and Character.isLetter methods to
check if a character is a digit or a letter respectively).
for(int i = 0; i < s.length(); i++) {
if (........................................................){
count++;
}
}
Exercise 3
The following two methods are supposed to compute the absolute value of 1 number and the
absolute value of the sum of 2 numbers respectively. Find and fix 2 errors to make these
methods work properly.
public static int abs(int n) { ............................
int c = a + b; ............................
Page 15 of 20
double s; ............................
s += v; ............................
Page 16 of 20
First, write a Java method merge that takes as parameter a non-empty double array representing
employees’ salaries. The method merges the values of every 2 adjacent elements. put the
merged values at the beginning of the array, and put 0 in the remaining positions. Suppose that
the original array will contain an even number of elements.
Finally, Use the following main method to test your different methods:
public class MergeSalary
{
private static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
double[] salaries = new double[10];
System.out.print("Enter 10 salaries: ");
readArray(salaries);
merge(salaries);
}
}
Hint: For the method merge, you can work on the same array or use a temporary array.
Run:
Enter 10 salaries:
1000 1000 2000 2000 3000 3000 4000 4000 5000 5000
Page 17 of 20
Second, write a java methods printArray(int[][]a) that writes the elements of an array
passed in parameter to the standard output.
Finally, use the following main method to test your different methods:
public class CheckSequence
{
public static void main(String[] args)
{
int[][] array1 = {{5,6,7,8},{9,10,11,12},{13,14,15,16}, {17,18,19,20}};
int[][] array2 = {{1, 3 , 2,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
if (isSequence(array1))
{
System.out.println("The following array elements are successive:");
printArray(array1);
}
if (isSequence(array2))
{
System.out.println("The following array elements are successive:");
printArray(array2);
}
}
}
Run Example:
The following array elements are successive:
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
Page 18 of 20
Write a Java method splitName() that takes a string (that contains 1 word or more (space-
separated)), split it into words, count the number of words, makes the first letter of each word
capital, and display each modified word in a line.
Write main() method that reads a full name (use nextLine() of the Scanner), removes
eventual extra spaces, and makes all characters of the full name in lower case. Then, sends the
name to splitName() method and displays the returned value.
Hints: use only String methods studied so far (don’t use split() which require array
manipulations that we didn’t study it yet)
Run:
Enter your full name: MohammAD BIN saud
Splitted name:
Mohammad
Bin
Saud
Page 19 of 20
Run Example 2:
Enter a hexadecimal string: 1f2bc9a
Decimal value is: 1 15 2 11 12 9 10
Hints: To check if a character c is a digit you can use Character.isDigit(c), and to check if it is
a letter you can use Character.isLetter(c).
Run Example 1:
Enter a password to check (at least 2 digits, 3 letters, and one char (- or _)): p5t4_i
The password is valid.
Run Example 2:
Enter a password to check (at least 2 digits, 3 letters, and one char (- or _)): i0z#-9k
The password is not valid.
Page 20 of 20