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:
Class name: Merger
Data members/instance variables:
n1: long integer to store first number
n2: long integer to store the second number
mergNum: 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 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.
import java.util.Scanner;
class Merger{
private long n1;
private long n2;
private long mergNum;
public Merger(){
n1 = 0L;
n2 = 0L;
mergNum = 0L;
}
public void readNum(){
Scanner in = new Scanner(System.in);
System.out.print("First number: ");
n1 = Long.parseLong(in.nextLine());
System.out.print("Second number: ");
n2 = Long.parseLong(in.nextLine());
}
public void joinNum(){
mergNum = Long.parseLong(n1 + "" + n2);
}
public void show(){
System.out.println("First number: " + n1);
System.out.println("Second number: " + n2);
System.out.println("Merged number: " + mergNum);
}
public static void main(String[] args){
Merger obj = new Merger();
obj.readNum();
if(obj.n1 <= 0 || obj.n2 <= 0){
System.out.println("Invalid Input!");
return;
}
obj.joinNum();
obj.show();
}
}