* This is for CS 101 Java class. I can only use \"for\" and “do-while” loops for
ID: 3728571 • 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
//copy the below code and save this with name "ans.java" and read the comments for better understanding
//if any problem or doubt occurs feel free to do comment in the comment section
import java.util.Scanner;
import java.util.*;
import java.lang.*;
public class ans {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//take input choice 1-3
System.out.println("=== Make Your Selection ===");
System.out.println("1.Common Characters");
System.out.println("2.1/(1-x) calculation");
System.out.println("3.Exit");
System.out.println("Your Selection:");
int ch = sc.nextInt();
//for loop for handling the choice
for( ;ch!=3; ){
//for handling the choice ,common characters
if(ch==1){
System.out.println("===Common Characters Program===");
System.out.println("Enter a string:");
String input1 = sc.next();
System.out.println("Enter another string:");
String input2 = sc.next();
//for finding common characters I used two set set1 and set2
//and insert characters in both set and after tha take intersection of both set
//remained set after intersection is our result
//print the resulted set(output of intersection set)
Set<Character> set1 = new HashSet<Character>();
Set<Character> set2 = new HashSet<Character>();
for(char c : input1.toCharArray()) {
set1.add(c);
}
for(char c : input2.toCharArray()) {
set2.add(c);
}
// Stores the intersection of set1 and set2 inside set1
set1.retainAll(set2);
System.out.println("Common characters: ");
for(char c : set1) {
System.out.print(" " + c);
}
System.out.println(" ");
}
//for handling if choice is 2 i.2; calculaiton of 1(1-x) sum
if(ch==2){
System.out.println("=== 1/(1-x) calculation ===");
System.out.println("Enter an x: (-1,1) :");
Double x = sc.nextDouble();
System.out.println("Enter Precision: ");
Double Precision = sc.nextDouble();
Double sum = 0.0,sum_prev;
int i=0;
do{
//sum_prev is used for using while checking condition
sum_prev=sum;
sum+=Math.pow(x,i);
System.out.println("Current Result is: "+sum);
i=i+1;
//in while condition of less than precision is check
}while((Math.abs(sum-sum_prev)>Precision));
System.out.println(" Result is: "+sum);
}
System.out.println(" ");
System.out.println("=== Make Your Selection ===");
System.out.println("1.Common Characters");
System.out.println("2.1/(1-x) calculation");
System.out.println("3.Exit");
System.out.println("Your Selection:");
ch = sc.nextInt();
}
System.out.println("Goodbye");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.