APLAB
APLAB
1.)
3.)
public class Main
{
public static void main(String[] args) {
A ob = new A(65f, 74f, 70f);
System.out.println("Student 1 Per: " +ob.getPercentage());
B obj = new B(50f , 79f , 80f , 71f);
System.out.println("Student 2 Per: "
Pranjal Tri pathi ( 201B188)
+obj.getPercentage());
}
}
abstract class Mark{
abstract float getPercentage();
}
class A extends Mark{
float sub1 , sub2 , sub3;
float per=0;
A(float a , float b, float c){
sub1 = a;
sub2 = b;
sub3 = c;
}
public float getPercentage(){
per = (sub1 + sub2 + sub3 )/300* 100;
return per;
}
}
class B extends Mark{
float sub1 , sub2 , sub3 , sub4;
float per=0;
B(float a , float b, float c,float d){
sub1 = a ;
sub2 = b;
sub3 = c;
sub4 = d;
}
public float getPercentage(){
per = (sub1 + sub2 + sub3 + sub4 )/400* 100;
return per;
}
}
5.)
public class Main{
public static void main(String[] args) {
Cats c = new Cats();
c.cats();
Dogs d = new Dogs();
d.dogs();
}
}
abstract class Animals{ Pranjal Tri pathi ( 201B188)
6.)
public class Main{
public static void main(String[] args) {
Area a = new Area();
a.RectangleArea(6.7f, 4f);
a.SquareArea(8f);
a.CircleArea(10);
}
}
abstract class Shape{
abstract void RectangleArea(float length , float breadth);
abstract void SquareArea(float radius);
abstract void CircleArea(float side);
}
class Area extends Shape{
double Area=0;
void RectangleArea(float length , float breadth){
Area = length * breadth;
System.out.println("Area of rectangle is: "+Area);
}
void SquareArea(float Side){
Area = Side * Side;
System.out.println("Area of Square is: "+Area);
}
void CircleArea(float radius){
Area = (radius * radius)*3.14;
System.out.println("Area of Circle is: "+Area);
Pranjal Tri pathi ( 201B188)
}
}
7.)
public class Main{
public static void main(String[] args) {
Area rect[] = new Area[4];
Area sq[] = new Area[4];
Area cir[] = new Area[5];
for(int i=0;i<=3;i++) {
rect[i]=new Area();
cir[i]=new Area();
sq[i]=new Area();
}
cir[4]=new Area();
for(int i=0;i<=3;i++) {
rect[i].rectangleArea(4.5f,2.3f);
sq[i].squareArea(3.4f);
cir[i].circleArea(3.1f);
}
cir[4].circleArea(2.1f);
System.out.println("Rectangle");
for(int i=0;i<4;i++)
System.out.println(rect[i].area);
System.out.println("Square");
for(int i=0;i<4;i++)
System.out.println(sq[i].area);
System.out.println("Circle");
for(int i=0;i<4;i++)
System.out.println(cir[i].area);
System.out.println(cir[4].area);
}
}
abstract class Shape{
abstract void rectangleArea(float length , float breadth);
abstract void squareArea(float radius);
abstract void circleArea(float side);
}
class Area extends Shape {
float area;
Area() {
area=0.0f;
}
void rectangleArea(float length , float breadth){
this.area = length * breadth;
} Pranjal Tri pathi ( 201B188)
LAB-8
1.)
class Node
{
double time;
String eventType;
Node next;
Node() {
time=0.0;
eventType="";
next=null;
}
}
public class Main {
void list(Node root,String s) {
root.time=Math.random();
root.eventType=s;
Node temp=root;
for(int i=1;i<10;i++) {
Node temp2=new Node();
temp2.time=Math.random();
temp2.eventType=s;
temp.next=temp2;
temp=temp.next;
}
}
void print(Node root) {
Node temp3=root;
double time=0.0;
for(int i=0;i<20;i++) {
time+=temp3.time;
System.out.println("Event "+temp3.eventType+" occurs at "+time+" time");
} Pranjal Tri pathi ( 201B188)
}
Node merge(Node head1, Node head2) {
Node mergedList=new Node();
if(head1 == null) {
return head2;
}
if(head2 == null) {
return head1;
}
if(head1.time < head2.time) {
mergedList = head1;
mergedList.next = merge(head1.next, head2);
}
else {
mergedList = head2;
mergedList.next = merge(head1, head2.next);
}
return mergedList;
}
public static void main(String args[]) {
Main ob=new Main();
Node root1=new Node();
Node root2=new Node();
Node root3=new Node();
Node root4=new Node();
ob.list(root1,"E1");
ob.list(root2,"E2");
ob.list(root3,"E3");
ob.list(root4,"E4");
Node root=new Node();
root=ob.merge(root1,root2);
ob.print(root);
}
}
LAB-9
1.)
A)
class University {
booleanstrangerThings = false;
void exam(booleanstrangerThings ) throws
Pranjal Tri pathi ( 201B188)
ExamSuspendException {
if (strangerThings) {
throw new ExamSuspendException();
}
}
}
class JUET extends University {
void t1() {
try {
exam(true);
} catch (ExamSuspendException e) {
e.printStackTrace();
}
}
}
class ExamSuspendException extends
Exception {
@Override
public void printStackTrace() {
System.out.println("Exam Suspended");
}
}
public class Firsta {
public static void main(String[] args) {
JUET juet = new JUET();
juet.t1();
}
}
B)
class University {
booleanstrangerThings = false;
void exam(booleanstrangerThings ) throws
ExamSuspendException {
if (strangerThings) {
throw new ExamSuspendException();
}
}
}
class JUET extends University {
void t1() throws ExamSuspendException { Pranjal Tri pathi ( 201B188)
exam(true);
}
}
class ExamSuspendException extends
Exception {
@Override
public void printStackTrace() {
System.out.println("Exam Suspended");
}
}
public class Firstb {
public static void main(String[] args) {
JUET juet = new JUET();
juet.t1();
}
}
C)
class University {
booleanstrangerThings = false;
void exam(booleanstrangerThings ) throws
ExamSuspendException {
if (strangerThings) {
throw new ExamSuspendException();
}
}
}
class JUET extends University {
void t1() throws ExamSuspendException {
exam(true);
}
}
class ExamSuspendException extends
Exception {
@Override
public void printStackTrace() {
System.out.println("Exam Suspended");
}
}
public class Firstc {
public static void main(String[] args) {
JUET juet = new JUET();
try { Pranjal Tri pathi ( 201B188)
juet.t1();
} catch (ExamSuspendException e) {
e.printStackTrace();
}
}
}
D)
class University {
booleanstrangerThings = false;
void exam(booleanstrangerThings ) throws
ExamSuspendException {
if (strangerThings) {
throw new ExamSuspendException();
}
}
}
class JUET extends University {
void t1() throws ExamSuspendException {
exam(true);
}
}
class ExamSuspendException extends
Exception {
@Override
public void printStackTrace() {
System.out.println("Exam Suspended");
}
}
public class Firstd {
public static void main(String[] args) throws
ExamSuspendException {
JUET juet = new JUET();
juet.t1();
}
}
2.)
A)
import java.util.Scanner;
class ClothException extends Exception {
@Override
public void printStackTrace() { Pranjal Tri pathi ( 201B188)
System.out.println("Cloth Exception");
}
}
class PantException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Pant Exception");
}
}
class ShirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Shirt Exception");
}
}
class TshirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Tshirt Exception");
}
}
class LaundryTesta {
void unfit(String type) throws
TshirtException, ShirtException,
PantException, ClothException {
if (type.equals("Tshirt")) {
throw new TshirtException();
} else if (type.equals("Shirt")) {
throw new ShirtException();
} else if (type.equals("Pant")) {
throw new PantException();
} else if (type.equals("Cloth")) {
throw new ClothException();
}
}
public static void main(String[] args) {
LaundryTestalaundryTest = new
LaundryTesta();
System.out.println("Enter the type of cloth
as Tshirt, Shirt, Pant or Cloth");
try {
laundryTest.unfit(new Pranjal Tri pathi ( 201B188)
Scanner(System.in).nextLine());
} catch (TshirtException e) {
e.printStackTrace();
} catch (ShirtException e) {
e.printStackTrace();
} catch (PantException e) {
e.printStackTrace();
} catch (ClothException e) {
e.printStackTrace();
} finally {
System.out.println("very dummy");
}
System.out.println("Last line of main");
}
}
B)
import java.util.Scanner;
class ClothException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Cloth Exception");
}
}
class PantException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Pant Exception");
}
}
class ShirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Shirt Exception");
}
}
class TshirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Tshirt Exception");
}
}
class LaundryTestb { Pranjal Tri pathi ( 201B188)
System.out.println("Pant Exception");
}
}
class ShirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Shirt Exception");
}
}
class TshirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Tshirt Exception");
}
}
class LaundryTestc {
void unfit(String type) throws
TshirtException, ShirtException,
PantException, ClothException {
if (type.equals("Tshirt")) {
throw new TshirtException();
} else if (type.equals("Shirt")) {
throw new ShirtException();
} else if (type.equals("Pant")) {
throw new PantException();
} else if (type.equals("Cloth")) {
throw new ClothException();
}
}
public static void main(String[] args) {
LaundryTestclaundryTest = new
LaundryTestc();
System.out.println("Enter the type of cloth
as Tshirt, Shirt, Pant or Cloth");
try {
laundryTest.unfit(new
Scanner(System.in).nextLine());
} catch (ClothException e) {
e.printStackTrace();
} catch (TshirtException e) {
e.printStackTrace();
} catch (ShirtException e) { Pranjal Tri pathi ( 201B188)
e.printStackTrace();
} catch (PantException e) {
e.printStackTrace();
} finally {
System.out.println("very dummy");
}
System.out.println("Last line of main");
}
LAB-10
1.)
class table{
void table1(int n) {
for(int i = 1;i<=10;i++) {
System.out.println(n + "*" + i +"="+ n*i);
}
}
}
class A implements Runnable{
public void run() {
table t = new table();
t.table1(2);
}
}
class B implements Runnable{
public void run() {
table t1 = new table();
t1.table1(3);
}
}
public class Multithread {
public static void main(String[] args) {
Thread t3 = new Thread(new A());
Thread t4 = new Thread(new B());
t3.start();
t4.start();
}
}
2.)
class table{
void table1(int n) { Pranjal Tri pathi ( 201B188)
for(int i = 1;i<=10;i++) {
System.out.println(n + "*" + i +"="+ n*i);
}
}
}
class A implements Runnable{
public void run() {
table t = new table();
t.table1(2);
sleep(2000);
}
}
class B implements Runnable{
public void run() {
synchronized(this) {
table t1 = new table();
t1.table1(3);
}}
}
public class Multithread {
public static void main(String[] args) {
Thread t3 = new Thread(new A());
Thread t4 = new Thread(new B());
t3.start();
t4.start();
}
}
3.)
class table{
void table1(int n) {
for(int i = 1;i<=10;i++) {
System.out.println(n + "*" + i +"="+ n*i);
}
}
}
class A implements Runnable{
public void run() {
synchronized(this) {
table t = new table() ;
t.table1(2) ;
}
} Pranjal Tri pathi ( 201B188)
}
class B implements Runnable
{
public void run()
{
synchronized(this)
{
table t1 = new table() ;
t1.table1(3) ;
}
}
}
public class Multithread
{
public static void main(String[] args)
{
Thread t3 = new Thread(new A()) ;
Thread t4 = new Thread(new B()) ;
t3.start() ;
t4.start() ;
}
}
LAB-11
1.)
import java.util.Random;
class Square extends Thread
{
int x;
Square(int n)
{
x = n;
}
public void run()
{
int sqr = x * x;
System.out.println("Square of " + x + " = " + sqr );
}
}
class Cube extends Thread
{ Pranjal Tri pathi ( 201B188)
int x;
Cube(int n);
{
x = n;
}
public void run()
{
int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub );
}
}
class Number extends Thread
{
public void run()
{
201B358
Random random = new Random();
for(int i =0; i<5; i++)
{
int randomInteger = random.nextInt(100);
System.out.println("Random Integer generated : " + randomInteger);
Square s = new Square(randomInteger);
s.start()
Cube c = new Cube(randomInteger);
c.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
}
public class Thr {
public static void main(String args[])
{
Pranjal Tri pathi ( 201B188)
2.)
import java.util.Arrays;
import java.util.Collections;
public class GFG {
static void twoWaySort(Integer arr[], int n)
{
int l = 0, r = n - 1;
int k = 0;
while (l < r)
{
while (arr[l] % 2 != 0)
{
l++;
k++;
}
201B358
while (arr[r] % 2 == 0 && l < r)
r--;
if (l < r)
{
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
}
}
Arrays.sort(arr, 0, k, Collections.reverseOrder());
Arrays.sort(arr, k, n);
}
public static void main(String[] args)
{
Integer arr[] = { 1, 3, 2, 7, 5, 4 };
Pranjal Tri pathi ( 201B188)
twoWaySort(arr, arr.length);
System.out.println(Arrays.toString(arr));
}
}
3.)
import java.util.*;
class StacKDemo
{
static void push(Stack st, int a)
{
st.push(new Integer(a));
System.out.println("Element "+a+" push to Stack");
System.out.println("Stack is: " + st);
}
static void pop(Stack st)
{
Integer a = (Integer) st.pop();
System.out.println("Element "+a+" pop to the stack");
System.out.println("Stack is: " + st);
}
public static void main(String[] args)
{
try
{
Stack stack = new Stack();
201B358
System.out.println("Stack: "+stack);
push(stack, 12);
push(stack, 15);
push(stack, 32);
push(stack, 54);
pop(stack);
pop(stack);
pop(stack);
pop(stack);
Pranjal Tri pathi ( 201B188)
pop(stack);
}
catch (EmptyStackException ex)
{
System.out.println("Stack is empty");
}
}
}
LAB-12
1.)
public class Set<T> {
T[] obj;
int count = 0;
{
obj = (T[]) new Object[100];
}
void insert(T ele) {
obj[count] = ele;
count++;
}
void delete(T ele) {
for (int i = 0; i< count; i++) {
if (this.obj[i].equals(ele)) {
int index = i;
for (int j = 0, k = 0; j <obj.length; j++) {
if (j == index) {
continue;
}
obj[k++] = obj[j];
}
break;
}
}
}
void print() {
Pranjal Tri pathi ( 201B188)
2.)
public class Set<T> {
private T[] set;
private int size;
public Set(int size) {
this.size = size;
set = (T[]) new Object[size];
}
public void insert(T value) throws SetOverflow {
if (size == set.length) {
throw new SetOverflow("Set is full");
}
set[size++] = value;
}
public T[] getSet() {
return set;
}
Pranjal Tri pathi ( 201B188)
3.)
public class Set<T> {
private T[] arr;
private int size;
s2.insert(1);
s2.insert(2);
s2.insert(3);
System.out.println(s1.equals(s2));
s2.insert(4);
ArrayList<Integer> l = new ArrayList<Integer>();
System.out.println(s1.equals(l));
s2 = new Set<Integer>(2);
s2.insert(1);
s2.insert(2);
System.out.println(s1.equals(s2));
}
}
4.)
public class Set<T extends Comparable> {
private T[] arr;
private int size;
public Set(int size) {
arr = (T[]) new Comparable[size];
this.size = 0;
}
public booleaninsert(T x) {
if (size == arr.length) {
return false;
} else {
for (int i = 0; i< size; i++) {
if (arr[i].compareTo(x) == 0) {
return false;
}
}
int i = 0;
while (i< size &&arr[i].compareTo(x) < 0) {
i++;
}
for (int j = size; j >i; j--) {
Pranjal Tri pathi ( 201B188)
s2.insert(4);
s2.insert(3);
s2.insert(2);
s2.insert(6);
s2.insert(5);
System.out.println(s1.equals(s2));
}
}
5.)
import java.util.Arrays;
LAB-13
1.)
import java.util.Scanner;
class Account{
int balance;
booleancheckBalance(int withAmt){
if(balance >= withAmt)
return true;
else return false;
}
void withdraw(int withAmt){
balance-= withAmt;
System.out.println("Your current balance is "+ balance);
}
}
class Customer implements Runnable{
Account acc;
String name;
public Customer(Account acc, String name){
this.acc = acc;
this.name = name;
}
public void run(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the amount to be withdrawn by
"+this.name);
int amt = sc.nextInt();
synchronized(acc){
if(acc.checkBalance(amt)){
System.out.println("Sucessfulwithdrawl by
"+this.name);
acc.withdraw(amt);
}
else
System.out.println("Insuffcient Balance for "
+this.name);
}
}
}
class SynExample{
Pranjal Tri pathi ( 201B188)
}
}
Threading 1:
class TablePrinter { Threading 2:
void printTable(int nu) { class TablePrinter {
synchronized(this){ void printTable(int nu) {
for (int i = 1; i<= 10; i++) for (int i = 1; i<= 10; i++)
System.out.println(nu + " x "
+ i + " = " + nu * i);
}
}
class A implements
System.out.println(nu + " x " + i + " = " + nu * i);
}}
}
class A implements Runnable {
TablePrinter t = new TablePrinter();
public void run() {
t.printTable(2);
}
}
class B implements Runnable {
TablePrinter t = new TablePrinter();
public void run() {
t.printTable(3);
}
}
class ThreadSyn1{
public static void main(String[] args) {
// System.out.println("Hello world!");
Thread t1 = new Thread(new A());
Pranjal Tri pathi ( 201B188)