0% found this document useful (0 votes)
5 views

Data Structure Lab 3

The MyMath class contains static methods to perform mathematical operations like difference, addition, multiplication, etc. recursively. The ArrayInt class implements an integer array with methods to insert, delete, sort and search. The TowerOfHanoi class recursively solves the tower of Hanoi problem by moving disks from the source to destination pole using an auxiliary pole.

Uploaded by

tahaayyad0
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Data Structure Lab 3

The MyMath class contains static methods to perform mathematical operations like difference, addition, multiplication, etc. recursively. The ArrayInt class implements an integer array with methods to insert, delete, sort and search. The TowerOfHanoi class recursively solves the tower of Hanoi problem by moving disks from the source to destination pole using an auxiliary pole.

Uploaded by

tahaayyad0
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

EX1:

public class MyMath {

public static int difference(int a,int b) {


if (b == 0) {
return a;
}
return difference(a,b-1)-1;
}

public static int addition(int a, int b) {


if (b == 0) {
return a;
}
return 1+addition(a,b-1);
}

public static int multiplication(int a, int b) {


if (b == 0) {
return 0;
}
return a+multiplication(a, b-1);
}

public static int factorial(int a) {


if (a==0) {
return 1;
}
return a*factorial(a-1);
}

public static double power(int a, int b) {


if(b==0){
return 1;
}
return a*multiplication(a,b-1);
}

public static int quotient(int a, int b) {


if (a < b) {
return 0;
}
return 1 + quotient(a - b, b);
}

public static boolean prime(int n) {


return prime(n,n-1);
}

private static boolean prime(int n, int d) {


if (d==1) {
return true;
}
if (n%d==0) {
return false;
}
return prime(n, d-1);
}
public static boolean remainder(int a, int b) {
if (a < b) {
return false;
}

return remainder(a-b,b);
}
}

---------------------------------------------------
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Difference: " + MyMath.difference(5, 3));
System.out.println("Addition: " + MyMath.addition(5, 3));
System.out.println("Multiplication: " + MyMath.multiplication(5, 3));
System.out.println("Factorial: " + MyMath.factorial(5));
System.out.println("Power: " + MyMath.power(2, 3));
System.out.println("Quotient: " + MyMath.quotient(10, 3));
System.out.println("Is Prime: " + MyMath.prime(7));
System.out.println("Remainder: " + MyMath.remainder(10, 3));
}

-----------------------------------------

EX2:

public class ArrayInt {


private int[] a;
private int currentNb;
private int size;

public ArrayInt(int size) {


this.size = size;
a = new int[size];
currentNb = 0;
}

public boolean isEmpty(){


return currentNb==0;
}
public boolean isFull(){
return currentNb==a.length;
}
public boolean InsertAtBack(int value){
if (!isFull()) {
a[currentNb] = value;
currentNb++;
return true;
} else {
System.out.println("Error: Array is full");
return false;
}
}
public boolean InsertAtFront(int value) {
if(!isFull()){
for(int i=currentNb;i>0;i--){
a[i]=a[i-1];

}
a[0]=value;
currentNb++;
return true;
} else {
System.out.println("Error: Array is full.");
return false;
}
}
public boolean InsertAtPosition(int position, int value) {
if (!isFull() && position >= 0 && position <= currentNb) {
for (int i = currentNb; i > position; i--) {
a[i] = a[i - 1];
}
a[position] = value;
currentNb++;
return true;
} else {
System.out.println("Error: Invalid position or array is full.");
return false;
}
}
public void Display() {
for (int i = 0; i < currentNb; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}

public void SortAscending() {


if (currentNb <= 1) {
return;
}

for (int i = 0; i < currentNb - 1; i++) {


int minIndex = i;

for (int j = i + 1; j < currentNb; j++) {


if (a[j] < a[minIndex]) {
minIndex = j;
}
}

if (minIndex != i) {
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
}
public void DeleteMultiplesOfThree() {
int newSize = currentNb;
for (int i = 0; i < newSize; i++) {
if (a[i] % 3 == 0) {
for (int j = i; j < newSize - 1; j++) {
a[j] = a[j + 1];
}
newSize--;
i--;
}
}
currentNb = newSize;
}

public void SelectionSort() {


for (int i = 0; i < currentNb - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < currentNb; j++) {
if (a[j] < a[minIndex]) {
minIndex = j;
}
}
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}

public boolean InterpolationSearch(int value) {


return InterpolationSearch(value, 0, currentNb - 1);
}

private boolean InterpolationSearch(int value, int low, int high) {


if (low <= high && value >= a[low] && value <= a[high]) {
int pos = low + ((high - low) / (a[high] - a[low])) * (value - a[low]);

if (a[pos] == value) {
return true;
}

if (a[pos] < value) {


return InterpolationSearch(value, pos + 1, high);
}

if (a[pos] > value) {


return InterpolationSearch(value, low, pos - 1);
}
}
return false;
}

public boolean IntervalBinarySearch(int value) {


return IntervalBinarySearch(0, currentNb - 1, value);
}

private boolean IntervalBinarySearch(int l, int r, int x) {


if (l <= r) {
int mid = l + (r - l) / 2;
if (a[mid] == x) {
return true;
}

if (a[mid] < x) {
return IntervalBinarySearch(mid + 1, r, x);
}

return IntervalBinarySearch(l, mid - 1, x);


}

return false;
}

-------------------------------------------

public static void main(String[] args) {


// TODO code application logic here
ArrayInt arr = new ArrayInt(10);
arr.InsertAtBack(10);
arr.InsertAtBack(20);
arr.InsertAtBack(30);
arr.InsertAtBack(40);
arr.InsertAtBack(50);

System.out.println("Interpolation Search (30): " +


arr.InterpolationSearch(30));
System.out.println("Interval Binary Search (20): " +
arr.IntervalBinarySearch(20));
}

-----------------------------------------

EX3:

public class TowerOfHanoi {

public static void towerOfHanoi(int n, char source, char auxiliary, char


destination) {
if (n == 1) {
System.out.println("Move disk 1 from " + source + " to " +
destination);
}
else{

towerOfHanoi(n - 1, source, destination, auxiliary);

System.out.println("Move disk " + n + " from " + source + " to " +


destination);

towerOfHanoi(n - 1, auxiliary, source, destination);


}}
}

---------------------------------------------
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("please enter the number of discs");
int numberOfDisks=sc.nextInt();
TowerOfHanoi.towerOfHanoi(numberOfDisks, 'A', 'B', 'C');
}

You might also like