You are on page 1of 8

Lab Exercise – 12

Q1.
Ans:
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() {
for (int i = 0; i < count; i++) {
System.out.println(obj[i]);
}
}
}
public class Main
{
public static void main(String[] args)
{
Set<Integer> st = new Set<Integer>();
st.insert("1");
st.insert("3");
st.insert("2");
st.insert("5");
st.insert("7");
st.insert("24");
st.print();
st.delete("2");
st.print();

42
}
}

Q2:
Ans:
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;
}
public int getSize() {
return size;
}
}
public class SetOverflow extends Exception {
public SetOverflow(String message) {
super(message);
}
}
public class Main {
public static void main(String[] args) {
Set<Integer> s = new Set<Integer>(3);
try {
s.insert(1);
s.insert(2);
s.insert(3);
s.insert(4);
} catch (SetOverflow e) {
System.out.println(e.getMessage());
}
}
}

Q3:
Ans:
public class Set<T>
{

43
private T[] arr;
private int size;

public Set(int size) {


arr = (T[]) new Object[size];
this.size = 0;
}
public boolean insert(T x) {
if (size == arr.length) {
return false;
} else {
for (int i = 0; i < size; i++) {
if (arr[i] == x) {
return false;
}
}
arr[size] = x;
size++;
return true;
}
}
public boolean equals(Object o) {
if (o instanceof Set) {
Set<T> s = (Set<T>) o;
if (s.size == this.size) {
for (int i = 0; i < this.size; i++) {
if (!s.arr[i].equals(this.arr[i])) {
return false;
}
}
return true;
} else {
return false;
}
} else {
return false;
}
}
}
import java.util.ArrayList;
public class Main
{
public static void main(String[] args)
{
Set<Integer> s1 = new Set<Integer>(3);
Set<Integer> s2 = new Set<Integer>(3);
s1.insert(1);
s1.insert(2);
s1.insert(3);
s2.insert(1);

44
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));
}
}

Q4:
Ans:
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 boolean insert(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--) {
arr[j] = arr[j - 1];
}
arr[i] = x;
size++;
return true;
}
}
public boolean equals(Object o) {
if (o instanceof Set) {
Set<T> s = (Set<T>) o;
if (s.size == this.size) {
for (int i = 0; i < this.size; i++) {
if (!s.arr[i].equals(this.arr[i])) {

45
return false;
}
}
return true;
} else {
return false;
}
} else {
return false;
}
}
}
public class Main
{
public static void main(String[] args)
{
Set<Integer> s1 = new Set<Integer>(6);
Set<Integer> s2 = new Set<Integer>(6);
s1.insert(1);
s1.insert(2);
s1.insert(3);
s1.insert(4);
s1.insert(5);
s1.insert(6);
s2.insert(1);
s2.insert(4);
s2.insert(3);
s2.insert(2);
s2.insert(6);
s2.insert(5);
System.out.println(s1.equals(s2));
}
}

Q5:
Ans:
import java.util.Arrays;
public class Main{
public static void main(String[] args) {
Integer[] a = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(a));
swap(a, 0, 4);
System.out.println(Arrays.toString(a));
}
public static <T> void swap(T[] a, int i, int j) {
T temp = a[i];
a[i] = a[j];
a[j] = temp;
}}

46
Lab Exercise – 13

Q1.
Ans:
import java.util.Scanner;
class Account{
int balance;

public Account(int balance){


this.balance = balance;
}

boolean checkBalance(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("Sucessful withdrawl by "+this.name);
acc.withdraw(amt);
}
else
System.out.println("Insuffcient Balance for " +this.name);
}
}
}
class SynExample{
public static void main(String args[]){
Account acc = new Account(1000);
Customer c1 = new Customer(acc, "First");
Customer c2 = new Customer(acc, "Second");
Thread t1 = new Thread(c1);

47
Thread t2 = new Thread(c2);
t1.start();
t2.start();

}
}

Threading 1:
class TablePrinter {
void printTable(int nu) {
synchronized(this){
for (int i = 1; i <= 10; i++)
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());
Thread t2 = new Thread(new B());
t1.start();
t2.start();
}
}

Threading 2:
class TablePrinter {
void printTable(int nu) {
for (int i = 1; i <= 10; i++)
System.out.println(nu + " x " + i + " = " + nu * i);
}
}
class A implements Runnable {
TablePrinter t;
A(TablePrinter ta) {
t = ta;
}

48
public void run() {
//TablePrinter t = new TablePrinter();
synchronized (t) {
t.printTable(2);
}
}
}
class B implements Runnable {
TablePrinter t;
B(TablePrinter ta) {
t = ta;
}
public void run() {
//TablePrinter t = new TablePrinter();
synchronized (t) {
t.printTable(3);
}
}

49

You might also like