Uploaded by uchirai.govere

Java lab 1

advertisement
Java lab 1 11/09/23
Upon completing this exercise, you should zip and submit your package to the link due 17/09
Task 1: File Reader
Task Description:
In this exercise, you will create a Java program that reads and displays the content of a text file. This
exercise will help you practice file I/O operations in Java.
Instructions:
1. Create a new Java class called `FileReader`.
2. Inside the `FileReader` class, write a `main` method.
3. In the `main` method, do the following:
- Prompt the user to enter the name of the file they want to read (e.g., "Enter the filename: ").
- Read the file name entered by the user.
4. Use Java's file I/O classes to open and read the specified file.
5. Display the content of the file line by line to the console.
6. Handle exceptions that may occur during file reading. For example, handle cases where the file
does not exist or there are I/O errors.
7. Close the file after reading.
Challenge:
- Modify the program to count and display the total number of lines in the file.
- Implement error handling to provide informative error messages for different file-related issues.
Hint: You can use classes like `File`, `FileReader`, and `BufferedReader` for file handling.
Task 2: Matrix Operation
Task Description:
In this exercise, you will create a Java program that reads and stores two matrices and performs a
matrix operation (addition or multiplication) on them. This exercise will help you practice working
with arrays and matrix operations in Java.
Instructions:
1. Create a new Java class called `MatrixOperation`.
2. Inside the `MatrixOperation` class, write a `main` method.
3. In the `main` method, do the following:
- Prompt the user to enter the dimensions (rows and columns) of the first matrix and the second
matrix. Ensure that the number of columns in the first matrix matches the number of rows in the
second matrix for multiplication.
- Create two 2D arrays (matrices) based on the dimensions provided by the user.
4. Prompt the user to enter the values for each element in the first matrix and the second matrix.
5. Perform the matrix operation (either addition or multiplication) based on user input. You can
implement both options and ask the user which operation to perform.
6. Display the result matrix.
7. Handle exceptions that may occur during input or calculations.
Challenge:
- Implement error handling to ensure that the dimensions of the matrices are suitable for the chosen
operation (e.g., multiplication is only possible if the number of columns in the first matrix matches
the number of rows in the second matrix).
Hint: You can use nested loops to input matrix elements and perform matrix operations.
Task 3: File Writer
Task Description:
In this exercise, you will create a Java program that takes user input and writes it to a text file. This
exercise will help you practice file I/O operations for writing data to a file.
Instructions:
1. Create a new Java class called `FileWriter`.
2. Inside the `FileWriter` class, write a `main` method.
3. In the `main` method, do the following:
- Prompt the user to enter a filename for the output file (e.g., "Enter the filename: ").
- Read the file name entered by the user.
4. Prompt the user to enter the text data they want to write to the file.
5. Use Java's file I/O classes to create and open the specified file for writing.
6. Write the user-provided text data to the file.
7. Close the file after writing.
Challenge:
- Modify the program to append the text to an existing file if it already exists, rather than overwriting
it.
- Implement error handling to handle cases where the file cannot be created or written due to I/O
errors.
Hint: You can use classes like `File`, `FileWriter`, and `BufferedWriter` for file writing.
Task 4: File Data Analysis
Task Description:
In this exercise, students will create a Java program to read data from a file, perform data analysis,
and write the results to another file.
Instructions:
1. The program should read the data from `values.txt`.
2. Calculate and store the following:
a. The smallest number
b. The largest number
c. The average of the numbers
3. Write the calculated results, as well as their full name and student ID, to a file named
`solutions.txt`.
4. Handle exceptions that may occur during file I/O operations.
Challenge:
Modify the program to allow the user to input the filename for `values.txt` and their full name and
student ID.
Download