Question

In: Computer Science

Create a Java program that asks a user to enter two file names. The program will...

Create a Java program that asks a user to enter two file names. The program will read in two files and do a matrix multiplication. Check to make sure the files exist.

first input is the name of the first file and it has
2 (length)

  • 4 5
  • 6 7
  • Second input is the name of the second file and it has
  • 2 (length)
  • 6 7
  • 8 9

try catch method

Solutions

Expert Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MatrixMultiplication {
  
//private static final String FILE1 = "matrix1.txt";
//private static final String FILE2 = "matrix2.txt";
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Scanner fileReader;
int rows1 = 0, rows2 = 0, cols1 = 0, cols2 = 0;
// read file1
System.out.print("Please enter the name of the first file: ");
String fileName1 = sc.nextLine().trim();
try
{
fileReader = new Scanner(new File(fileName1));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine();
cols1 = line.split(" ").length;
rows1++;
}
if(rows1 < 2)
{
System.out.println(fileName1 + " does not have 2 lines!");
System.exit(0);
}
}catch(FileNotFoundException fnfe){
System.out.println(fileName1 + " could not be found!");
System.exit(0);
}
  
// read file2
System.out.print("Please enter the name of the second file: ");
String fileName2 = sc.nextLine().trim();
try
{
fileReader = new Scanner(new File(fileName2));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine();
cols2 = line.split(" ").length;
rows2++;
}
if(rows2 < 2)
{
System.out.println(fileName2 + " does not have 2 lines!");
System.exit(0);
}
}catch(FileNotFoundException fnfe){
System.out.println(fileName2 + " could not be found!");
System.exit(0);
}
  
int[][] arr1 = new int[rows1][cols1];
int[][] arr2 = new int[rows2][cols2];
  
// read file1 for matrix
try
{
fileReader = new Scanner(new File(fileName1));
while(fileReader.hasNextLine())
{
for(int i = 0; i < arr1.length; i++)
{
String line[] = fileReader.nextLine().split(" ");
for(int j = 0; j < line.length; j++)
{
arr1[i][j] = Integer.parseInt(line[j]);
}
}
}
}catch(FileNotFoundException fnfe){
System.out.println(fileName1 + " could not be found!");
System.exit(0);
}
  
// read file2 for matrix
try
{
fileReader = new Scanner(new File(fileName2));
while(fileReader.hasNextLine())
{
for(int i = 0; i < arr2.length; i++)
{
String line[] = fileReader.nextLine().split(" ");
for(int j = 0; j < line.length; j++)
{
arr2[i][j] = Integer.parseInt(line[j]);
}
}
}
}catch(FileNotFoundException fnfe){
System.out.println(fileName1 + " could not be found!");
System.exit(0);
}
  
int[][] product = matrixProduct(arr1, arr2, rows1, cols1, cols2);
System.out.println("The product matrix is:");
printMatrix(product);
}
  
private static int[][] matrixProduct(int[][] arr1, int[][] arr2, int rows1, int cols1, int cols2)
{
int[][] product = new int[rows1][cols2];
for(int i = 0; i < rows1; i++)
{
for(int j = 0; j < cols2; j++)
{
for(int k = 0; k < cols1; k++)
{
product[i][j] = arr1[i][k] * arr2[k][j];
}
}
}
return product;
}
  
private static void printMatrix(int[][] arr)
{
for(int i = 0; i < arr.length; i++)
{
for(int j = 0; j < arr[0].length; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}

****************************************************************** SCREENSHOT *********************************************************


Related Solutions

Create a program that asks the user for the names of two car dealerships and the...
Create a program that asks the user for the names of two car dealerships and the # of cars sold in each one. Then output that data in two columns as shown below. The "Store location" column has a width of 25, while the "Cars sold" column has a width of 9. Also, notice the alignment of the second column. The program should end with the "Press Enter to end this program" prompt. OUTPUT Enter the location for the first...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
In a file called ThreeOperations.java, write a program that: Asks the user to enter two real...
In a file called ThreeOperations.java, write a program that: Asks the user to enter two real numbers (doubles, not integers) called N1 and N2. It is OK if your program crashes when the user does not enter valid double numbers. Computes and displays the following operations between the two: multiplication, division, exponentiation. For the results of these two operations, only two decimal digits should be displayed. For example: if the user enters numbers 11 and 7, your program output should...
In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
In a file called LengthSum.java, write a program that: Asks the user to enter a string....
In a file called LengthSum.java, write a program that: Asks the user to enter a string. Asks the user to enter a second string. Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please enter a second...
In a file called Conversions.java, write a program that: Asks the user to enter a double...
In a file called Conversions.java, write a program that: Asks the user to enter a double number. Stores that number into a variable called z. Casts variable z into an integer, and stores the result into a variable called z1. Creates a variable z2, and sets it equal to the integer closest to z. Creates a variable z3, and sets it equal to the floor of z. Creates a variable z4, and sets it equal to the ceiling of z....
In a file called FourCapitalizations.java, write a program that: Asks the user to enter a string....
In a file called FourCapitalizations.java, write a program that: Asks the user to enter a string. Prints out four different versions of that string: The original version of the string. The upper-case version of the string. The lower-case version of the string. A version where the character at position 0 capitalized, and all other characters in lower case. For example: if the user enters string "gaNDalF12 !! AB3w", your program output should look EXACTLY like this: Please enter a string:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT