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 ......................................................

TCS IRA 13 DECEMBER JAVA CODING QUESTION SOLUTION.

Create a class newspaper with below attributes. regNo-int name-String publicationYear-int price-int Write parametrised constructor as required. Create class Solution with main method. Implement 2 static methods findTotalPriceByPublicationYear and  searchNewspaperByName in solution class. findTotalPriceByPublicationYear method:    This method will take array of the Newspaper objects and int parameter type.This method will return the total price of newspapers from array of  Newspaper objects,If the publication year attribute matches with the int  parameter passed .If no Newspaper with the given publication year is present  in the array ,then the method should return 0. The main method should print total price if the returned value is greater  than 0.Else it should print "No Newspaper found with the mentioned attribute". searchNewspaperByName method :    This method will take the array of the Newspaper objects and the String  parameter type...