0% found this document useful (0 votes)
8 views7 pages

Java Lab Exp-6,7

The document outlines two Java experiments focusing on the use of packages and I/O operations. The first experiment demonstrates creating a package for matrix operations, while the second illustrates file reading and writing using the Java I/O package. Additionally, coding questions prompt the creation of a user input program utilizing various input methods.

Uploaded by

Techno Business
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Java Lab Exp-6,7

The document outlines two Java experiments focusing on the use of packages and I/O operations. The first experiment demonstrates creating a package for matrix operations, while the second illustrates file reading and writing using the Java I/O package. Additionally, coding questions prompt the creation of a user input program utilizing various input methods.

Uploaded by

Techno Business
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EXPERIMENT – 06

Objective:

To create a Java program that demonstrates the use of Java packages.

Theory:
Java packages are used to organize classes into namespaces, making it easier to
manage and reuse code. A package is a collection of related classes and interfaces.
By using packages, we can avoid naming conflicts and create a more organized
codebase.

Code:

// Package declaration
package com.example.myapp;

// Class in the package


public class MyClass {
public void displayMessage() {
System.out.println("Hello, world!");
}
}

// Main class
public class Main {
public static void main(String[] args) {
// Creating an instance of MyClass
com.example.myapp.MyClass myClass = new
com.example.myapp.MyClass();
// Calling the displayMessage method
myClass.displayMessage();
}
}

Explanation:

We declare a package named com.example.myapp at the beginning of the


file. This indicates that the MyClass class belongs to this package.

17
Inside the package, we define a class MyClass with a method
displayMessage that prints "Hello, world!".

In the Main class, we create an instance of MyClass using the fully qualified
name com.example.myapp.MyClass and call the displayMessage method.

Output:

Hello, world!

In this example, we demonstrate the use of Java packages by creating a


package com.example.myapp and a class MyClass inside it. We then use the
fully qualified name com.example.myapp.MyClass to create an instance of
MyClass in the Main class. This allows us to organize our code into
packages and access classes from other packages.

Coding Questions:
(a) Create a package named Mathematics and add a class Matrix with
methods to add and subtract matrices (2x2). Write a Java program importing
the Mathematics package and use the classes defined in it.

Step 1: Create the Mathematics package

package Mathematics;

public class Matrix {


private int[][] matrix;

public Matrix(int[][] matrix) {


this.matrix = matrix;
}

// Method to add two matrices


public static Matrix add(Matrix m1, Matrix m2) {
int[][] result = new int[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
result[i][j] = m1.matrix[i][j] + m2.matrix[i][j];

18
}
}
return new Matrix(result);
}

// Method to subtract two matrices


public static Matrix subtract(Matrix m1, Matrix m2) {
int[][] result = new int[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
result[i][j] = m1.matrix[i][j] - m2.matrix[i][j];
}
}
return new Matrix(result);
}

// Method to print the matrix


public void printMatrix() {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}

Step 2: Use the Mathematics package in another Java program

import Mathematics.Matrix;

public class Main {


public static void main(String[] args) {
int[][] matrix1 = { {1, 2}, {3, 4} };
int[][] matrix2 = { {5, 6}, {7, 8} };

Matrix m1 = new Matrix(matrix1);


Matrix m2 = new Matrix(matrix2);

19
// Adding matrices
Matrix sum = Matrix.add(m1, m2);
System.out.println("Sum of matrices:");
sum.printMatrix();

// Subtracting matrices
Matrix difference = Matrix.subtract(m1, m2);
System.out.println("Difference of matrices:");
difference.printMatrix();
}
}

Output:

Sum of matrices:
68
10 12
Difference of matrices:
-4 -4
-4 -4

20
EXPERIMENT – 07

Objective:
To create a Java program that demonstrates the use of the Java I/O package for
reading and writing files.

Theory:
The Java I/O (Input/Output) package provides classes and interfaces for reading
and writing data to and from files, streams, and other I/O devices. It includes
classes like File, FileInputStream, FileOutputStream, BufferedReader,
BufferedWriter, etc., which are used for file handling operations.

Code:

import java.io.*;

// Main class
public class Main {
public static void main(String[] args) {
// Specify the file path
String filePath = "example.txt";

// Write to a file
try (BufferedWriter writer = new BufferedWriter(new
FileWriter(filePath))) {
writer.write("Hello, world!");
} catch (IOException e) {
System.err.println("Error writing to file: " + e.getMessage());
}

// Read from a file


try (BufferedReader reader = new BufferedReader(new
FileReader(filePath))) {
String line = reader.readLine();
System.out.println("Contents of the file: " + line);
} catch (IOException e) {
System.err.println("Error reading from file: " + e.getMessage());
}
}

21
}

Explanation:

• We import the necessary classes from the java.io package for file handling
operations.
• In the main method, we specify the file path (example.txt) where we want to
read from and write to.
• We use a BufferedWriter to write the string "Hello, world!" to the file.
• We use a BufferedReader to read the contents of the file and print them to the
console.

Output:

Contents of the file: Hello, world!


In this example, we demonstrate the use of the Java I/O package for file
handling. We write the string "Hello, world!" to a file using a BufferedWriter
and then read the contents of the file using a BufferedReader and print them to
the console

Coding Questions:

(a) Write a program in Java to take input from user by using all the following
methods:
Command Line Arguments, DataInputStream Class, BufferedReader Class,
Scanner Class, Console Class

import java.io.*;
import java.util.Scanner;

public class UserInputDemo {


public static void main(String[] args) throws IOException {
// Command Line Arguments
if (args.length > 0) {
System.out.println("Command Line Argument: " + args[0]);
} else {
System.out.println("No Command Line Argument provided.");
}

22
// DataInputStream Class
DataInputStream dataInputStream = new DataInputStream(System.in);
System.out.print("Enter input using DataInputStream: ");
String dataInputStreamInput = dataInputStream.readLine();
System.out.println("DataInputStream Input: " + dataInputStreamInput);

// BufferedReader Class
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter input using BufferedReader: ");
String bufferedReaderInput = bufferedReader.readLine();
System.out.println("BufferedReader Input: " + bufferedReaderInput);

// Scanner Class
Scanner scanner = new Scanner(System.in);
System.out.print("Enter input using Scanner: ");
String scannerInput = scanner.nextLine();
System.out.println("Scanner Input: " + scannerInput);

// Console Class
Console console = System.console();
if (console != null) {
String consoleInput = console.readLine("Enter input using Console: ");
System.out.println("Console Input: " + consoleInput);
} else {
System.out.println("No console available for input.");
}
}
}

Output:
Command Line Argument: HelloWorld
Enter input using DataInputStream: DataStreamInput
DataInputStream Input: DataStreamInput
Enter input using BufferedReader: BufferedReaderInput
BufferedReader Input: BufferedReaderInput
Enter input using Scanner: ScannerInput
Scanner Input: ScannerInput
No console available for input.

23

You might also like