Here is the java logical programs and exercises for practice with solutions for beginners.

Java Logical Program Question

Question:

User ID Generation : Joseph’s team has been assigned the task of creating user-ids for all participants of an online gaming competition. Joseph has designed a process for generating the user-id using the participant’s First_Name,Last_Name, PIN code and a number N. The process defined by Joseph is as below-

Step1 – Compare the lengths of First_Name and Last_Name of the participant. The one that is shorter will be called “Smaller Name” and the one that is longer will be called the “Longer Name”. If both First_Name and Last_Name are of equal length, then the name that appears earlier in alphabetical order will be called “Smaller Name” and the name that appears later in alphabetical order will be all the “Longer Name”.

Step2 – The user ID should be generated as below –
First Letter of the longer name + Entire word of smaller name + Digit at position N in the PIN when traversing the PIN from left to right + Digit at position N in the PIN when traversing the PIN from right to left.

Step3 – Toggle the alphabets of the user ID generated in step 2 i.e uppercase alphabet should become lowercase and lowercase alphabets should become uppercase.

Example-1 – If the participant’s details are as below –
First_Name = Rajiv
Last_Name = Roy
PIN = 560037
N = 6

Step1 – Length of Last_Name is less than the Length of First_Name, so the smaller name is “Roy” and the longer name is “Rajiv”

Step2 – The user ID with be = First Letter of the longer name + entire word of the smaller name + Digit at position N in the PIN when traversing the PIN from left to right + Digit at position N in the PIN when traversing the PIN from right to left = First Letter of “Rajiv” + entire word of “Roy” + 6th Digit of PIN from left + 6th digit of PIN from right
= R + Roy + 7 + 5
Therefore, user-id = RRoy75

Step3 – Toggle the alphabets in the user-id. So, user-id=rrOY75





Example-2 – If the participant’s details are as below –
First_Name = Manoj
Last_Name = Kumar
PIN = 561327
N = 2

Step1 – Length of First_Name is equal to the Last_Name.
Alphabetically, ‘Kumar’ appears earlier than ‘Manoj’ (by comparing alphabetic positions of ‘K’ and ‘M’) so the Smaller Name is “Kumar” and the Longer Name is “Manoj”.

Step2 – The user ID with be = First Letter of the longer name + entire word of the smaller name + Digit at position N in the PIN when traversing the PIN from left to right + Digit at position N in the PIN when traversing the PIN from right to left = First Letter of “Manoj” + entire word of “Kumar” + 2nd Digit of PIN from left + 2nd digit of PIN from right
= M + Kumar + 6 + 2
Therefore, user-id = MKumar62

Step3 – Toggle the alphabets in the user-id. So, user-id=mkUMAR62

Example-3 – If the participant’s details are as below –
First_Name = Kumud
Last_Name = Kumar
PIN = 561327
N = 2

Step1 – Length of First_Name is equal to the Last_Name.
Alphabetically, ‘Kumar’ appears earlier than ‘Kumud’ (by comparing alphabetic positions of ‘Kuma‘ and ‘Kumu‘) so the Smaller Name is “Kumar” and the Longer Name is “Kumud”.

Step2 – The user ID with be = First Letter of the longer name + entire word of the smaller name + Digit at position N in the PIN when traversing the PIN from left to right + Digit at position N in the PIN when traversing the PIN from right to left = First Letter of “Kumud” + entire word of “Kumar” + 2nd Digit of PIN from left + 2nd digit of PIN from right
= K + Kumar + 6 + 2
Therefore, user-id = KKumar62

Step3 – Toggle the alphabets in the user-id. So, user-id=kkUMAR62

Java Logical Program for Practice with solution for beginners

package testexamples;

import java.util.Scanner;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		String firstName,lastName;
		int pin,N;
		System.out.println("Enter First Name");
		firstName=in.nextLine();
		System.out.println("Enter Last Name");
		lastName=in.nextLine();
		System.out.println("Enter pin");
		pin=in.nextInt();
		System.out.println("Enter N");
		N=in.nextInt();
		System.out.println("Your Key is: "+Test.userIdGeneration(firstName, lastName, pin, N));
		
	}
	
	public static String userIdGeneration(String input1,String input2,int input3,int input4)
	{
		String longer="",smaller="",finalStr="";
		if(input1.length()>input2.length())
		{
			longer=input1;
			smaller=input2;
			finalStr=longer.charAt(0)+smaller;
			String temp="";
			for(int i=0;i<finalStr.length();i++)
			{
				char ch=finalStr.charAt(i);
				if(ch>=65 && ch<=90)
				{
					temp+=(char) ((int)ch+32);
				}
				if(ch>=97 && ch<=122)
				{
					temp+=(char) ((int)ch-32);
				}
			}
			finalStr=temp;
			int temp1=input3;
			int count=0;
			while(temp1>0)
			{
				count++;
				temp1/=10;
			}
			temp1=input3;
			int a[]=new int[count];
			for(int i=0;i<count;i++)
			{
				a[i]=temp1%10;
				temp1/=10;
			}
			finalStr+=a[count-input4]+""+a[input4-1];
			
		}
		if(input1.length()<input2.length())
		{
			longer=input2;
			smaller=input1;
			finalStr=longer.charAt(0)+smaller;
			String temp="";
			for(int i=0;i<finalStr.length();i++)
			{
				char ch=finalStr.charAt(i);
				if(Character.isUpperCase(ch))
				{
					temp+=(char) ((int)ch+32);
				}
				if(ch>=97 && ch<=122)
				{
					temp+=(char) ((int)ch-32);
				}
			}
			finalStr=temp;
			int temp1=input3;
			int count=0;
			while(temp1>0)
			{
				count++;
				temp1/=10;
			}
			temp1=input3;
			int a[]=new int[count];
			for(int i=0;i<count;i++)
			{
				a[i]=temp1%10;
				temp1/=10;
			}
			finalStr+=a[count-input4]+""+a[input4-1];
			
		}
		if(input1.length()==input2.length())
		{
			if(input1.compareTo(input2)>0)
			{
				longer=input1;
				smaller=input2;
			}
			else
			{
				longer=input2;
				smaller=input1;
			}
			
			finalStr=longer.charAt(0)+smaller;
			String temp="";
			for(int i=0;i<finalStr.length();i++)
			{
				char ch=finalStr.charAt(i);
				if(Character.isUpperCase(ch))
				{
					temp+=(char) ((int)ch+32);
				}
				if(ch>=97 && ch<=122)
				{
					temp+=(char) ((int)ch-32);
				}
			}
			finalStr=temp;
			int temp1=input3;
			int count=0;
			while(temp1>0)
			{
				count++;
				temp1/=10;
			}
			temp1=input3;
			int a[]=new int[count];
			for(int i=0;i<count;i++)
			{
				a[i]=temp1%10;
				temp1/=10;
			}
			finalStr+=a[count-input4]+""+a[input4-1];
			
		}

		
		return finalStr;
	}

}