TCS 5th June OPA Solution

 Welcome to always code

This is the Solution for the Java Online Procured Assessment Conducted by TCS. Yesterday TCS conducted another OPA on Java

The questions in TCS OPA are all similar kinds of questions. If you know basic object-oriented programming, it is easy to clear the OPA or solve the questions.

The question that was given on yesterday's OPA was Movie Management.
 The question is simple
 They asked to write our the main function and implement two functions net was the first function is
1)findAvgBudgetByDirector()
2)getMovieByRatingBudget()
 and we need to create another class Movie to initialize all the attributes of Movie attributes.



Solution.java:

//create main class
import java.io.*;
import java.util.*;
class Solution{
    public static void main(String args[]){
        //take input according to test case
        Scanner s= new Scanner(System.in);   //to take input
        Movie[] m=new Movie[4];  //movie array to store movie objects
        for(int i=0;i<4;i++){
            int mid=s.nextInt();
            String dir=s.nextLine();
            int rating=s.nextInt();
            int budget=s.nextInt();
            m[i]=new Movie(mid,dir,rating,budget);  //create movie object for each movie input and initailize
        }
        String reqname=s.nextLine();
        int reqrating=s.nextInt();
        int reqbudget=s.nextInt();
        //create methods
        int o1=findAvgBudgetByDirector(m,reqname);
        if(o1==0){
            System.out.println("Sorry- No director");
        }
        else{
            System.out.println(o1);
        }
        //create 2nd method
        Movie o2=getMovieByRatingBudget(m,reqrating,reqbudget);
        if(o2==null){
            System.out.println("Sorry - No Movie");
        }
        else{
            System.out.println(o2.mid);
        }


    }
    public static int findAvgBudgetByDirector(Movie[] m,String reqname){
        //this method need to return the avg budget of the director else return 0
        int s=0,j=0;
        for(int i=0;i<4;i++){
            if((m[i].dir).equalsIgnoreCase(reqname)){
                s=s+m[i].budget;
                j++;
            }
        }
        if(j>0){return s/j;}
        else{
            return 0;
        }
    }
    public static Movie getMovieByRatingBudget(Movie[] m,int rating,int budget){
        //this method needs to return the mvieid if rating and budget equals to particular movie and also check if budget is a factor of rating
        Movie x=new Movie();  //using default constructor
        for(int i=0;i<4;i++){
            if(m[i].rating==rating && m[i].budget==budget && m[i].budget%m[i].rating==0){
                x.mid=m[i].mid;   //storing movieid in x object
                return x;
            }
        }
        return null;
    }
}
//create movie class
class Movie{
    int mid,rating,budget;
    String dir;
    Movie(){
        //default constructor
    }
    Movie(int mid,String dir,int rating,int budget){
        this.mid=mid;
        this.dir=dir;
        this.rating=rating;
        this.budget=budget;
    }
}


Input:
1101
rathan
5
100
1201
puri
4
700
1301
rathan
4
300
1401
puri
5
300
rathan
5
300

O/p:
200
1401


given name is rathan and rathan has movies 1101 and 1301 so average budjet of two movies is 100+300/2==200;

Second method given 5 and 300 as parameters so and 300 is also factor of 5 so the required movie id is 1401.

You can also implement getter and setter methods in the Movie class.
If you also want the implementation using getter and setter methods comment.

And also if you have any doubts feel free to comment.

Thank you

Subscribe to Always Code for more Coding Questions.
I also planning to upload one day builds so that everyone can able to build projects within one day.

Post a Comment

Previous Post Next Post