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

Write a console program that repeatedly prompts the user to enter data until the

ID: 3883686 • Letter: W

Question

Write a console program that repeatedly prompts the user to enter data until they type done

(any case, Upper, Lower, or Mixed). As they enter the data, assign it to a two-dimension array

where the first dimension contains exactly what they type and the second dimension contains

the first non-whitespace character of what they type, in lowercase. If they enter a blank line, it

is acceptable to skip that line. When they type done, do these steps:

1. display: As Entered

2. for each entry in the array, display the index of the first dimension, the second

dimension's value, and the first dimension's value on a single line separated by :

(colon).

3. display: Bubble Sorted

4. using a bubble sort, for each entry in the array, display the original index of the first

dimension, the second dimension's value, and the first dimension's value on a single

line separated by : (colon) sorted by the second dimension.

5. display: Selection Sorted

6. using a selection sort, for each entry in the array, display the original index of the first

dimension, the second dimension's value, and the first dimension's value on a single

line separated by : (colon) sorted by the first dimension.

Correct .JAVA file: The program must store the values entered by the user in a 2-dimensional array and do a bubble-sort and a selection-sort on the data.

import java.util.Scanner;

/**
*
* Date: 08/25/2017
* Assignment: A4
* Student Number: MA5331550
* Description: This program repeatly prompts the user to user to enter data until they type done. Ask data is

entered it assigned to a two-dimension array.
*/

public class A4MA5331550 {
public static void main(String args[]) {
// Create a Scanner to read from System Input like Keyboard.
Scanner input = new Scanner(System.in);
String data = "";
// Create an infinite loop by placing the while loop condition as 'true'
// so that it will always be true.
while (true) {
System.out.println("Enter your data: ");
// Read from the input device via Scanner
data = input.next();
// Ignoring the case, if the data entered is 'done' then break and
// exit the loop.
if (data.equalsIgnoreCase("done")){
System.out.println("Done, BYE!");
break;
}
System.out.println("You have entered: " + data);
}
}
}

Explanation / Answer


public class A4MA5331550 {
public static void main(String args[]) {

// Create a Scanner to read from System Input like Keyboard.
Scanner input = new Scanner(System.in);
String data = "";
String dataCopyLower="";
String firstCharacter="";

String[][] array=new String[1000][2];//2-d array that will store data
int index=0;

// Create an infinite loop by placing the while loop condition as 'true'
// so that it will always be true.
while (true) {
System.out.println("Enter your data: ");
// Read from the input device via Scanner

data = input.next();

// Ignoring the case, if the data entered is 'done' then break and
// exit the loop.

if (data.equalsIgnoreCase("done"))
{
System.out.println("Done, BYE!");
break;
}

//if data is not a space " " then we will store it directly in the 1st column of 2d array and convert it into lowercase
//to extract the first character which also needs to be stored in 2nd column of the 2d array in lowercase.  
if(!data.equals(" "))

{
dataCopyLower=data.toLowerCase();
firstCharacter=dataCopyLower.substring(0,1);
array[index][0]=data;
array[index][1]=firstCharacter;
index++;

}


}

//after the completion of above loop 'index' equals to the 'length of array' which I have used throughout the solution

//Part 1: display-As Entered
System.out.println("Part-1:");
int i=0;
while(i<index)
{
System.out.print(array[i][0]+" ");
i++;
}
System.out.println("");


//Part 2: for each entry in the array, display the index of the first dimension, the second

//dimension's value, and the first dimension's value on a single line separated by :(colon).

System.out.println("Part-2:");
i=0;
while(i<index)
{
System.out.println("index = "+i+": Second Dimension Value = "+array[i][1]+": First Dimension's Value = "+array[i][0]);
i++;
}


//I think part 3 and 4 are same ;Same is with 1&2 and 5&6
//part 1,3,5 just tell us what to do-not specifically-just heading
//part 2,4,6 are final questions

//Part 3. display: Bubble Sorted
//Part 4:using a bubble sort, for each entry in the array, display the original index of the first

//dimension, the second dimension's value, and the first dimension's value on a single

//line separated by : (colon) sorted by the second dimension.

//In bubble sort the main idea is to bring the biggest element to the bottom,by swapping.
//Similarly ,second largest comes just above it and so forth till all of it is sorted

System.out.println("Part-3:After Bubble Sorting-");

   
        int n = index;  
        int temp = 0;  
         for(i=0; i < n; i++){  
                 for(int j=1; j < (n-i); j++){  
                          if(array[j-1][1].compareTo(array[j][1]) ==1 )//compareTo() method gives 1, when in s1.compareTo(s2), s1>s2.
                  //therefore, here the condition is true when array[j-1][1]>array[j][1]
       {  
                                 
        //We need to swap both first and second column whenever we swap two elements
        //since both the columns of the 2d array element will together constitute the element

        //swap elements (1st column)
                                 temp = array[j-1][0];  
                                 array[j-1][0] = array[j][0];  
                                 array[j][0] = temp;
         
         //also(2nd column)
         temp = array[j-1][1];  
                                 array[j-1][1] = array[j][1];  
                                 array[j][1] = temp;  
          
                         }  
                          
                 }  
         }

i=0;
while(i<index)
{
System.out.println("index = "+i+": Second Dimension Value = "+array[i][1]+": First Dimension's Value = "+array[i][0]);
i++;
}

//Part 5. display: Selection Sorted

//Part 6. using a selection sort, for each entry in the array, display the original index of the first

//dimension, the second dimension's value, and the first dimension's value on a single

//line separated by : (colon) sorted by the first dimension.

//In selection sort the main idea is to bring the smallest element to the top,by swapping.
//Similarly ,second smallest comes just below it and so forth till all of it is sorted

System.out.println("Part-5:After Selection Sorting-");
  for ( i = 0; i < index - 1; i++)  
        {  
            int k = i;  
            for (int j = i + 1; j < index; j++){  
                if (array[j][0].compareTo(array[k][0]) == -1 )//compareTo() METHOD gives -1, when in s1.compareTo(s2), s1<s2.
                //therefore, here the condition is true when array[j][0]<array[K][0]
               
    {  
                    k = j;//searching for lowest index  
                }  
            
  }  
            int smallerNumber = arr[k][0];   
            array[k][0] = arr[i][0];  
            array[i][0] = smallerNumber;  

   //also to swap the second part of the column(element)

   int smallerNumber = arr[k][1];   
            array[k][1] = array[i][1];  
            array[i][1] = smallerNumber;  
        }  

i=0;
while(i<index)
{
System.out.println("index = "+i+": Second Dimension Value = "+array[i][1]+": First Dimension's Value = "+array[i][0]);
i++;
}

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote