0% found this document useful (0 votes)
5 views4 pages

File 1

Uploaded by

Shivani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

File 1

Uploaded by

Shivani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

-------------------Day 4-----------------

-----------Oops with array/Non-Primitive array---------


------------Non-Primitive Array---------------
import java.util.*;

class College
{
private int id,cNo,pcode;
private String name,addr;
public College(int id, int cNo, int pcode, String name, String addr) {
super();
this.id = id;
this.cNo = cNo;
this.pcode = pcode;
this.name = name;
this.addr = addr;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getcNo() {
return cNo;
}
public void setcNo(int cNo) {
this.cNo = cNo;
}
public int getPcode() {
return pcode;
}
public void setPcode(int pcode) {
this.pcode = pcode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}

public class First


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
College college[] = new College[n];
for(int i=0;i<college.length;i++)
{
int id = sc.nextInt(); sc.nextLine();
String name = sc.nextLine();
int contactNo = sc.nextInt(); sc.nextLine();
String address = sc.next();
int pincode = sc.nextInt();
college[i] = new College(id, contactNo, pincode, name, address);
}
for(int i=0;i<n;i++)
{
System.out.println("ID: "+college[i].getId());
System.out.println("Contact: "+college[i].getcNo());
System.out.println("Pincode: "+college[i].getPcode());
System.out.println("Name: "+college[i].getName());
System.out.println("Address: "+college[i].getAddr());
}

}
}

---------------------Array With Method------------------


import java.util.*;

class College
{
private int id,cNo,pcode;
private String name,addr;
public College(int id, int cNo, int pcode, String name, String addr) {
super();
this.id = id;
this.cNo = cNo;
this.pcode = pcode;
this.name = name;
this.addr = addr;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getcNo() {
return cNo;
}
public void setcNo(int cNo) {
this.cNo = cNo;
}
public int getPcode() {
return pcode;
}
public void setPcode(int pcode) {
this.pcode = pcode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}

public class First


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
College college[] = new College[n];
for(int i=0;i<college.length;i++)
{
int id = sc.nextInt(); sc.nextLine();
String name = sc.nextLine();
int contactNo = sc.nextInt(); sc.nextLine();
String address = sc.next();
int pincode = sc.nextInt();
college[i] = new College(id, contactNo, pincode, name, address);
}
sc.nextLine();
String searchaddress = sc.nextLine();
College res1 = findCollegeWithMaximumPincode(college);
if(res1!=null)
{
System.out.println("Id: "+res1.getId());
System.out.println("Name: "+res1.getName());
System.out.println("ContactNo.: "+res1.getcNo());
System.out.println("Address: "+res1.getAddr());
System.out.println("PinCode: "+res1.getPcode());
}
else
System.out.println("No College");

College res2 = searchCollegeByAddress(college,searchaddress);


if(res2==null)
{
System.out.println("No College Found!!!!");
}
else {
System.out.println("Id: "+res2.getId());
System.out.println("Name: "+res2.getName());
System.out.println("ContactNo.: "+res2.getcNo());
System.out.println("Address: "+res2.getAddr());
System.out.println("PinCode: "+res2.getPcode());
}

} // Main method Ends

public static College findCollegeWithMaximumPincode(College col[])


{
int max=0;
College result=null;
for(int i=0;i<col.length;i++)
{
if(col[i].getPcode()>max)
{ result = col[i];
max = col[i].getPcode(); }
}

if(result != null)
return result;
else
return null;
}

public static College searchCollegeByAddress(College c[], String address)


{
College ans = null;
for(int i=0;i<c.length;i++)
{
if(c[i].getAddr().equalsIgnoreCase(address))
ans=c[i]; }

if(ans != null)
return ans;
else
return null;
}

You might also like