Need help with Intro to Java Programming problem To complete

Need help with Intro to Java Programming problem:

To complete the assignment, complete the file called Hw2.java containing the Hw2 class containing the methods as described below. You will complete the file by writing the following methods:

public static void print2DIArray(int [][] output): The method prints the elements of the two-dimensional array output to the standard output, one row per line.

public static int[][] enter2DPosArray(): The method enters a two-dimensional integer array from the user. It forces the user to enter positive (> 0) values for the number of rows and columns. It raises an InputMismatchException if the user enters anything other than positive (> 0) values for any array entry. The function will need to declare and use a Scanner object. The Scanner object should be closed before any exceptions are raised and before the method returns if no exceptions are raised.

public static int[][] transposition(int [][] anArray): The method returns a new two-dimensional array representing the transpose of anArray, that is, an array in which each row has become a column. I provide examples in the sample output below and also in the comments in the template file.

I have provided main() and printArray() methods that you should use unchanged. You will simply write the functions described above.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Here is the HW2.java file that we are provided:

package hw2;

import java.util.*;

public class Hw2 {

   // Do not modify this method
   public static void main(String[] args) {
      
       try
       {
           int [][] iArray = enter2DPosArray();
           System.out.println(\"The original array values:\");
           print2DIArray(iArray);
           int [][] tArray = transposition(iArray);
           System.out.println(\"The transposed array values:\");
           print2DIArray(tArray);
       }

       catch (InputMismatchException exception)
       {
           System.out.println(\"The array entry failed. The program will now halt.\");
       }

   }
  
       // A function that prints a 2D integer array to standard output
       // It prints each row on one line with newlines between rows
       public static void print2DIArray(int[][] output) {
      
       }

   // A function that enters a 2D integer array from the user
   // It raises an InputMismatchException if the user enters anything other
   // than positive (> 0) values for the number of rows, the number of
   // columns, or any array entry
   public static int[][] enter2DPosArray() throws InputMismatchException {

       // A stub -- remove this when you write the method
       return new int[0][0];
   }

   /* Return a new array representing the transposition of anArray, that is,
   * an array in which each row has become a column.
   * For example, the array:
   * 11 12 13
   * 14 15 16
   * would become:
   * 11 14
   * 12 15
   * 13 16
   * Do NOT hard-code anything into this function -- use the parameter.
   */
   public static int[][] transposition(int [][] anArray) {

       // A stub -- remove this when you write the method
       return new int[0][0];
   }

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following is some sample output from my solution. The first sample output shows a completely correct run of the main() method:

How many rows (> 0) should the array have? 3
How many columns (> 0) should the array have? 2
Enter a positive (> 0) integer value: 1
Enter a positive (> 0) integer value: 2
Enter a positive (> 0) integer value: 3
Enter a positive (> 0) integer value: 4
Enter a positive (> 0) integer value: 5
Enter a positive (> 0) integer value: 6
The original array values:
1 2
3 4
5 6
The transposed array values:
1 3 5
2 4 6

The following shows a sample run in which the user types a non-positive value when prompted for the number of rows:

How many rows (> 0) should the array have? -1
How many rows (> 0) should the array have? 1
How many columns (> 0) should the array have? 5
Enter a positive (> 0) integer value: 1
Enter a positive (> 0) integer value: 2
Enter a positive (> 0) integer value: 3
Enter a positive (> 0) integer value: 4
Enter a positive (> 0) integer value: 5
The original array values:
1 2 3 4 5
The transposed array values:
1
2
3
4
5

The following shows a sample run in which the user types a non-positive value when prompted for the number of columns (after entering a correct value for the rows):

How many rows (> 0) should the array have? 2
How many columns (> 0) should the array have? -3
How many columns (> 0) should the array have? -2
How many columns (> 0) should the array have? -1
How many columns (> 0) should the array have? 5
Enter a positive (> 0) integer value: 1
Enter a positive (> 0) integer value: 2
Enter a positive (> 0) integer value: 3
Enter a positive (> 0) integer value: 4
Enter a positive (> 0) integer value: 5
Enter a positive (> 0) integer value: 6
Enter a positive (> 0) integer value: 7
Enter a positive (> 0) integer value: 8
Enter a positive (> 0) integer value: 9
Enter a positive (> 0) integer value: 10
The original array values:
1 2 3 4 5
6 7 8 9 10
The transposed array values:
1 6
2 7
3 8
4 9
5 10

The following shows a sample run in which the user types a non-positive value when prompted for one of the array values (after entering correct values for the rows and columns):

How many rows (> 0) should the array have? 1
How many columns (> 0) should the array have? 2
Enter a positive (> 0) integer value: 3
Enter a positive (> 0) integer value: -1
The array entry failed. The program will now halt.

Solution

import java.util.*;
public class Hw2 {
// Do not modify this method
public static void main(String[] args) {
  
try
{
int [][] iArray = enter2DPosArray();
System.out.println(\"The original array values:\");
print2DIArray(iArray);
int [][] tArray = transposition(iArray);
System.out.println(\"The transposed array values:\");
print2DIArray(tArray);
}
catch (InputMismatchException exception)
{
System.out.println(\"The array entry failed. The program will now halt.\");
}
}
  
// A function that prints a 2D integer array to standard output
// It prints each row on one line with newlines between rows
public static void print2DIArray(int[][] output) {
  
for(int i=0;i<output.length;i++)
{
for(int j=0;j<output[0].length;j++)
System.out.print(output[i][j]+\" \");
System.out.println(\" \");
}
  
}
// A function that enters a 2D integer array from the user
// It raises an InputMismatchException if the user enters anything other
// than positive (> 0) values for the number of rows, the number of
// columns, or any array entry
public static int[][] enter2DPosArray() throws InputMismatchException {
// A stub -- remove this when you write the method
int r=0,c=0;
int[][] arr;
Scanner sc = new Scanner(System.in);
while(r<1)
{
System.out.print(\"How many rows (> 0) should the array have? \");
r=sc.nextInt();
}
while(c<1)
{
System.out.print(\"How many columns (> 0) should the array have? \");
c=sc.nextInt();
}
  
arr=new int[r][c];
System.out.println(\"\ Enter elements\ \");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(\"Enter a positive (> 0) integer value: \");

arr[i][j]=sc.nextInt();
if(arr[i][j] <0 )throw new InputMismatchException(\"The array entry failed. The program will now halt.\");
}
}

return arr;
}
/* Return a new array representing the transposition of anArray, that is,
* an array in which each row has become a column.
* For example, the array:
* 11 12 13
* 14 15 16
* would become:
* 11 14
* 12 15
* 13 16
* Do NOT hard-code anything into this function -- use the parameter.
*/
public static int[][] transposition(int [][] anArray) {
  
int[][] arr= new int[anArray.length][anArray[0].length];
for(int i=0;i<anArray.length;i++)
{
for(int j=0;j<anArray[0].length;j++)
arr[j][i]= anArray[i][j];
}

return arr;
}
}

Need help with Intro to Java Programming problem: To complete the assignment, complete the file called Hw2.java containing the Hw2 class containing the methods
Need help with Intro to Java Programming problem: To complete the assignment, complete the file called Hw2.java containing the Hw2 class containing the methods
Need help with Intro to Java Programming problem: To complete the assignment, complete the file called Hw2.java containing the Hw2 class containing the methods
Need help with Intro to Java Programming problem: To complete the assignment, complete the file called Hw2.java containing the Hw2 class containing the methods
Need help with Intro to Java Programming problem: To complete the assignment, complete the file called Hw2.java containing the Hw2 class containing the methods

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site