Question 8

a) A class SumArray contains an array of integers which removes the duplicate elements and finds the pair of elements who’s sum equals to the input limit.
Original array  :
2 ,2 ,7 ,7 ,4 ,-5 ,11 ,5 ,20 sum = 15
Array after removing duplicate elements :  
2 ,7 ,4 ,-5 ,11 ,5 ,20
Pairs of elements and their sum :
4 + 11 =  15
-5 + 20 =  15
Some members of the class are given below:
class name     : SumArray
Data members/instance variables:
arr[]            : array to store the integer elements
size : to store the size of the array
num            : to store the limit to find the sum                   
Member funcions/methods:           
SumArray()             : default constructor
SumArray(int n)      : parameterised constructor to initialise the data member size =n
void get()                  : to accept the size, array elements and the limit
void  sumSame()      : Prints the pari of elements and the their sum
void removeDuplicate()  : remove the duplicate elements from the array
void display()          : display the array
Define the class SumArray giving the details of the constructors and other functions. Define the  main() function to create an object and call the function accordingly to enable the task

import java.util.Scanner;
public class SumArray
{
    int arr[];
    int size;
    int num;
    public SumArray()
    {
    }
     public SumArray(int n)
    {
        size=n;
    }
    public void get()
    {
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter size of array");
        size = sc.nextInt();
        arr=new int[size];
        for(int i=0;i<size;i++){         
                System.out.println("Enter the array element "+(i+1));
                arr[i] = sc.nextInt();
            }
        System.out.println("Enter the number");
        num = sc.nextInt();            
    } 
    public void  sumSame()
    {
        System.out.println("Pairs of elements and their sum : ");
 
        for (int i =  0; i < size; i++)
            {
                for (int j  = i+1; j < size; j++)
                {
                    if(arr[i]+arr[j] == num)
                    {
                        System.out.println(arr[i]+" + "+arr[j]+" =  "+num);
                    }
                }
            }
    }
    public void removeDuplicate()
    {
            for(int i=0;i<size;i++)
		{
		    for(int j=i+1;j<size;){
		        if(arr[i] == arr[j]){
		            for(int k = j;k<size-1; k++){
		                arr[k] = arr[k+1];
		              }
		              size = size-1;
		          }
		          else
		          j++;		
			}
		}
   }    
   public void display()
   {
       for(int i=0;i<size;++i)
            if(i==0)
                System.out.print(arr[i]);
            else
                System.out.print(" ,"+arr[i]);
       System.out.println();
    }
    public static void main()
    {
        SumArray s1= new SumArray();
        s1.get();
        System.out.println("Original array  : ");        
        s1.display();
        s1.removeDuplicate();
        System.out.println("Array after removing duplicate elements : ");
        s1.display();        
        s1.sumSame();       
    }
}
This entry was posted in Term 2. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *