Wednesday, September 8, 2010

Application Of User Defined Functions In BlueJ Programs - Part II


In this post I will try to show how we can utilize our own defined functions to manage the program effectively. In every program we call different predefined or library class functions from the body of another function. Here in the following programs of today’s post I will call different own defined functions from the body of another own defined function. This technique helps to organize the program is a better shape.

Program is to check whether a number is palprime or not

A palprime number is a number which is a prime number and at the same time a palindrome.

 class palprime
{
public void show(int n)
{
if(prime(n) && palin(n))
System.out.println("the number is palprime");
else
System.out.println("the number is not palprime");
}
boolean prime(int n)
{
int i;
for( i=2;i< n;i++)
{
if(n%i==0)
break;
}
if(i==n)
return true;
else
return false;
}
 boolean palin(int n)
{
int x;
int rev=0;
for(int i=n;i >0;i=i/10)
{
x=i%10;
rev=rev * 10 + x;
}
if(rev==n)
return true;
else
return false;
}
 public static void main(String args[])
{
palprime ob=new palprime();
ob.show(195);
}
}

 Brief stydy of the functions of the program

We can reat ‘public void show(int n)’ as the pivot function in this program. User will call this function with value and this function in tern call two other user defined functions, ‘boolean prime(int n)’ and ‘boolean palin(int n)’ and pass the value to the function argument. Both are return type functions and return boolean value. The functions checks whether the number is prime and palindrome recpectively and return boolean value accordingly.

Programs to check the prime factors of a number

In this program also I have defined two functions to display the result. The codes of the program are as follows:-

class PrimeFactors
{
public void show(int n)
{
for(int i=1;i< n;i++)
{
if(n%i==0 && prime(i))
System.out.println(i);
}
}
boolean prime(int n)
{
int i;
for( i=2;i< n;i++)
{
if(n%i==0)
break;
}
if(i>=n)
return true;
else
return false;
}
public static void main(String args[])
{
PrimeFactors ob=new PrimeFactors();
ob.show(195);
}
}

 Brief stydy of the user defined functions of the program

Initially the number is passed as argument to the function ‘public void show(int n)’. This function genarates the factors and pass the factor as argument to another boolean return type function ‘boolean prime(int n)’ which checks whther the argument value is prime of not.

Programs to display twin prime numbers within a specified range

Twin prime numbers means two consecutive prime numbers with a gap of 1.

class TwinPrime
{
public void show(int n,int m)
{
for(int i=n;i< =m-2;i++)
{
if(prime(i) && prime(i + 2))
System.out.println(i +" "+(i + 2));
}
}
boolean prime(int n)
{
int i;
for( i=2;i< n;i++)
{
if(n%i==0)
break;
}
if(i==n)
return true;
else
return false;
}
public static void main(String args[])
{
TwinPrime ob=new TwinPrime();
ob.show(10,159);
}
}

 Functions of the program
‘public void show (int n,int m)’ function is the pivotal function of this program. The range is passed to the function and it displays the twin prime numbers within the range. ‘boolean prime (int n)’ is another defined function in this program which is called from the show function twice on each iteration of the loop inside show() function. The values passed as argument are consecutive values with a gap of 1 and from the return value of the prime () function it is decided whether the numbers are twin prime or not.

Related PostBlueJ Programs on Number

8 comments:

  1. Can I get the Answers for the following Questions. I dont know how to answer exactly.
    1. Define an object
    2. What is a method.
    3. Can there be objects without having any methods?
    4.Why are methods so important for the description of objects
    5. What is a class
    6. What is an abstraction
    7. Can there be multiple abstractions of a real world entity
    8. How are classes and abstraction linked
    9. How do u map an abstraction into software
    10.What is an object factory

    ReplyDelete
  2. Dear Smila,
    Since you have number of questions, I will post them in my normal posting very soon.

    ReplyDelete
  3. How do you explain these two statements to children?
    TwinPrime ob=new TwinPrime();
    ob.show(10,159);

    ReplyDelete
  4. Your question is not clear to me. Please state clearly what you want to know. The statement 'ob.show(10,159)' is described in 'Functions of the program' part and about the first statement? It is used to create the object and hold the reference of the object in 'ob'.

    ReplyDelete
  5. How do i get back to main after i am done in a different method.this is part of a user defined program.please help me as it is part of my board project

    ReplyDelete
  6. Whenever you call any function, after execution the control comes back to the calling program. If you are calling any function from 'main ()', after the execution control will automatically comes back to main.

    ReplyDelete
  7. can you write the armstrong number program??

    ReplyDelete
    Replies
    1. Please search this site. This program is posted earlier.

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner