Mock answers tcs Java and awk

<-------class college------>

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;

}

}


<-----vehicle----------->


 import java.io.*;

import java.util.*;


 class Vehicle {

   

    int number;

    String name;

    double price;

    

    Vehicle(int number, String name, double price)

    {

        this.number=number;

        this.name=name;

        this.price=price;

    }

     

      public int getNumber()

      {

          return this.number;

      }

      

      public String getName()

      {

          return this.name;

      }

      

      public double getPrice()

      {

          return this.price;

      }

    }

    

    


public class Main {

    public static void main(String args[] ){

        /* Enter your code here. Read input from STDIN. Print output to STDOUT */

        Scanner sc=new Scanner(System.in);

        int n=sc.nextInt();

        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 name=sc.nextLine();

         sc.close();

         

         Vehicle r1=findVehicleWithMinimumPrice(v);

         if(r1!=null)

         {

             System.out.println("number-"+r1.getNumber());

             System.out.println("name-"+r1.getName());

             System.out.println("price-"+r1.getPrice());

         }

         else{

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

         }

         

         Vehicle r2=searchVehicleByName(v, name);

         if(r2!=null)

         {

             System.out.println("number-"+r2.getNumber());

             System.out.println("name-"+r2.getName());

             System.out.println("price-"+r2.getPrice());

         }

         else{

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

         }

    }

    

    public static Vehicle findVehicleWithMinimumPrice(Vehicle[] v){


        // Enter your code here

        Vehicle m=null;

        double minp=Double.MAX_VALUE;

        

        if(v!=null)

        {

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

            {

                if(v[i].getPrice()<minp)

                {

                    minp=v[i].getPrice();

                    m=v[i];

                }

            }

        }

        

        if(m==null)

        {

            return null;

        }

        else

        {

            return m;

        }

    }


public static Vehicle searchVehicleByName(Vehicle[] v,String name){

 

    // Enter your code here

         Vehicle m=null;

        

        if(v!=null)

        {

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

            {

                if(v[i].getName().equalsIgnoreCase(name))

                {

                    m=v[i];

                }

            }

        }

        

        if(m==null)

        {

            return null;

        }

        else

        {

            return m;

        }

 }


}


<-------lawyer--------->


import java.io.*;
import java.util.*;

public class Solution
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Lawyer[] arr = new Lawyer[n];
for(int i=0;i<n;i++){
int id = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
double salary = sc.nextDouble();
sc.nextLine();
int age = sc.nextInt();
sc.nextLine();
arr[i] = new Lawyer(id,name,salary,age);
}

String name1 = sc.nextLine();
double res1 = findAverageAgeOfLawyer(arr);
if(res1==0){
System.out.println("No Lawyer found with mentioned attribute.");
}
else {System.out.println("Average of age "+res1);}

Lawyer res2 = searchLawyerByName(arr,name1);
if(res2==null){
System.out.println("No Lawyer found with mentioned attribute.");
}else{
System.out.println("id-"+ res2.id);
System.out.println( "name-" + res2.name);
System.out.println( "salary-" + res2.salary);
System.out.println("age-" + res2.age);}
}



public static double findAverageAgeOfLawyer(Lawyer[] laws)
{
double c=0;
for(int i=0;i<laws.length;i++){
c = c+laws[i].age;
}
if(c!=0){
return c/laws.length;
}else return 0;
}
public static Lawyer searchLawyerByName(Lawyer[] laws, String lname)
{
for(int i=0;i<laws.length;i++){
if(laws[i].name.equals(lname)){
return laws[i];
}
}return null;
}

}

class Lawyer
{
int id;
String name;
double salary;
int age;

Lawyer(int id,String name,double salary,int age){
this.id=id;
this.name=name;
this.salary=salary;
this.age=age;

}

}


<----------Player---------->

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(St

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


 }



Awk code:

<----------english geography ---------->

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;

 }

 }'



<--------sangamithra------->

awk 'BEGIN{ FS="|"}

{

 if($4=="Sangamithra" && $5>=90 && $6>=90)

 {

 print $1"|"$2"|"$3"|"$4"|"($5+$6)/2;

 }

 } END{}'|sort -k5 -t'|'


<--------avg salary--------->

awk 'BEGIN{FS="-";sum=0;count=0;}

{

 if($4>0){

 sum=sum+$4;

 count=count+1;


 }


}END{

 avg=sum/(count-1);

 if(avg>0){

 print("Average Salary: "avg)

 }

 else

 print "Average Salary: 0";

}'



<--------total assets------->

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;

}'

1 comment:

  1. import java.util.*;

    public class Solution{

    public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    sc.nextLine();

    College[] colleges = 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();

    colleges[i] = new College(id,name,contactNo,address,pincode);
    }
    String input1 = sc.nextLine();


    //method calling
    College ans1 = findCollegeWithMaximumPincode(colleges);
    if(ans1 != null){
    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);
    }
    else{
    System.out.println("No College found with mentioned attribute.");
    }

    College ans2 = searchCollegeByAddress(colleges, input1);
    if(ans2 != null){
    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);
    }
    else{
    System.out.println("No College found with mentioned attribute.");
    }

    }

    public static College findCollegeWithMaximumPincode(College[] colleges){
    int max = colleges[0].pincode;
    for(int i = 0;i<colleges.length;i++){
    if(max<colleges[i].pincode){
    max = colleges[i].pincode;
    }
    }
    for(int i = 0; i<colleges.length;i++){
    if(max == colleges[i].pincode){
    return colleges[i];
    }
    }
    return null;
    }

    public static College searchCollegeByAddress(College[] colleges, String input1){
    for(int i = 0; i<colleges.length; i++){
    if(colleges[i].address.equalsIgnoreCase(input1)){
    return colleges[i];
    }
    }
    return null;
    }
    }


    class College{
    int id;
    String name;
    int contactNo;
    String address;
    int pincode;

    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;
    }
    public int getId(){
    return id;
    }
    public void setId(int id){
    this.id = id;
    }

    }

    ReplyDelete