Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

* This is for CS 101 Java class. I CAN ONLY USE \"for\" and “do-while” loops for

ID: 3729347 • Letter: #

Question

* This is for CS 101 Java class. I CAN ONLY USE "for" and “do-while” loops for this lab assignment. I CANNOT USE “while" or any other repetition method. Also I CANNOT USE arrays or CANNOT CREATE methods other than main method. *

Create a new project Lab05b. Write a menu driven program, that asks the user to provide an integer from 1 to 3. If any number given out of this range, the program should ignore that number and should continue to ask numbers 1-3. If number 1 is given, it should ask two Strings from user and show the common characters in both Strings. It should be case-insensitive, meaning 'A' and 'a' should mean the same thing. There should be no duplicates, even if 'a' is found more than once, it should be displayed only once. If number 2 is given, it should ask the user to provide an integer x and a precision. It should calculate the approximate value of 11 (1 - x) by using the formula given below. Your program stops calculation whenever the increment is less than given precision value. =1+x+x2 +x3 +x4 +x5 + for-1

Explanation / Answer

Program

package commonchars;
import java.util.Scanner;
import java.io.*;
import java.lang.Math;


public class CommonChars {

       public static void main(String[] args) {
        
    String str1,str2,str3;
    double j,x=0,n=0,sum=0;
         

Scanner in = new Scanner(System.in);
int num;
do
    {
System.out.println("1) Common Characters");
System.out.println("2) 1/(1-x) Calculation");
System.out.println("3) Exit");
System.out.println("Your Selection : ");
num = in.nextInt();

if(num==1)
{
System.out.println("= = = Common Characters Program = = = ");
System.out.println("Enter a test string: ");
str3 = in.nextLine();
System.out.println("Enter a string: ");
str1 = in.nextLine();
System.out.println("Enter another string: ");
str2 = in.nextLine();

System.out.println(" String 1: "+str1);
System.out.println(" String 2: "+str2);
StringBuffer sb = new StringBuffer();

// get unique chars from both the strings

str1 = uniqueChar(str1);

str2 = uniqueChar(str2);


int str1Len = str1.length();

int str2Len = str2.length();

// compare each char in first word with chars in another word

for (int i = 0; i < str1Len; i++)

{

for (int j = 0; j < str2Len; j++)

{

// found match stop the loop

if (str1.charAt(i) == str2.charAt(j))

{
sb.append(str1.charAt(i));

break;

}

}
}


System.out.println(" Common Characters :" + sb.toString());
}
else if (num==2)
{
System.out.println("= = = 1/(1-x) Calculation = = = ");
System.out.println("Enter the value of x:");
x=in.nextInt();
System.out.println("Enter the value of n:");
n=in.nextInt();
for(j=0;j<=n;j++)
sum=sum+Math.pow(x,j);
System.out.println("Sum is:"+sum);


}
else if(num==3)
{
System.out.println("= = = Exiting from Program = = = ");
System.exit(0);
}
}while((num==1)||(num==2));

}


public static String uniqueChar(String inputString)

{

String outputString = "";

int len = inputString.length();

for (int i = 0; i < len; i++)

{

if (!outputString.contains(String.valueOf(inputString.charAt(i))))

{

outputString += String.valueOf(inputString.charAt(i));

}

}

return outputString;

}

   
}