Java Lab Manual(C-20)
Java Lab Manual(C-20)
Experiment-1
Source Code:
System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);
System.out.println("Product of " + floatNum + " and " + doubleNum + " is: " + product);
}
Output:
Experiment-2
Source Code:
int sum = 0;
if (i % 2 == 0) {
sum += i;
}
Experiment-3
Source Code:
import java.io.*;
import java.util.Scanner;
dos.writeUTF(name);
dos.writeInt(age);
} catch (IOException e) {
} catch (IOException e) {
fos.write(content.getBytes());
} catch (IOException e) {
int ch;
System.out.print((char) ch);
System.out.println();
} catch (IOException e) {
scanner.close();
}
Experiment-4
Source Code:
System.out.println(part);
}
Output:
Experiment-5
Aim: To write a program to create class and objects and adding methods
Source Code:
this.length = length;
this.width = width;
return length;
return width;
this.length = length;
this.width = width;
}
Output:
//write this new java file
rectangle1.setLength(8.2);
rectangle1.setWidth(4.6);
area1 = rectangle1.calculateArea();
perimeter1 = rectangle1.calculatePerimeter();
}
Experiment-6
Source Code:
public Rectangle() {
length = 0.0;
width = 0.0;
this.length = length;
this.width = width;
return length;
return width;
this.length = length;
this.width = width;
}
Output:
public class ConstructorOverloadingProgram {
rectangle1.setLength(8.2);
rectangle1.setWidth(4.6);
area1 = rectangle1.calculateArea();
}
Experiment-7
Source Code:
import java.io.*;
if (args.length != 2) {
return;
try {
System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);
writer.write("Sum of " + num1 + " and " + num2 + " is: " + sum);
reader.lines().forEach(System.out::println);
} catch (NumberFormatException e) {
Output:
System.out.println("Error: Please enter valid integers.");
} catch (IOException e) {
}
Output:
Experiment-8
Source Code:
printNumber(5);
printNumber(2.5);
printNumber("Ten");
printNumber(3, 4);
}
Output:
Experiment-9
Source Code:
class Vehicle {
protected String brand;
car.displayInfo();
car.honk();
}
}
Output:
Experiment-10
Source Code:
class Animal {
@Override
dog.makeSound();
}
Output:
Experiment-11
Source Code:
mypackage/
MathOperations.java
Main.java
package mypackage;
return a + b;
return a - b;
import mypackage.MathOperations;
}
Output:
Experiment-12
Source Code:
interface Animal {
void makeSound();}
@Override
@Override
dog.makeSound();
cat.makeSound();
}
Experiment-13
Source Code:
import java.util.*;
searchStudentByPin(students, searchPin);
tasks.add("Task4"); // Insert
tasks.remove("Task2"); // Delete
// 3. Searching in a Hashtable
hashTable.put(1, "Apple");
hashTable.put(2, "Banana");
hashTable.put(3, "Cherry");
int searchKey = 2;
System.out.println(entry.getValue());
if (s.pin == pin) {
return;
class Student {
int pin;
String name;
Output:
double percentage;
this.pin = pin;
this.name = name;
this.percentage = percentage;
return "PIN: " + pin + ", Name: " + name + ", Percentage: " + percentage;
class Employee {
int id;
String name;
double salary;
this.id = id;
this.name = name;
this.salary = salary;
return "ID: " + id + ", Name: " + name + ", Salary: " + salary;
}
Experiment-14.
Source Code:
import java.util.Scanner;
// 1. Try-Catch-Finally Example
try {
} catch (Exception e) {
} finally {
try {
}
Output:
int result = numbers[index] / 1; // Changed from `/ 0` to prevent immediate exception
} catch (ArrayIndexOutOfBoundsException e) {
} catch (ArithmeticException e) {
} catch (Exception e) {
try {
int a = scanner.nextInt();
int b = scanner.nextInt();
try {
int division = a / b;
} catch (ArithmeticException e) {
} catch (Exception e) {
} finally {
scanner.close();
}
Experiment-15
Source Code:
try {
Thread.sleep(500);
} catch (InterruptedException e) {
super(name);
}
class SharedResource {
try {
System.out.println("Producing Data...");
available = true;
} catch (Exception e) {
try {
while (!available) {
System.out.println("Consuming Data...");
available = false;
} catch (InterruptedException e) {
t2.start();
t3.start();
// 3. Priority Example
p1.setPriority(Thread.MIN_PRIORITY);
p2.setPriority(Thread.MAX_PRIORITY);
p1.start();
p2.start();
// 4. Inter-Thread Communication
producer.start();
consumer.start();
}
Experiment-16
Source Code:
import java.awt.*;
import javax.swing.*;
super.paintComponent(g);
setBackground(Color.WHITE);
// Draw a rectangle
g.setColor(Color.RED);
// Draw an oval
g.setColor(Color.BLUE);
// Draw a line
g.setColor(Color.GREEN);
// Draw a circle
g.setColor(Color.ORANGE);
}
public static void main(String[] args) {
frame.add(panel);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
Experiment-17
Source Code:
import java.awt.*;
import java.awt.event.*;
TextField textField;
Button button;
Checkbox checkbox;
List list;
AWTControlsDemo() {
setSize(500, 400);
setLayout(null);
// Mouse Label
add(mouseLabel);
addMouseListener(this);
// Key Label
add(keyLabel);
// Text Field
textField = new TextField();
textField.addKeyListener(this);
add(textField);
// Button
button.addActionListener(this);
add(button);
// Text Label
add(textLabel);
// Checkbox
checkbox.addItemListener(this);
add(checkbox);
add(checkboxLabel);
// List
list.add("A");
list.add("B");
list.add("C");
list.addItemListener(this);
add(list);
add(listLabel);
setVisible(true);
@Override
mouseLabel.setText("Mouse Clicked!");
@Override
@Override
@Override
@Override
@Override
@Override
@Override
@Override
textLabel.setText("Button clicked!");
@Override
if (e.getSource() == checkbox) {
if (e.getSource() == list) {
new AWTControlsDemo();