Skip to main content

pra mock java solution .

 

Question:

Create a class College with the below attributes.

 id-int 

 name -String 

 contactNo-int

address-String 

pinCode-int


Write the getters and setters and parametrized constructor in the above 

mentioned sequence as required.


Create the class Solution with the main method.

Implement the two static methods:

 1.findCollegeWithMaximumPincode 

 2.searchCollegeByAddress


findCollegeWithMaximumPincode method:

   Create the Static method in the Solution Class.

 This method will take array of the College objects and return the 

College object having maximum pincode if found else return null if not 

found.

   for this method ,main method will print College object with maximum

pincode if the returned value is not null.if the returned value is null ,

then the main method will print "No college found with mentioned attribute".



searchCollegeByAddress method:

   Create the Static method in the Solution Class.

This method will take array of College objects as input and address as 

input and return College object having the mentioned address if found 

else return null if not found.

   for this method main method will print College object details as it is ,

 if the returned value is not null.if the returned value is null then ,

main method will print "No college found with mentioned attribute".

 

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

input 1:

4

109 

ACT

2500256

mumbai

695001

107

MCE

2500254

malapuram

612354

113

CTE

2500252

chennai

623145

102

SCT

2500255

AP

523641

AP


OUTPUT1:

id-109

name-ACT

contactNo-2500256

address-mumbai

pincode-695001

id-102

name-SCT

contactNo-2500255

address-AP

pincode-523641




INPUT2:

4

111

MJT

2500251

Calicut

401235

105

MET

2500256

kochi

668745

115

IIT

2500262

banglore

569874

110

ACT

2500263

delhi

687945

delhi


output2:

id-110

name-ACT

contactNo-2500263

address-delhi

pincode-687945

id-110

name-ACT

contactNo-2500263

address-delhi

pincode-687945


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


code :




import java.util.Scanner;


public class Solution {

    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);

        College[] colleges=new College[4];

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

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

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

            String b=sc.nextLine();

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

            String d=sc.nextLine();

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


            colleges[i]=new College(a,b,c,d,e);


        }

        String  input=sc.nextLine();


       College ans1= findCollegeWithMaximumPincode(colleges);

      if(ans1==null)

      {

          System.out.println("No college found with mentioned attribute");

      }

      else

      {

          System.out.println("id-"+ans1.id);

          System.out.println("name-"+ans1.name);

          System.out.println("contactNo-"+ans1.contactNo);

          System.out.println("address-"+ans1.address);

          System.out.println("pincode-"+ans1.pincode);

      }



       College ans2= searchCollegeByAddress(colleges,input);

       if(ans2==null)

       {

           System.out.println("No college found with mentioned attribute");

       }

       else

       {

           System.out.println("id-"+ans2.id);

           System.out.println("name-"+ans2.name);

           System.out.println("contactNo-"+ans2.contactNo);

           System.out.println("address-"+ans2.address);

           System.out.println("pincode-"+ans2.pincode);

       }

    }

   public static College findCollegeWithMaximumPincode(College[] colleges)

    {

        int maxx=colleges[0].pincode;

        for (int i = 1; i <colleges.length ; i++) {

            if(colleges[i].pincode>maxx)

            {

                maxx=colleges[i].pincode;

            }

        }

        for (int i = 0; i <colleges.length ; i++) {

            if(colleges[i].pincode==maxx)

            {

                return colleges[i];

            }

        }


        return null;

    }


   public static College searchCollegeByAddress(College[] colleges,String input)

    {

        for (int i = 0; i < colleges.length; i++) {

            if(colleges[i].address.equalsIgnoreCase(input))

            {

                return colleges[i];

            }

        }


        return null;

    }



}



class College{

    int id;

    String name;

    int contactNo;

    String address;

    int pincode;


    //constructor



    public College(int id, String name, int contactNo, String address, int pincode) {

        this.id = id;

        this.name = name;

        this.contactNo = contactNo;

        this.address = address;

        this.pincode = pincode;

    }


    //getter and setter


    public int getId() {

        return id;

    }


    public void setId(int id) {

        this.id = id;

    }


    public String getName() {

        return name;

    }


    public void setName(String name) {

        this.name = name;

    }


    public int getContactNo() {

        return contactNo;

    }


    public void setContactNo(int contactNo) {

        this.contactNo = contactNo;

    }


    public String getAddress() {

        return address;

    }


    public void setAddress(String address) {

        this.address = address;

    }


    public int getPincode() {

        return pincode;

    }


    public void setPincode(int pincode) {

        this.pincode = pincode;

    }

}


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

Comments

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