Friday 2 June 2017

JAVA Tutorial-3

  1. Create an array of String variables and initialize the array with the names of the months from January to December. Create an array containing 12 random decimal values between 0.0 and 100.0. Display the names of each month along with the corresponding decimal value. Calculate and display the average of the 12 decimal values.
  2. Write a program to accept a line and check how many consonants and vowels are there in line.
  3. Write a program to find length of string and print second half of the string.
  4. Write a program to find that given number or string is palindrome or not.
  5. Write a program that sets up a String variable containing a paragraph of text of your choice. Extract the words from the text and sort them into alphabetical order.
    Display the sorted list of words. Also count the number of words that start with capital letters. You could use a simple sorting method called the bubble sort.
    To sort an array into ascending order the process is as follows:
    a. Starting with the first element in the array, compare successive elements (0 and 1, 1 and 2, 2 and 3, and so on).
    b. If the first element of any pair is greater than the second, interchange the two elements.
    c. Repeat the process for the whole array until no interchanges are necessary. The array elements will now be in ascending order.
  6. Create a class which ask the user to enter a sentence, and it should display count of each vowel type in the sentence. The program should continue till user enters a word quit. Display the total count of each vowel for all sentences.

1. Create an array of String variables and initialize the array with the names of the months from January to December. Create an array containing 12 random decimal values between 0.0 and 100.0. Display the names of each month along with the corresponding decimal value. Calculate and display the average of the 12 decimal values.

 
class Month
{
   public static void main(String a[])
   {
 String[] mon={"January","Febuary","March","April","May","June","July","August","september","October","November","December"}; 
 int i;
 double sum=0; 
 double n[]=new double[12];
 System.out.println("Random number");
 for(i=0;i<12;i++)
 {
  n[i]=((100.00*Math.random())+1);
 }
 for(i=0;i<12;i++)
 {
  System.out.print(+n[i]+".");
  System.out.println(mon[i]);
  sum=sum+n[i];
 }  
 System.out.println("Sum of 12 number :"+sum); 
 System.out.println("Average :"+(sum/12));
   }
}

Output

2. Write a program to accept a line and check how many consonants and vowels are there in line.

 
class Consonants
{
   public static void main(String a[])
   {
 String s=a[0];
 int l=(int)(s.length()),i,vcnt=0,ccnt=0;
 for(i=0;i<l;i++)
 {
  char c=s.charAt(i);
  if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
   vcnt++;
  else
   ccnt++;
 }
 System.out.println("Total Vowels :"+vcnt);
 System.out.println("Total Constants :"+ccnt);
   }
}

Output

3. Write a program to find length of string and print second half of the string.

 
import java.util.Scanner;
class SecondHalfStr
{
   public static void main(String str[])
   {
 System.out.print("Enter a String: ");
 Scanner sc=new Scanner(System.in);
 String st=sc.nextLine();
        System.out.println("Full String Length: "+st.length());
 String st1=st.substring((st.length())/2);
 System.out.println("Half of the String are: "+st1);
   }
}

Output

4. Write a program to find that given number or string is palindrome or not.

 
import java.util.Scanner;
class Palindrome
{
   public static void main(String str[])
   {
 Scanner sc=new Scanner(System.in);
 int ch=0;
 while(ch!=3)
 {
  System.out.println("\n1.Number");
  System.out.println("2.String"); 
  System.out.println("3.Exit");
  System.out.print("Enter your Choice to Print 'PALINDROME' or not :");
  ch=sc.nextInt();
  switch(ch)
  {
   case 1:
   System.out.println("Enter Numbers :");
   int no=sc.nextInt(); 
   int temp=no,rev=0;
   while(temp!=0)
   {
    rev = rev * 10;
    rev = rev + temp%10;
    temp = temp/10;
   }
   if(rev==no)
   {
    System.out.println(rev+" Number is 'PALINDROME'");
   }
   else
   {
           System.out.println(rev+" Number is not 'PALINDROME'");
   }
      break;
    case 2:
   System.out.print("\nEnter String :");
   String s=sc.next();
   String s2="";
   for(int i=(s.length())-1;i>=0;i--)
   {
    s2=s2+s.charAt(i);
   }
   if(s==s2)
   {
    System.out.println(s+" String is not 'PALINDROME'");
   }
   else
   {
    System.out.println(s+" String is 'PALINDROME'");
   }
   break;
                        case 3:
                                ch=3;
                        break;
   default:
   System.out.println("Invalid Choice");
   break;
  }
 }
   }
}

Output

5. Write a program that sets up a String variable containing a paragraph of text of your choice. Extract the words from the text and sort them into alphabetical order.
Display the sorted list of words. Also count the number of words that start with capital letters. You could use a simple sorting method called the bubble sort.
To sort an array into ascending order the process is as follows:
a. Starting with the first element in the array, compare successive elements (0 and 1, 1 and 2, 2 and 3, and so on).
b. If the first element of any pair is greater than the second, interchange the two elements.
c. Repeat the process for the whole array until no interchanges are necessary. The array elements will now be in ascending order.

 
import java.util.Scanner;
class Splitstring
{
 String s;
 String st[];
 int count=0;
 Splitstring(String str)
 {
  s=str;
 }
 void splitsort()
 {
  int i;
  st=s.split(" ");
  for(i=0;i<(st.length)-1;i++)
  {
   for(int j=0;j<(st.length)-1;j++)
   {
    if(st[j].compareTo(st[j+1])>0)
    { 
     String temp=st[j];
     st[j]=st[j+1];
     st[j+1]=temp;
    }
   } 
  }
  for(i=0;i<(st.length);i++)
  {
   if(st[i].charAt(0)>=64 && st[i].charAt(0)<=90)
   {
    count++;
   }
  }
 }
 void display()
 {
  System.out.println("Sorted Array");
  for(int i=0;i<(st.length);i++)
  {
   System.out.println(st[i]);
  }
  System.out.println("Start with capital Alphabet in array: "+count);
 }
 public static void main(String str[])
 {
  Scanner sc=new Scanner(System.in);
  String a;
  System.out.print("Enter tthe String: ");
  a=sc.nextLine();
  Splitstring spl=new Splitstring(a);
  spl.splitsort();
  spl.display();
 }
}

Output

6. Create a class which ask the user to enter a sentence, and it should display count of each vowel type in the sentence. The program should continue till user enters a word quit. Display the total count of each vowel for all sentences

 
import java.util.Scanner;
class AskUserDemo
{
 String st;
 int vo[]=new int[5];
 void str()
 {
  Scanner sc=new Scanner(System.in);
  while(true)
  {
   System.out.println("Enter the Sentence: ");
   st=sc.nextLine();
   if(st.equals("quit"))
   {
    break;
   }
   else
   {
    for(int i=0;i<(st.length());i++)
    {
     if(st.charAt(i)=='a' || st.charAt(i)=='A')
     {
      vo[0]++;
     }
     else if(st.charAt(i)=='e' || st.charAt(i)=='E')
     {
      vo[1]++;
     }
     else if(st.charAt(i)=='i' || st.charAt(i)=='I')
     {
      vo[2]++;
     }
     else if(st.charAt(i)=='o' || st.charAt(i)=='O')
     {
      vo[3]++;
     }
     else if(st.charAt(i)=='u' || st.charAt(i)=='U')
     {
      vo[4]++;
     }
    }
   }
   System.out.println("a or A is "+vo[0]+" times");
   System.out.println("e or E is "+vo[1]+" times");
   System.out.println("i or I is "+vo[2]+" times");
   System.out.println("o or O is "+vo[3]+" times"); 
   System.out.println("u or U is "+vo[4]+" times");
  }
 }
 public static void main(String str[])
 {
  AskUserDemo as=new AskUserDemo();
  as.str();
 }
}

Output

Share:

0 comments:

Post a Comment