Monday, March 20, 2023

BlueJ Program On String Sorting And Swapping First And Last Character

 
                     A class SwapSort has been defined to string related manipulations on a input word.
                     Some of the members of the class are as follows
                     wrd: To store the word
                     len: length of the word
                     swapwrd: to store the swapped word
                     sortwrd: to store the sorted word
                     
                     SwapSort(): default constructor ti initialize the members with valid value
                     void readWord(): accept the word in upper case 
                     void sortWord(): sort the word in ascending order and stores in sortwrd
                     void swapChar(): Exchange the first and last character of the word and stores in swapwrd
                     void display (): Display all the words (original, swapped and sorted
                         

import java.util.*;
public class SwapSort
{
String wrd, swapwrd, sortwrd;
int len;
Scanner sc=new Scanner(System.in);
SwapSort()
{
    wrd="";
    sortwrd="";
    swapwrd="";
    len=0;
}

void readWord()
{
    System.out.print("\nEnter the word: ");
    wrd=sc.nextLine().toUpperCase();
}

void swapChar()
{
    len=wrd.length();
    System.out.println(wrd.charAt(len-1));
    swapwrd=swapwrd+wrd.charAt(len-1);
    for(int i=1;i<len-1;i++)
    swapwrd=swapwrd+wrd.charAt(i);
    swapwrd=swapwrd+wrd.charAt(0);
}
void sortWord()
{
    char c;
    char ch[]=new char[len];
    for(int i=0;i<len;i++)
    ch[i]=wrd.charAt(i);
    for(int i=0;i<len-1;i++)
    {
         for(int j=i+1;j<len;j++)
         {
             if(ch[i]>ch[j])
             {
                 c=ch[i];
                 ch[i]=ch[j];
                 ch[j]=c;
             }
         }
    }
    sortwrd=new String(ch);
                 
  }
       
void display()
{
    System.out.println("\nOriginal Word:"+wrd);
    System.out.println("\nSorted Word:"+sortwrd);
    System.out.println("\nSwapped Word:"+swapwrd);
}
    public static void main(String[] args) 
    {
    SwapSort ob=new SwapSort();
   ob.readWord();
   ob.swapChar();
   ob.sortWord();
   ob.display();
}
}

Sunday, March 12, 2023

Mixing Two Ascending order Arrays into Third Array

 
A class Mixer has been defined to merge two sorted integer arrays in ascending order. Some of the member function and variables are as follows.

int arr[]: stores the elements in ascending order
int n=size of array

Mixer (int nn): constructor to assign n=nn;

void accept(): accepts elements in ascending order without any duplicate

Mixer mix(Mixer A): Merge current object array elements with parameterised object array into the array of a third object and returns the object

void display(): display array of all objects

Define main () method and create objects and invoke all methods


import java.util.*;
public class Mixer
{
int arr[];
int n;
Mixer(int nn)
{
     n=nn;
     arr=new int [n];
}
void accept()
{
   Scanner sc=new Scanner(System.in);
   for(int i=0;i<n;i++)
   {
       System.out.print("\nEnter Value:");
       arr[i]=sc.nextInt();
   }    
}
Mixer mix(Mixer A)
{
     int x=n+A.n;
     Mixer ob=new Mixer(x);
     int i,j,k;
     i=0;
     j=0;
     k=0;
     while(i<n && j<A.n)
     {
         if(arr[i]<=A.arr[j])
         {
             ob.arr[k++]=arr[i];
             i++;
         }
         else
         {
           ob.arr[k++]=A.arr[j];
             j++;  
             
         }
         if(i==n|| j==A.n)
         break;
     }
         if(i==n)
     {
         for(;k<x;k++)
         {
              ob.arr[k]=A.arr[j];
             j++;  
         }
     }
     else
     {
        for(;k<x;k++)
         {
             ob.arr[k]=arr[i];
             i++;
         }
     }
     return ob;
     }
    void display()
    {
        for(int i=0;i<n;i++)
        System.out.print(arr[i]+" ");
    }
public static void Mixer(String[] args) 
{
Mixer ob1,ob2,ob3;
ob1=new Mixer(5);
ob2=new Mixer(6);
System.out.print("\n1st Array Input\n");
ob1.accept();
System.out.print("\n2nd Array Input\n");
ob2.accept();
ob3=ob1.mix(ob2);
System.out.print("\n1st Array Values\n");
ob1.display();
System.out.print("\n2nd Array Values\n");
ob2.display();
System.out.print("\n3rd Array Values\n");
ob3.display();

}
}

Monday, January 16, 2023

Sorting boundary elements of a 2 d array display the boundary elements along with sum

 


Using Scanner Class

import java.util.*;
public class Main
{

int x,t,r,c,i,j,n,m;
int a[][],b[];
void show()
{
t=1;
Scanner br=new Scanner(System.in);
c=0;
while(true)
{

System.out.println("enter the number of rows:");
n=br.nextInt();
if(n<2 || n>20)
continue;
System.out.println("enter the number of columns:");
m=br.nextInt();
if(m<2 || m>20)
continue;
else
break;
}

a=new int[n][m];
b=new int[2*(m+n)];
for(i=0;i< n;i++)
{
for(j=0;j< m;j++)
{
System.out.println("enter value:");
a[i][j]=br.nextInt();
}
}
System.out.println("\nEntered values are\n");
for(i=0;i< n;i++)
{
for(j=0;j< m;j++)
{
System.out.print(" "+a[i][j]);
}
System.out.println();
}
System.out.println("\n\n");
for(i=0;i< m;i++)
{
b[c++]=a[0][i];
}
for(i=1;i<=n-1;i++)
{
b[c++]=a[i][m-1];
}
for(i=m-2;i >=0;i--)
{
b[c++]=a[n-1][i];
}
for(i=n-2;i >0;i--)
{
b[c++]=a[i][0];
}

bsort();
r=0;
c=-1;
t=0;
for(i=1;i<=m;i++)
{
a[r][++c]=b[t++];
}
for(i=1;i<=n-1;i++)
{
a[++r][c]=b[t++];
}
for(i=1;i<=m-1;i++)
{
a[r][--c]=b[t++];
}
for(i=1;i<=n-2;i++)
{
a[--r][c]=b[t++];
}
System.out.println("\nAfter sorting the matrix\n");
for(i=0;i< n;i++)
{
 for(j=0;j< m;j++)
 {
  System.out.print(" "+a[i][j]);
  }
  System.out.println();
 }
 x=0;
 System.out.println("\nAfter sorting the boundary values\n");
for(i=0;i< n;i++)
{
 for(j=0;j< m;j++)
 {
  if(i==0 || i==n-1 ||  j==0 ||  j==m-1)
{
  System.out.print(" "+a[i][j]);
  x=x+a[i][j];
}
  else
  System.out.print(" ");
  }
  System.out.println();
 }
System.out.println("\nSum of the boundary elements "+x);
}
private void bsort()
{
 int flag;
 for(i=0;i< c;i++)
 {
  flag=0;
  for(j=0;j< c-i-1;j++)
  {
   if(b[j] >b[j+1])
   {
    flag=1;
    t=b[j];
    b[j]=b[j+1];
    b[j+1]=t;
    }
   }
   if(flag==0)
   break;
  }
  }
public static void main(String args[])throws Exception
{
Main ob=new Main();
ob.show();
}
}


Using BufferedReader Class

import java.io.*;
public class Boundary
{
int x,t,r,c,i,j,n,m;
int a[][],b[];
void show()throws IOException
{
t=1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
c=0;
while(true)
{
System.out.println("enter the number of rows:");
n=Integer.parseInt(br.readLine());
if(n<2 || n>20)
continue;
System.out.println("enter the number of columns:");
m=Integer.parseInt(br.readLine());
if(m<2 || m>20)
continue;
else
break;
}
a=new int[n][m];
b=new int[2*(m+n)];
for(i=0;i< n;i++)
{
for(j=0;j< m;j++)
{
System.out.println("enter value:");
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("\nEntered values are\n");
for(i=0;i< n;i++)
{
for(j=0;j< m;j++)
{
System.out.print(" "+a[i][j]);
}
System.out.println();
}
System.out.println("\n\n");
for(i=0;i< m;i++)
{
b[c++]=a[0][i];
}
for(i=1;i<=n-1;i++)
{
b[c++]=a[i][m-1];
}
for(i=m-2;i >=0;i--)
{
b[c++]=a[n-1][i];
}
for(i=n-2;i >0;i--)
{
b[c++]=a[i][0];
}
bsort();
r=0;
c=-1;
t=0;
for(i=1;i<=m;i++)
{
a[r][++c]=b[t++];
}
for(i=1;i<=n-1;i++)
{
a[++r][c]=b[t++];
}
for(i=1;i<=m-1;i++)
{
a[r][--c]=b[t++];
}
for(i=1;i<=n-2;i++)
{
a[--r][c]=b[t++];
}
System.out.println("\nAfter sorting the matrix\n");
for(i=0;i< n;i++)
{
 for(j=0;j< m;j++)
 {
  System.out.print(" "+a[i][j]);
  }
  System.out.println();
 }
 x=0;
 System.out.println("\nAfter sorting the boundary values\n");
for(i=0;i< n;i++)
{
 for(j=0;j< m;j++)
 {
  if(i==0 || i==n-1 ||  j==0 ||  j==m-1)
{
  System.out.print(" "+a[i][j]);
  x=x+a[i][j];
}
  else
  System.out.print(" ");
  }
  System.out.println();
 }
System.out.println("\nSum of the boundary elements "+x);
}
private void bsort()
{
 int flag;
 for(i=0;i< c;i++)
 {
  flag=0;
  for(j=0;j< c-i-1;j++)
  {
   if(b[j] >b[j+1])
   {
    flag=1;
    t=b[j];
    b[j]=b[j+1];
    b[j+1]=t;
    }
   }
   if(flag==0)
   break;
  }
  }
public static void main(String args[])throws Exception
{
Boundary ob=new Boundary();
ob.show();
}
}



Sample Input & Output

enter the number of rows:
3
enter the number of columns:
4
enter value:
5
enter value:
6
enter value:
7
enter value:
55
enter value:
4
enter value:
22
enter value:
1
enter value:
34
enter value:
5
enter value:
4
enter value:
34
enter value:
3

Entered values are

 5 6 7 55
 4 22 1 34
 5 4 34 3




After sorting the matrix

 3 4 4 5
 55 22 1 5
 34 34 7 6

After sorting the boundary values

 3 4 4 5
 55   5
 34 34 7 6

Sum of the boundary elements 157

Monday, December 19, 2022

Display the words of a sentence in ascending order of their frequencies

import java.util.*;
class Main
{
    String str1,str[],str2[];
    int arr[];
    int i,j,len,c,k,x;
    String temp;
    StringTokenizer stk;
    Scanner sc=new Scanner(System.in);
    void show()
    {
        i=0;
        k=0;
        System.out.print("\nEnter Number of Sentences:");
        c=sc.nextInt();
        if(c<1||c>4)
        {
        System.out.print("\nInvalid Entry");
        System.exit(0);
    }
    Scanner sc=new Scanner(System.in);
    while(true)
    {
        System.out.print("Enter the sentences:");
        str1=sc.nextLine().trim();
        len=str1.length();
        if(str1.charAt(len-1)=='.'||str1.charAt(len-1)==','||str1.charAt(len-1)=='?'||str1.charAt(len-1)=='!')
        break;
        else
        System.out.print("\nTerminate the sentence with specified characters:");
    }
    stk=new StringTokenizer(str1,".,?! ");
    c=stk.countTokens();
    str=new String[c];
    str2=new String[c];
    arr=new int[c];
    for(i=0;i<c;i++)
    arr[i]=-1;
    i=0;
    while(stk.hasMoreTokens())
    {
       str[i++]=stk.nextToken();
    }
    for(i=0;i<c-1;i++)
        {
          for(j=i+1;j<c;j++)  
          if(str[i].compareTo(str[j])>0)
          {
              temp=str[i];
              str[i]=str[j];
              str[j]=temp;
            }
        }
    
    str1=str[0];
    k=1;
    for(i=1;i<c;i++)
    {
        if(str[i].equals(str1))
        k++;
        else
        {
            str2[x]=str1;
            arr[x++]=k;
            k=1;
            str1=str[i];
             }
    }
    str2[x]=str1;
    arr[x++]=k;
    for(i=0;i<x;i++)
  {
    for(j=i+1;j<x;j++) 
    {
        if(arr[i]>arr[j])
        {
    int t=arr[i];
  arr[i]=arr[j];
  arr[j]=t;
  temp=str2[i];
  str2[i]=str2[j];
  str2[j]=temp;
}
}
}
System.out.println("\nWord Frequency");
for(i=0;i<x;i++)
System.out.println(str2[i]+" "+arr[i]);
}
public static void main(String[] args)
{
    new Main().show();
}
}

Sample Input & Output

Enter Number of Sentences:3
Enter the sentences:this is a test. this is another, where are others?

Word Frequency
a 1
another 1
are 1
others 1
test 1
where 1
this 2
is 2

Sunday, December 18, 2022

Arrange the words of a sentence in descending order of their lengths

 import java.io.*;
import java.util.*;
public class Gcd 
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int i,j,x;
    String str,sArr[]; 
    StringTokenizer stk;
    void accept() throws Exception
    {
        
        x=0;
        System.out.print("Enter a sentence: ");
         str=br.readLine(); 
         stk=new StringTokenizer(str);
         i=stk.countTokens();
         sArr=new String[i];
         while(stk.hasMoreTokens())
         {
             sArr[x++]=stk.nextToken();
            }
            for(i=0;i<x-1;i++)
            {
                 for(j=i+1;j<x;j++)
                 {
                     if(sArr[i].length()<sArr[j].length())
                     {
                         str=sArr[i];
                         sArr[i]=sArr[j];
                         sArr[j]=str;
                        }
                    }
                }
                System.out.println("\nWords in decending order according to length as follows");
                for(i=0;i<x;i++)
                System.out.println(sArr[i]);
            }
         
                  
public static void main(String args[])throws Exception
{
    Gcd ob=new Gcd();
    ob.accept();
}
}

Sample Input Output

Enter a sentence: this is another test of desending order

Words in desending order according to length as follows
desending
another
order
this
test
is
of


Saturday, December 17, 2022

ICSE Function Overloading Program On Different Values

 A design a class to overload a function num_calc() as follows:


a) void num_calc(int num, char ch) with one integer argument and one character argument computes 

the square of integer argument if choice ch is ‘s’ otherwise finds its cube.   


b) void num_calc(int a, int b, char ch) with two integer arguments and one character argument,

computes the product of integer arguments if ch is ‘p’ else adds the integers.


c) void num_calc(String s1, String s2) with two string arguments, which prints whether the 

strings are equal or not.


Program


                
class Diff
{
    public void num_calc(int num, char ch)
  {
        if(ch == 's' || ch == 'S')
      {
            int s = num * num;
            System.out.println("Square = " + s);
        }
        else
    {
            int c = num * num * num;
            System.out.println("Cube = " + c);
        }
    }
    public void num_calc(int a, int b, char ch)
{
        if(ch == 'p' || ch == 'P')
{
            int p = a * b;
            System.out.println("Product = " + p);
        }
        else
{
            int s = a + b;
            System.out.println("Sum = " + s);
        }
    }
    public void num_calc(String s1, String s2)
{
        if(s1.equalsIgnoreCase(s2))
            System.out.println("They are equal");
        else
            System.out.println("They are unequal");
    }
  public static void main(String args[])
    {
Diff ob=new Diff();
     ob.num_calc(1,'s');
      ob.num_calc(1,2,'p');
     ob.num_calc("java","j2se");
    }
}

Friday, December 16, 2022

Function Overloading ICSE Program Comparing Values

 Design a class to overload a function compare() as follows:


(a) void compare(int, int): to compare two integer values and print the greater of the two integers.


(b) void compare(char, char): to compare the numeric values of two characters and print the character 

with higher numeric value.


(c) void compare(String, String): to compare the length of the two strings and print the longer of 

the two.


Program

                

class Comp
{
    public void compare(int a, int b)
{
        int large = (a > b)? a : b;
        System.out.println(large);
    }
    public void compare(char a, char b)
{
        char large = (a > b)? a : b;
        System.out.println(large);
    }
    public void compare(String a, String b)
{
        String large = (a.length() > b.length())? a : b;
        System.out.println(large);
    }
 
  public static void main(String args[])
  {
Comp ob=new Comp();
    ob.compare(9,18);
    ob.compare('o','x');
    ob.compare("Neymar","Messi");
  }
}

Thursday, December 15, 2022

Function Overloading Polygon Function For ICSE

 Design a class to overload a function polygon() as follows:


(i) void polygon(int n, char ch): with one integer argument and one character 

argument that draws a filled square of side n using the character stored in ch.


Input value of n = 2, ch = ‘O’

Output:

OO

OO


(ii) void polygon(int x, int y): with two integer arguments that draws a filled 

rectangle of length x and breadth y, using the symbol ‘@’.

Input value of x = 2, y = 5

Output:

@@@@@

@@@@@



(iii) void polygon(): with no arguments that draws a filled triangle shown below.

Example:


Output:

*

**

***


Program

                

class Poly
{
  
public static void polygon(int n, char ch)
{
        for(int i = 1; i <= n; i++)
       {
            for(int j = 1; j <= n; j++)
           {
                System.out.print(ch);
           }
            System.out.println();
       }
  }
    public static void polygon(int x, int y)
{
        for(int i = 1; i <= y; i++)
       {
            for(int j = 1; j <= x; j++)
          {
                System.out.print("@");
            }
            System.out.println();
       }
    }
    public static void polygon()
{
       for(int i=0;i<3;i++)
{
 for(int j= 0;j<=i;j++)
{
System.out.print("*");
}
        System.out.println();
}

    public static void main(String args[])
    {
        polygon(5,'o');
        polygon(4,7);
        polygon();
    }
}

Subscribe via email

Enter your email address:

Delivered by FeedBurner