You are on page 1of 4

Alexandria National University

Faculty of Computer and Information


Lecturer: Dr. Amr AboHani, Dr. Ahmed Saleh, Dr. Ahmed Moustafa
Course: Programming 2
Lab #: 0
-------------------------------------------------------------------------------------------------------------------------------
Lab Objectives
1- Review on Programming 1.
Part 1: In Lab (Discuss with students)
Question 1. Write a Method Power() that receives two numbers x and y and
calculates without using the pow() function.
Question 2. Write a method that takes a number and tests whether it is prime or
NOT.
isprime(n): returns true if the integer number n is a prime number
Prime numbers are divisible only by itself and 1 ex.2,3,5,..
Question 3. Write a program that prints out the factorial of numbers from 1 to 10.
[hint: You must write a method to calculate factorial]
Question 4. Create a Java method named isPalindrome that checks if a given string
is a palindrome (reads the same forwards and backwards).
Question 5. Write a Java method to check whether every digit of a given integer
is Even. Return true if every digit is Even otherwise false.
Expected Output:
Input an integer: 8642
Check whether every digit of the said integer is even or not!
true
Question 6. Write a Java method to count all the words in a string.
Test Data:
Input the string: The quick brown fox jumps over the lazy dog.
Expected Output:
Number of words in the string: 9
[HINT: Test case of multi-spaces between words]

Page 1 of 4
Alexandria National University
Faculty of Computer and Information
Lecturer: Dr. Amr AboHani, Dr. Ahmed Saleh, Dr. Ahmed Moustafa
Course: Programming 2
Lab #: 0
-------------------------------------------------------------------------------------------------------------------------------
Part 2: Students work in Lab.
Question 1. What is the output of the following?
a-

b-

Page 2 of 4
Alexandria National University
Faculty of Computer and Information
Lecturer: Dr. Amr AboHani, Dr. Ahmed Saleh, Dr. Ahmed Moustafa
Course: Programming 2
Lab #: 0
-------------------------------------------------------------------------------------------------------------------------------
Question 2. Write a Java method to find the smallest number among three
numbers.
Test Data:
Input the first number: 25
Input the Second number: 37
Input the third number: 29
Part 3: Homework
Question 1. What is the output of the following
a-
class Test{
static void set (int x){
int y=x+2;
x*=2;
}
public static void main(String[] args) {
{
int x=10,y=11;
set(x);
set(y);
System.out.println("X= "+ x+" ,Y= "+y);
}
}

b-
public class Test1 {
static void func(int x, double y){ System.out.println((x+y/2)); }
static void func(double x, int y) { System.out.println((x-y*2)); }
public static void main(String[] args) {
int a = 2;
double b = 5;
func(a, b);
func(b, a);
Page 3 of 4
Alexandria National University
Faculty of Computer and Information
Lecturer: Dr. Amr AboHani, Dr. Ahmed Saleh, Dr. Ahmed Moustafa
Course: Programming 2
Lab #: 0
-------------------------------------------------------------------------------------------------------------------------------
}
}
c-
class Test{
static void swap1(int x[]){
int temp;
temp=x[0];
x[0]=x[1];
x[1]=temp;
}
public static void main(String[] args) {
int A[]={2,6};
System.out.println("First two element in array before call swap1 are
"+A[0]+","+A[1]);
swap1(A);
System.out.println("First two element in array After call swap1 are
"+A[0]+","+A[1]);
}
}

Page 4 of 4

You might also like