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

Oop Set 7

The document discusses several practical programming exercises involving object-oriented programming concepts like arrays, lists, stacks, sets and more. It includes code snippets and output for tasks like creating array lists, evaluating expressions using stacks, comparing performance of different data structures, and performing various operations on lists.

Uploaded by

adada
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)
29 views7 pages

Oop Set 7

The document discusses several practical programming exercises involving object-oriented programming concepts like arrays, lists, stacks, sets and more. It includes code snippets and output for tasks like creating array lists, evaluating expressions using stacks, comparing performance of different data structures, and performing various operations on lists.

Uploaded by

adada
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/ 7

Enrollment No.

200170107069 Practical 7

Name: Patel Aditi Mukeshkumar


Enrollment No.: 200170107069
Subject: Object Orientated Programming

Practical : 7
Practical 7.1)
Write a program that creates an Array List and adds a Loan object, a Date object , a
string, and a Circle object to the list, and use a loop to display all elements in the list by
invoking the object’s to String() method.

import java.util.*; public class Practical7_01 {


class Loan{ public static void main(String[] args) {
String personName; ArrayList<String> arr = new
double amount; ArrayList<>();
int year; Loan l = new Loan("Harris", 100000,
int duration; 2022, 5);
Loan(String name, double amount, int arr.add(l.toString());
year, int duration){ Date d = new Date();
this.personName = name; arr.add(d.toString());
this.amount = amount; Circle c = new Circle(10);
this.year = year; arr.add(c.toString());
this.duration = duration; System.out.println("Content of array list
} is : ");
} System.out.println(arr);
class Circle{ }
int r; }
Circle(int r){
this.r = r;
}
}

Code:

Output:

Object Orientated Programming 1|Page


Enrollment No. 200170107069 Practical 7

Practical 7.2)
Evaluate expression using stack class.
Code:

import java.util.Stack; case '*':


public class Practical7_02 { p1 = p2 * p1;
public static void main(String[] args) { break;
String post = "45+75/23*5-6^2"; case '/':
System.out.print(post + " = "); if (p1 == 0) {
Stack<Integer> ob = new Stack<Integer>(); System.out.println("Cannot divide
for (int i = 0; i < post.length(); i++) { by zero");
char ch = post.charAt(i); break;
if (ch >= 48 && ch <= 57) }
ob.push(ch - 48); p1 = p2 / p1;
else { break;
int p1 = ob.pop(); case '^':
int p2 = ob.pop(); p1 = (int) Math.pow(p2, p1);
switch (ch) { }
case '+': ob.push(p1);
p1 = p2 + p1; }
break; }
case '-': System.out.print(ob.pop());
p1 = p2 - p1; }
break; }

Output:

Object Orientated Programming 2|Page


Enrollment No. 200170107069 Practical 7

Practical 7.3)
Demonstrate test time and removed time required for hash set, tree set, an array set,
linked list.
Code:

import java.util.*; System.out.println("Test time for


public class Practical7_03 { linked list is " + getTestTime(list2) + " ms");
static final int N = 40300; System.out.println("Remove
public static void main(String[] args) { element time for linked list is " +
List<Integer> list = new ArrayList<>(); getRemoveTime(list2) + " ms");
for (int i = 0; i < N; i++) }
list.add(i); public static long
Collection<Integer> set1 = new getTestTime(Collection<Integer> c) {
HashSet<>(list); long startTime =
System.out.println("Test time for hash System.currentTimeMillis();
set is " + getTestTime(set1) + " ms");
System.out.println("Remove element for (int i = 0; i < N; i++)
time for hash set is " + getRemoveTime(set1) c.contains((int) (Math.random() * 2 *
+ " ms"); N));
Collection<Integer> set3 = new
TreeSet<>(list); return System.currentTimeMillis() -
System.out.println("Test time for tree startTime;
set is " + getTestTime(set3) + " ms"); }
System.out.println("Remove element public static long
time for tree set is " + getRemoveTime(set3) getRemoveTime(Collection<Integer> c) {
+ " ms"); long startTime =
Collection<Integer> list1 = new System.currentTimeMillis();
ArrayList<>(list);
System.out.println("Test time for array for (int i = 0; i < N; i++)
list is " + getTestTime(list1) + " ms"); c.remove(i);
System.out.println("Remove element
time for array list is " + getRemoveTime(list1) return System.currentTimeMillis() -
+ " ms"); startTime;
Collection<Integer> list2 = new }
LinkedList<>(list); }

Output:

Object Orientated Programming 3|Page


Enrollment No. 200170107069 Practical 7

Practical 7.4)
Using list perform following operation on it in java program. (use Array List and LinkedList)
1. Creating a new list 2. Basic operations 3. Iterating over a list
4. Searching for an element in a list 5. Sorting a list 6. Copying one list into another
7. Shuffling elements in a list 8. Reversing elements in a list 9. Extracting a portion of a list
10. Converting between Lists and arrays 11. List to Stream 12. Concurrent lists
Code:
import java.util.*;
System.out.println();
public class Practical7_04 { System.out.println("Search for 3 in Arraylist :
public static void main(String[] args) { " + arr.contains(3));
ArrayList<Integer> arr = new ArrayList<>(); System.out.println("Search for 3 in LinkedList
LinkedList<Integer> list = new : " + list.contains(3));
LinkedList<>(); System.out.println("Search for 5 in Arraylist :
for (int i = 0; i < 10; i++) { " + arr.contains(5));
System.out.println("Search for 5 in LinkedList
arr.add(i);
: " + list.contains(5));
list.add(i); Collections.shuffle(arr);
} Collections.shuffle(list);
System.out.println("After insertion Arraylist System.out.println("After shuffling Arraylist
and LinkedList is : "); and LinkedList is : ");
System.out.println("ArrayList : " + arr); System.out.println("ArrayList : " + arr);
System.out.println("LinkedList : " + list); System.out.println("LinkedList : " + list);
Collections.sort(arr);
arr.remove(7);
Collections.sort(list);
list.remove(7); System.out.println("After shuffling Arraylist
System.out.println("After removal Arraylist and LinkedList is : ");
and LinkedList is : "); System.out.println("ArrayList : " + arr);
System.out.println("ArrayList : " + arr); System.out.println("LinkedList : " + list);
System.out.println("LinkedList : " + list); ArrayList<Integer> arr1 = new
ArrayList<>(arr);
arr.set(1, 5);
LinkedList<Integer> list1 = new
list.set(1, 5);
LinkedList<>(list);
System.out.println("After updation Arraylist System.out.println("Coppied Arraylist and
and LinkedList is : "); LinkedList is : ");
System.out.println("ArrayList : " + arr); System.out.println("ArrayList : " + arr1);
System.out.println("LinkedList : " + list); System.out.println("LinkedList : " + list1);
System.out.println("Iteration of ArrayList : Collections.reverse(arr);
Collections.reverse(list);
");
System.out.println("After shuffling Arraylist
ObjectforOrientated Programming : arr) {
(Integer iterable_element 4|Page
and LinkedList is : ");
System.out.print(iterable_element + " "); System.out.println("ArrayList : " + arr);
} System.out.println("LinkedList : " + list);
Enrollment No. 200170107069 Practical 7

Integer strs[] = new Integer[arr.size()];


arr.toArray(strs);
System.out.println("ArrayList to array");
for(int i=0; i<strs.length; i++){
System.out.print(strs[i] + " ");
}
System.out.println();
strs = new Integer[list.size()];
list.toArray(strs);
System.out.println("LinkedList to array");
for(int i=0; i<strs.length; i++){
System.out.print(strs[i] + " ");
}
System.out.println();
System.out.println("ArrayList to string " + arr.toString());
System.out.println("LinkedList to string " + list.toString());
}
}
Output:

Object Orientated Programming 5|Page


Enrollment No. 200170107069 Practical 7

Practical 7.5)
Write a java program to evaluate arithmetic operation using stack.
Code:

import java.util.Stack; while (!ops.empty())


public class Practical7_05 { values.push(applyOp(ops.pop(),values.pop(),va
public static int evaluate(String expression) lues.pop()));
{ return values.pop();
char[] tokens = }
expression.toCharArray(); public static boolean hasPrecedence(char
Stack<Integer> values = new op1, char op2) {
Stack<Integer>(); if (op2 == '(' || op2 == ')')
Stack<Character> ops = new return false;
Stack<Character>(); if ((op1 == '*' || op1 == '/') && (op2 == '+'
for (int i = 0; i < tokens.length; i++) { || op2 == '-'))
if (tokens[i] == ' ') return false;
continue; else
if (tokens[i] >= '0' && tokens[i] <= '9') { return true;
String str = ""; }
while (i < tokens.length && tokens[i] public static int applyOp(char op, int b, int a)
>= '0' && tokens[i] <= '9') {
str += tokens[i++]; switch (op) {
values.push(Integer.parseInt(str)); case '+':
i--; return a + b;
} case '-':
else if (tokens[i] == '(') return a - b;
ops.push(tokens[i]); case '*':
else if (tokens[i] == ')') { return a * b;
while (ops.peek() != '(') case '/':
values.push(applyOp(ops.pop(), return a / b;
values.pop(), values.pop())); }
ops.pop(); return 0;
}else if (tokens[i] == '+' || tokens[i] == }
'-' || tokens[i] == '*' || tokens[i] == '/') { public static void main(String[] args) {
while (!ops.empty() && System.out.println("Evaluated Expressions
hasPrecedence(tokens[i],ops.peek())) are : ");
System.out.println("3 * 4 + (5 * 6)-32 / 8 +
values.push(applyOp(ops.pop(),values.pop(), (19 / 4) * 43 + 5 - 32 + 9 + 12 * 3 = " +
values.pop())); evaluate("3 * 4 + (5 * 6)-32 / 8 + (19 / 4) * 43 +
ops.push(tokens[i]); 5 - 32 + 9 + 12 * 3"));
} }
} }

Output:

Object Orientated Programming 6|Page


Enrollment No. 200170107069 Practical 7

Practical 7.6)
Implement a java program to show various operation of queue.
Code:

import java.util.LinkedList;
import java.util.Queue;
public class Practical7_06 {
public static void main(String[] args)
{
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < 10; i++)
q.add(i);
System.out.println("Elements of queue " + q);
int removedele = q.remove();
System.out.println("removed element-" + removedele);
System.out.println(q);
int head = q.peek();
System.out.println("head of queue-" + head);
int size = q.size();
System.out.println("Size of queue-" + size);
}
}
Output:

Object Orientated Programming 7|Page

You might also like