In this exercise you will create array of objects and perfor

In this exercise you will create array of objects and perform search in the array based on user provided values. To accomplish the task: Create an array of student class which can store 10 instances Take input/data from user to initialize student instances on run time to fill student array Create a function named FindStudent outside main method (at the level where main is defined) which can accept an array of type student and returns a single instance of student. Create a function with two arguments with following signature Student FindStudent (Student students [], int rollNumber) Inside this function write code to search student in array against provided roll number (remember, array and roll number to find are being provided as arguments) If student is found return found student instance from array Print found student data (use stew e.g. to adjust display)

Solution


// simple program to insert data into array and search.

import java.util.*;

// student class definition
class Student{
   int rollnumber;
   String name;
   Student(int rollnumber,String name){           // constructer with required parameters
       this.rollnumber=rollnumber;
       this.name = name;
   }
}
// definition of FindStudents class
public class FindStudents{
   public static void main(String args[]){
       Student[] stu = new Student[10];
       Scanner sc = new Scanner(System.in);
       int i=0;
       // taking data from user for 10 students.
       for(;i<10;i++){
           System.out.println(\"Enter data for student no \"+(i+1)+ \":-\");
           System.out.print(\"\ Enter rollnumber:-\\t\");
           int rollnumber = sc.nextInt();
           System.out.print(\"\ Enter name:-\\t\");
           String name = sc.next();
           stu[i]= new Student(rollnumber,name);
           System.out.println(\"\ \\tInserted Data !!\ \");
       }
       // this will ask the user repeatedly which element he want to search and shows the results.
       while(true){
           System.out.println(\"\ Enter rollnumber you want to search. 999 for quit:-\\t\");
           int rollnumber = sc.nextInt();
           if(rollnumber == 999){
               break;
           }
           // calling FindStudent function
           Student temp = FindStudent(stu,rollnumber);
           if(temp.rollnumber == -1){
               System.out.println(\"\ \\t\"+temp.name+\"\ \");
               continue;
           }
           System.out.println(\"\ Student Found: \ \\trollnumber:-\\t\"+temp.rollnumber+\"name:-\\t\"+temp.name+\"\ \");
       }
   }
   // definition of FindStudent definition
   public static Student FindStudent(Student students[], int rollnumber){
       int length= students.length;
       Student temp;
       // over students array checking for required element.
       for(int i=0;i<length;i++){
           if(students[i].rollnumber == rollnumber){       // if element found
               temp = students[i];
               return temp;               // return element
           }
       }
       temp = new Student(-1,\"rollnumber_not_found\"); // if not found return element with false indication message.
       return temp;
   }
}

 In this exercise you will create array of objects and perform search in the array based on user provided values. To accomplish the task: Create an array of stu
 In this exercise you will create array of objects and perform search in the array based on user provided values. To accomplish the task: Create an array of stu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site