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

import java.io.*; import java.util.*; class Test{ static String[]list = new Stri

ID: 3609206 • Letter: I

Question

import java.io.*;
import java.util.*;

   
class Test{
static String[]list = new String[100];
static String[]num = new String[100];
public static void main(String[] args)throws IOException{

Scanner in = new Scanner (new FileReader("input.txt"));
PrintWriter out = new PrintWriter (newFileWriter("output.txt"));

String []num = new String[100];
int z = 0;
String str = in.next();

     while(!(str.equals("$$$$"))){            
       list[z] = str;
       num[z] = getWord(str);
       z++;
       str = in.next();
      }


bubbleSort(list, num,z);

int y = binarySearch("7253",num,0,z);
System.out.println("I am binary returned " +y);
System.out.println("I am binary returned " +list[y]);

in.close();
out.close();
}//main



private static void bubbleSort(String[] list, String[] num, int n){
String temp;
String revs;

for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1 - i; j++) {

if (list[j].compareTo( list[j + 1]) > 0 ) {
temp = list[j];
revs = num[j];

list[j] = list[j + 1];
list[j + 1] = temp;
num[j] = num[j+1];
num[j+1] = revs;
}//End of If Statement
}//End of For Loop
}//End of For Loop
}//End of Private method bubbleSort


public static String getWord(String word){
word = word.toLowerCase() ;
int l = word.length();
String str = " ";

for(int i = 0; i < l; i++){
char y = word.charAt(i);

if(y < 100){
str +=2;
}else if(y < 103){
str +=3;
}else if( y < 106){
str +=4;
}else if( y < 109){
str += 5;
}else if(y < 112){
str += 6;
}else if(y < 116){
str += 7;
}else if (y < 119){
str += 8;
}else{
str +=9;
}
}//end for
return str;
}//end getWord();



public static int binarySearch(String key, String[]b, int lo, inthi){
while(lo <= hi-1){
int mid = (lo + hi)/2;
   int cmp = key.compareTo(b[mid]);
if (cmp==0)return mid;
if (cmp < 0)hi = mid -1;
else lo = mid + 1;
}//end while
return lo;
}//end binarysearch
}//class data(string array)
225533
246673
246736
6377243
662453
6683
6683
7253
74663
7733428483
7253
77355
8398
Problem: I am searching for a known valued in the data above"7253". When I test the binarySeach() it is returning a value of13(there are only 12 items in the list) I cannot figure out theproblem. Help needed! import java.io.*;
import java.util.*;

   
class Test{
static String[]list = new String[100];
static String[]num = new String[100];
public static void main(String[] args)throws IOException{

Scanner in = new Scanner (new FileReader("input.txt"));
PrintWriter out = new PrintWriter (newFileWriter("output.txt"));

String []num = new String[100];
int z = 0;
String str = in.next();

     while(!(str.equals("$$$$"))){            
       list[z] = str;
       num[z] = getWord(str);
       z++;
       str = in.next();
      }


bubbleSort(list, num,z);

int y = binarySearch("7253",num,0,z);
System.out.println("I am binary returned " +y);
System.out.println("I am binary returned " +list[y]);

in.close();
out.close();
}//main



private static void bubbleSort(String[] list, String[] num, int n){
String temp;
String revs;

for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1 - i; j++) {

if (list[j].compareTo( list[j + 1]) > 0 ) {
temp = list[j];
revs = num[j];

list[j] = list[j + 1];
list[j + 1] = temp;
num[j] = num[j+1];
num[j+1] = revs;
}//End of If Statement
}//End of For Loop
}//End of For Loop
}//End of Private method bubbleSort


public static String getWord(String word){
word = word.toLowerCase() ;
int l = word.length();
String str = " ";

for(int i = 0; i < l; i++){
char y = word.charAt(i);

if(y < 100){
str +=2;
}else if(y < 103){
str +=3;
}else if( y < 106){
str +=4;
}else if( y < 109){
str += 5;
}else if(y < 112){
str += 6;
}else if(y < 116){
str += 7;
}else if (y < 119){
str += 8;
}else{
str +=9;
}
}//end for
return str;
}//end getWord();



public static int binarySearch(String key, String[]b, int lo, inthi){
while(lo <= hi-1){
int mid = (lo + hi)/2;
   int cmp = key.compareTo(b[mid]);
if (cmp==0)return mid;
if (cmp < 0)hi = mid -1;
else lo = mid + 1;
}//end while
return lo;
}//end binarysearch
}//class data(string array)
225533
246673
246736
6377243
662453
6683
6683
7253
74663
7733428483
7253
77355
8398
Problem: I am searching for a known valued in the data above"7253". When I test the binarySeach() it is returning a value of13(there are only 12 items in the list) I cannot figure out theproblem. Help needed!

Explanation / Answer

/*I put some comments to help youout * there are a few things wrongwith this, but since you asked specifically about the binary searchmethod i looked *at that more. *You can't use the compareTo()method with String versions of Integers and expect to get the sameanswer. *Your binary search goes streightto to the end of the list of numbers no matter what. *You have to convert the Stringsto Integers, or ints. *Use Integer.parseInt(string) toget the number, it returns an int. Then you can easily usemid<key and so on... * *Your bubble sort doesn't workproperly, you might want to test the binary search method with analready *sorted set ofnumbers. * *Anything highlighted, I had tochange to test it. */
import java.io.*; import java.util.*;
   class Test{ static String[]list = new String[100]; static String[]num = new String[100]; public static void main(String[] args)throwsIOException{
Scanner in = new Scanner (new FileReader("input.txt")); PrintWriter out = new PrintWriter (newFileWriter("output.txt")); //you aren't ever writing anything tooutput.txt
String []num = new String[100]; int z = 0;   String str = in.next();
   while(!(str.equals("$$$$"))){          list[z] = str;    num[z] = list[z]; //getWord(str); why is this here??? what is thepurpose of the getWord(str) method if all id does is //make every number 222...    z++;    str = in.next();    }

//bubbleSort(list, list,z); //bubble sort doesn't work //you can see by printing out the arraythat goes through the following for. //i printed the array 'num', becausethis is the array that needs to be sorted and is used for thebinary search
for(int a=0; a<z; a++) //I added thisfor you to use to see if the numbers are sortedcorrectly System.out.println(num[a]); int y = binarySearch("7253",num,0,z-1); //your index was too high, it isn't supposed to be numberof elements, //but 0 to n index System.out.println("I am binary returned " +y); System.out.println("I am binary returned " +list[y]);
in.close(); out.close(); }//main


private static void bubbleSort(String[] list, String[] num,int n) { String temp; String revs;
for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1 - i; j++) {
if (list[j].compareTo( list[j + 1]) > 0 ) { temp = list[j]; revs = num[j];
list[j] = list[j + 1]; list[j + 1] = temp; num[j] = num[j+1]; num[j+1] = revs; }//End of If Statement }//End of For Loop }//End of For Loop }//End of Private method bubbleSort

public static String getWord(String word){ word = word.toLowerCase() ; int l = word.length(); String str = " ";
for(int i = 0; i < l; i++){ char y = word.charAt(i);
if(y < 100){ str +=2; }else if(y < 103){ str +=3; }else if( y < 106){ str +=4; }else if( y < 109){ str += 5; }else if(y < 112){ str += 6; }else if(y < 116){ str += 7; }else if (y < 119){ str += 8; }else{ str +=9; } }//end for return str; }//end getWord();

/*Looks okay, but you have to useIntegers instead of Strings if you want to use a compareTo()method *Either that or ints. *Just a reminder: the array has tobe sorted in ascending order for the Binary Search towork */ public static int binarySearch(String key, String[]b, int lo,int hi){ while(lo <= hi-1){ int mid = (lo + hi)/2; System.out.println("Front: "+lo+" "+mid+" "+hi);    int cmp = key.compareTo(b[mid]); System.out.println(cmp);   if (cmp==0)return mid;   if (cmp < 0)hi = mid -1; else if(cmp>0)lo = mid + 1; System.out.println("End : "+lo+" "+mid+" "+hi); }//end while return lo; }//end binarysearch }//class