Java Lab File
Java Lab File
S.No. Topic
13. Program to read bytes from the file and display the
content on the screen.
Code:-
Output:-
2. Program to calculate the Area of Rectangle and the Area of the circle.
Code:-
Output:-
Code:-
StaticExample.incrementCounter();
StaticExample.displayCounter();
}
}
Output:-
Code:-
Output:-
5. Program to implement the Abstract class.
Code:-
Code:-
class AccessSpecifierExample {
// Public variable - accessible from anywhere
public int publicVar = 10;
Output:-
7. Program to Implement the Dynamic Method Dispatch in Java
Code :-
// Superclass
class Animal {
// Overridden method
void sound() {
System.out.println("Animal makes a sound");
}
}
// Subclass Dog
class Dog extends Animal {
// Overriding method
@Override
void sound() {
System.out.println("Dog barks");
}
}
// Subclass Cat
class Cat extends Animal {
// Overriding method
@Override
void sound() {
System.out.println("Cat meows");
}
}
Output:-
Code:-
Code:-
Output:-
import java.util.Scanner;
try {
// Attempting to divide numerator by denominator
int result = divide(numerator, denominator);
System.out.println("Result of division: " + result);
} catch (ArithmeticException e) {
// Catching arithmetic exceptions (e.g., division by zero)
System.out.println("Error: " + e.getMessage());
} catch (Exception e) {
// Catching any other exceptions
System.out.println("An error occurred: " + e.getMessage());
} finally {
// Closing resources or doing cleanup tasks
scanner.close();
System.out.println("Program execution completed.");
}
}
Output:-
11. Java Program to implement the String operations
Code:-
// Concatenation
String concatenatedString = str1 + " " + str2;
System.out.println("Concatenated String: " + concatenatedString);
// Length
System.out.println("Length of str1: " + str1.length());
// Comparison
System.out.println("Comparison of str1 and str2: " +
str1.equals(str2)); // false
System.out.println("Comparison of str1 and str4: " +
str1.equals(str4)); // true
System.out.println("Case insensitive comparison of str1 and str3: "
+ str1.equalsIgnoreCase(str3)); // true
// Substring
String substr = str1.substring(1, 3); // from index 1 to 2
System.out.println("Substring of str1: " + substr);
// Uppercase and lowercase
System.out.println("Uppercase of str1: " + str1.toUpperCase());
System.out.println("Lowercase of str2: " + str2.toLowerCase());
// Index of
System.out.println("Index of 'e' in str1: " + str1.indexOf('e'));
// returns first occurrence
// Replace
String replacedString = str1.replace('l', 'w');
System.out.println("String after replacement: " + replacedString);
// Trim
String str5 = " Hello ";
System.out.println("Trimmed String of str5: " + str5.trim());
// Split
String sentence = "Java is a powerful programming language";
String[] words = sentence.split(" ");
System.out.println("Words in the sentence:");
for (String word : words) {
System.out.println(word);
}
}
}
Output:-
12. Write a program to copy bytes from one file to another file.
Code:-
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
try {
fileInputStream = new FileInputStream(sourceFilePath);
fileOutputStream = new FileOutputStream(destinationFilePath);
// Read from the source file and write to the destination file
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
Output:-
13. Write a program to read bytes from the file and display the content on
the screen.
Code:-
import java.io.FileInputStream;
import java.io.IOException;
try {
fileInputStream = new FileInputStream(filePath);
Output:-
14. Using Stream class to copy the contents of a file from one to another.
Code:-
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Output:-
15. Multi Thread program in which every thread increment the shared
variable
Code:-
class SharedResource {
private int counter = 0;
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
sharedResource.increment();
}
}
}
Output:-
Code:-
// File: Emp.java
package employee;
e.calculateNetPay();
e.printPayrollDetails();
}
}
Output:-