TCS IPA 14april java 35marks

Question: input: 1 velammal chennai 3 true 2 CIT chennai 4.3 true 3 IIT madurai 5 false 4 NIT chennai 3.8 true chennai 3 output: 3 Answer: import java.util.Scanner; public class Main {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         University[] uni = new University[4];         for (int i = 0; i < uni.length; i++) {             int universityId = sc.nextInt();             sc.nextLine();             String universityName = sc.nextLine();             String location = sc.nextLine();             double rating = sc.nextDouble();             sc.nextLine();             boolean innovationLab = sc.nextBoolean();             sc.nextLine();...

TCS IPA Practice Question 1

Question:

TO Check the ipa practice question page will be shown 


Answer:

import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int objectSize = sc.nextInt();
player []arr = new player[objectSize];
for(int i =0;i<objectSize;i++)
{
    int pId = sc.nextInt();sc.nextLine();
    String pName = sc.nextLine();
    int run = sc.nextInt();sc.nextLine();
    String pType = sc.nextLine();
    String PmatchType = sc.nextLine();
    
    arr[i] =new player(pId, pName, run, pType, PmatchType);
}
String pType = sc.nextLine();
String PmatchType = sc.nextLine();
int output1 = findPlayerWithLowestRuns(arr, pType);
player [] output2 = findPlayerByMatchType(arr, PmatchType);
if(output1>0)
System.out.println(output1);
else
System.out.println("No Such player");
if(output2!=null)
{
    for(int i = 0;i<output2.length;i++)
    {
        System.out.println(output2[i].getplayerId());
    }
}
else
{
    System.out.println("No player given match type");
}
}
public static int findPlayerWithLowestRuns(player []arr, String playerType)
{
    int lruns = 0;
    boolean cond = false;
    for(int i =0;i<arr.length;i++)
    {
        if(playerType.equals(arr[i].getplayerType()))
        {
            cond = true;
            lruns = arr[i].getruns();
            if(arr[i].getruns()<lruns)
            lruns = arr[i].getruns();
        }
    }
    if(cond==true)
    return lruns;
    else
    return 0;
}
public static player[] findPlayerByMatchType(player[] arr, String matchType)
{
    int count = 0;
    player temp;
    int out = 0;
    boolean mcond = false;
    for(int i = 0;i<arr.length;i++)
    {
        if(matchType.equals(arr[i].getmatchType()))
        {
            mcond = true;
            count++;
        }
    }
    player [] arr1 = new player[count];
    for(int i = 0;i<arr.length;i++)
    {
        if(matchType.equals(arr[i].getmatchType()))
        {
            arr1[out++] = arr[i];
        }
    }
    
    for(int i =0;i<count;i++)
    {
        for(int j=i+1;j<count;j++)
        {
            if(arr1[i].getplayerId()<arr1[j].getplayerId())
            {
                temp = arr1[i];
                arr1[i] = arr1[j];
                arr1[j] = temp;
            }
        }
    }
    if(mcond==true)
    {
        return arr1;
    }
    else
    return null;
}
}
class player
{
    private int playerId;
    private String playerName;
    private int runs;
    private String playerType;
    private String matchType;
    
    player(int playerId, String playerName, int runs, String playerType, String matchType)
    {
        this.playerId = playerId;
        this.playerName = playerName;
        this.runs = runs;
        this.playerType = playerType;
        this.matchType = matchType;
    }
    
    public int getplayerId()
    {
        return playerId;
    }
    public void setplayerId(int playerId)
    {
        this.playerId = playerId;
    }
    public  String getplayerName()
    {
        return playerName;
    }
    public  void setplayerName(String playerName)
    {
        this.playerName = playerName;
    }
    public  int getruns()
    {
        return runs;
    }
    public  void setruns(int runs)
    {
        this.runs = runs;
    }
    public  String getplayerType()
    {
        return playerType;
    }
    public  void setplayerType(String playerType)
    {
        this.playerType = playerType;
    }
    public  String getmatchType()
    {
        return matchType;
    }
    public  void setmatchType(String matchType)
    {
        this.matchType = matchType;
    }
}

Comments

Popular posts from this blog

TCS IPA 15 mark python practice Question

TCS IPA 15 mark java Question asked two times 29jan and 18 march

TCS IPA practice queston 2