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

1. Write a method named capitalizeFirstLetters that prints a capitalized version

ID: 3576762 • Letter: 1

Question

1. Write a method named capitalizeFirstLetters that prints a capitalized version of a passed in string to the screen, that is, it changes the first letter of each word in the string to upper case (if it is not already upper case). So "I really enjoy attending lab!" becomes "I Really Enjoy Attending Lab!". The string to be printed should be a parameter to the method, and the method should have a void return. You should test your method in main by getting a line of input from the user and applying the method to it. (Hint: you may want to browse the Java String docs for useful methods on strings).

2. Write a method named transpose that calculates the transpose of an n-by-n matrix when called, that is, it exchanges the rows and columns of a matrix without using extra space. The n-by-n matrix should be passed in as a parameter and nothing needs to be returned. You should test your method in main by creating a matrix of random values and passing that in to your method. Print the original matrix first, before calling transpose, then print it again after calling transpose.

3. Write a method named isRagged that determines if a passed in two dimensional array is ragged. It should take a 2d array as a parameter, and return true if it is ragged and false if it is not ragged. You should test your method in main by coding some different 2d array inputs (ragged and non-ragged).

4. Write a method named findMin that takes in an array of strings and a starting index, and calculates the minimum string (i.e. the earliest word alphabetically) in the array starting from the passed in index. The method should return the index where the minimum string is located. As before, make sure to test you method in main (you can hard code the array or use Scanner on keyboard input to populate the array).

5. Answer this question as a comment in your java file: how would you use the method findMin above to sort an array of Strings?

Explanation / Answer

import java.util.*;
//Class Operation defined
public class Operation
{
   //Capitalize first character method
   static void capitalizeFirstLetters(String st)
   {
       //Adds a space at the beginning of the string
       st = " " + st;
       //Loops till end of the string
       for(int c = 0; c < st.length(); c++)
       {
           //Extract a character
           char ch = st.charAt(c);
           //Checks if the character is space
           if(ch == ' ')
           {
               //Converts the next character to upper case
               System.out.print(" " + Character.toUpperCase(st.charAt(c+1)));
               //Increse the counter
               c++;
           }
           else
               System.out.print(ch);
       }      
   }

   //Transpose matrix method
   static void transpose(int [][] array, int row, int column)
   {      
       System.out.println("The above matrix before Transpose is ");
       //Loops till number of row
       for(int i = 0; i < row; i++)
       {
           //Loops till number of columns
           for(int j = 0; j < column; j++)
           {
               //Display the data at i and j location
           System.out.print(array[i][j]+" ");
           }
           //Prints new line
           System.out.println(" ");
       }
      System.out.println("The above matrix after Transpose is ");
       //Moves till number of columns
       for(int i = 0; i < column; i++)
       {
           //Moves till number of rows
           for(int j = 0; j < row; j++)
       {
               //Display the data at j and i location
               System.out.print(array[j][i]+" ");
       }
           //Prints new line
           System.out.println(" ");
   }
   }
   //Cheacks and returns true if a 2D matrix is ragged returns false if a 2D matrix is not ragged
   static boolean isRagged(int [][] arr)
   {
       //Find out number of rows and creates a 1D array of counted row length
       int a[] = new int[arr.length];
       //Sets the flag to 0
       int f = 0;
       //Moves till length of array a
       for(int c = 0; c < a.length; c++)  
       {
           //Finds out each column length
           a[c] = arr[c].length;
           //Displays each column length
           System.out.print(" Row " + (c+1) + " " + a[c]);
       }
       //Moves till end of array a
       for(int c = 0; c < a.length; c++)
       {
           //Moves Row + 1 position to the length of array a
           for(int d = c + 1; d < a.length; d++)
           {
               //Compares the length of consicutive columns for equality
               if(a[c] == a[d])
                   //if equal set the flag to 1
                   f = 1;
           }
       }
       if(f == 0)
           return true;
       else
           return false;
   }

   //Returns the index position of minimum string from the specified position
   static int findMin (String s[], int pos)
   {
       //Assumes the specified index position length is the mininum and store it in min
       int min = s[pos].length();
       //Sets the specified index position to p
       int p = pos;
       //Loops from specified index position + 1 to the end of all the string
       for(int c = pos + 1; c < s.length; c++)
       {
           //Compares the length of next string with the min
           if(s[c].length() < min)
           {
               //If it is less then update the length
               min = s[c].length();
               //Update the position
               p = c;
           }
       }
       //Return the minimum string index position
       return p;
   }
   //Main method
   public static void main(String ss[])
   {
       String s;
       //Scanner class to accept data
       Scanner sc = new Scanner(System.in);
       //Accept a string
       System.out.println("Enter a string: ");
       s = sc.nextLine();
       //Call the capitalized method
       capitalizeFirstLetters(s);
      
       //Transpose Matrix
       int i, j;
       //Accept number of rows and number of columns
       System.out.println(" Enter total rows and columns: ");
       int r = sc.nextInt();
       int c = sc.nextInt();
       //Creates a 2D matrix as per the entered row and column
       int arr[][] = new int[r][c];
       //Enter data to the matrix
       System.out.println(" Enter matrix:");
      for(i = 0; i < r; i++)
       {
           for(j = 0; j < c; j++)
           {
               arr[i][j] = sc.nextInt();
        }
       }
       //Call the transpose matrix method
       transpose(arr, r, c);

       //Ragged Array
       //Creates a 2D matrix with data
       int[][] a = new int[][]
       {
       new int[] { 1, 2, 3 },
       new int[] { 1, 2, 3}
       };
       //Creates a 2D matrix with data
       int[][] b = new int[][]
       {
       new int[] { 1, 2, 3 },
       new int[] { 1, 2, 3, 4}
       };
       //Checks whethere the matrix is Ragged or not
       if(isRagged(a))
           System.out.println(" Ragged Array");
       else
           System.out.println(" Not a Ragged Array");
       if(isRagged(b))
           System.out.println(" Ragged Array");
       else
           System.out.println(" Not a Ragged Array");

       //Find minimum string starting from a specified index position
       //Creates a 2D String matrix
       String str[] = {"This", "is", "Try to do it", "fine", "ok", "Mind it"};
       //Accept the index position
       System.out.println("Enter the index position: ");
       int pos = sc.nextInt();
       //Calls and displays the minimum string
       System.out.println(" Minimum string is located at index: " + findMin(str, pos));
   }
}

Output:

Enter a string:
this is
This Is
Enter total rows and columns:
2
3

Enter matrix:
1
2
3
4
5
6
The above matrix before Transpose is
1 2 3
4 5 6
The above matrix after Transpose is
1 4
2 5
3 6

Row 1 3
Row 2 3
Not a Ragged Array

Row 1 3
Row 2 4
Ragged Array
Enter the index position:
1

Minimum string is located at index: 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote