help do this lab Write a method named capitalizeFirstLetters that prints a capit
ID: 3576844 • Letter: H
Question
help do this lab
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. T
he 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
package dec11;
import java.util.Scanner;
public class StringCaps {
public static void main(String[] args) {
System.out.println("Enter a string: ");
Scanner in = new Scanner(System.in);
String str = in.nextLine();
capitalizeFirstLetters(str);
int[][] matrix = new int[3][4];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = (int) (Math.random() * 1000);
}
}
System.out.println("orignal matrix: ");
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
System.out.print(matrix[row][col] + " ");
}
System.out.println();
}
transpose(matrix);
}
private static void transpose(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int[][] trasposedMatrix = new int[n][m];
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
trasposedMatrix[x][y] = matrix[y][x];
}
}
System.out.println("TrasposedMatrix matrix: ");
for (int row = 0; row < trasposedMatrix.length; row++) {
for (int col = 0; col < trasposedMatrix[row].length; col++) {
System.out.print(trasposedMatrix[row][col] + " ");
}
System.out.println();
}
}
private static void capitalizeFirstLetters(String str) {
String[] arr = str.split(" ");
StringBuilder s = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
String s1 = arr[i];
s.append(s1.substring(0, 1).toUpperCase() + s1.substring(1) + " ");
}
System.out.println(s);
}
}
output:
Enter a string:
I really enjoy attending lab!
I Really Enjoy Attending Lab!
orignal matrix:
361 901 124 935
726 286 530 916
928 369 591 580
TrasposedMatrix matrix:
361 726 928
901 286 369
124 530 591
935 916 580
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.