top of page

OPA May8 Java

Create a class Player with below attributes:
playerId - int
skill - String
level - String 
points - int

 

Write getters, setters and parameterized constructor in the above mentioned attribute sequence as required. 

 

Create class Solution with the main method. 

Implement two static methods - findPointsForGivenSkill and getPlayerBasedOnLevel in Solution class.

 

findPointsForGivenSkill method:
This method will take two input parameters - array of Player objects and string parameter skill. 
The method will return the sum of the points attribute from player objects for the skill passed as parameter. 
If no player with the given skill is present in the array of player objects, then the method should return 0. 

 

getPlayerBasedOnLevel method:
This method will take two String parameters level and skill, along with the array of Player objects.
The method will return the player object, if the input String parameters matches with the level and skill attribute of the player object and its point attribute is greater than or equal to 20. If any of the conditions are not met, then the method should return null.

 

Note : No two player object would have the same value for skill and level attribute.
       All player object would have the points greater than 0.
       All the searches should be case insensitive.


These above mentioned static methods should be called from the main method. 

For findPointsForGivenSkill method - The main method should print the points as it is if the returned price is greater than 0, or it
should print "The given Skill is not available".

For getPlayerBasedOnLevel method - The main method should print the playerId of the returned player object. If the returned value is null
then it should print "No player is available with specified level, skill and eligibility points".

 

Before calling these static methods in main, use Scanner object to read the values of four Player objects referring attributes in the above mentioned attribute sequence. 
Next, read the value for skill and level.
Please consider the skill value read above as skill parameter for both the static methods.


Consider below sample input and output:
Input:

101
cricket
Basic
20
102
cricket
Intermediate
25
111
football
Intermediate
50
113
baseball
Advanced
100
cricket
Intermediate

 

Output:
45
102

​

Solutions:

​

​

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)
    {
        //code to read values 
        //code to call required method
        //code to display the result
        Scanner sc = new Scanner(System.in);
        Player players[] = new Player[4];
        for(int i = 0; i<4; i++) {
            int playerId = sc.nextInt();
            sc.nextLine();
            String skill = sc.nextLine();
            String level = sc.nextLine();
            int points = sc.nextInt();
            sc.nextLine();
            players[i] = new Player(playerId, skill, level, points);
        }
        String newskill = sc.nextLine();
        String newlevel = sc.nextLine();
        int totalPoints = findPointsForGivenSkill(players, newskill);
        Player chosenOne = getPlayerBasedOnLevel(players, newskill, newlevel);
        if(totalPoints>0)
            System.out.println(totalPoints);
        else
            System.out.println("The given Skill is not available");
        if(chosenOne == null)
            System.out.println("No player is available with specified level, skill and eligibility points");
        else
            System.out.println(chosenOne.getPlayerId());
    }

    public static int findPointsForGivenSkill(Player[] players, String skill)
      {
          //method logic
          int points = 0;
          for(int i = 0; i<4; i++) {
              if(players[i].getSkill().equalsIgnoreCase(skill))
                points = points + players[i].getPoints();
          }
          return points;
      }

    public static Player getPlayerBasedOnLevel(Player[] players, String skill, String level)
      {
          //method logic
          for(int i=0;i<4; i++) {
              if(players[i].getSkill().equalsIgnoreCase(skill) && players[i].getLevel().equalsIgnoreCase(level)){
                  if(players[i].getPoints() >= 20)
                    return players[i];
              }
          }
          return null;
      }
}

class Player
{
    //code to build Player class`
    private int playerId;
    private String skill;
    private String level;
    private int points;
    public int getPlayerId() {
        return playerId;
    }
    public void setPlayerId(int playerId) {
        this.playerId = playerId;
    }
    public String getSkill() {
        return skill;
    }
    public void setSkill(String skill) {
        this.skill = skill;
    }
    public String getLevel() {
        return level;
    }
    public void setLevel(String level) {
        this.level = level;
    }
    public int getPoints() {
        return points;
    }
    public void setPoints(int points) {
        this.points = points;
    }
    public Player(int playerId, String skill, String level, int points) {
        this.playerId = playerId;
        this.skill = skill;
        this.level = level;
        this.points = points;
    }
}

Know your TCS: Services

OPA May22 Python

Create a class Doctor with below attributes:

doctorId – Numeric type

doctorName – String type

specialization – String type

consultationFee – Numeric type

 

doctorId represents the unique identification number of a Doctor object.

doctorName represents the name of the doctor.

specialization represents the doctor specialization and consultationFee represents the doctors fee.

 

Define the __init__ method to initialize the attributes in the above sequence.

 

Create a class Hospital with below attribute:

doctorDB   - is of type dictionary with Doctor objects [ Serial number of a Doctor in the Hospital and the respective Doctor object as a key : value pair ]

 

Define the __init__ method to initialize the dictionary attribute in the Hospital class . It initializes the dictionary of Doctor objects in the Hospital class with the dictionary supplied from main program while creating the Hospital object.

 

The Dictionary( with Doctor serial number and the respective Doctor object as key : value pair ) is created and filled in main program by adding each Doctor object. This Doctor object is created with the input data related to a respective Doctor object . This dictionary after filling to be passed as an argument to the Hospital constructor and this will be initialized to doctorDB dictionary with in the Hospital class.

 

Define two methods – searchByDoctorName and calculateConsultationFeeBySpecialization in Hospital class.

 

searchByDoctorName:

This method will take Doctor name as a parameter, find the respective Doctor object based on the doctor name given and return to main function with a list of Doctor objects, whose name matches with the given name, supplied as an argument.

 

If there is no Doctor found with the given name then return None to main program and display the message "No Doctor Exists with the given DoctorName" in the main function.

 

Hint:

a. Use the dictionary, doctorDB in Hospital object to find out the Doctor object(s) based on the given Doctor name.

b. Display the Doctor object /list of doctor objects (returned by this function) in the main function - Refer sample testcase below for more details

 

calculateConsultationFeeBySpecialization:

This method will take a Doctor specialization as parameter and return the total consultationFee of all the Doctors ,whose specialization is same as supplied as an argument from the main program. 

If there is no Doctor found with the given specialization then return 0 to main program and display the message "No Doctor with the given specialization" in the main function, excluding the double quotes

 

These two methods(described above) should be called from the main function / program and in the above order.

 

Hint

a. Use the dictionary, doctorDB in hospital to get the consultation fee of each of Doctor (Doctor object in the Hospital ) for the given specialization supplied as argument .

b.Display the Total Fee in the  in the main function  - Refer sample testcase below for more details

 

Instructions to implement Main function:

a. You would required to write the main program completely, hence please follow the below instructions for the same.

b. You would required to write the main program, inlign to the sample input data mentioned below and to read the same  .

c. Create the respective objects(Doctor and Hospital) 

  i.Create a Doctor object after reading the data related to one Doctor and add the doctor object to Doctors dictionary with Serial number and Doctor object as key:value pair.

  ii.Repeat above point for the total number of Doctor objects , read in the first line of input data.

  iii. Create a Hospital Object with the dictionary of Doctor objects, created in the previous step (c.i)

d. Call the methods ( searchByDoctorName and calculateConsultationFeeBySpecialization) mentioned above in the same order , they appear in the question text from main function .

e. Display the data returned by the functions , in the main program as per the format mentioned in the sample output.

    If no Doctor exists with the given name(return value None from the respective function )then display the message "No Doctor Exists with the given DoctorName" in the main function, excluding the double quotes.
    If no Doctor exists with the given specialization (return value 0 from the respective function )then display the message "No Doctor with the given specialization" in the main function, excluding the double quotes.

 

Sample Input (below) data description:

1.The 1st input taken in the main section is the number of Doctor objects to be created and added to the dictionary of Doctors in the Main program

2.The next set of inputs are the doctorId, doctorName, specialization and consultationFee of first Doctor

3. For each Doctor object repeat point#2  and this point is repeated for number of Doctor objects given in the first line of input

4.The last but one line of input refers the doctorName to be searched  ie an argument for searchByDoctorName function

5. Last line of input represents the specialization, supplied as an argument to calculateConsultationFeeBySpecialization function, to get the total consultationFee of all the Doctors for a given specialization.

 

Sample Input :

5

90901

GovindRajulu

ENT

500

90902

Madhuri

Dermatologist

700

90903

Divya

Gynaecologist

600

90904

Suryam

Cardiologist

900

90905

Madhuri

Cardiologist

1000

Madhuri

Cardiologist

 

Output :

90902

Madhuri

Dermatologist

700

90905

Madhuri

Cardiologist

1000

1900

​

​

Solutions:

​

class Doctor:
    def __init__(self,docid,docname,specs,fee):
        self.docid=docid
        self.docname=docname
        self.specs=specs
        self.fee=fee
class Hospital:
    def __init__(self,doc):
        self.doctorDB=doc

    def searchByDoctorName(self,name):
        l=[]
        for i in doctorDB.keys():
            if doctorDB[i].docname==name:
                l=l+[doctorDB[i]]
        return l
    def calculateConsultationFeeBySpecialization(self,spec):
        s=0
        for i in doctorDB.keys():
            if doctorDB[i].specs==spec:
                s=s+doctorDB[i].fee
        return s
if __name__ == '__main__':
    n=int(input())
    lst=[]
    for i in range(n):
        did=int(input())
        name=input()
        spec=input()
        fee=int(input())
        d=Doctor(did,name,spec,fee)
        lst=lst+[d]
    searchname=input()
    searchspec=input()
    doctorDB={}
    c=1
    for i in lst:
        doctorDB[c]=i
        c=c+1
    h=Hospital(doctorDB)
    rname=h.searchByDoctorName(searchname)
    rspec=h.calculateConsultationFeeBySpecialization(searchspec)
    if len(rname)==0:
        print('No Doctor Exists with the given DoctorName')
    else:
        for i in rname:
            print(i.docid)
            print(i.docname)
            print(i.specs)
            print(i.fee)
    if rspec==0:
        print('No Doctor with given sepcialization')
    else:
        print(rspec)

©2018 by The real one. Proudly created with Wix.com

bottom of page