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

Popular posts from this blog

Travel_Agencies

  Problem Statement Create a class TravelAgencies with below attributes: regNo – int agencyName – String pakageType – String price – int flightFacility – boolean Write getters, setters for the above attributes . Create constructor which takes parameter in the above sequence. Create class Solution with main method. Implement two static methods – findAgencyWithHighestPackagePrice and agencyDetailsforGivenIdAndType in Solution class. findAgencyWithHighestPackagePrice method: This method will take array of TravelAgencies objects as an input parameter and return the highest package price from the given array of objects. agencyDetailsForGivenldAndType method: This method will take three input parameters -array of TravelAgencies objects, int parameter regNo and String parameter packageType. The method will return the TravelAgencies object based on below conditions. FlightFacility should be available. The input parameters(regNo and packageType) should matched with the regNo

December 23 java coding question PRA solution

23 dec pra java coding question solution. Question: Create the class Student with below attributes. id-int name-String marks-int age-int write getters and setters and parametrized constructor in Student class. Create class Solution with main method. implement 2 static methods-findStudentWithMaximumAge and searchStudentById in Solution class. findStudentWithMaximumAge method:     This method will take the Array of the Students objects as input and  returns the Student object having maximum Age.   For this method,the main method should print the student object details with maximum age as it is.    searchStudentById method:     This method will take 2 input parameters.Array of the Students objects and an int value  id.and returns the Student object having the mentioned id  if found, else return null if not found.   For this method ,main method should print the details of Student objects  as it is,if the returned value is not null. or it should print  "No Student found with mentioned

TCS XPLORE CAMERA PROCTORED ASSESSMENT EXAM JAVA CODING SOLUTION 10 MAY,2021. FOOTWEAR PROBLEM.

 create a class Footwear which consists of the below attributes.   footwearId=int   footwearName=String   footwearType=String   price =int the above attributes should be private. write getter and setter and parametrised constructor as required. create the class footwearProgrammm with the main method. implement the 2 static methods.getCountByType and getSecondHighestPriceByBrand in the Solution class. getCountByType method:    this method will take two input parameters. array of the Footwear objects and string parameter footwear type. this method will return the count of the footwears from array of the footwear objects for the given type of footwear. if no footwear with the given footwear type is found in the array of footwear abjects,then the method should return 0. getSecondHighestPriceByBrand method:   this method will take 2 input parameters-array of footwear objects and string parameter inputFootwearName.the method will return the second highest footwear objects based on the price