Question:
Post Graduate course of CSE department can accommodate a maximum of 20 students. Each
PG student in the department is identified by his/her Roll No, USN, Name, Semester and
Mobile Number. Using the appropriate Collection class, write a Java Program to simulate the
following scenarios:
a. On request, the capacity got increased from 20 to 25.
b. Only 10 students enroll for the course.
c. Four students from Roll No.s 3 – 6 voluntarily un-enroll from the course.
d. After a month, six more students get enrolled in the course.
e. Print the information of all the students currently enrolled in the course.
Post Graduate course of CSE department can accommodate a maximum of 20 students. Each
PG student in the department is identified by his/her Roll No, USN, Name, Semester and
Mobile Number. Using the appropriate Collection class, write a Java Program to simulate the
following scenarios:
a. On request, the capacity got increased from 20 to 25.
b. Only 10 students enroll for the course.
c. Four students from Roll No.s 3 – 6 voluntarily un-enroll from the course.
d. After a month, six more students get enrolled in the course.
e. Print the information of all the students currently enrolled in the course.
Java Program:
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Map;
class StudentDetails{
String name;
String usn;
String div;
String Course="Java";
int sem;
StudentDetails(String name, String usn, String div, int sem){
this.name=name;
this.usn=usn;
this.div=div;
this.sem=sem;
}
}
public class PgStudent {
public static void main(String[] args) {
Map<Integer,StudentDetails> map=new HashMap<Integer,StudentDetails>();
//Creating students
StudentDetails s1=new StudentDetails ("amar","2sd15cs08","b",4);
StudentDetails s2=new StudentDetails("Akbar","2sd16cs10","b",4);
StudentDetails s3=new StudentDetails("Antony","2sd16cs09","b",4);
StudentDetails s4=new StudentDetails("Ajay","2sd16cs01","b",4);
StudentDetails s5=new StudentDetails("siya","2sd16cs02","b",4);
StudentDetails s6=new StudentDetails("Riya","2sd16cs03","b",4);
StudentDetails s7=new StudentDetails("Maya","2sd16cs04","b",4);
StudentDetails s8=new StudentDetails("Shriya","2sd16cs05","b",4);
StudentDetails s9=new StudentDetails("Fida","2sd16cs06","b",4);
StudentDetails s10=new StudentDetails("Jiya","2sd16cs07","b",4);
//Adding Students to map
map.put(1,s1);
map.put(2,s2);
map.put(3,s3);
map.put(4,s4);
map.put(5,s5);
map.put(6,s6);
map.put(7,s7);
map.put(8,s8);
map.put(9,s9);
map.put(10,s10);
//Traversing map
for(Map.Entry<Integer, StudentDetails> entry:map.entrySet()){
int key=entry.getKey();
StudentDetails b=entry.getValue();
System.out.println("Student "+key);
System.out.println(b.name+" "+b.usn+" "+b.div+" "+b.sem+" "+b.Course);
}
System.out.println("-----------------------------------------------------------");
System.out.println("Now students having Usn 3 to 6 will unroll from the course");
map.remove(3);
System.out.println("Student having roll no 3 unrolled");
map.remove(4);
System.out.println("Student having roll no 4 unrolled");
map.remove(5);
System.out.println("Student having roll no 5 unrolled");
map.remove(6);
System.out.println("Student having roll no 6 unrolled");
System.out.println("-----------------------------------------------------------");
System.out.println("List of students after un-rolling from course");
System.out.println("-----------------------------------------------------------");
for(Map.Entry<Integer, StudentDetails> entry:map.entrySet()){
int key=entry.getKey();
StudentDetails b=entry.getValue();
System.out.println("Student "+key);
System.out.println(b.name+" "+b.usn+" "+b.div+" "+b.sem+" "+b.Course);
}
System.out.println("-----------------------------------------------------------");
//After a month 6 more students enrolled
StudentDetails s11=new StudentDetails ("ADI","2sd15cs11","b",4);
StudentDetails s12=new StudentDetails("JAI","2sd16cs12","b",4);
StudentDetails s13=new StudentDetails("RUHI","2sd16cs13","b",4);
StudentDetails s14=new StudentDetails("RIMA","2sd16cs14","b",4);
StudentDetails s15=new StudentDetails("AMY","2sd16cs15","b",4);
StudentDetails s16=new StudentDetails("ARYA","2sd16cs16","b",4);
map.put(3,s11);
map.put(4,s12);
map.put(5,s13);
map.put(6,s14);
map.put(11,s15);
map.put(12,s16);
System.out.println("Currently Updated list");
for(Map.Entry<Integer, StudentDetails> entry:map.entrySet()){
int key=entry.getKey();
StudentDetails b=entry.getValue();
System.out.println("Student "+key);
System.out.println(b.name+" "+b.usn+" "+b.div+" "+b.sem+" "+b.Course);
}
System.out.println("-----------------------------------------------------------");
}
}
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Map;
class StudentDetails{
String name;
String usn;
String div;
String Course="Java";
int sem;
StudentDetails(String name, String usn, String div, int sem){
this.name=name;
this.usn=usn;
this.div=div;
this.sem=sem;
}
}
public class PgStudent {
public static void main(String[] args) {
Map<Integer,StudentDetails> map=new HashMap<Integer,StudentDetails>();
//Creating students
StudentDetails s1=new StudentDetails ("amar","2sd15cs08","b",4);
StudentDetails s2=new StudentDetails("Akbar","2sd16cs10","b",4);
StudentDetails s3=new StudentDetails("Antony","2sd16cs09","b",4);
StudentDetails s4=new StudentDetails("Ajay","2sd16cs01","b",4);
StudentDetails s5=new StudentDetails("siya","2sd16cs02","b",4);
StudentDetails s6=new StudentDetails("Riya","2sd16cs03","b",4);
StudentDetails s7=new StudentDetails("Maya","2sd16cs04","b",4);
StudentDetails s8=new StudentDetails("Shriya","2sd16cs05","b",4);
StudentDetails s9=new StudentDetails("Fida","2sd16cs06","b",4);
StudentDetails s10=new StudentDetails("Jiya","2sd16cs07","b",4);
//Adding Students to map
map.put(1,s1);
map.put(2,s2);
map.put(3,s3);
map.put(4,s4);
map.put(5,s5);
map.put(6,s6);
map.put(7,s7);
map.put(8,s8);
map.put(9,s9);
map.put(10,s10);
//Traversing map
for(Map.Entry<Integer, StudentDetails> entry:map.entrySet()){
int key=entry.getKey();
StudentDetails b=entry.getValue();
System.out.println("Student "+key);
System.out.println(b.name+" "+b.usn+" "+b.div+" "+b.sem+" "+b.Course);
}
System.out.println("-----------------------------------------------------------");
System.out.println("Now students having Usn 3 to 6 will unroll from the course");
map.remove(3);
System.out.println("Student having roll no 3 unrolled");
map.remove(4);
System.out.println("Student having roll no 4 unrolled");
map.remove(5);
System.out.println("Student having roll no 5 unrolled");
map.remove(6);
System.out.println("Student having roll no 6 unrolled");
System.out.println("-----------------------------------------------------------");
System.out.println("List of students after un-rolling from course");
System.out.println("-----------------------------------------------------------");
for(Map.Entry<Integer, StudentDetails> entry:map.entrySet()){
int key=entry.getKey();
StudentDetails b=entry.getValue();
System.out.println("Student "+key);
System.out.println(b.name+" "+b.usn+" "+b.div+" "+b.sem+" "+b.Course);
}
System.out.println("-----------------------------------------------------------");
//After a month 6 more students enrolled
StudentDetails s11=new StudentDetails ("ADI","2sd15cs11","b",4);
StudentDetails s12=new StudentDetails("JAI","2sd16cs12","b",4);
StudentDetails s13=new StudentDetails("RUHI","2sd16cs13","b",4);
StudentDetails s14=new StudentDetails("RIMA","2sd16cs14","b",4);
StudentDetails s15=new StudentDetails("AMY","2sd16cs15","b",4);
StudentDetails s16=new StudentDetails("ARYA","2sd16cs16","b",4);
map.put(3,s11);
map.put(4,s12);
map.put(5,s13);
map.put(6,s14);
map.put(11,s15);
map.put(12,s16);
System.out.println("Currently Updated list");
for(Map.Entry<Integer, StudentDetails> entry:map.entrySet()){
int key=entry.getKey();
StudentDetails b=entry.getValue();
System.out.println("Student "+key);
System.out.println(b.name+" "+b.usn+" "+b.div+" "+b.sem+" "+b.Course);
}
System.out.println("-----------------------------------------------------------");
}
}
Credits: Sonali
0 comments:
Post a Comment