Thursday 4 August 2011

FUNCTIONS


1. Write a program to accept a sentence and reverse each word in that sentence and print it. Program should also count number of palindrome word present in that sentence.
Define a class that has the following functions.
  String reverseWord(String word) -> which reverse the word and return it.
  boolean isPalindrom(String word) -> which check and return true if the word is palindrome else return false.
  public void main(String Sentence) -> which print the sentence after reversing each word, and then print number of palindrome word present in it. class palindromeCount
{
  String reverseWord(String word)
  {
    String reverseWord="";
    int l=word.length();
    for(int i=l-1;i>=0;i--)
    reverseWord=reverseWord+word.charAt(i);
    return(reverseWord);
  }
  boolean isPalindrom(String word)
  {
    String w=reverseWord(word);
    return(word.equals(w));
  }
  public void main(String Sentence)
  {
    Sentence+=" ";
    int p=0,ctr=0;
    while(p<Sentence.length())
    {
      int x=Sentence.indexOf(' ',p);
      String word=Sentence.substring(p,x);
      System.out.print(reverseWord(word)+" ");
      if(isPalindrom(word)==true)
        ctr++;
      p=x+1;
    }
    System.out.println("\nNo of Palindrom Word : "+ctr);
  }
} 2. A number is called Armstrong number if the sum of cube of each digit of the number is equal to that number. (e.g. 153 is a Armstrong number because 153= 13 + 53 +33).
Define a class armStrong that has the following functions.
  private int sumOfDigit(int N) -> which return the sum of cube of each digits present in N.
  public static void main(int x, int y) -> which display all the arm strong number between the range x and y. class armStrong
{
  private static int sumOfDigit(int N)
  {
    int sum=0;
    while(N>0)
    {
      int digit=N%10;
      sum=sum+(digit*digit*digit);
      N=N/10;
    }
    return(sum);
  }
  public static void main(int x, int y)
  {
    for(int number=x;number<=y;number++)
    {
      if(number==sumOfDigit(number))
        System.out.println("Armstrong Number is "+number);
    }
  }
} 3. Write a program to input a positive natural number N and output all combination of consecutive natural numbers which added up to give N
  Example: If the N is 15 then output should be
    1 2 3 4 5 = 15
    4 5 6 = 15
    7 8 = 15
Define a class consecutive that has the following functions.
  private static void printSet(int i, int j) -> which print one line series of numbers starting from i to j, as mentioned in the above format.
  public static void main(int N) -> which will print all number of possibilities upto N, as mentioned in the above example. public class consecutive
{
  private static void printSet(int i, int j)
  {
    int s=0;
    for(int k=i;k<j;k++)
    {
      s+=k;
      System.out.print(k+" ");
    }
    System.out.println("= "+s);
  }
  public static void main(int N)
  {
    int i,j,s;
    for(i=1;i<N;i++)
    {
      s=0; j=i;
      while(s<N)
      {
        s=s+j; j++;
      }
      if(s==N)
      {
        printSet(i,j);
      }
    }
  }
}
3. Write a program to accept a sentence then convert it to Title Case. That means first character of each word should be capital and remaining are small letter. (e.g. MY NAME IS AMITABH become My Name Is Amitabh)
Define a class that has the following functions.
  private String properWord(String word) -> which convert the word to proper case (AMIT becomes Amit) and return it.
  public void main(String Sentence) -> which print the given sentence to in the above mention format. public class properSentence
{
  private String properWord(String word)
  {
    word=word.toLowerCase();
    char firstChar=Character.toUpperCase(word.charAt(0));
    String newWord=firstChar+word.substring(1);
    return(newWord);
  }
  public void main(String Sentence)
  {
    Sentence+=" ";
    int p=0;
    while(p<Sentence.length())
    {
      int x=Sentence.indexOf(' ',p);
      String word=Sentence.substring(p,x);
      System.out.print(properWord(word)+" ");
      p=x+1;
    }
  }
}
4. Write a program to determine if the input number is a lucky number or not. A lucky number is a number whose digits when added (till a single digit is obtained) it equals to 1. For example,
  4567 = 7+6+5+4 = 22 22=2+2=4 4 is not = 1
    So 4567 is not a lucky number.
  4627 = 7+6+2+4 = 19 19=1+9=10 10=1+0=1
    So 4627 is a lucky number.
Define a class luckyNumber that has the following functions.
  private int sumOfDigit(int N) -> which return the sum of digits present in N.
  public void isLucky(int number) -> which check and display a proper message, if the given number is Lucky number or not. class luckyNumber
{
  public int sumOfDigit(int N)
  {
    int sum=0;
    while(N>0)
    {
      int digit=N%10;
      sum=sum+digit;
      N=N/10;
    }
    return(sum);
  }
  public void isLucky(int number)
  {
    int digit;
    while(number>9)
    {
      number=sumOfDigit(number);
    }
    if(number==1)
      System.out.println("Lucky Number");
    else
      System.out.println("Not Lucky Number");
  }
}
5. A class clock has following members:
Instance variable: hour and minute of integer type.
Member function/methods:
  public clock(int,int) -> constructor to initialize hour and min.
  public void showTime() -> to display the time.
  public void addTime(clock,clock) -> to add to clock objects.
Write another class time with main() function which create two different objects FT, ST print their sum using the above clock class function. If FT = 6 Hrs 35 Min and ST = 3 Hrs 45 Min then output 10 Hrs 20 Min. class clock
{
  int hour, min;
  public clock(int h,int m)
  {
    hour=h;
    min=m;
  }
  public void showTime()
  {
    System.out.println(hour+" Hrs "+min+" Min");
  }
  public void addTime(clock T1, clock T2)
  {
    min=(T1.min+T2.min)%60;
    hour=(T1.hour+T2.hour)+(T1.min+T2.min)/60;
  }
}
public class time
{
  public static void main()
  {
    clock FT=new clock(6,35);
    clock ST=new clock(3,45);
    clock output=new clock(0,0);
    output.addTime(FT,ST);
    output.showTime();
  }
}
6. Convert each ward to Piglatin word in a sentence.
Define a class that has the following functions.
  private static int vowelPos(String word) -> which find and return the first vowel position in the word.
  private static String Piglatin(String word) -> which convert the word to Piglatin word and return it.
  public void main(String Sentence) -> which print the sentence after converting each word to Piglatin word. public class pigSentence
{
  private static int vowelPos(String word)
  {
    for(int i=0;i<word.length();i++)
    {
      char c=word.charAt(i);
      if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
        break;
    }
    return(i);
  }
  private static String Piglatin(String word)
  {
    int p=vowelPos(word);
    String newWord="";
    if(p==0)
      newWord=word;
    else
      newWord=word.substring(p)+word.substring(0,p)+"A";
    return(newWord);
  }
  public static void main(String Sentence)
  {
    Sentence+=" ";
    int p=0;
    while(p<Sentence.length())
    {
      int x=Sentence.indexOf(' ',p);
      String word=Sentence.substring(p,x);
      System.out.print(Piglatin(word)+" ");
      p=x+1;
    }
  }
}
7. N is a perfect number if the sum of all factors of the number (including 1 but excluding the number) is equal to N (for e.g. 6=1+2+3). N is a prime number if it is divisible by 1 and itself.
Define a class numProblem that has the following functions.
  int nthPrime(int N) -> which return the nth prime number(first prime number is 2) (e.g. 3rd prime number is 5)
  void perfectNosBelow(int N) -> which first print out the nth prime number and then print out all perfect numbers less then the nth prime number. public class numProblem
{
  public static int nthPrime(int N)
  {
    int ctr=0,Prime=2;
    while(true)
    {
      boolean flag=true;
      for(int i=2;i<Prime;i++)
      {
        if(Prime%i==0)
        {
          flag=false;
          break;
        }
      }
      if(flag==true) ctr++;
      if(ctr==N) break;
      Prime++;
    }
    return(Prime);
  }
  public static void perfectNosBelow(int N)
  {
    int X=nthPrime(N);
    System.out.println(N+"th Prime no. : "+X);
    for(int i=1;i<X;i++)
    {
      int sum=0;
      for(int j=1;j<i;j++)
      {
        if(i%j==0)
        {
          sum+=j;
        }
      }
      if(sum==i)
      {
        System.out.println(i);
      }
    }
  }
}
8. Your computer science teacher is trying to analyze the performance of the class in the previous exam. He has a class called performance that contains the marks of 50 students. These are not sorted. He wants to find two quantities. (i) Mode - The most frequently occurring mark in the class. If two or more marks occur equally frequently then the highest of these marks is the mode. (ii) Mode frequently - Frequency at mode. Important: You are not allowed to sort the marks.
Some of the instance variable and member functions of the performance class are given below.
  mark[] -> an array to store Marks of 50 students
  mode -> to store the Mode
  freqatmode -> the frequency at mode.
  void readmarks() -> for reading the marks into the array.
  void calcmodeandFrequency() -> a single function that calculates both mode and frequency at mode. import java.io.*;
class performance
{
  int mark[]=new int[10];
  int mode=0;
  int freqatmode=0;
  public void readmark() throws IOException
  {
    InputStreamReader isr=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(isr);
    for(int i=0;i<10;i++)
    {
      System.out.print("Enter Marks : ");
      mark[i]=Integer.parseInt(in.readLine());
    }
  }
  public void calcmodeandfrequency()
  {
    int f,i,j;
    for(i=0;i<10;i++)
    {
      f=0;
      for(j=0;j<10;j++)
      {
        if(mark[i]==mark[j])
          f++;
      }
      if(f>=freqatmode && mark[i]>=mode)
      {
        mode=mark[i];
        freqatmode=f;
      }
    }
  }
  public void main() throws IOException
  {
    readmark();
    calcmodeandfrequency();
    System.out.println("Marks : "+mode);
    System.out.println("Frequency : "+freqatmode);
  }
}[
1. Write a class MUHABRA to assign a proverbial sentence in a string variable using constructor. Then find and print the following using three different functions whose prototypes is given below:
i) No. of Words. void noOfWord()
ii) No. of Alphabets. void noOfAlphabets()
iii) No. of Special Characters. void noOfSpecialChar() public class MUHABARA
{
  String n;
  MUHABARA()
  {
    n="Minds are like Parachutes. They work best when open.";
  }
  void noOfWord()
  {
    int nw=0;
    for(int i=0;i<n.length();i++)
    {
      char c=n.charAt(i);
      if(c==' ')
        nw++;
    }
    System.out.println("No. of Words : "+(nw+1));
  }
  void noOfAlphabets()
  {
    int alph=0;
    for(int i=0;i<n.length();i++)
    {
      char c=n.charAt(i);
      if((c>=65 && c<=90) || (c>=97 && c<=122))
        alph++;
    }
    System.out.println("No. of Alphabet : "+alph);
  }
  void noOfSpecialChar()
  {
    int nd=0,sl=0,cl=0,ns=0;
    int l=n.length();
    for(int i=0;i<l;i++)
    {
      char c=n.charAt(i);
      int asc=c;
      if(asc>=48 && asc<=57)
        nd++;
      if(asc>=65 && asc<=90)
        cl++;
      if(asc>=97 && asc<=122)
        sl++;
      if(c==' ')
        ns++;
    }
    System.out.println("No. of Special Character : "+(l-(nd+sl+cl+ns)));
  }
  public static void main()
  {
    MUHABARA m=new MUHABARA();
    m.noOfWord();
    m.noOfAlphabets();
    m.noOfSpecialChar();
  }
}
9. A class clock has following members:
Instance variable: hour and minute of integer type.
Member function/methods:
public clock(int,int) -> constructor to initialize hour and min.
public void showTime() -> to display the time.
public void addTime(clock,clock) -> to add to clock objects.
Write another class time with main() function which create two different objects FT, ST print their sum using the above clock class function. If FT = 6 Hrs 35 Min and ST = 3 Hrs 45 Min then output 10 Hrs 20 Min. class clock
{
  int hour, min;
  public clock(int h,int m)
  {
    hour=h;
    min=m;
  }
  public void showTime()
  {
    System.out.println(hour+" Hrs "+min+" Min");
  }
  public void addTime(clock T1, clock T2)
  {
    min=(T1.min+T2.min)%60;
    hour=(T1.hour+T2.hour)+(T1.min+T2.min)/60;
  }
}
public class time
{
  public static void main()
  {
    clock FT=new clock(6,35);
    clock ST=new clock(3,45);
    clock output=new clock(0,0);
    output.addTime(FT,ST);
    output.showTime();
  }
}
10. A number is called Armstrong number if the sum of cube of each digit of the number is equal to that number. (e.g. 153 is a Armstrong number because 153= 13 + 53 +33).
Define a class armStrong with the following function definitions (do not write main function).
private int sumOfCubeOfDigit(int N) -> which return the sum of cube of each digits present in N.
public static void main(int x, int y) -> which display all the arm strong number between the range x and y. class armStrong
{
  private static int sumOfDigit(int N)
  {
    int sum=0;
    while(N>0)
    {
      int digit=N%10;
      sum=sum+(digit*digit*digit);
      N=N/10;
    }
    return(sum);
  }
  public static void main(int x, int y)
  {
    for(int number=x;number<=y;number++)
    {
      if(number==sumOfDigit(number))
        System.out.println("Armstrong Number is "+number);
    }
  }
}
11. Write a class ARRAY with void main() function to assign numbers into matrix mat[3][4] (using constructor) and compute the following using two separate functions, whose prototype is given below:
i) sum of each row. void sumOfRows()
ii) sum of each column. void sumOfColumn() // sum of each row and column total in 2D array //
import java.io.DataInputStream;
import java.io.IOException;
public class ARRAY
{
  int a[][]=new int[3][4];
  public ARRAY() throws IOException
  {
    DataInputStream x= new DataInputStream(System.in);
    int i,j;
    for(i=0;i<3;i++)
    {
      for(j=0;j<4;j++)
      {
        System.out.println("Enter Number : ");
        a[i][j]=Integer.parseInt(x.readLine());
      }
    }
  }
  public void sumOfColumns()
  {
    int i,j;
    for(i=0;i<3;i++)
    {
      for(j=0;j<4;j++)
        System.out.print(a[i][j]+" ");
      System.out.println();
    }
    int sum=0;
    for(j=0;j<4;j++)
    {
      sum=0;
      for(i=0;i<3;i++)
      {
        sum=sum+a[i][j];
      }
      System.out.println("Column "+(j+1)+" total is : "+sum);
    }
  }
  public void sumOfRows()
  {
    int i,j;
    for(i=0;i<3;i++)
    {
      for(j=0;j<4;j++)
        System.out.print(a[i][j]+" ");
      System.out.println();
    }
    int sum=0;
    for(i=0;i<3;i++)
    {
      sum=0;
      for(j=0;j<4;j++)
      {
        sum=sum+a[i][j];
      }
      System.out.println("Row "+(i+1)+" total is : "+sum);
    }
  }
  public static void main() throws IOException
  {
    ARRAY arr=new ARRAY();
    arr.sumOfRows();
    arr.sumOfColumns();
  }
}

No comments:

Post a Comment