Data Stucture Lab 1
Data Stucture Lab 1
Question 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 int SearchValue(int value) {
for (int i = 0; i < currentNb; i++) {
if (a[i] == value) {
return i;
}
}
return -1;
}
public boolean DeleteElement(int value) {
int index = SearchValue(value);
if (index != -1) {
for (int i = index; i < currentNb - 1; i++) {
a[i] = a[i + 1];
}
currentNb--;
return true;
} else {
System.out.println("Error: Element not found.");
return false;
}}
public void MultiplesOfEvenThree() {
for (int i = 0; i < currentNb; i++) {
if (a[i] % 2 == 0 && a[i] % 3 == 0) {
System.out.print(a[i] + " ");
}
}
System.out.println();
}
return min2;
}
public void EvenOrOdd(int value) {
int index = SearchValue(value);
if (index != -1) {
if (a[index] % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
} else {
System.out.println("Value not found in the array.");
}
}
public void SortAscending() {
if (currentNb <= 1) {
return;
}
if (minIndex != i) {
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
}
---------------------------------------------------------------------------------
array.InsertAtBack(2);
array.InsertAtBack(4);
array.InsertAtBack(6);
array.InsertAtBack(8);
array.InsertAtFront(1);
array.InsertAtFront(3);
array.InsertAtPosition(4, 5);
System.out.println("Array elements:");
array.Display();
int searchValue = 5;
int searchResult = array.SearchValue(searchValue);
if (searchResult != -1) {
System.out.println(searchValue + " found at index " + searchResult);
} else {
System.out.println(searchValue + " not found in the array.");
}
array.DeleteElement(3);
System.out.println("Array elements after deleting 3:");
array.Display();
System.out.println("Odd numbers:");
array.OddNb();
System.out.println("Even numbers:");
array.EvenNb();
array.EvenOrOdd(6);
array.EvenOrOdd(7);
array.InsertAtBack(15);
array.InsertAtBack(25);
array.InsertAtBack(35);
array.DeleteMultiplesOfFive();
System.out.println("Array elements after deleting multiples of 5:");
array.Display();
}
Question 2:
public GymSubscriberDirectory() {
subscribers = new GymSubscriber[MAX_SUBSCRIBERS];
size = 0;
}
----------------------------------------------------
@Override
public String toString() {
return "Name: " + name + ", Phone: " + phoneNumber + ", ID Card: " + idCard
+ ", Gender: " + gender;
}
}
------------------------------------------------------------
directory.addSubscriber(subscriber1);
directory.addSubscriber(subscriber2);
directory.printSubscriberList();
directory.searchByName("Ahmad mohsen");
directory.searchByIdCard("ID67890");
}
}