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
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
0 comments:
Post a Comment