[go: up one dir, main page]

0% found this document useful (0 votes)
25 views16 pages

Mianfardanrazzaque 2380258 R 2

Uploaded by

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

Mianfardanrazzaque 2380258 R 2

Uploaded by

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

POST REFLECTION 02

Question 01:

public class DynamicArray {


private int[] data;
private int size;

public DynamicArray() {
data = new int[10];
size = 0;
}
public void add(int value) {
if (size == data.length) {
int[] newData = new int[data.length * 2];
System.arraycopy(data, 0, newData, 0, size);
data = newData;
}
data[size++] = value;
}

public int get(int index) {


if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
return data[index];
}

public void remove(int index) {


if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
System.arraycopy(data, index + 1, data, index, size - index - 1);
size--;
}

public int size() {


return size;
}

public void display() {


for (int i = 0; i < size; i++) {
System.out.print(data[i] + " ");
}
System.out.println();
}

public static void main(String[] args) {


DynamicArray array = new DynamicArray();
array.add(1); array.add(2); array.add(3); array.add(4);
array.display();
System.out.println("Element at index 2: " + array.get(2));
array.remove(1);
array.display();
System.out.println("Current size: " + array.size());
}
}

Output:

Question 02:

import java.lang.reflect.Array;
import java.util.Arrays;

public class Main {


public static int[] insertIntoArray(int[] arr,int element, int index){
int[] newArr = new int[arr.length +1];
int j =0;
for (int i = 0; i < newArr.length; i++) {
if (i == index){
newArr[j] = element;
}
else {
newArr[i] = arr[j];
j++;
}
}

return newArr;
}
public static void main(String[] args) {
int[] arr1 = {1,2,3,4,5};
System.out.println("Orignal Array: "+Arrays.toString(arr1));

arr1 = insertIntoArray(arr1,9,3);
System.out.println("After Insertaion: "+Arrays.toString(arr1));

}
}

Output:
Question 03:

import java.lang.reflect.Array;
import java.util.Arrays;

public class Main {


public static int[] removeIntoArray(int[] arr, int index){
int[] newArr = new int[arr.length - 1];
int j = 0;

for (int i = 0; i < arr.length; i++) {


if (i != index) {
newArr[j] = arr[i];
j++;
}
}
return newArr;
}
public static void main(String[] args) {
int[] arr1 = {1,2,3,4,5};
System.out.println("Orignal Array: "+Arrays.toString(arr1));

arr1 = removeIntoArray(arr1,3);
System.out.println("After Removal: "+Arrays.toString(arr1));
}
}

Output:
Question 04:
import javax.lang.model.element.Element;
import java.lang.reflect.Array;
import java.util.Arrays;

public class Main {


public static void searchingInArray(int[] arr, int Element){
int n = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == Element){
System.out.println("Element: "+Element+ " is found in array in position " + i+ ".");
n++;
break;
}
}
if (n == 0){
System.out.println("Element not found in Array.");
}

}
public static void main(String[] args) {
int[] arr1 = {1,2,3,4,5};
searchingInArray(arr1, 4);
}
}

Output:
Question 05:
public class Main {
public static int findMaxInArray(int[] arr){
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i]> max){
max = arr[i];
}
}
return max;
}

public static void main(String[] args) {


int[] arr = {-1,-2,-3,4,5};
System.out.println("Maximum number in this array is: " + findMaxInArray(arr));
}
}

Output:
Question 06:
public class Main {
public static int findMinInArray(int[] arr){
int min = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min){
min = arr[i];
}
}
return min;
}

public static void main(String[] args) {


int[] arr = {-1,2,3,4,5};
System.out.println("Minimum Number in this Array is: " + findMinInArray(arr));
}
}

Output:
Question 07:
public class Main {
public static void findCommonNumInArray(int[] arr) {
int n = 0;
boolean rep = false;

for (int i = 0; i < arr.length; i++) {


for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
n++;
System.out.println(n + ") Common Number in this array is: " + arr[i]);
rep = true;
break;
}
}
}

if (!rep) {
System.out.println("No Common Number Found in this Array.");
}
}

public static void main(String[] args) {


int[] arr = {8, -2, -6, -6, 8, 5};
findCommonNumInArray(arr);
}
}

Output:
Question 08:
public class Main {

public static void findProductOfNumInArray(int[] arr, int divisor) {


int no = 0;
System.out.println("Numbers in the array divisible by " + divisor + ":");

for (int i = 0; i < arr.length; i++) {


if (arr[i] % divisor == 0) {
System.out.println((no + 1) + ") " + arr[i]);
no++;
}
}
if (no == 0) {
System.out.println("No numbers in the array are divisible by " + divisor + ".");
}
}
public static void main(String[] args) {
int[] arr = {6, -2, 13, 15, 4, 5, 8};
findProductOfNumInArray(arr,3);
}
}

Output:
Question 09:

public class Main {

public static int findAverageNoOfArray(int[] arr) {


int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
return sum/arr.length;

}
public static void main(String[] args) {
int[] arr = {5, 5};
System.out.println("Average Number of Array is: " +findAverageNoOfArray(arr));
}
}

Output:
Question 10:
import java.util.Arrays;

public class Main{


public static int[] reverseArray(int[] arr){
int start = 0,End = arr.length-1;
while (start < End){
int temp = arr[start];
arr[start] = arr[End];
arr[End] = temp;
start++;
End--;

}
return arr;
}
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
arr = reverseArray(arr);
System.out.println("Reversed Array: "+Arrays.toString(arr));

}
}

Output:
Question 11:
public class Main {

public static void findDistinctNumInArray(int[] arr) {


int n = 0;
boolean foundDistinct = false;

for (int i = 0; i < arr.length; i++) {


boolean isDistinct = true;
for (int j = 0; j < arr.length; j++) {
if (i != j && arr[i] == arr[j]) {
isDistinct = false;
break;
}
}
if (isDistinct) {
n++;
System.out.println(n + ") Distinct Number in this array is: " + arr[i]);
foundDistinct = true;
}
}

if (!foundDistinct) {
System.out.println("No Distinct Number Found In this Array.");
}
}

public static void main(String[] args) {


int[] arr = {5, 2, -3, -3, 4, 5};
findDistinctNumInArray(arr);
}
}

Output:
Question 12:
import java.util.Arrays;

public class Main {


public static int[] replaceNumber(int[] arr,int element, int index){
if (index < 0 && index > arr.length){
System.out.println("Invalid Index. ");
return null;
}
else{
arr[index] = element;
}
return arr;
}
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
arr = replaceNumber(arr,8,2);
System.out.println(Arrays.toString(arr));
}
}

Output:
Question 13:
import javax.lang.model.element.Element;
import java.lang.reflect.Array;
import java.util.Arrays;

public class Main {


public static void findEvenInArray(int[] arr){
int n = 0;
for (int i = 0; i < arr.length; i++) {
if ((arr[i] % 2) == 0){
System.out.println(arr[i]);
n++;

}
}
if (n == 0){
System.out.println("No Even Number found in this array. ");
}

}
public static void main(String[] args) {
int[] arr1 = {1,2,3,4,5,6,7,8,9,10};
findEvenInArray(arr1);
}
}

Output:
Question 14:

public class Main {


public static void findOddInArray(int[] arr){
int n = 0;
for (int i = 0; i < arr.length; i++) {
if ((arr[i] % 2) != 0){
System.out.println(arr[i]);
n++;

}
}
if (n == 0){
System.out.println("No Odd Number found in this array. ");
}

}
public static void main(String[] args) {
int[] arr1 = {1,2,3,4,5,6,8};
findOddInArray(arr1);
}
}

Output:
Question 15:
public class Main {
private static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

public static void findPrimeNumInArr(int[] arr) {


System.out.println("Prime numbers in the array are:");
for (int num : arr) {
if (isPrime(num)) {
System.out.println(num);
}
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
findPrimeNumInArr(arr);
}
}

Output:

You might also like