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...
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...
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...
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...
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to enter two strings. First, the program prompts: Please enter a string. The program should read in the string, and prompts: Please enter another string. The program reads in two strings and it prints a menu to the user. The program asks for user to enter an option and performs one of the following: Here are the options on the menu: Option a: checks if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT