Java Lab Exp-6,7
Java Lab Exp-6,7
Objective:
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;
// 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:
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!
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.
package Mathematics;
18
}
}
return new Matrix(result);
}
import Mathematics.Matrix;
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());
}
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:
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;
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