Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Friday, 8 March 2019

Sixth Week Term Work Program 2 (Arraylist)

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.

Solution:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Student{
Scanner in=new Scanner(System.in);
int Rollno;
String usn;
String Name;
int sem;
String Mobileno;
Student() {
System.out.print("Enter The Roll No:");
Rollno=in.nextInt();
System.out.println();
System.out.print("Enter The USN:");
usn=in.next();
System.out.println();
System.out.print("Enter The Name:");
Name=in.next();
System.out.println();
System.out.print("Enter The Sem");
sem=in.nextInt();
System.out.println();
System.out.print("Enter The MobileNo:");
Mobileno=in.next();
System.out.println();

}
Student(String name, int Rollno, String Mobile, String Usn, int sem)
{
this.Name=name;
this.Rollno=Rollno;
this.Mobileno=Mobile;
this.usn =Usn;
this.sem = sem;

}
public String getName() 
{
return Name;
}


public int getRollno() 
{
return Rollno;
}
public String getMobile() 
{
return Mobileno;
}

public String getUsn() 
{
return usn;
}


public int getSemester() 
{
return sem;
}

}
class Course extends Student
{
String course;


Course(String name, int Rollno, String Mobile, String Usn, int sem) 
{
super(name, Rollno, Mobile, Usn, sem);
System.out.println("Enter Course Name:");
course = in.next();


}
public String getCourse() 
{
return course;
}

public void setCourse(String course) 
{
this.course = course;
}
}
public class Pgstudent {
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
int choice;
int i=0,size=20;
List<Student> studentsList = new ArrayList<Student>(size); // array list to store user input For Student
List<Course> studentsListCourse = new ArrayList<Course>(size); // array list to store user input For Course

while (true) {
System.out.println("\n\n*******************STUDENT RECORD*******************\n");
System.out.println("1. Increase the Capacity");
System.out.println("2. Enter Student Record");
System.out.println("3. Display Record");
System.out.println("4. Enroll Course");
System.out.println("5. DeRoll Course");                                                                        
System.out.println("6. Display Enrolled Student Details");
System.out.println("7. Exit");
System.out.println("Enter choice: ");
choice = in.nextInt();
switch (choice) {
case 1:size=25;
System.out.println("Size Changed to 25");
break;

case 2: if(i<size){

System.out.println("============" + "=================");
System.out.println("Students " + "Personal Details");
System.out.println("============" + "=================");
Student student = new Student();

studentsList.add(student); // add student
i++;
}

else {
System.out.println("U Cannot Store The Student Record");
}
break;
case 3:
for (int j = 0; j < studentsList.size(); j++)
{

Student st = studentsList.get(j);

System.out.println("Information of Student : " + st.getRollno()); 
System.out.println("");
System.out.println("Name: " + st.getName() + " - Roll No: "+st.getRollno() + " - Mobile No: " + st.getMobile() + " - Student USN: " + st.getUsn() + " - Semester: " + st.getSemester());
System.out.println("");
}

break;
case 4:
System.out.println("============ENROLL" + "=================");
System.out.println("============" + "=================");
System.out.println("Enter the Roll NO");
int Froll=in.nextInt(),flag=0;
for (int j = 0; j < studentsList.size(); j++)
{
Student ct = studentsList.get(j);
if(ct.getRollno()==Froll) {
flag=1;
Course course=new Course(ct.getName(),ct.getRollno(), ct.getMobile(),ct.getUsn(),ct.getSemester());
studentsListCourse.add(course); // add student

}
}
if(flag==0) {
System.out.println("No Student Exist With Given RollNo Sorry Try Again");
}

break;
case 5:
System.out.println("Enter the Roll NO");
int Roll=in.nextInt(),flag1=0;
for (int j = 0; j < studentsListCourse.size(); j++)
{

Course ct = studentsListCourse.get(j);
if(ct.getRollno()==Roll) {
flag1=1;
System.out.println("Information of Student : " +ct.getRollno()); 
System.out.println("");
System.out.println("Name: " + ct.getName() + " - Roll No: "+ct.getRollno() + " - Mobile No: " + ct.getMobile() + " - Student USN: " + ct.getUsn() + " - Semester: " + ct.getSemester()+" -Course: "+ct.getCourse());
System.out.println("");
studentsListCourse.remove(ct);
}
}
if(flag1==0) {
System.out.println("Enter Roll No Did Not Enroll Only");
}


break;
case 6:
for (int j = 0; j < studentsListCourse.size(); j++)
{

Course ct = studentsListCourse.get(j);
System.out.println("Information of Student : " + (j + 1)); 
System.out.println("");
System.out.println("Name: " + ct.getName() + " - Roll No: "+ct.getRollno() + " - Mobile No: " + ct.getMobile() + " - Student USN: " + ct.getUsn() + " - Semester: " + ct.getSemester()+"-Course:"+ct.getCourse());
System.out.println("");

}
break;
case 7:
System.out.println("THANKS FOR VISTING");
System.exit(0);
break;
default:
System.out.println("Enter A Valid Option");

}
}   
}

}



Credit: Shubham

0 comments:

Post a Comment