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

Class Running Notes 10th Nov

Noteeeee

Uploaded by

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

Class Running Notes 10th Nov

Noteeeee

Uploaded by

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

Dt : 10/11/2022

Ex-program : DemoSet1.java

package maccess;
import java.util.*;
public class DemoSet1 {
@SuppressWarnings({ "rawtypes", "unchecked", "removal" })
public static void main(String[] args) {
//Set object created to hold Unlimited any type of
Objects
HashSet ob1 = new HashSet();

ii
ob1.add(new Integer(123));//Adding Integer Object to Set

ath
ob1.add(new String("NIT"));//Adding String Object to Set
ob1.add(new StringBuffer("Java"));//Adding Buffer object
Set
System.out.println("****display from Set<E>*****");
System.out.println(ob1.toString());

aip
//Set object created to hold Unlimited Integer Objects
HashSet<Integer> ob2 = new HashSet<Integer>();
ob2.add(new Integer(11));
hM
ob2.add(new Integer(10));
ob2.add(new Integer(16));
System.out.println(ob2.toString());

//Set object created to hold Unlimited String Objects


HashSet<String> ob3 = new HashSet<String>();
tes

ob3.add(new String("Task"));
ob3.add(new String("Thread"));
ob3.add(new String("Test"));
System.out.println(ob3.toString());
a
nk

}
}
Ve

o/p:

****display from Set<E>*****

[NIT, 123, Java]

[16, 10, 11]

[Task, Test, Thread]


====================================================================

Ex-Program : DemoSet2.java

package maccess;
import java.util.*;
public class DemoSet2 {
@SuppressWarnings("removal")
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String name=null;
Set<Integer> ob = null;

ii
try(s;){

ath
try {
while(true) {
System.out.println("****Choice*****");

aip
System.out.println("1.HashSet\n2.LinkedHashSet\n3.TreeSet\n4.exi
t");
System.out.println("Enter the Choice:");
switch(s.nextInt()) {
hM
case 1:
ob = new HashSet<Integer>();
name="HashSet";
break;
case 2:
ob = new LinkedHashSet<Integer>();
tes

name="LinkedHashSet";
break;
case 3:
ob = new TreeSet<Integer>();
a

name="TreeSet";
nk

break;
case 4:
System.out.println("Operations stopped
of Set");
Ve

System.exit(0);
break;
default:
System.out.println("Invalid
Choice...");
}//end of switch
System.out.println("****Operations on
"+name+"****");
xyz:
while(true) {
System.out.println("****Choice****");

System.out.println("1.add\n2.remove\n3.exit");
System.out.println("Enter the
Choice:");
switch(s.nextInt()) {
case 1:
System.out.println("Enter the
ele:");
ob.add(new Integer(s.nextInt()));
System.out.println(ob.toString());

ii
break;
case 2:

ath
if(ob.isEmpty()) {
System.out.println("Set is
empty...");
}else {
System.out.println("Enter the

aip
ele to be removed:");
if(ob.remove(new
Integer(s.nextInt()))) {
System.out.println("Ele
hM
removed Successfully..");

System.out.println(ob.toString());
}else {
System.out.println("Element
tes

not founded...");
}
}
break;
case 3:
a

System.out.println("Operations
nk

Stopped on "+name);
break xyz;
default:
System.out.println("Invalid
Ve

Choice...");
}//end of switch
}//end of while

}//end of loop
}catch(Exception e) {e.printStackTrace();}
}//end of try
}
}
=================================================================

*imp

Set<E> holding Usser defined class Objects:

BookDetails.java

package test;
public class BookDetails extends Object{

ii
//Instance Variables

ath
public String code,name,author;
public float price;
public int qty;

//Constructor to initialize Instance variables

aip
public BookDetails(String code,String name,String
author,float price,int qty){
this.code=code;
this.name=name;
hM
this.author=author;
this.price=price;
this.qty=qty;
}
@Override
public String toString()
tes

{
return code+"\t"+name+"\t"+author+"\t"+price+"\t"+qty;
}
}
a
nk

DemoSet3.java(MainClass)

package maccess;
Ve

import java.util.*;

import test.*;

public class DemoSet3 {

@SuppressWarnings("removal")

public static void main(String[] args) {


Scanner s = new Scanner(System.in);

String name=null;

Set<BookDetails> ob = null;

try(s;){

try {

while(true) {

ii
System.out.println("****Choice*****");

ath
System.out.println("1.HashSet\n2.LinkedHashSet\n3.TreeSet\n4.exit");

System.out.println("Enter the Choice:");

aip
switch(Integer.parseInt(s.nextLine())) {

case 1:
hM
ob = new HashSet<BookDetails>();

name="HashSet";

break;
tes

case 2:

ob = new LinkedHashSet<BookDetails>();
a

name="LinkedHashSet";
nk

break;

case 3:
Ve

ob = new TreeSet<BookDetails>();

name="TreeSet";

break;

case 4:

System.out.println("Operations stopped of Set");


System.exit(0);

break;

default:

System.out.println("Invalid Choice...");

}//end of switch

System.out.println("****Operations on "+name+"****");

ii
xyz:

ath
while(true) {

System.out.println("****Choice****");

aip
System.out.println("1.add\n2.remove\n3.display\n4.exit");

System.out.println("Enter the Choice:");


hM
switch(Integer.parseInt(s.nextLine())) {

case 1:

System.out.println("Enter the code:");


tes

String bC=s.nextLine();

System.out.println("Enter the name:");


a

String bN=s.nextLine();
nk

System.out.println("Enter the author:");

String bA=s.nextLine();
Ve

System.out.println("Enter the price:");

float bP = Float.parseFloat(s.nextLine());

System.out.println("Enter the qty:");

int bQ = Integer.parseInt(s.nextLine());

ob.add(new BookDetails(bC,bN,bA,bP,bQ));
System.out.println("BookDetails added Successfully..");

break;

case 2:

if(ob.isEmpty()) {

System.out.println("Set is empty...");

}else {

ii
System.out.println("Enter the ele(code) to be removed:");

ath
String code2 = s.nextLine();

boolean p=false;

aip
Iterator<BookDetails> it = ob.iterator();

while(it.hasNext())
hM
{

BookDetails bd = (BookDetails)it.next();

if(bd.code.equals(code2)) {
tes

p=true;

ob.remove(bd);
a

System.out.println("Ele removed Successfully..");


nk

break;

}
Ve

}//end of loop

if(!p)

System.out.println("Element Not found...");

}
}

break;

case 3:

System.out.println("****BookDetails****");

ob.forEach((k)->

ii
System.out.println(k.toString());

ath
});

break;

aip
case 4:

System.out.println("Operations Stopped on "+name);


hM
break xyz;

default:

System.out.println("Invalid Choice...");
tes

}//end of switch

}//end of while
a
nk

}//end of loop

}catch(Exception e) {e.printStackTrace();}
Ve

}//end of try

==============================================================

You might also like