1. Write a Java program to create a Set containing list of items of type String and print the items in the list using Iterator interface. Also print the list in reverse/ backward direction.
Student.java
package com.hiraymca; public class Student {
int rollno; String name;
Double percentage;
public Student(int rollno, String name, Double percentage) {this.rollno = rollno;
this.name = name; this.percentage = percentage;
}
public Student()
{
}
public int getRollno() {return rollno;
}
public void setRollno(int rollno) {this.rollno = rollno;
}
public String getName() {return name;
}
public void setName(String name) {this.name = name;
}
public Double getPercentage() {return percentage;
}
public void setPercentage(Double percentage) {this.percentage = percentage;
}
setExample.java package com.hiraymca;
import java.util.ArrayList; import java.util.Collection;
import java.util.Collections;
import static java.util.Collections.list;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NavigableSet;
import java.util.Scanner;
import java.util.TreeSet;
/**
*
* @author VIKRAM
*/
public class setExample {
public static void main(String []args)
{
String ans=null;
int rn;
String sn;
double per;
Scanner sc=new Scanner(System.in);
Student s=new Student();
int choice;
//Creating set of students HashSet<Student>studentSet=new HashSet<Student>(); Iterator i;
do
{
System.out.println(“Menu”); System.out.println(“1.Adding student”); System.out.println(“2.List all students”); System.out.println(“3.List in reverse direction”); System.out.println(“4.Exit”); System.out.print(“Enter your choice(1..4)”); choice=sc.nextInt();
int flag=0; switch(choice)
{
case 1:
System.out.print(“Enter rollno”); rn=sc.nextInt(); System.out.print(“Enter name”); sn=sc.next();
System.out.print(“Enter percentage”); per=sc.nextDouble();
s=new Student(rn,sn,per); studentSet.add(s);
break;
case 2:
System.out.println(“Rollno”+”\t”+”Name”+”\t”+”Percentage”); studentSet.forEach((s1) -> {System.out.println(s1.getRollno()+”\t”+s1.getName()+”\t”+s1.getPercentage());
});
break; case 3:
Comparator<Student> c=
Comparator.comparing(Student::getRollno,Comparator.reverseOrder()).thenComparing(Student::getNa me,Comparator.reverseOrder());
List<Student>list=new ArrayList<>(studentSet);
list.sort(c);
System.out.println(“Rollno”+”\t”+”Name”+”\t”+”Percentage”); for(Student s2:list)
{
System.out.println(s2.getRollno()+”\t”+s2.getName()+”\t”+s2.getPercentage());
}
break;
case 4:
System.exit(0); break;
//adding student in HashSet
System.out.print(“Do you wish to continue(y/n)”); ans=sc.next();
}while(ans.equals(“Y”)||ans.equals(“y”));
//reteriving the student from set
}
}
1. Write a Java program using Set interface containing list of items and perform the following operations:
1. Add items in the set.
2. Insert items of one set in to other set.
3. Remove items from the set
4. Search the specified item in the set
package com.hiraymca;
public class Student {
int rollno; String name;
Double percentage;
public Student(int rollno, String name, Double percentage) {this.rollno = rollno;
this.name = name; this.percentage = percentage;
}
public Student()
{
}
public int getRollno() {return rollno;
}
public void setRollno(int rollno) {this.rollno = rollno;
}
public String getName() {return name;
}
public void setName(String name) {this.name = name;
}
public Double getPercentage() {return percentage;
}
public void setPercentage(Double percentage) {this.percentage = percentage;
}
}
package com.hiraymca;
import java.util.HashSet; import java.util.Iterator; import java.util.Scanner;
import java.util.function.Predicate;
/**
*
* @author VIKRAM
*/
public class setExample {
public static void main(String []args)
{
String ans=null; int rn;
String sn; double per;
Scanner sc=new Scanner(System.in); Student s=new Student();
//Creating set of students HashSet<Student>studentSet=new HashSet<Student>(); Iterator i;
//Creating Students
do
{
System.out.println(“Menu”); System.out.println(“1.Adding student”); System.out.println(“2.Removing student”); System.out.println(“3.Search student”); System.out.println(“4.List all students”); System.out.println(“5.Exit”); System.out.print(“Enter your choice(1..4)”); choice=sc.nextInt();
int flag=0; switch(choice)
{
case 1:
System.out.print(“Enter rollno”); rn=sc.nextInt(); System.out.print(“Enter name”); sn=sc.next();
System.out.print(“Enter percentage”); per=sc.nextDouble();
s=new Student(rn,sn,per); studentSet.add(s);
break; case 2:
System.out.print(“Enter roll no to remove element”); rn=sc.nextInt();
i=studentSet.iterator(); while(i.hasNext())
{
s=(Student)i.next(); if(s.getRollno()==rn)
{
i. remove();
System.out.println(“Element is successfully removed”); break;
}
else
{
System.out.println(“Rollno=“+rn);
}
}
break; case 3:
System.out.println(“Enter the name of student”); sn=sc.next();
i=studentSet.iterator(); if(!i.hasNext())
{
System.out.println(“List is empty”);
}
while(i.hasNext())
{
s=(Student)i.next(); if(s.getName().equals(sn))
{
System.out.println(“Record found”);
System.out.println(s.getRollno()+” “+s.getName()+” “+s.getPercentage()); flag=1;
break;
}
if(flag!=1)
{
System.out.println(“Record not found search again”);
}
}
break;
case 4:
System.out.println(“Rollno”+”\t”+”Name”+”\t”+”Percentage”); studentSet.forEach((s1) -> {
System.out.println(s1.getRollno()+” “+s1.getName()+” “+s1.getPercentage());
});
break; case 5:
System.exit(0); break;
}
//adding student in HashSet
System.out.print(“Do you wish to continue(y/n)”); ans=sc.next();
}while(ans.equals(“Y”)||ans.equals(“y”));
//reteriving the student from set
}
}
Menu
1.Adding student 2.Removing student 3.Search student 4.List all students 5.Exit
Enter your choice(1..4)1 Enter rollno 1
Enter name AAA
Enter percentage 99.98
Do you wish to continue(y/n)y Menu
1.Adding student 2.Removing student 3.Search student 4.List all students 5.Exit
Enter your choice(1..4)1
Enter rollno 2 Enter name BBB
Enter percentage 89.45
Do you wish to continue(y/n)y Menu
1.Adding student 2.Removing student 3.Search student 4.List all students 5.Exit
Enter your choice(1..4)1 Enter rollno 3
Enter name CCC
Enter percentage55.24
Do you wish to continue(y/n)y Menu
1.Adding student 2.Removing student 3.Search student 4.List all students 5.Exit
Enter your choice(1..4)3 Enter the name of student CCC
Record found 3 CCC 55.24
Do you wish to continue(y/n)y Menu
1.Adding student 2.Removing student 3.Search student 4.List all students 5.Exit
Enter your choice(1..4)4 Rollno Name Percentage 3 CCC 55.24
2 BBB 89.45
Do you wish to continue(y/n)y Menu
1.Adding student 2.Removing student 3.Search student 4.List all students 5.Exit
Enter your choice(1..4)2
Enter roll no to remove element2 Rollno=2
Element is successfully removed Do you wish to continue(y/n)y Menu
1. Adding student 2.Removing student 3.Search student 4.List all students 5.Exit
Enter your choice(1..4)4 Rollno Name Percentage 3 CCC 55.24
1 AAA 99.98
Do you wish to continue(y/n)n