Merger Program ISC 2015

A class Merger concatenates two positive integers that are greater than 0 and produces a new merged integer.

Example: If the first number is 23 and the second number is 764, then the concatenated number will be 23764.

Some of the members of the class are given below:

Class name: Merger
Data members/instance variables:
n1: long integer to store first number.
n2: long integer to store second number.
mergeNum: long integer to store the merged number.
Member functions:
Merger(): constructor to initialize the data members.
void readNum(): to accept the values of the data members n1 and n2.
void joinNum(): to concatenate the numbers n1 and n2 and store it in mergeNum.
void show(): to display the original numbers and the merged number with appropriate messages.

Specify the class Merger, giving the details of the constructor, void readNum(), void joinNum() and void show(). Define the main() function to create an object and call the functions accordingly to enable the task.

import java.io.*;
class Merger{
    private long n1;
    private long n2;
    private long mergeNum;
    public Merger(){
        n1 = 0L;
        n2 = 0L;
        mergeNum = 0L;
    }
    public void readNum()throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("First number: ");
        n1 = Long.parseLong(br.readLine());
        if(n1 <= 0)
            n1 = 1;
        System.out.print("Second number: ");
        n2 = Long.parseLong(br.readLine());
        if(n2 <= 0)
            n2 = 1;
    }
    public void joinNum(){
        String a = Long.toString(n1);
        String b = Long.toString(n2);
        String c = a + b;
        mergeNum = Long.parseLong(c);
    }
    public void show(){
        System.out.println("First number: " + n1);
        System.out.println("Second number: " + n2);
        System.out.println("Merged number: " + mergeNum);
    }
    public static void main(String args[])
    throws IOException{
        Merger obj = new Merger();
        obj.readNum();
        obj.joinNum();
        obj.show();
    }
}

Leave a Reply

%d bloggers like this: