6.
1-Program to implement method overloading for area of Circle, Square,
Rectangle, and Circle:
class Overload {
public void area(double radius) {
[Link](3.14 * radius * radius);
public void area(int side) {
[Link](side * side);
public void area(int length, int width) {
[Link](length * width);
public static void main(String[] args) {
Overload a = new Overload();
[Link]("Circle: ");
[Link](5.5);
[Link]("Square: ");
[Link](4);
[Link]("Rectangle: ");
[Link](5, 5);
6.2-Program to implement method overloading to find the volume of
Cube, Cylinder, and Cone:
class VolumeOverload {
public double volume(int side) {
return side * side * side;
public double volume(double radius, double height) {
return 3.14 * radius * radius * height;
public double volume(double radius, float height) {
return (1.0 / 3) * 3.14 * radius * radius * height;
public static void main(String[] args) {
VolumeOverload v = new VolumeOverload();
[Link]("Volume of Cube: " + [Link](3));
[Link]("Volume of Cylinder: " + [Link](4.0, 6.0));
[Link]("Volume of Cone: " + [Link](4.0, 6.0f));
7-create a class student with USN, name, course, spec, Sem. Derive a class Marks with 6
sub marks, calculate total and arg. Read and display data using inheritance:
import [Link];
// Base class: Student
class Student {
String usn, name, course, spec;
int sem;
void readData(Scanner sc) {
[Link]("Enter USN: ");
usn = [Link]();
[Link]("Enter Name: ");
name = [Link]();
[Link]("Enter Course: ");
course = [Link]();
[Link]("Enter Specialization: ");
spec = [Link]();
[Link]("Enter Semester (number): ");
while (![Link]()) {
[Link]("Please enter a valid semester (number): ");
[Link]();
sem = [Link]();
[Link](); // Clear newline from buffer
void displayData() {
[Link]("\n--- Student Details ---");
[Link]("USN: " + usn);
[Link]("Name: " + name);
[Link]("Course: " + course);
[Link]("Specialization: " + spec); void displayMarks() {
[Link]("Semester: " + sem); displayData();
}
[Link]("\n--- Marks
Details ---");
for (int i = 0; i < 6; i++) {
}
class Marks extends Student {
int[] marks = new int[6];
int total = 0;
double average;
void readMarks(Scanner sc) {
readData(sc);
[Link]("Enter marks for 6 subjects:");
for (int i = 0; i < 6; i++) {
[Link]("Subject " + (i + 1) + ": ");
while (![Link]()) {
[Link]("Please enter a valid mark (number): ");
[Link]();
marks[i] = [Link]();
total += marks[i];
average = total / 6.0;
[Link](); // Clear newline
8.1-ArithmeticException:
public class ArithmeticExceptionExample {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
int c = a / b; // This will cause ArithmeticException
} catch (ArithmeticException e) {
[Link]([Link]()); // Prints the exception
message
[Link]("Don't divide any number by 0.");
ArrayIndexOutOfBoundsException:
8.2: Array Index Out of Bounds Exception
public class TryException2 {
public static void main(String[] args) {
try {
int[] arr = new int[5]; // Array of size 5 (indexes 0 to 4)
arr[6] = 90; // This will throw ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
[Link]([Link]()); // Shows the exception
message
[Link]("Index out of range!");
9.1-write a java program to create 3 thread classes A, Band C implement
Start, run functions:
// Thread class A
class A extends Thread {
public void run() {
[Link]("Thread A Started");
for (int i = 1; i <= 5; i++) {
[Link]("From Thread A: i = " + i);
}
[Link]("Exit from Thread A");
// Thread class B
class B extends Thread {
public void run() {
[Link]("Thread B Started");
for (int j = 1; j <= 5; j++) {
[Link]("From Thread B: j = " + j);
[Link]("Exit from Thread B");
// Thread class C
class C extends Thread {
public void run() {
[Link]("Thread C Started");
for (int k = 1; k <= 5; k++) {
[Link]("From Thread C: k = " + k);
[Link]("Exit from Thread C");
// Main class to start all threads
public class ThreadTest {
public static void main(String[] args) {
A threadA = new A();
B threadB = new B();
C threadC = new C();
[Link]();
[Link]();
[Link]();
9.2-Thread priority:
// Thread class A
class A extends Thread {
public void run() {
[Link]("Thread A Started");
for (int i = 1; i <= 4; i++) {
[Link]("From Thread A: i = " + i);
[Link]("Exit from A");
}
}
// Thread class B
class B extends Thread {
public void run() {
[Link]("Thread B Started");
for (int j = 1; j <= 4; j++) {
[Link]("From Thread B: j = " + j);
[Link]("Exit from B");
// Thread class C
class C extends Thread {
public void run() {
[Link]("Thread C Started");
for (int k = 1; k <= 4; k++) {
[Link]("From Thread C: k = " + k);
[Link]("Exit from C");
// Main class
public class ThreadTest {
public static void main(String[] args) {
A threadA = new A();
B threadB = new B();
C threadC = new C();
// Setting priorities
[Link](Thread.MIN_PRIORITY); // Priority 1
[Link](Thread.NORM_PRIORITY); // Priority 5
[Link](Thread.MAX_PRIORITY); // Priority 10
// Starting threads
[Link]();
[Link]();
[Link]();
10.1-write data to file
import [Link];
import [Link];
public class WriteToFileExample {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("[Link]");
[Link]("Hello, this is a sample file.\n");
[Link]("We are writing this using FileWriter.");
[Link]();
[Link]("Successfully wrote to the file.");
} catch (IOException e) {
[Link]("An error occurred while writing.");
[Link]();
10.2-read data from a file
import [Link];
import [Link];
public class ReadFile {
public static void main(String[] args) {
FileReader fr = null;
try {
fr = new FileReader("D:\\[Link]");
int ch;
while ((ch = [Link]()) != -1) {
[Link]((char) ch);
[Link]();
} catch (IOException fe) {
[Link]("File not found");