0% found this document useful (0 votes)
12 views4 pages

Programming 2 Lab Exercises

This document outlines the objectives and questions for Programming Lab #0. The lab objectives include reviewing concepts from Programming 1 and working through 6 programming questions. Students will work through the questions in lab and have additional homework questions. The homework further tests concepts like method overloading, passing arrays to methods, and analyzing code output.

Uploaded by

toqarabah739
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)
12 views4 pages

Programming 2 Lab Exercises

This document outlines the objectives and questions for Programming Lab #0. The lab objectives include reviewing concepts from Programming 1 and working through 6 programming questions. Students will work through the questions in lab and have additional homework questions. The homework further tests concepts like method overloading, passing arrays to methods, and analyzing code output.

Uploaded by

toqarabah739
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

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);
[Link]("X= "+ x+" ,Y= "+y);
}
}

b-
public class Test1 {
static void func(int x, double y){ [Link]((x+y/2)); }
static void func(double x, int y) { [Link]((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};
[Link]("First two element in array before call swap1 are
"+A[0]+","+A[1]);
swap1(A);
[Link]("First two element in array After call swap1 are
"+A[0]+","+A[1]);
}
}

Page 4 of 4

Common questions

Powered by AI

The isprime(n) method determines if an integer is prime by checking divisibility only by 1 and itself. For example, with input 11, the method would attempt to divide 11 by every integer from 2 up to the square root of 11. Since none of these divisions result in an integer, 11 is concluded to be a prime number. The method returns true since the only factors of 11 are 1 and 11 itself .

The method examines each digit of the input number, checking if it is even by using the modulus operation (digit % 2 == 0). If it finds any digit that is odd, it immediately returns false. For the input 8642, all digits are even, so the method iterates through each and returns true, confirming that every digit is even .

The method calculates word count by splitting the string on spaces. Based on the test data, it accurately counts 9 words. However, its effectiveness in handling edge cases like multiple spaces between words could vary. A robust implementation should trim spaces and ensure continuous spaces don't affect the true word count. It's crucial for correctness in diverse input cases .

In the first homework question, the method set(int x) demonstrates the concept of immutability by not altering the original variables in the main method. When set is called with x and y, it only modifies the local copy of the variable x within the method's scope, therefore not affecting the original x and y in the main method. Immutability is shown as the primitive types are passed by value, hence the outputs remain as X= 10 ,Y= 11 .

The method compares three numbers using conditional statements to determine the smallest. For inputs 25, 37, and 29, it first checks if 25 is less than both 37 and 29; it is, so it identifies 25 as the smallest. The logic ensures each number gets compared appropriately, identifying the minimum of the set .

In the Test1 class, method overloading is demonstrated by defining two methods with the same name, func, but with different parameter types. The first func accepts an int and a double, while the second takes a double and an int. This allows different operations depending on the order and type of arguments provided. The JVM determines which method to call based on the arguments. Therefore, func(a, b) invokes the first version, adding x with y/2, yielding 4.5, and func(b, a) calls the second version, resulting in (5 - 2*2) or 1.0 .

The Power() method can be implemented using a loop to multiply x by itself y times, or alternatively using recursion where y equals zero returns one, and otherwise, it returns x multiplied by the Power() method called with x and y-1. This iteration or recursion accumulates the result of x raised to the power of y without relying on Java's pow() function .

The swap1 method effectively alters the contents of the array due to Java's pass-by-reference nature for arrays. When the method is called with an array, the method accesses and modifies the original array elements directly, swapping the first and second elements. This is reflected in the change from '2,6' to '6,2' before and after the call. This demonstrates successful in-place modification of the array contents .

The isPalindrome method checks if a string reads the same forwards and backwards by comparing characters from both ends moving toward the center. For instance, for input 'racecar', the method successively checks if 'r' equals 'r', 'a' equals 'a', and so on. If any characters don't match, it returns false; otherwise, true when all matched .

Encapsulation in the swap1 method is exemplified by passing the entire array reference, allowing the method to encapsulate the sorting logic internally without exposing internal state manipulation outside. This aligns with encapsulation's principle of detail hiding and interface exposing, making the method effective as it modifies data structure through direct reference .

You might also like