Showing posts with label Java Method Returning Boolean Value. Show all posts
Showing posts with label Java Method Returning Boolean Value. Show all posts

Saturday, 30 May 2015

Java Method Returning Boolean Value

The Java supports primitive data type boolean, which can be used as return type.

Example: The following program uses a method that returns boolean value true or false depending on whether a given positive integer number greater than 2 is prime or not.

1 class CheckPrime

2 { static boolean checkPrime(int number)

3 { int root = (int) Math.sqrt(number);

4 for(int i  = 2; i <= root; i++)

5 { if( number % i = = 0)

6 return false; 

7 }

8 return true;

9 } 

10 public static void main(String a[])

11 { int n = Integer.parseInt(a[0]);

12 boolean prime = checkPrime(n);

13 if(prime)

14 System.out.println("The number " + n + " is prime");

15 else

16 System.out.println("The number " + n + " is not prime");

17 }

18 }

13. Examples

Example: The following example shows the static implementation (using array) of stack.

1 class Stack

2 { private int top = -1;

3 private int a[] = new int[10];

4 void push(int x)

5 { if(top == 9)

6 { System.out.println("Stack Full");

7 return;

8 }

9 top = top + 1;

10 a[top] = x;

11 }

12 int pop()

13 { if(top ==  -1)

14 { System.out.println("Stack Empty");

15 return -1; //assuming that -1 is not a valid data

16 }

17 int x = a[top]; top = top - 1;

18 return x;

19 }

20 }

class StackDemo

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

2 { Stack s1 = new Stack();

3 Stack s2 = new Stack();

4 for(int i = 0; i < 10; i++)

5 { s1.push(i);

6 }

7 for(int i = 10; i < 20; i++)

8 { s2.push(i);

9 }

10 int x;

11 for(int i = 0; i < 10; i++)

12 { x = s1.pop();

13 System.out.print(x + " ");

14 }

15 System.out.println();

16 for(int i = 0; i < 10; i++)

17 { x = s2.pop();

18 System.out.print(x + " ");

19 }

20 }

Output:

9 8 7 6 5 4 3 2 1 0

19 18 17 16 15 14 13 12 11 10

Example: The following example demonstrates that only one copy per class is created in case of static members. Thus the implementation is not correct if data member top and a[] are declared to be static as in that case all the objects of Stack class will share the same memory.

1 class Stack

2 { static int top = -1;

3 static int a[] = new int[10];

4 static void push(int x)

5 { if(top == 9)

6 { System.out.println("Stack Full"); 

7 return;

8 }

9 top = top + 1;

10 a[top] = x;

11 }

12 static int pop()

13 { if(top ==  -1)

14 { System.out.println("Stack Empty");

15 return -1; //assuming that -1 is not a valid data

16 }

17 int x = a[top];

18 top = top - 1;

19 return x;

20 }

21 }

1 class StackDemo

2 { public static void main(String args[])

3 { Stack s1 = new Stack();

4 Stack s2 = new Stack();

5 for(int i = 0; i < 10; i++)

6 { s1.push(i);

7 }

8 for(int i = 10; i < 20; i++)

9 { s2.push(i);

10 }

11 int x;

12 for(int i = 0; i < 10; i++)

13 { x = s1.pop();

14 System.out.print(x + " ");

15 }

16 System.out.println();

17 for(int i = 0; i < 10; i++)

18 { x = s2.pop();

19 System.out.print(x + " ");

20 }

21 }

22 }

Output:

Stack Full

Stack Full

Stack Full

Stack Full

Stack Full

Stack Full

Stack Full

Stack Full

Stack Full

Stack Full

9 8 7 6 5 4 3 2 1 0

Stack Empty

-1 Stack Empty

-1 Stack Empty

-1 Stack Empty

-1 Stack Empty

-1 Stack Empty

-1 Stack Empty

-1 Stack Empty

-1 Stack Empty

-1 Stack Empty

-1

Example: The following example shows the dynamic implementation (using linked list) of stack.

1 class Node

2 { Node nextNode;

3 int x;

4 }

1 class Stack

2 { Node top;

3 void push(int item)

4 { Node temp;

5 if(top == null)

6 { top = new Node();

7 top.x = item; top.nextNode = null;

8 }

9 else

10 { temp = new Node();

11 temp.x = item;

12 temp.nextNode = top;

13 top = temp;

14 }

15 }

16 int pop()

17 { if(top == null)

18 { System.out.println("Stack is empty");

19 return(-1);

20 }

21 else

22 { int x = top.x; top = top.nextNode;

23 return x;

24 }

25 }

26 } 

1 class StackTest

2 { static public void main(String args[])

3 { Stack st = new Stack();

4 st.pop();

5 for(int i = 0; i < 5; i++)

6 st.push(i); 

7 for(int i = 0; i <= 5; i++)

8 System.out.println(st.pop()); 

9 }

10 }

Output:

Stack is empty

4

3

2

1

0

Stack is empty

-1