[go: up one dir, main page]

0% found this document useful (0 votes)
37 views6 pages

22K-4818 - Lab 1

The document contains 6 Java programming questions that involve arrays and ArrayLists. Question 1 asks the user to input 5 numbers and print them. Question 2 defines a method to rotate an array by a given number of steps. Question 3 creates and prints two 2D arrays and their sum. Question 4 prints a jagged array and calculates the total sum. Question 5 demonstrates adding and removing elements from an ArrayList. Question 6 prompts the user to input names, replaces a matching name with "orange", and prints the updated ArrayList.

Uploaded by

k224818
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)
37 views6 pages

22K-4818 - Lab 1

The document contains 6 Java programming questions that involve arrays and ArrayLists. Question 1 asks the user to input 5 numbers and print them. Question 2 defines a method to rotate an array by a given number of steps. Question 3 creates and prints two 2D arrays and their sum. Question 4 prints a jagged array and calculates the total sum. Question 5 demonstrates adding and removing elements from an ArrayList. Question 6 prompts the user to input names, replaces a matching name with "orange", and prints the updated ArrayList.

Uploaded by

k224818
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/ 6

DS –LAB 1

22K-4818

Q1.
import java.util.Scanner;

public class task1 {


public static void main(String[] args) {
int arr[] = new int[5];
System.out.println("Input 5 Numbers");
for (int i = 0; i < 5; i++) {
Scanner inp = new Scanner(System.in);
arr[i] = inp.nextInt();
}
System.out.println("Output:");
for (int j = 0; j < 5; j++) {
System.out.println(arr[j]);
}
}
}

Q2.
public class task2 {
public static void rotate(int arr[]) {
int steps = 2; // Number of steps to rotate
int length = arr.length;

steps = steps % length; // Handle cases where steps > length


int count = 0;
for (int start = 0; count < length; start++) {
int current = start;
int prevValue = arr[start];

do {
int next = (current + steps) % length;
int temp = arr[next];
arr[next] = prevValue;
prevValue = temp;
current = next;
count++;
} while (start != current);
}
}

public static void main(String[] args) {


int arr[] = {1, 2, 3, 4, 5};

System.out.print("Original Array: ");


for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();

rotate(arr);

System.out.print("Rotated Array: ");


for (int num : arr) {
System.out.print(num + " ");
}
}
}

Q3.
public class task3 {
public static void main(String[] args) {
int arr1[][] = new int[2][2];
arr1[0][0] = 1 ;
arr1[0][1] = 2 ;
arr1[1][0] = 3;
arr1[1][1] = 4;

System.out.println("Array 1: ");
for (int i = 0; i < 2 ; i++) {
for (int j = 0; j < 2 ; j++) {
System.out.print(arr1[i][j]);
}
System.out.println();
}

int arr2[][] = new int[2][2];


arr2[0][0] = 1 ;
arr2[0][1] = 2 ;
arr2[1][0] = 3;
arr2[1][1] = 4;

System.out.println("Array 2: ");
for (int i = 0; i < 2 ; i++) {
for (int j = 0; j < 2 ; j++) {
System.out.print(arr2[i][j]);
}
System.out.println();
}

System.out.println("Array 1 + Array 2: ");

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


for (int j = 0; j < 2 ; j++) {
System.out.print(arr1[i][j] + arr2[i][j]);
}
System.out.println();
}
}
}

Q4.
public class task4 {
public static void main(String[] args) {
int[][] jaggedArray = {
{1, 2, 3},
{4, 5},
{6, 7, 8, 9}
};

System.out.println("Jagged Array:");
for (int i = 0; i < jaggedArray.length; i++) {
for (int j = 0; j < jaggedArray[i].length; j++) {
System.out.print(jaggedArray[i][j] + " ");
}
System.out.println();
}

int totalSum = 0;
for (int i = 0; i < jaggedArray.length; i++) {
for (int j = 0; j < jaggedArray[i].length; j++) {
totalSum += jaggedArray[i][j];
}
}

System.out.println("Sum of all indexes: " + totalSum);


}
}

Q5.
import java.util.ArrayList;

public class task5 {


public static void main(String[] args) {
ArrayList<Integer> dynamicArray = new ArrayList<>();

dynamicArray.add(10);
dynamicArray.add(11);
dynamicArray.add(14);
dynamicArray.add(16);

System.out.println("Original Array: " + dynamicArray);

int newEl = 20;


dynamicArray.add(newEl);

System.out.println("Array after adding an element: " + dynamicArray);


dynamicArray.remove(dynamicArray.size() - 1);
dynamicArray.remove(dynamicArray.size() - 1);

System.out.println("Array after removing two elements: " +


dynamicArray);
}
}

Q6.
import java.util.ArrayList;
import java.util.Scanner;

public class task6 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

ArrayList<String> fruits = new ArrayList<>();

for (int i = 0; i < 6; i++) {


System.out.print("Enter a name of a fruit or a person: ");
String input = scanner.nextLine();
fruits.add(input);
}

System.out.println("Original Array: " + fruits);

System.out.print("Enter a name of a person: ");


String personName = scanner.nextLine();

for (int i = 0; i < fruits.size(); i++) {


if (fruits.get(i).equals(personName)) {
fruits.set(i, "orange");
}
}

System.out.println("Updated Array: " + fruits);


scanner.close();
}
}

You might also like