Skip to main content

feb 7 IRA JAVA SOLUTION

Create the class Student with below Attributes:

studentId-int

studentName-String

score- int

collegeName-String


The above attribute should be private ,write getters and setters and 

parametrized constructor as required.Create class Solution with main

method.



Implement 2 Static methods-

getLowestScoredStudentByCollegeName and getSecondHighestScoredStudent


getLowestScoredStudentByCollegeName method:

  The method will take array of Student objects and a String value as 

input parameters and return the Student with lowest score from array of

Student objects for the given collegeName.(String parameter passed).

  If no student with above condition is present in  the array of Student

objects,then the method should return null.

 The main method should print the studentId,studentName,score,collegeName

on separate lines from the returned Student object if the returned value is 

not null. If the returned value is null then main method should print

"No such student found".





getSecondHighestScoredStudent method: 

   This method will take array of the Student Objects and return Student

Object having second highest score among the students only if the score 

of that Student is odd number and greater than 500.

   If no student with above condition is present in  the array of Student

objects,then the method should return null.

  The main method should print the studentId,studentName,score,collegeName

on separate lines from the returned Student object if the returned value is 

not null. If the returned value is null then main method should print

"No such student found".



***************************************************************************

first Input:

112

Steve

585

CIT

212

Adam

505

ABC

324

Mark

504

ABC

415

SAM

509

GCE

ABC

output: 

324

Mark

504

ABC

415

SAM

509

GCE


second input:

112

Steve

585

CIT

213

George

506

ABC

324

Mark

504

ABC

415 

SAM

510

GCE

GIT


output:

No such student found

No such student found


****************************************************************************



code:


import java.util.Arrays;

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {

         Scanner sc=new Scanner(System.in);

         Student[] students=new Student[4];


        for (int i = 0; i < 4; i++) {

            int a=sc.nextInt();sc.nextLine();

            String b=sc.nextLine();

            int c=sc.nextInt();sc.nextLine();

            String d=sc.nextLine();

             students[i]  = new Student(a,b,c,d);

        }

        String input=sc.nextLine();

        Student ans1=getLowestScoredStudentByCollegeName(students,input);

       if(ans1==null)

       {

           System.out.println("No such student found");

       }

       else

       {

           System.out.println(ans1.getStudentId());

           System.out.println(ans1.getStudentName());

           System.out.println(ans1.getScore());

           System.out.println(ans1.getCollegeName());

       }


      Student ans2 = getSecondHighestScoredStudent(students);

      if(ans2==null)

      {

          System.out.println("No such student found");

      }

      else

      {

          System.out.println(ans2.getStudentId());

          System.out.println(ans2.getStudentName());

          System.out.println(ans2.getScore());

          System.out.println(ans2.getCollegeName());

      }


    }

   public static Student getLowestScoredStudentByCollegeName(Student[] students, String input)

    {


        int low=Integer.MAX_VALUE;

        for (int i = 0; i <4 ; i++) {

           if(students[i].getCollegeName().equalsIgnoreCase(input) && students[i].getScore()<low)

           {

               low=students[i].getScore();

           }

        }


        for (int i = 0; i < 4; i++) {

            if(students[i].getScore()==low)

            {

                return students[i];

            }

        }


        return null;

    }


   public static Student getSecondHighestScoredStudent(Student[] students)

    {


        int[] arr=new int[0];

        for (int i = 0; i <4 ; i++) {

            if(students[i].getScore()>500  && students[i].getScore()%2!=0)

            {

               arr= Arrays.copyOf(arr,arr.length+1);

               arr[arr.length-1]=students[i].getScore();

            }

        }


        Arrays.sort(arr);

        if(arr.length<=1)

        {

            return null;

        }


        int shs=arr[arr.length-2];



        for (int i = 0; i < 4; i++) {

           if(students[i].getScore()==shs)

           {

              return students[i];

           }

        }


        return null;

    }


}



class Student{

  private  int studentId;

  private  String studentName;

   private int score;

   private String collegeName;


    //constructor


    public Student(int studentId, String studentName, int score, String collegeName) {

        this.studentId = studentId;

        this.studentName = studentName;

        this.score = score;

        this.collegeName = collegeName;

    }


    //getters and setters


    public int getStudentId() {

        return studentId;

    }


    public void setStudentId(int studentId) {

        this.studentId = studentId;

    }


    public String getStudentName() {

        return studentName;

    }


    public void setStudentName(String studentName) {

        this.studentName = studentName;

    }


    public int getScore() {

        return score;

    }


    public void setScore(int score) {

        this.score = score;

    }


    public String getCollegeName() {

        return collegeName;

    }


    public void setCollegeName(String collegeName) {

        this.collegeName = collegeName;

    }

}


************************************************************************************

Didn't understand the Solution?

Watch the video given below.








Comments

  1. Getting this below error while compiling it on an online Java compiler
    Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Solution.main(Solution.java:22)

    ReplyDelete
    Replies
    1. Please help. Had copy pasted the above code to check the output. Came across this error

      Delete

Post a Comment