Sei sulla pagina 1di 11

NAME - ADITYA THAKUR Submitted To : MR.

T DHARANI
KUMAR
SAP ID - 500045347
Batch B1 , CCVT 4TH SEM SIGNATURE/REMARKS

Assignment 2

Advance Programming using Java (CSEG237)

Submitted By
Name: ADITYA THAKUR
R110215013
500045347

B.Tech (CSE-CCVT), 4th Semester


To

Mr. T DHARANI KUMAR


Assistant Professor (Industry Fellow)
Department of Virtualization

CENTER FOR INFORMATION TECHNOLOGY


University of Petroleum and Energy Studies
P.O. Bidholi, Via Prem Nagar
Dehradun, Uttarakhand - 248007 (India)
2016

P a g e 1 | 11
NAME - ADITYA THAKUR Submitted To : MR.T DHARANI
KUMAR
SAP ID - 500045347
Batch B1 , CCVT 4TH SEM SIGNATURE/REMARKS

QUESTION 2

package ddd;

import java.util.*;
class Arrlst{
public static void main(String args[]){
ArrayList<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(9);
list.add(-3);
list.add(100);
//Traversing list through Iterator
Iterator itr=list.iterator();
System.out.println("The ArrayList:");
while(itr.hasNext()){ //Reading all elements from ArrayList by using
Iterator
System.out.println(itr.next());
}
System.out.println();
ArrayList<Integer> list1=list; //duplicate object of an
ArrayList instance
Iterator itr1=list.iterator();
System.out.println("The duplicate object of ArrayList:");
while(itr1.hasNext()){
System.out.println(itr1.next());
}
System.out.println();
System.out.println("The array list before reversing:"+list);
Collections.reverse(list); //Reversing ArrayList
System.out.println("The array list after reversing:"+list);
}

P a g e 2 | 11
NAME - ADITYA THAKUR Submitted To : MR.T DHARANI
KUMAR
SAP ID - 500045347
Batch B1 , CCVT 4TH SEM SIGNATURE/REMARKS

OUTPUT

P a g e 3 | 11
NAME - ADITYA THAKUR Submitted To : MR.T DHARANI
KUMAR
SAP ID - 500045347
Batch B1 , CCVT 4TH SEM SIGNATURE/REMARKS

QUESTION 2

package ddd;

import java.util.*;
class HshMp{
public static void main(String args[]){
Scanner r=new Scanner(System.in);
HashMap<String,String> hm=new HashMap<String,String>();
String s1, s2;
hm.put("first","I");
hm.put("second","Me");
hm.put("third","Myself");
System.out.println("Enter the key t be searched"); //find whether specified key exists
or not.
s1=r.nextLine();
if(hm.containsKey(s1))
System.out.println("The map contains the key");
else
System.out.println("The map does not contain the key");
System.out.println("Enter the key t be searched");
s2=r.nextLine();
if(hm.containsValue(s2)) //finding whether specified value exists or
not
System.out.println("The map contains the value");
else
System.out.println("The map does not contain the value");
System.out.println("All the keys os the HashMap");
Set<String> keys = hm.keySet(); //getting all keys from the given
HashMap
for(String key: keys){
System.out.println(key);
}
System.out.println("Key"+" "+"Value");
for(Map.Entry m:hm.entrySet()){ //getting all key-value pair as Entry
objects
System.out.println(m.getKey()+" "+m.getValue());
}
}
}

P a g e 4 | 11
NAME - ADITYA THAKUR Submitted To : MR.T DHARANI
KUMAR
SAP ID - 500045347
Batch B1 , CCVT 4TH SEM SIGNATURE/REMARKS

OUTPUT

P a g e 5 | 11
NAME - ADITYA THAKUR Submitted To : MR.T DHARANI
KUMAR
SAP ID - 500045347
Batch B1 , CCVT 4TH SEM SIGNATURE/REMARKS

QUESTION 3 Part a) and b)

package ddd;
import java.util.*;
public class HshSet {

public static void main(String[] args) {


// TODO Auto-generated method stub
HashSet<String> hs = new HashSet<String>();

hs.add("first");
hs.add("second");
hs.add("third");
System.out.println(hs);
HashSet<String> subSet = new HashSet<String>();
subSet.add("s1");
subSet.add("s2");
hs.addAll(subSet);
System.out.println("HashSet content after add of another collection:");
System.out.println(hs);
System.out.println("Clearing HashSet:");
hs.clear();
System.out.println("Cont After clear:");
System.out.println(hs);
}

P a g e 6 | 11
NAME - ADITYA THAKUR Submitted To : MR.T DHARANI
KUMAR
SAP ID - 500045347
Batch B1 , CCVT 4TH SEM SIGNATURE/REMARKS

OUTPUT

P a g e 7 | 11
NAME - ADITYA THAKUR Submitted To : MR.T DHARANI
KUMAR
SAP ID - 500045347
Batch B1 , CCVT 4TH SEM SIGNATURE/REMARKS

QUESTION 3 Part c)

package ddd;

import java.util.HashSet;

public class HshSet2 {

public static void main(String a[]){

HashSet<Price> lhs = new HashSet<Price>();


lhs.add(new Price("Guava", 30));
lhs.add(new Price("Watermelon", 60));
lhs.add(new Price("Mango", 80));
for(Price pr:lhs){
System.out.println(pr);
}
Price key = new Price("Guava", 30);
System.out.println("Does set contains key? "+lhs.contains(key));
}
}

class Price{

private String item;


private int price;

public Price(String itm, int pr){


this.item = itm;
this.price = pr;
}

public int hashCode(){


System.out.println("In hashcode");
int hashcode = 0;
hashcode = price*20;
hashcode += item.hashCode();
return hashcode;
}

public boolean equals(Object obj){


System.out.println("In equals");
if (obj instanceof Price) {
Price pp = (Price) obj;
return (pp.item.equals(this.item) && pp.price == this.price);
} else {
return false;
}
}

public String getItem() {


return item;
}

P a g e 8 | 11
NAME - ADITYA THAKUR Submitted To : MR.T DHARANI
KUMAR
SAP ID - 500045347
Batch B1 , CCVT 4TH SEM SIGNATURE/REMARKS
public void setItem(String item) {
this.item = item;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}

public String toString(){


return "item: "+item+" price: "+price;
}
}

OUTPUT

P a g e 9 | 11
NAME - ADITYA THAKUR Submitted To : MR.T DHARANI
KUMAR
SAP ID - 500045347
Batch B1 , CCVT 4TH SEM SIGNATURE/REMARKS

QUESTION 4

P a g e 10 | 11
NAME - ADITYA THAKUR Submitted To : MR.T DHARANI
KUMAR
SAP ID - 500045347
Batch B1 , CCVT 4TH SEM SIGNATURE/REMARKS

P a g e 11 | 11

Potrebbero piacerti anche