You are on page 1of 8

Create a class College with below attributes

 id-int
 nam-string
 contactNo-int
 address-String
 pincode-int

Write getters, setters and parameterized constructor in the above mentioned


attribute sequence as required.
Create class Solution with main method

Implement two static methods-


 findCollegeWithMaximumPincode
 searchCollegeByAddress

1. findCollegeWithMaximumPincode
create a static method
findCollegeWithMaximumPincode in the Solution class. This method will take array
of College objects and returns the College object having the maximum pincode if
found else return null if not found.

2. searchCollegeByAddress
create a static method
searchCollegeByAddress in the Solution class. This method will take array of
College objects and returns the College object having the mentioned Address if
found else return null if not found.

This method should be called from main method.


Write code to perform following tasks:
1. Take necessary input variable and call findCollegeWithMaximumPincode. For this
method - The main method should print the College object with maximum of
mentioned attribute as if it is returned value is not null, or it should print "No College
found with mentioned attribute".
2. Take necessary input variable and call searchCollegeByAddress. For this method
- The main method should print the College object details as if it is returned value is
not null, or it should print "No College found with mentioned attribute".

Consider below sample input and output to test your code.

Input 1

4
109
ACT
2500256
mumbai
695001
107
MCE
2500254
malapuram
612354
113
CTE
2500252
chennai
623145
103
SCT
2500255
AP
523641
AP
Output 1
id-109
name-ACT
contactNo-2500256
address-mumbai
pincode-695001
id-102
name-sct
contact-2500255
address-AP
pincode-523641

Input 2
4
111
MJT
2500251
Calicut
401235
105
MET
2500256
kochi
668745
115
IIT
2500262
banglore
569874
110
ACT
2500263
delhi
687945
delhi

Output 2
id-110
name-ACT
contactNo-2500263
address-delhi
pincode-687945
id-110
name-ACT
contactNo-2500263
address-delhi
pincode-687945
PROGRAM
import java.util.*;
class College{
int id;
String name;
int contactNo;
String address;
int pincode;

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;
}

public int getId()


{
return this.id;
}

public String getName()


{
return this.name;
}

public int getContactNo()


{
return this.contactNo;
}

public String getAddress()


{
return this.address;
}

public int getPincode()


{
return this.pincode;
}
}

public class Solution{


public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
College []c=new College[n];
for(int i=0;i<n;i++)
{
int id=sc.nextInt();
sc.nextLine();
String name=sc.nextLine();
int contactNo=sc.nextInt();
sc.nextLine();
String address=sc.nextLine();
int pincode=sc.nextInt();
sc.nextLine();

c[i]=new College(id,name,contactNo,address,pincode);
}

String address=sc.nextLine();
sc.close();

College r1=findCollegeWithMaximumPincode(c);
if(r1!=null)
{
System.out.println("id-"+r1.getId());
System.out.println("name-"+r1.getName());
System.out.println("contactNo-"+r1.getContactNo());
System.out.println("address-"+r1.getAddress());
System.out.println("pincode-"+r1.getPincode());
}
else
{
System.out.println("No College found with mentioned
attribute.");
}

College r2=searchCollegeByAddress(c,address);
if(r2!=null)
{
System.out.println("id-"+r2.getId());
System.out.println("name-"+r2.getName());

System.out.println("contactNo-"+r2.getContactNo());
System.out.println("address-"+r2.getAddress());
System.out.println("pincode-"+r2.getPincode());
}
else
{
System.out.println("No College found with
mentioned attribute.");
}

}
public static College
findCollegeWithMaximumPincode(College []c)
{
College m=null;
int maxpin=0;

if(c!=null) {
for (int i = 0; i < c.length; i++) {
if (c[i].getPincode() > maxpin) {
maxpin = c[i].getPincode();
m = c[i];
}
}
}

if(m==null)
{
return null;
}
else
{
return m;
}
}

public static College searchCollegeByAddress(College []c,


String address)
{
College m=null;

if(c!=null && address.length()!=0) {


for (int i = 0; i < c.length; i++) {
if
(c[i].getAddress().equalsIgnoreCase(address)) {
m = c[i];
}
}
}

if(m==null)
{
return null;
}
else
{
return m;
}
}

You might also like