[go: up one dir, main page]

0% found this document useful (0 votes)
52 views10 pages

Pass Parameters by Value: Public Class Increment (Public Static Void Main (String Args) (

The document discusses passing parameters by value in Java. It explains that when a parameter is passed to a method, the value is passed rather than the variable itself. Any changes to the parameter inside the method do not affect the original variable. This is illustrated with examples of passing an integer to an increment method and passing two integers to a swap method. Neither method affects the original variables.

Uploaded by

Saddam Qaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views10 pages

Pass Parameters by Value: Public Class Increment (Public Static Void Main (String Args) (

The document discusses passing parameters by value in Java. It explains that when a parameter is passed to a method, the value is passed rather than the variable itself. Any changes to the parameter inside the method do not affect the original variable. This is illustrated with examples of passing an integer to an increment method and passing two integers to a swap method. Neither method affects the original variables.

Uploaded by

Saddam Qaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Pass Parameters by Value

public class Increment {


public static void main(String[] args) { public static void increment(int n) {
int x = 1; n++;
System.out.println("Before the call, x is " + x); System.out.println("n inside the method is " + n);
increment(x); }
System.out.println("after the call, x is " + x); }
} The value of n is changed.
Executing increment does not
The value of x is affect n
passed to n. Space required for
the increment
method
Space required for the
increment method n: 2
n= 1
Space required for the
Space required for the main method
Space required for the
main method
main method
x= 1 x= 1
x= 1

The main method The increment The increment method


is invoked method is invoked is executing

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 1
public class TestPassByValue {
public static void main(String[] args) {
// Declare and initialize variables Example
int num1 = 1;
int num2 = 2;
System.out.println("Before invoking the swap method, num1 is " + num1 + " and num2 is " + num2);
// Invoke the swap method to attempt to swap two variables
swap(num1, num2);
System.out.println("After invoking the swap method, num1 is " + num1 + " and num2 is " + num2);
} The values n1 and n2 are
The values of num1 and swapped/. Executing swap does
num2 are passed to n1 not affect num1 and num2
and n2.
Space required for
Space required for the the swap method
swap method
temp: temp:1
n2: 2 n2: 1
n1: 1 n1: 2
Space required for the Space required for the Space required for the
main method main method main method
num2: 2 num2: 2 num2: 2
num1: 1 num1: 1 num1: 1

The main method The swap method The swap method


is invoked is invoked is executing

public static void swap(int n1, int n2) {


System.out.println("\tInside the swap method");
System.out.println("\t\tBefore swapping n1 is " + n1 + " n2 is " + n2);
// Swap n1 with n2
int temp = n1;
n1 = n2;
n2 = temp;
System.out.println("\t\tAfter swapping n1 is " + n1 + " n2 is " + n2);
}
}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 2
CAUTION
A return statement is required for a value-returning method. The
method shown below in (a) is logically correct, but it has a
compilation error because the Java compiler thinks it possible that
this method does not return any value.
public static int sign(int n) { public static int sign(int n) {
if (n > 0) Should be if (n > 0)
return 1; return 1;
else if (n == 0) else if (n == 0)
return 0; return 0;
else if (n < 0) else
return –1; return –1;
} }
(a) (b)

To fix this problem, delete if (n < 0) in (a), so that the compiler will
see a return statement to be reached regardless of how the if
statement is evaluated.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 3
Problem
Write a program that displays the first 100 palindromic prime numbers. Example 151 is
prime and palindrome.

First write the below methods:


a. public static int reverse(int number), example reverse(1234) returns 4321
b. public static boolean isPrime(int number), example isPrime(11) returns true
c. public static boolean isPalindrome(int number), example isPalindrome(121) returns true
d. Public static void main method(String[] args)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 4
Part (a): Display an integer reversed
 Write a method with the following header to return an integer in reverse
order:
public static int reverse(int number)
For example, reverse(1234) returns 4321.

Java reverse an int value - Principles


1. Modding (%) the input int by 10 will extract off the rightmost digit. example: (1234 % 10) = 4
2. Multiplying an integer by 10 will "push it left" exposing a zero to the right of that number,
example: (5 * 10) = 50
3. Dividing an integer by 10 will remove the rightmost digit. (75 / 10) = 7

Java reverse an int value - Pseudocode:


a. Extract off the rightmost digit of your input number. (1234 % 10) = 4
b. Take that digit (4) and add it into a new reversedNum.
c. Multiply reversedNum by 10 (4 * 10) = 40, this exposes a zero to the right of your (4).
d. Divide the input by 10, (removing the rightmost digit). (1234 / 10) = 123
e. Repeat at step a with 123

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 5
Solution part (a)
Step 1 — Isolate the last digit in number
public class Exercise05_04 {
lastDigit = number % 10
public static void main(String[] args) { Step 2 — Append lastDigit to reverse
System.out.print("Enter an integer: "); reverse = (reverse * 10) + lastDigit
java.util.Scanner input = new Scanner(System.in); Step 3-Remove last digit from number
int number = input.nextInt(); number = number / 10
int result= reverse(number); Iterate this process
while (number > 0)
System.out.print(result);
}//end main method

public static int reverse(int number) {


int rev=0;
while (number != 0) {
int remainder = number % 10;
rev=rev*10+remainder;
number = number / 10;
}
return rev;
}//end method

}//end of class

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 6
Part (b)
 Write a method to return whether an integer is prime or not:
public static boolean isPrime(int number)
For example, isPrime(11) returns true.

1. public static boolean isPrime(int num) {


2. for (int i = 2; i <= num / 2; i++) {
3. if (num % i == 0) {
4. return false;
5. }
6. }
7. return true;
8. }

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 7
Part (c)
Write the below method to return true if the number is a palindrome
public static boolean isPalindrome(int number)
Note Use the reverse method to implement isPalindrome.
A number is a palindrome if its reversal is the same as itself.

/** Return true if number is palindromic */


public static boolean isPalindrome(int number) {
if(number == reverse(number)) The code is equivalent to:
return (number == reverse(number));
return true;
else
return false;
}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 8
Part (d)
A palindromic prime is a prime number and also palindromic. For example, 131 is a prime and also a
palindromic prime, as are 313 and 757.

Write a program that displays the first 100 palindromic prime numbers.

1. public static void main(String[] args) {


2. int count = 0, i=1;
3. while(count<100) {
 if (isPrime(i) && isPalindrome(i)) {
count++; // Increase count
 System.out.print(i + " ");
 }


i++;
 }//end while loop
1. }

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 9
Part (d)
A palindromic prime is a prime number and also palindromic. For example, 131 is a prime and also a
palindromic prime, as are 313 and 757.

Write a program that displays the first 100 palindromic prime numbers.

1. public static void main(String[] args) {


2. int count = 0, i=1;
3. while(count<100) {
 if (isPrime(i) && isPalindrome(i)) {
count++; // Increase count
 System.out.print(i + " ");
 }
1. if (count % 10 == 0) {
2. System.out.println();
3. }

i++;
 }//end while loop
1. }

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 10

You might also like