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

Assignment No 2 OOP

The document is an assignment submission from Bushra Athar to Sir Nasir Mehdi on object oriented programming. It includes 4 Java programs demonstrating copying arrays, using ArrayLists, and HashMaps. The programs show how to copy elements from one array to another, add/remove elements from an ArrayList, and add/update/remove key-value pairs from a HashMap.

Uploaded by

zoha
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)
32 views

Assignment No 2 OOP

The document is an assignment submission from Bushra Athar to Sir Nasir Mehdi on object oriented programming. It includes 4 Java programs demonstrating copying arrays, using ArrayLists, and HashMaps. The programs show how to copy elements from one array to another, add/remove elements from an ArrayList, and add/update/remove key-value pairs from a HashMap.

Uploaded by

zoha
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/ 5

COMSATS University Islamabad,

Sahiwal Campus

Department of Computer Science


Assignment # 02

Submitted to:
Sir Nasir Mehdi
Submitted by:
Bushra Athar
Registration#:
SP22-BCS-033
Subject:
Object Oriented Programming
Topic:
Array, ArrayList, Hashmaps
Date:
12/4/2023

1
1.Copy Arrays

Program:
package copyarray;

public class MainClass {


public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = new int[array1.length];

// Copying elements from array1 to array2


for (int i = 0; i < array1.length; i++) {
array2[i] = array1[i];
}

// Printing array2
System.out.print("Array2: ");
for (int i = 0; i < array2.length; i++) {
System.out.print(array2[i] + " ");
}
}
}

Output:
Array2: 1 2 3 4 5

2
3.ArrayLists
Program:
package arraylist;

import java.util.ArrayList;

public class MainClass {


public static void main(String[] args) {
// create an ArrayList of integers
ArrayList<Integer> numbers = new ArrayList<Integer>();

// add some numbers to the ArrayList


numbers.add(5);
numbers.add(10);
numbers.add(15);

// print out the contents of the ArrayList


System.out.println("The ArrayList contains: " + numbers);

// get the size of the ArrayList


int size = numbers.size();
System.out.println("The ArrayList has " + size + " elements.");

// access an element of the ArrayList


int first = numbers.get(0);
System.out.println("The first element is " + first);

// remove an element from the ArrayList


numbers.remove(1);

3
System.out.println("After removing the second element, the ArrayList contains: " + numbers);
}
}

Output:
The ArrayList contains: [5, 10, 15]
The ArrayList has 3 elements.
The first element is 5
After removing the second element, the ArrayList contains: [5, 15]

4. Hashmaps
Program:
package hashmaps;

import java.util.HashMap;

public class MainClass {


public static void main(String[] args) {
// Creating a HashMap object
HashMap<String, Integer> hm = new HashMap<String, Integer>();

// Adding key-value pairs to the HashMap


hm.put("Bushra", 19);
hm.put("Faiza", 20);
hm.put("Zoha", 20);

// Accessing values from the HashMap


int age1 = hm.get("Bushra");
int age2 = hm.get("Faiza");
int age3 = hm.get("Zoha");

4
System.out.println("Bushra's age is " + age1);
System.out.println("Faiza's age is " + age2);
System.out.println("Zoha's age is " + age3);

// Updating a value in the HashMap


hm.put("Faiza", 21);

// Removing a key-value pair from the HashMap


hm.remove("Zoha");

// Printing all the key-value pairs in the HashMap


System.out.println("All key-value pairs in the HashMap:");
for (String key : hm.keySet()) {
int value = hm.get(key);
System.out.println(key + " is " + value + " years old.");
}
}
}

Output:
Bushra's age is 19
Faiza's age is 20
Zoha's age is 20
All key-value pairs in the HashMap:
Bushra is 19 years old.
Faiza is 21 years old.

You might also like