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

Showing posts with label SIXTH WEEK LAB TERM WORK. Show all posts
Showing posts with label SIXTH WEEK LAB TERM WORK. Show all posts

Thursday, 4 April 2019

Sixth Week Term Work Program 2 (Hashing)

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.

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("-----------------------------------------------------------");
}


}

Credits: Sonali 

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

Thursday, 7 March 2019

Sixth week Term Work Program 4

Question:
Write a Java program to that simulates copy command.

Solution:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

// file streams (STREAMS)
public class program {
public static void main(String[] args)  {
FileInputStream fin=null;
FileOutputStream fout=null;
int num;
try {
fin=new FileInputStream(args[0]);
fout=new FileOutputStream(args[1]);
while((num=fin.read())!=-1) {
fout.write(num);
System.out.print((char)num);
}

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

}
finally {
System.out.println();
if(fin!=null)
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
if(fout!=null)
try {
fout.close();
}catch(Exception e) {
e.printStackTrace();

}
}

}

}

Output:
//Command Line Argument
123456789000111 My name Khan And I am not A hero.

Sixth Week Term Work Program 3 [ IA-1 2 B) ]

Question:
Write a Java program the implements a generic sort method using any of the sorting techniques.

public class GenericSorting {
//Modification: Sorting Wrt To String Length.
public static <T extends Comparable<T>> void sortingMethod1(T array[])
   {
   int i,j,n;
   n=array.length;
   for(i=0;i<n;i++)
   for(j=0;j<n-i-1;j++)
   if((((String) array[j]).length())>((String) array[j+1]).length())
   {
   T temp;
   temp=array[j];
   array[j]=array[j+1];
   array[j+1]=temp;
   }
   
   for(T element: array)
  System.out.printf("%s ", element);
   System.out.println();
   }

   public static <T extends Comparable<T>> void sortingMethod(T array[])
   {
   int i,j,n;
   n=array.length;
   for(i=0;i<n;i++)
   for(j=0;j<n-i-1;j++)
   if(array[j].compareTo(array[j+1])>0)
   {
   T temp;
   temp=array[j];
   array[j]=array[j+1];
   array[j+1]=temp;
   }
   
   for(T element: array)
  System.out.printf("%s ", element);
   System.out.println();
   }
   public static void main(String args[])
   {
   Integer i[]={3,2,1,7,6};
           String s[]={"pear","orange","apple"};
   sortingMethod(i);
 sortingMethod(s);
 
 System.out.println("\nSorting As per String Length");
 sortingMethod1(s);
 
   }
}

Output:
1 2 3 6 7 
apple orange pear 

Sorting As per String Length

pear apple orange 

Blue Marked Are Modification Asked By Ma'am In B1 Batch.

Sixth Week Term Work Program 1 [ IA-1 3 A) ]

Question:
 Write a Java program to create the below-mentioned Student record using appropriate
Collection class to perform the following operations on a single student:
a. Add a student
b. Display all the details of the student
c. Delete/remove student record
Members of the class Student is given below:

Student
name
USN
division
CGPA


Solution:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class SingleStudentRecord {
 ArrayList<Comparable> stud=new   ArrayList<Comparable>(4);
static Scanner in=new Scanner(System.in);
public void EnterRecord() {
System.out.print("Enter The Name:");
stud.add(in.next());
System.out.println();
System.out.print("Enter The USN:");
stud.add(in.next());
System.out.println();
System.out.print("Enter The Division:");
stud.add(in.next());
System.out.println();
System.out.print("Enter The CGPA:");
stud.add(in.nextFloat());
System.out.println();
}
public void DisplayRecord() {
Iterator<Comparable> itr=stud.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}        

}
public void DeleteRecord() {
System.out.println("Deleted The Student Record");
stud.removeAll(stud);
}
public static void main(String[] args) {
SingleStudentRecord s=new SingleStudentRecord();
int choice;
int i=0;
    while (true) {
        System.out.println("\n\n*******************STUDENT RECORD*******************\n");
        System.out.println("1. Enter Student Record");
        System.out.println("2. Display Record");
        System.out.println("3. Delete Record");
        System.out.println("4. Exit");
        System.out.println("Enter choice: ");
        choice = in.nextInt();
        switch (choice) {
        case 1: if(i==0)
        {
        s.EnterRecord();
        i++;
        }
        else {
        System.out.println("U Can Store Only One Student Record");
        }
                break;
        case 2:
        if(i==1)
        {
        s.DisplayRecord();
        }
        else {
        System.out.println("No Record Exists");
        }
        break;
        case 3:if(i==1) {
        s.DeleteRecord();
        i--;
        }
        else {
        System.out.println("No Record To delete");
        }
        break;
        case 4:
        System.out.println("THANKS FOR VISTING");
        System.exit(0);
        break;
        default:
        System.out.println("Enter A Valid Option");

}
}
}
}

Output:

**********STUDENT RECORD**********

1. Enter Student Record
2. Display Record
3. Delete Record
4. Exit
Enter choice: 
1
Enter The Name:Amit

Enter The USN:2Sd16cs012

Enter The Division:B

Enter The CGPA:8.4


Enter choice: 
2
Amit
2Sd16cs012
B
8.4


Enter choice: 
3
Deleted The Student Record


Enter choice: 
4
THANKS FOR VISTING