Question

Homework 12: Matrix Operations Read Chapter 8 on Multi-dimensional arrays. This chapter has sample code that will be helpful5. After displaying the matrix, display a menu to the user with the following options: P Print matrix R Reverse rows ScolumnS2. transpose() This method accepts a 2D matrix named matrix and returns a new matrix whose columns are rows of the matrix pas3. columnSum() This method accepts a 2D matrix named matrix and returns a one dimensional array of numbers such that the valu4. reverseRows() This method accepts a 2D matrix named matrix and returns a new matrix of the same size as the original but t

/***************************************************

  Name: 
  Date: 
  Homework #7
  
  Program name:        HexUtilitySOLUTION
  Program description: Accepts hexadecimal numbers as input.
                       Valid input examples: F00D, 000a, 1010, FFFF, Goodbye, BYE
                       Enter BYE (case insensitive) to exit the program.
  
****************************************************/

import java.util.Scanner;

public class HexUtilitySOLUTION {

  public static void main(String[] args) {

    // Maximum length of input string
    final byte INPUT_LENGTH = 4;

    String userInput = "";                   // Initialize to null string
    Scanner input = new Scanner(System.in);

    // Process the inputs until BYE is entered
    do {
      // Input a 4 digit hex number
      System.out.print("\nEnter a hexadecimal string, or enter BYE to quit:  ");
      userInput = input.next().toUpperCase();

      // Process the input
      switch (userInput) {

        case "BYE": break;
            
        default:        if (userInput.length() != INPUT_LENGTH) {
                          // Input length is incorrect
                          System.out.printf("      The input %s is the wrong length, it should be %d characters long.\n", userInput, INPUT_LENGTH);
                          break;
                        }

                        // Input length is correct
                        if (isValidHex(userInput)) {  //Call method isValidHex
                          // The input is a valid hexadecimal string
                          System.out.printf("      The input %s is a valid hexadecimal value.\n", userInput);
                        }

                        else {
                          // String is either the wrong length or is not a valid hexadecimal string
                          System.out.printf("      The input %s is not a valid hexadecimal value.\n", userInput);
                        }
                        break;
        }
    } while (!userInput.equals("BYE"));

    // Exit the program
    System.out.println("\nGoodbye!");
    input.close();
  }
 
  // Method to validate the input.
  // This method returns true or false to the caller (main).
  // This method accepts one parameter - the user input.
  public static boolean isValidHex(String userIn) {
    boolean isValid = false;
        
    // The length is correct, now check that all characters are legal hexadecimal digits
    for (int i = 0; i < userIn.length(); i++) {
      char thisChar = userIn.charAt(i);

      // Is the character a decimal digit (0..9)? If so, advance to the next character
      if (Character.isDigit(thisChar)) {
        isValid = true;
      }

      else {
        // Character is not a decimal digit (0..9), is it a valid hexadecimal digit (A..F)?
        if ((thisChar >= 'A') && (thisChar <= 'F')) {
          isValid = true;
        }
        else {
          // Found an invalid digit, no need to check other digits, exit this loop
          isValid = false;
          break;
        }
      }
    }
        
    // Returns true if the string is a valid hexadecimal string, false otherwise
    return isValid;
  }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Matrix.java

import java.util.Scanner;

public class Matrix {

   public static void main(String[] args) {
       int row, col;
       char chioice;

       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       while (true) {
           // Getting the input entered by the user
           System.out.print("Enter no of rows :");
           row = sc.nextInt();
           if (row < 1 || row > 4) {
               System.out.println("** Invalid.Must be between 1-4 **");
           } else
               break;

       }

       while (true) {
           // Getting the input entered by the user
           System.out.print("Enter no of cols :");
           col = sc.nextInt();
           if (col < 1 || col > 4) {
               System.out.println("** Invalid.Must be between 1-4 **");
           } else
               break;

       }
       int array[][] = new int[row][col];
       for (int i = 0; i < row; i++) {

           for (int j = 0; j < col; j++) {
               System.out.print("Enter Element in row#" + (i) + " column#"
                       + (j) + ":");
               array[i][j] = sc.nextInt();
           }
       }

       while (true) {
           System.out.println("\nP Print matrix");
           System.out.println("R Reverse Rows ");
           System.out.println("S ColumnSum");
           System.out.println("T Transpose");
           System.out.println("Q Quit");
           chioice = sc.next().charAt(0);
           switch (chioice) {
           case 'P':
           case 'p': {
               printMatrix(array);
               continue;
           }
           case 'R':
           case 'r': {
               reverseMatrix(array);
               continue;
           }

           case 'S':
           case 's': {
               columnSum(array);
               continue;
           }
           case 'T':
           case 't': {
               transpose(array);
               continue;
           }
           case 'Q':
           case 'q': {
               break;
           }
           default: {
               System.out.println("** Invalid Choice **");
           }

           }
           break;
       }

   }

   private static void transpose(int[][] array) {
   for(int i=0;i<array[0].length;i++)
   {
   for(int j=0;j<array.length;j++)
   {
   System.out.print(array[j][i]+" ");
   }
   System.out.println();
   }

      
   }

   private static void columnSum(int[][] array) {
       int sum=0;
       for (int i = 0; i < array.length; i++) {
           for (int j = 0; j < array[0].length; j++) {
               System.out.print(array[i][j] + " ");
           }
           System.out.println();
       }
      
       for(int j=0;j<array[0].length;j++)
       {
           sum=0;
           for(int i=0;i<array.length;i++)
           {
              
               sum+=array[i][j];
           }
           System.out.print(sum+" ");
          
       }
       System.out.println();
      
   }

   private static void reverseMatrix(int[][] array) {
       for(int j = 0; j < array.length; j++){
       for(int i = 0; i < array[j].length / 2; i++) {
       int temp = array[j][i];
       array[j][i] = array[j][array[j].length - i - 1];
       array[j][array[j].length - i - 1] = temp;
       }
       }
      
   }

   private static void printMatrix(int[][] array) {
       for (int i = 0; i < array.length; i++) {
           for (int j = 0; j < array[0].length; j++) {
               System.out.print(array[i][j] + " ");
           }
           System.out.println();
       }

   }

}
=======================================

Output:

Enter no of rows :3
Enter no of cols :2
Enter Element in row#0 column#0:0
Enter Element in row#0 column#1:1
Enter Element in row#1 column#0:10
Enter Element in row#1 column#1:11
Enter Element in row#2 column#0:20
Enter Element in row#2 column#1:21

P Print matrix
R Reverse Rows
S ColumnSum
T Transpose
Q Quit
p
0 1
10 11
20 21

P Print matrix
R Reverse Rows
S ColumnSum
T Transpose
Q Quit
R

P Print matrix
R Reverse Rows
S ColumnSum
T Transpose
Q Quit
p
1 0
11 10
21 20

P Print matrix
R Reverse Rows
S ColumnSum
T Transpose
Q Quit
s
1 0
11 10
21 20
33 30

P Print matrix
R Reverse Rows
S ColumnSum
T Transpose
Q Quit
T
1 11 21
0 10 20

P Print matrix
R Reverse Rows
S ColumnSum
T Transpose
Q Quit
Q

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
/*************************************************** Name: Date: Homework #7 Program name: HexUtilitySOLUTION Program description: Accepts hexadecimal numbers as input. Valid...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For the program described below, document your code well. Use descriptive identifier names. Use s...

    For the program described below, document your code well. Use descriptive identifier names. Use spaces and indentation to improve readability. Include a beginning comment block as well as explanatory comments throughout. In the beginning comment block, add your name, program name, date written, program description. The description briefly describes what the program does. Include a comment at the beginning of each method to describe what the method does. Write a program to perform operations on square matrices (i.e. equal number...

  • For the program described below, document your code well. Use descriptive identifier names. Use spaces and...

    For the program described below, document your code well. Use descriptive identifier names. Use spaces and indentation to improve readability. Include a beginning comment block as well as explanatory comments throughout. In the beginning comment block, add your name, program name, date written, program description. The description briefly describes what the program does. Include a comment at the beginning of each method to describe what the method does. Write a program to perform operations on square matrices (i.e. equal number...

  • Assignment 14.3: Valid Email (10 pts) image source Write a program that takes as input an...

    Assignment 14.3: Valid Email (10 pts) image source Write a program that takes as input an email address, and reports to the user whether or not the email address is valid. For the purposes of this assignment, we will consider a valid email address to be one that contains an @ symbol The program must allow the user to input as many email addresses as desired until the user enters "q" to quit. For each email entered, the program should...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • 21 Write a program that asks the user to input the length and breadth of a...

    21 Write a program that asks the user to input the length and breadth of a soccer field, and then computes and returns the number of square meters of grass required to cover the field The formula for the area is the product of length and breadth (5) 22 The following program uses the break statement to terminate an infinite while loop to print 5 numbers Rewrite the program to use a while loop to display numbers from 1 to...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • P7.5– A theater seating chart is implemented as a two-dimensional array of ticket prices, like this:...

    P7.5– A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 10 10 10 10 20 20 20 20 20 20 10 10 10 10 20 20 20 20 20 20 10 10 20 20 30 30...

  • Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right...

    Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right corner to the bottom left corner and i dont know how to do it please help me thank you dd another method to the bottom of the "TwoDimArraysMethods.java" file called "printDiagonalRL()"                                         public static void printDiagonalRL(int[][] matrix) 4. Call this method in the main file ("TwoDimArraysAsParam.java") passing it "board." e.g. TwoDimArraysMethods.printDiagonal(board); 5. Your new method should print any numbers along the diagonal from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT