100% found this document useful (2 votes)
350 views

Experiment-2 Name of Student: Waghule Shubham Kalyan Batch: B3 Branch: CS-D Roll No.: 94 Problem Statement

The document describes an experiment involving class Adder with methods getdata() and numsum(). Getdata() accepts a 1D integer array and target sum from the user. Numsum() returns any two elements from the input array that sum to the target sum, or an empty array if no such pair exists. The code implements this class with a constructor, getdata() to input the array and target, and numsum() to find the summing pair or return empty array. It is tested on two cases and outputs the pair or empty array as expected.

Uploaded by

WAGHULE SHUBHAM
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
350 views

Experiment-2 Name of Student: Waghule Shubham Kalyan Batch: B3 Branch: CS-D Roll No.: 94 Problem Statement

The document describes an experiment involving class Adder with methods getdata() and numsum(). Getdata() accepts a 1D integer array and target sum from the user. Numsum() returns any two elements from the input array that sum to the target sum, or an empty array if no such pair exists. The code implements this class with a constructor, getdata() to input the array and target, and numsum() to find the summing pair or return empty array. It is tested on two cases and outputs the pair or empty array as expected.

Uploaded by

WAGHULE SHUBHAM
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment-2

Name of Student: Waghule Shubham Kalyan

Batch: B3 Branch: CS-D Roll No.: 94

Problem Statement

There is a class Adder which has two data members of type 1D int array and int variable. It
has two functions: getdata and numsum. Function getdata accepts non-empty array of distinct
integers from user in 1D int array data member and a targetsum in another data member. The
function numsum adds any two elements from an input array which is equal to targetsum and
return an array of resulting two elements, in any order. If no two numbers sum up to the
target sum, the function should return an empty array. Note that the target sum is to be
obtained by summing two different integers in the array; you can’t add a single integer to
itself in order to obtain the target sum. You can assume that there will be at most one pair of
numbers summing up to the target sum. Use constructor. Use extra variables if needed.

Sample Input and Output


Test Case 1

Input Expected
Values
Parameters Output
1D Array [3,5,-4,8,11,1,-1,7]
[8,7]
targetsum 15

Test Case 2

Input Expected
Values
Parameters Output
1D Array [3,5,-4,8,11,1,-1,6]
[]
targetsum 15
Code:
import java.util.*;

public class Adder {

    private int []arr;


    private int targetSum;

    public Adder(int arrS) {


        arr = new int[arrS];
        targetSum = 0;
    }

    public void getData(int arrS) {


        System.out.println("Enter array elements: ");
        Scanner s = new Scanner(System.in);
       
        for (int i = 0; i < arrS; i++) {
            arr[i] = s.nextInt();
        }

        System.out.println("\nEnter your target sum: ");


        targetSum = s.nextInt();

        System.out.println("Array: " + Arrays.toString(arr));


        s.close();
    }

    public int[] numSum(int arrS) {


        int sum_arr[] = new int[0];

        for (int i = 0; i < arrS; i++) {


            for (int j = 0; j < arrS; j++) {
                if (arr[i] + arr[j] == targetSum && arr[i] != arr[j]) {
                    sum_arr = new int[] { arr[i], arr[j] };
                    return sum_arr;
                }
            }
        }
        return sum_arr;
    }

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);


        System.out.println("Enter the Array size: ");
        int arrS = s.nextInt();
       
        Adder obj = new Adder(arrS);
        obj.getData(arrS);
       
        System.out.println(obj.targetSum);
       
        int [] sum_arr = obj.numSum(arrS);
       
        System.out.println("\n\nSub-array with sum "+obj.targetSum+" is
"+Arrays.toString(sum_arr));
        s.close();

    }
}

Results:
Actual Output
Test Case 1 Test Case 2

You might also like