Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

1. User Input and Replace String Template “Hello <<UserName>>, How are you?


a. I/P -> Take User Name as Input.
Ensure UserName has min 3 char
b. Logic -> Replace <<UserName>> with the proper name
c. O/P -> Print the String with User Name
solution=>>>>>>

package BridgeLabs;
import java.util.Scanner;
public class Template {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name and name length should be greater than
3");
String name = sc.nextLine();
if(name.length()>3) {
System.out.println("Hello "+name+" How are you?");
}else {
System.out.println("name length should be greater than 3");
}
}
}

<-----question no 2--->
2. Simulate Stopwatch Program
a. Desc -> Write a Stopwatch Program for measuring the time that elapses between
the start and end clicks
b. I/P -> Start the Stopwatch and End the Stopwatch
c. Logic -> Measure the elapsed time between start and end
d. O/P -> Print the elapsed time. Intermediate
solution=>>>>>>

package BridgeLabs;

import java.util.Scanner;

public class StopWatch {

public static long timer() {


return System.currentTimeMillis();
}
public static void main(String[] args) {
long start = 0;
long stop = 0;
System.out.println("Press s to start the timer");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if(s.equalsIgnoreCase("s")) {
start=timer();
}
System.out.println("Press s to end the timer");
String e = sc.nextLine();
if(e.equalsIgnoreCase("s")) {
stop=timer();
}
String message;
long elapsed =stop-start;
long mils = elapsed%1000;
elapsed = elapsed/1000;
long secs = elapsed%60;
elapsed = elapsed/60;
long minutes = elapsed%60;
long hours = elapsed/60;
if (hours > 0) {
message = hours + " hours and " + minutes + " minutes";
} else if (minutes > 0) {
message = minutes + " minutes and " + secs + " seconds";
} else if (secs > 0) {
message = secs + " seconds and " + mils + " milliseconds";
} else {
message = mils + " milliseconds";
}
System.out.println(message);
}
}

<-----question no 4--->
2. Take a string from user at runtime.
Check that using those characters of string is it possible to make a palindrome
string?
If yes then print the palindrome string.
E.g. abcde O/p: palindrome not possible.
E.g. abcabc O/p: palindrome possible. abccba
solution=>>>>>>

package BridgeLabs;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class Palindromes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String!");
String str = sc.nextLine();
findPalindromes(str);
}

private static void findPalindromes(String str) {

HashMap<Character,Integer> hm = new HashMap<Character,Integer>();


for(int i=0;i<str.length();i++) {
char k = str.charAt(i);
if(hm.containsKey(k)){
hm.put(k, hm.get(k)+1);
}else {
hm.put(k, 1);
}
}
int oddCount=0;
String oddString="";
String fHalf = "" ;
Set<Map.Entry<Character,Integer>> entrySet = hm.entrySet();
for (Entry<Character, Integer> itr: entrySet) {
if((Integer)itr.getValue() % 2 !=0) {
oddCount++;
for(int i=0;i<itr.getValue();i++) {
oddString+=itr.getKey();
}
}else {
for(int i=0;i<itr.getValue()/2;i++) {
fHalf = fHalf+itr.getKey();
}

}
}

if(oddCount>1 || oddCount==1 && str.length()%2==0) {


System.out.println("palindrome not possible.");
return;
}
StringBuffer sHalf= new StringBuffer(fHalf);
sHalf.reverse();
System.out.println((oddCount == 1) ?
(fHalf + oddString + sHalf) :
(fHalf + sHalf));
}

<-----question no 5--->

1. For a linked list perform the following functions.


a. Reverse the link in single link list

package BridgeLabs;
import java.util.LinkedList;
public class ResverseLinkList {
public static void main(String[] args) {
LinkedList ll= new LinkedList();
for(int i=1;i<10;i++) {
ll.add(i);
}
System.out.println("Before Reverse "+ll);
reverseLinkList(ll);
}
private static void reverseLinkList(LinkedList ll) {
int size = ll.size();
for (int i =0; i <size/2; i++) {
int temp = (Integer) ll.get(i);
ll.set(i, ll.get((size-1)-i));
ll.set((size-1)-i,temp);
System.out.println("After Reverse Using The Stack "+ll);
}

}
}

<-----question no 5--->
b. Reverse the link using recursion
solution=>>>>>>

package BridgeLabs;

import java.util.LinkedList;

public class ReverseUsingRecursion {


static LinkedList lr = new LinkedList();
public static void main(String[] args) {
LinkedList ll= new LinkedList();
for(int i=1;i<=10;i++) {
ll.add(i);
}
System.out.println("Before Reverse "+ll);
reverseLinkList(ll);
System.out.println("After Reverse Using The Recursion "+lr);
}

private static void reverseLinkList(LinkedList ll) {


while(ll.size() !=0) {
lr.add(ll.removeLast());
reverseLinkList(ll);
}

}
}

<-----question no 5--->
c. Reverse the link using stack.
solution=>>>>>>

package BridgeLabs;

import java.util.LinkedList;
import java.util.Stack;

public class ReverseUsingStack {


public static void main(String[] args) {
LinkedList ll= new LinkedList();
for(int i=1;i<=10;i++) {
ll.add(i);
}
System.out.println("Before Reverse "+ll);
reverseLinkList(ll);
}

private static void reverseLinkList(LinkedList ll) {


LinkedList ls = new LinkedList();
Stack s = new Stack();
for (int i = 0; i < ll.size(); i++) {
s.push(ll.get(i));
}
int size = s.size();
for (int j = 0; j <size ; j++) {
ls.add(s.pop());
}
System.out.println("After Reverse Using The Stack "+ls);
}

You might also like