* This is for CS 101 Java class. I CAN ONLY USE \"for\" and “do-while” loops for
ID: 3728882 • 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-1Explanation / Answer
import java.util.Scanner;
class Project
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int no,a;
do
{
System.out.println("=== Make Your Selection ===");
System.out.println("1) Common Characters");
System.out.println("2) 1/(1-x) calcutions");
System.out.println("3) Exit");
System.out.println("Enter Your Option");
no=sc.nextInt();
}while (no>3);
if(no==1)
{
String s1,s2,s3=" ",s4=" ",s5;
System.out.println("Enter Two String");
s1=sc.next();
s2=sc.next();
for (int i = 0; i < s1.length(); i++)
{
for (int j = i + 1; j < s1.length(); j++)
{
if (s1.charAt(i) != s1.charAt(j))
{
s3=s3+s1.charAt(i);
break;
}
}
}
for (int i = 0; i < s2.length(); i++)
{
for (int j = i + 1; j < s2.length(); j++)
{
if (s2.charAt(i) != s2.charAt(j))
{
s4=s4+s2.charAt(i);
break;
}
}
}
System.out.print("Common Characters ");
for(int i=0;i<s3.length();i++)
{
for(int j=0;j<s4.length();j++)
{
if(s3.charAt(i) == s4.charAt(j))
{
System.out.print(s3.charAt(i)+" ");
}
}
}
}
else if(no==2)
{
double x,y,total=1.0,multi,i;
System.out.println("Enter Integer Number");
x=sc.nextDouble();
System.out.println("Enter precession");
y=sc.nextDouble();
multi=x;
for (i = 1; i <= x; i++)
{
total = total + multi;
multi = multi * x;
System.out.println("Current value is:"+total);
}
}
else
{
System.out.println("Good Bye");
System.exit(0);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.