0% found this document useful (0 votes)
19 views7 pages

Data Stucture Lab 1

The document describes a GymSubscriberDirectory class that manages an array of GymSubscriber objects. The GymSubscriberDirectory can add subscribers, search by name or ID, and print the subscriber list. It also describes a GymSubscriber class with name, phone, ID, and gender attributes. The main method tests adding two sample subscribers and calling search and print methods.

Uploaded by

tahaayyad0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Data Stucture Lab 1

The document describes a GymSubscriberDirectory class that manages an array of GymSubscriber objects. The GymSubscriberDirectory can add subscribers, search by name or ID, and print the subscriber list. It also describes a GymSubscriber class with name, phone, ID, and gender attributes. The main method tests adding two sample subscribers and calling search and print methods.

Uploaded by

tahaayyad0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

By: Taha Ayad

Question 1:

public class ArrayInt {


private int a[];
private int currentNb;

public ArrayInt(int size){


this.a=new int[size];
this.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 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();
}

public boolean OccurrenceOnlyTwice(int value) {


int count = 0;
for (int i = 0; i < currentNb; i++) {
if (a[i] == value) {
count++;
if (count > 2) {
System.out.println("Didn't occur only twice");
return false;
}
}
}
if (count == 2) {
System.out.println("Occurred only twice");
return true;
} else {
System.out.println("Didn't occur only twice");
return false;
}
}

public void OddNb() {


for (int i = 0; i < currentNb; i++) {
if (a[i] % 2 != 0) {
System.out.print(a[i] + " ");
}
}
System.out.println();
}

public void EvenNb() {


for (int i = 0; i < currentNb; i++) {
if (a[i] % 2 == 0) {
System.out.print(a[i] + " ");
}
}
System.out.println();
}
public int FindSecondMinimum() {
if (currentNb < 2) {
System.out.println("Array has less than 2 elements.");
return -1;
}

int min1 = a[0];


int min2 = a[1];

if (min2 < min1) {


int temp = min1;
min1 = min2;
min2 = temp;
}

for (int i = 2; i < currentNb; i++) {


int current = a[i];
if (current < min1) {
min2 = min1;
min1 = current;
} else if (current < min2 && current != min1) {
min2 = current;
}
}

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

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 DeleteMultiplesOfFive() {


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

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

public static void main(String[] args) {


ArrayInt array = new ArrayInt(10);

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("Even multiples of 3:");


array.MultiplesOfEvenThree();
array.OccurrenceOnlyTwice(2);
array.OccurrenceOnlyTwice(4);

System.out.println("Odd numbers:");
array.OddNb();

System.out.println("Even numbers:");
array.EvenNb();

int secondMin = array.FindSecondMinimum();


if (secondMin != -1) {
System.out.println("Second minimum element: " + secondMin);
}

array.EvenOrOdd(6);
array.EvenOrOdd(7);

System.out.println("Array elements before sorting:");


array.Display();
array.SortAscending();
System.out.println("Array elements after sorting:");
array.Display();

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 class GymSubscriberDirectory {


private static final int MAX_SUBSCRIBERS = 50;
private GymSubscriber[] subscribers;
private int size;

public GymSubscriberDirectory() {
subscribers = new GymSubscriber[MAX_SUBSCRIBERS];
size = 0;
}

public void addSubscriber(GymSubscriber subscriber) {


if (size < MAX_SUBSCRIBERS) {
subscribers[size] = subscriber;
size++;
System.out.println("Subscriber added successfully.");
} else {
System.out.println("Directory is full. Cannot add more subscribers.");
}
}

public void searchByName(String name) {


System.out.println("Search results for name: " + name);
for (int i = 0; i < size; i++) {
if (subscribers[i].getName().equalsIgnoreCase(name)) {
System.out.println(subscribers[i]);
}
}
}

public void searchByIdCard(String idCard) {


System.out.println("Search results for ID Card: " + idCard);
for (int i = 0; i < size; i++) {
if (subscribers[i].getIdCard().equals(idCard)) {
System.out.println(subscribers[i]);
}
}
}

public void printSubscriberList() {


System.out.println("Gym Subscriber List:");
for (int i = 0; i < size; i++) {
System.out.println(subscribers[i]);
}
}
}

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

public class GymSubscriber {


private String name;
private String phoneNumber;
private String idCard;
private String gender;

public GymSubscriber(String name, String phoneNumber, String idCard, String


gender) {
this.name = name;
this.phoneNumber = phoneNumber;
this.idCard = idCard;
this.gender = gender;
}

public String getName() {


return name;
}

public String getIdCard() {


return idCard;
}

@Override
public String toString() {
return "Name: " + name + ", Phone: " + phoneNumber + ", ID Card: " + idCard
+ ", Gender: " + gender;
}
}

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

public class Main {


public static void main(String[] args) {
GymSubscriberDirectory directory = new GymSubscriberDirectory();

GymSubscriber subscriber1 = new GymSubscriber("Ahmad mohsen", "123-456-


7890", "ID12345", "Male");
GymSubscriber subscriber2 = new GymSubscriber("karma milos", "987-654-
3210", "ID67890", "Female");

directory.addSubscriber(subscriber1);
directory.addSubscriber(subscriber2);

directory.printSubscriberList();

directory.searchByName("Ahmad mohsen");
directory.searchByIdCard("ID67890");
}
}

You might also like