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

How to Introduce Yourself to project team and client when you join a new project as fresher in service based company ?

Question:     How to Introduce Yourself to project team /client  when you join a new project as fresher ? Answer:  Hii Everyone,    Today I will be talking about how we can introduce ourselves to a team  when we join a new project in any servicebased company as a fresher.     After I completed my Engineering ,I got placed into TCS.I got allocated  into a project.I introduced myself to a team.So I will be telling about how   exactly I introduced myself to the team.The impressive introduction is  necessary if you want team members to treat you better in this work from  home period.If you fail to impress team members ,If your introduction is not  impressive ,then probably your team members will ignore you.    Before I tell you about how exactly I introduced myself,I would like  to talk about the points that you should remember before giving your  introduction. .start your introduction with Hii Ev...

Automating Infrastructure on Google Cloud with Terraform: Challenge Lab

As more and more organizations move to the cloud, the need for powerful infrastructure automation tools has never been greater. That's where Terraform comes in, a cutting-edge tool that allows you to automate the deployment, management, and scaling of your infrastructure with ease. With Terraform, you can define your entire infrastructure as code, making it easy to maintain, version, and reproduce. Whether you're deploying infrastructure to AWS, Google Cloud, or another cloud provider, Terraform provides a powerful and flexible platform for automating your workflows. In this blog post, we'll take a look at how to automate infrastructure on google cloud with terraform. Task A. Create the configuration files  Task B. Import infrastructure  Task C.Configure a remote backend  Task D. Modify and update infrastructure  Task E.Destroy resources  Task F. Use a module from the Registry  Task G. Configure a firewall ......................................................

Solving the TCS CPA Java Question(December,2021) asked by Subscriber (Sim class).

 Question:      Create a class Sim with below attributes: simId - int customerName - String balance - double ratePerSecond - double circle - String Write getters, setters and parameterized constructor as required.  Public class Solution is already created with main method. Code inside main method should not be altered else your solution might be  scored as zero.You may copy the code from main method in eclipse to  verify your implementation.  Implement static method - transferCustomerCircle in Solution class. This method will take first parameter as array of Sim class objects,  second parameter as circle to be transferred (which is String parameter  circle1) and third parameter as new circle (which is String parameter  circle2). Method will transfer the customer to new circle (circle2), where the  circle attribute would match second parameter (circle1).  Method will return array of Sim objects for which circle is transferr...