Write a program that accepts an input string array/s and prompts the user to select a task from the menu as shown below.
Menu
====
Menu
====
- Sort array
- Perform binary search in array
- Check if two arrays are equal
- Replace/fill array with a default value
SOLUTION:
//Importing classes import java.util.Arrays; import java.util.Scanner; public class ArraysClassExample { static String s[]=new String[3]; public static void main(String args[]) { System.out.println("1. Sort array"); System.out.println("2. Perform binary search in array"); System.out.println("3. Check if two arrays are equal"); System.out.println("4. replace/fil array with the default value"); System.out.println("5. Exit"); int ch; ArraysClassExample st=new ArraysClassExample(); gets(); System.out.println("Enter your choice"); Scanner np=new Scanner(System.in); ch=np.nextInt(); switch(ch) { case 1: st.sort(); break; case 2: bsearch(); break; case 3: st.bequals(); break; case 4: st.breplace(); break; default : System.out.println("Wrong Choice"); break; } } static void gets() { System.out.println("Enter the three strings"); Scanner src=new Scanner(System.in); for ( int i = 0; i < 3; i++ ) { s[i]=src.next(); } System.out.println("The three strings are :"); for ( int i = 0; i < 3; i++ ) { System.out.println(s[i]); } } void sort() { System.out.println("The string after sort :"); Arrays.sort(s); for( int i = 0; i < 3; i++ ) { System.out.println(s[i]); } } static void bsearch() { String r; System.out.println("Enter the string you want to search :"); Scanner sr=new Scanner(System.in); r=sr.next(); System.out.println("Binary search operation :"); int j; j=Arrays.binarySearch(s, r); if ( j >= 0 ) { System.out.println(j); } else { System.out.println("The string you entered is not correct"); } } void bequals() { System.out.println("Equals check condition :"); String s2[]=new String[3]; System.out.println("Enter the three strings"); Scanner src=new Scanner(System.in); for( int i = 0; i < 3; i++ ) { s2[i]=src.next(); } for(int i = 0; i < 3; i++) { if(s[i].equals(s2[i])) { System.out.println("String is equal"); } else { System.out.println("string is not equal"); } } } void breplace() { for( int i = 0; i < 3; i++ ) { s[i]=null; } for( int i = 0; i < 3; i++ ) { System.out.println("The replace value is "+s[i]); } } }
what is Question1 st=new Question1(); as described in line 16?
ReplyDeleteSorry, that is typing mistake.
DeleteNow i have corrected the code.
The actual code is
ArraysClassExample st=new ArraysClassExample();
Thank you for your feedback.