Wednesday 1 January 2020

Menu DrivenJava Program to Add new Student, Sort Sudents by id, Search Student by id

import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;

import java.util.Collections; 
class Student implements Comparable<Student>
{
private long id;
private String name;
private int m1,m2,m3,total;
Student(long tid, String tname, int tm1, int tm2, int tm3)
{
id=tid;name=tname;m1=tm1;m2=tm2;m3=tm3; total =tm1+tm2+tm3;
}
public String toString()
{
return "regd id = "+ id+" name = "+name +" m1 marks = "+m1 +" m2 marks = "+m2 +" m3 marks = "+m3 ;
}
public long getId()
{
return id;
}
public int compareTo(Student tob)
{       
return (this.getId() < tob.getId() ? -1 : 1);
}

}

public class StudentDemo
{
public static void main(String args[])
{
System.out.println("1. add new student 2. Print all  3. sort id 4. search id 5. exit");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
Student st;
ArrayList<Student> students = new ArrayList<Student>();
int nos=0;
do{
switch(choice)
{
case 1:
System.out.println("enter id, name, m1 ,m2 and m3 marks");
st  = new Student(sc.nextInt(),sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());
students.add(st);
break;
case 2:
for(Student ele: students)
System.out.println(ele);
break;
case 3:
Collections.sort(students);
for(Student ele: students)
System.out.println(ele);
break;
case 4:
System.out.println("enter id to search");
int key=sc.nextInt(),flag=0;
for(Student ele: students)
if(ele.getId() == key)
{
System.out.println("search element found");
System.out.println("ele");
flag=1;
break;
}
if(flag == 0)
System.out.println("search element not found");
break;
case 5:
System.exit(0);
}
System.out.println("1. add new student 2. Print all 3. sort id 4. search id 5. exit");
choice = sc.nextInt();
}while(choice>0 && choice <6);
}
}


=================================================================
Sorting using comparator

import java.util.*;
import java.lang.*;
import java.io.*;

// A class to represent a student.
class Student
{
int rollno;
String name, address;

// Constructor
public Student(int rollno, String name,
String address)
{
this.rollno = rollno;
this.name = name;
this.address = address;
}

// Used to print student details in main()
public String toString()
{
return this.rollno + " " + this.name +
" " + this.address;
}
}

class Sortbyroll implements Comparator<Student>
{
// Used for sorting in ascending order of
// roll number
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}

// Driver class
class Main
{
public static void main (String[] args)
{
Student [] arr = {new Student(111, "bbbb", "london"),
new Student(131, "aaaa", "nyc"),
new Student(121, "cccc", "jaipur")};

System.out.println("Unsorted");
for (int i=0; i<arr.length; i++)
System.out.println(arr[i]);

Arrays.sort(arr, new Sortbyroll());

System.out.println("\nSorted by rollno");
for (int i=0; i<arr.length; i++)
System.out.println(arr[i]);
}

}


1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete