Pass Parameters by Value: Public Class Increment (Public Static Void Main (String Args) (
Pass Parameters by Value: Public Class Increment (Public Static Void Main (String Args) (
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
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.
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.
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
}//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.
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.
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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 10