icse-promo

Question

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 is 764,then the concatenated number will be 23764.
Some of the members of the class are given below:
Classname:Merger
Data members/instance variables:
n1:long integer to store first number
n2:long integer to store second number
mergNum:long integer to store mergednumber
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 mergNum
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.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class Merger
{
    long nl,n2,mergNum;
    Merger()
    {
        nl=n2=mergNum=0;
    }

    void readNum()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter two numbers");
        nl=sc.nextLong();
        n2=sc.nextLong();
    }
    
    void JoinNum()
    {
        String s1=Long.toString(nl);
        String s2=Long.toString(n2);
        String s3=s1+s2;
        mergNum=Long.valueOf(s3);
    }

    void show()	
    {
        System.out.println("First Number="+nl);
        System.out.println("Second Number="+n2);
        System.out.println("Merged Number="+mergNum);
    }
    
    public static void main()
    {
        Merger ob1=new Merger();
        ob1.readNum();
        ob1.JoinNum();
        ob1.show();
    }
}





				
			

Coding Store

Leave a Reply

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