0% found this document useful (0 votes)
137 views13 pages

Mock Solution

The document contains code to define classes for College, Vehicle, Lawyer and Player with attributes like id, name, address etc. and methods to get/set these attributes. It also contains methods to find objects with minimum/maximum attributes (like pincode, price, age) or search by specific attribute (like name, address) from arrays of these objects. Main method takes user input to create arrays of these objects and call the find/search methods to output details of the matched object.

Uploaded by

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

Mock Solution

The document contains code to define classes for College, Vehicle, Lawyer and Player with attributes like id, name, address etc. and methods to get/set these attributes. It also contains methods to find objects with minimum/maximum attributes (like pincode, price, age) or search by specific attribute (like name, address) from arrays of these objects. Main method takes user input to create arrays of these objects and call the find/search methods to output details of the matched object.

Uploaded by

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

awk 'BEGIN{FS="|"; eco=0; gco=0; IGNORECASE=1}

{
if($3 == "English")
{eco++};
if( $3 == "Geography")
{gco++};
}
END{
if(gco==0 && eco==0){
print "No records found.";}
else{
print "Count of students for English Major = "eco;
print "Count of students for Geography Major = "gco;
}
}'

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 getContactNo()
{
return cNo;
}
public void setContactNo(int cNo)
{
this.cNo = cNo;
}
public int getPincode()
{
return pcode;
}
public void setPincode(int pcode)
{
this.pcode = pcode;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return addr;
}
public void setAddress(String addr)
{
this.addr = addr;
}
}
public class Solution
{
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.nextLine();
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.getContactNo());
System.out.println("address-"+res1.getAddress());
System.out.println("pincode-"+res1.getPincode());
}
else
{
System.out.println("No College found with mentioned attribute");
}

College res2 =searchCollegeByAddress(college,searchaddress);


if(res2!=null)
{
System.out.println("id-"+res2.getId());

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

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

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].getAddress().equalsIgnoreCase(address))
{
ans=c[i];

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

awk 'BEGIN{FS=" "; IGNORECASE = 1; Total=0;}{


if($3== "sales"){
Total= Total + $4;
}
}END{
if(Total>0){
print("Total Salary = "Total)
}
else{
print("No employee found.")
}
}'

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.Scanner;
class Vehicle
{
int number;
String name;
double price;
public Vehicle(int number,String name,double price)
{
super();
this.number=number;
this.name=name;
this.price=price;
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number=number;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price=price;
}
}
public class Solution
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
Vehicle[] v=new Vehicle[n];
for(int i=0;i<v.length;i++)
{
int number=sc.nextInt();
sc.nextLine();
String name=sc.nextLine();
double price=sc.nextDouble();
sc.nextLine();
v[i]=new Vehicle(number,name,price);
}
String sname=sc.nextLine();
Vehicle vr=findVehicleWithMinimumPrice(v);
if(vr==null)
{
System.out.println("No Vehicle found with mentioned attribute");
}
else
{
System.out.println("number-"+vr.getNumber());
System.out.println("name-"+vr.getName());
System.out.println("price-"+vr.getPrice());
}
Vehicle r=searchVehicleByName(v,sname);
if(r==null)
{
System.out.println("No Vehicle found with mentioned attribute");
}
else
{
System.out.println("number-"+r.getNumber());
System.out.println("name-"+r.getName());
System.out.println("price-"+r.getPrice());
}
sc.close();
}
public static Vehicle searchVehicleByName(Vehicle[] v,String sname)
{
int pos=-1;
for(int i=0;i<v.length;i++)
{
if(v[i].getName().equalsIgnoreCase(sname))
{
pos=i;
}
}
if(pos==-1)
return null;
else
return v[pos];
}
public static Vehicle findVehicleWithMinimumPrice(Vehicle[] v)
{
Vehicle p=v[0];
double min=v[0].getPrice();
for(int i=0;i<v.length;i++)
{
if(v[i].getPrice()<min)
{
min=v[i].getPrice();
p=v[i];
}
}
if(min!=0)
return p;
else
return null;
}
}

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution


{

public static void main(String[] args)


{

Scanner sc = new Scanner(System.in);


int n = sc.nextInt();

Lawyer[] lawyers = new Lawyer[n];


for(int i=0;i<lawyers.length;i++){
int id = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
double salary = sc.nextDouble();
int age = sc.nextInt();
sc.nextLine();
lawyers[i] = new Lawyer(id, name, salary, age);
}
double avg = findAverageAgeOfLawyer(lawyers);
if(avg > 0){
System.out.println("Average of age "+ avg);
}else{
System.out.println("No Lawyer found with mentioned attribute.");
}
String nm = sc.nextLine();
Lawyer lw = searchLawyerByName(lawyers, nm);
if(lw==null){
System.out.println("No Lawyer found with mentioned attribute.");
}else{
System.out.println("id-"+ lw.getId());
System.out.println("name-"+ lw.getName());
System.out.println("salary-"+ lw.getSalary());
System.out.println("age-"+ lw.getAge());
}
}

public static double findAverageAgeOfLawyer(Lawyer[] d)


{
int count = 0;
double tot = 0, avg=0;

for(int i=0;i<d.length;i++)
{
tot = tot + d[i].getAge();
count++;
}
avg = tot/count;
if(count == 0)
{
return 0;
}
else
{
return avg;
}

}
public static Lawyer searchLawyerByName(Lawyer[] d, String name)
{
for(Lawyer l : d){
if(l.getName().equalsIgnoreCase(name)){
return l;
}
}
return null;

}
class Lawyer
{

private int id;


private String name;
private double salary;
private int age;

public int getId()


{
return id;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getAge()
{
return age;
}
public void setId(int id)
{
this.id = id;
}
public void setName(String name)
{
this.name = name;
}
public void setSalary(double salary)
{
this.salary = salary;
}
public void setAge(int age)
{
this.age = age;
}

Lawyer(int id, String name, double salary, int age){


this.id = id;
this.name = name;
this.salary = salary;
this.age = age;
}

==========================================================

awk 'BEGIN{FS="-";c=0}{
if($4 > 40000)
c=c+1;
}END{
print "Total count:",c;
}'
awk 'BEGIN{FS=",";total=0;IGNORECASE=1;}
{
if($3=="finance")
total=total+$4;
}
END{
if(total==0)
print"No Asset Found";
else
print"Total Asset Price = "total;
}'
=================================================
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {


public static void main(String[] args) throws Exception{
//Enter your Code here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Player[] p = new Player[n];
for(int i=0;i<p.length;i++)
{
int id =sc.nextInt();
int matchesPlayed = sc.nextInt();
int totalRuns = sc.nextInt();sc.nextLine();
String name = sc.nextLine();
String team = sc.nextLine();
p[i] = new Player(id, matchesPlayed, totalRuns, name, team);
}
int id1 = sc.nextInt();
Player res1 = findPlayerWithMinimumMatchesPlayed(p);
if(res1 == null)
{
System.out.println("No Player found with mentioned attribute");
}
else
{
System.out.println("id-" + res1.getId());
System.out.println("matchesPlayed-" + res1.getMatchesPlayed());
System.out.println("totalRuns-" + res1.getTotalRuns());
System.out.println("name-" + res1.getName());
System.out.println("team-" + res1.getTeam());
}
Player res2 = searchPlayerById(p, id1);
if(res2 == null)
{
System.out.println("No Player found with mentioned attribute");
}
else{
System.out.println("id-" + res2.getId());
System.out.println("matchesPlayed-" + res2.getMatchesPlayed());
System.out.println("totalRuns-" + res2.getTotalRuns());
System.out.println("name-" + res2.getName());
System.out.println("team-" + res2.getTeam());
}

public static Player findPlayerWithMinimumMatchesPlayed(Player[] players)


{

Player obj1 = players[0];


int min=players[0].getMatchesPlayed();
for(int i=0;i<players.length;i++)
{

if(min > players[i].getMatchesPlayed())


{

min=players[i].getMatchesPlayed();
obj1=players[i];
}

return obj1;

public static Player searchPlayerById(Player[] players, int id)


{
//Enter your Code here
int c = 0;
Player pObj = null;
for(int i=0;i<players.length;i++)
{
if(id == players[i].getId())
{

pObj=players[i];

}
}

return pObj;

class Player
{
//Enter your Code here
int id;
int matchesPlayed;
int totalRuns;
String name;
String team;
public int getId()
{
return id;
}
public int getMatchesPlayed()
{
return matchesPlayed;
}
public int getTotalRuns()
{
return totalRuns;
}
public String getName()
{
return name;
}
public String getTeam()
{
return team;
}
public void setId(int id)
{
this.id = id;
}
public void setMatchesPlayed(int matchesPlayed)
{
this.matchesPlayed = matchesPlayed;
}
public void setTotalRuns(int totalRuns)
{
this.totalRuns = totalRuns;
}
public void setName(String name)
{
this.name= name;
}
public void setTeam(String team)
{
this.team = team;
}
Player(int id, int matchesPlayed, int totalRuns, String name, String team)
{
this.id = id;
this.matchesPlayed = matchesPlayed;
this.totalRuns = totalRuns;
this.name = name;
this.team = team;
}
}

You might also like