Ap Lab
Ap Lab
Code :-
import java.util.Scanner;
Output :-
Experiment 2 :- Control Structure – Write program to demonstrate the use of if
, else , switch , for , while and do-while loops .
Code :-
import java.util.Scanner;
//if - else
int marks;
Scanner sc = new Scanner(System.in);
System.out.println("enter marks");
marks = sc.nextInt();
Output :-
Code : -
//Switch case
int day ;
System.out.println("enter the day number");
day = sc.nextInt();
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
break;
}
Output :-
Code :-
//for loop
for(int i = 0 ; i < 6; i++){
for(int j = 5-i ; j < 6 ; j++){
System.out.print("* ");
}
System.out.println();
}
Output :-
Code :-
//while loop
int i = 1;
while(i < 10){
System.out.println(i);
i++;
}
Output : -
Code :-
//do while loop
int k = 1;
do{
System.out.println(k);
k++;
}while(k < 8);
Output : -
Experiment 3 :- Classes and Objects – Create a class Car with attributes like
model , year and price. Instantiate objects of this class.
Code :-
class car{
String model;
int year;
int price;
}
Output :-
Experiment 4 :- Inheritance – Implement a class hierarchy with base class
animal and derived classes Dog and Cat.
Code :-
class animal{
String color;
int height;
int age ;
}
c.age = 7;
c.color = "black";
c.height = 2;
Output : -
Experiment 5 :- Polymorphism – Demonstrate the method overriding and
overloading with class shape and derived classes Circle , Square and Triangle.
Code :-
class shape {
public void sides() {
System.out.println("shape is not defined");
}
// method overridding
System.out.println("Method Over Ridding");
s.sides();;
c.sides();
sq.sides();
t.sides();
System.out.println();
Output : -
Experiment 6:- Encapsulation – Create a class student with private attributes
and public getter and setter methods.
Code :-
class student{
private int age;
private String name;
private int rollno ;
//getters
public String getName(){
return name;
}
Code :-
interface Operation {
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
}
Output : -
Experiment 8 :- Linked List – Create a simple linked list implementation and
perform basic operations like insertion , deletion and traversal.
Code : -
class Node {
int data;
Node next;
class LinkedList {
Node head;
public LinkedList() {
head = null;
}
list.insert(10);
list.insert(20);
list.insert(30);
list.insert(40);
System.out.println("list after inserting 4 nodes");
list.traverse();
list.deleteNode();
Output :-
Experiment 9 :- Stack – Implement a stack using an array or a linked list and
demonstrate push , pop and peek operations.
Code :-
//implementation of stack using array
class StackArray {
private int maxSize;
private int top;
private int[] stackArray;
class StackLinkedList {
private Node top;
public StackLinkedList() {
top = null;
}
System.out.println();
Output :-
Experiment 10 :- Try-Catch-Finally - Write a program to demonstrate the use of
try-catch-finally blocks for exception handling.
Code:-
class ExceptionHandlingDemo{
}
}
Output :-
Experiment 11 :- Thread Creation - Create and start multiple threads using
Thread class and Runnable interface.
Code :-
// thread creation using Thread class
class MyThread extends Thread {
private String threadName;
MyThread(String name) {
this.threadName = name;
}
MyRunnable(String name) {
this.threadName = name;
}
thread1.start();
thread2.start();
thread3.start();
t1.start();
t2.start();
t3.start();
}
}
Output :-
Experiment 12:- Thread Synchronization - Demonstrate thread synchronization
using the synchronized keyword and wait/notify methods.
Code:-
class NumberPrinter {
private int count = 1;
private final int max;
evenThread.start();
oddThread.start();
}
}
Output : -
Experiment 13:- JDBC - Connect to a database using JDBC, execute SQL queries,
and retrieve results.
Code:-
import java.sql.*;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.err.println("Driver not found: " + e.getMessage());
}
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String email = rs.getString("email");
} catch (SQLException e) {
System.err.println("SQL Exception: " + e.getMessage());
}
}
}
Output :-
Experiment 14 :- Design Patterns: Implement common design patterns like
Singleton, Factory, and Observer.
Code:-
import java.util.*;
// Singleton Pattern
class Singleton {
private static Singleton instance;
public int value;
private Singleton() {
value = 0;
}
// Factory Pattern
interface Vehicle {
void drive();
}
interface Observer {
void update(int state);
}
subject.registerObserver(observer1);
subject.registerObserver(observer2);
subject.setState(10);
subject.unregisterObserver(observer1);
subject.setState(20);
}
}
Output:-
Experiment 15 :- Producer-Consumer Problem - Implement solution of
Producer-Consumer Problem.
Code:-
import java.util.LinkedList;
import java.util.Queue;
class Buffer {
private Queue<Integer> queue = new LinkedList<>();
private int capacity;
queue.add(value);
System.out.println("Produced: " + value);
notifyAll();
}
// Producer Thread
class Producer extends Thread {
private Buffer buffer;
// Consumer Thread
class Consumer extends Thread {
private Buffer buffer;
producer.start();
consumer.start();
}
}
Output :-
Experiment 16 : - File Reading and Writing - Write a program to read data from
a file and write data to a file.
Code: -
import java.io.*;
Output: -