Skip to main content

Movie_Find_Avg_Budget_By_Director

 Problem Statement

Create a class Movie with the below attributes: movieId - int director - String rating - int budget - 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 - findAvgBudgetByDirector and getMovieByRatingBudget in Solution class.

findAvgBudgetByDirector method: This method will take two input parameters - array of Movie objects and string parameter director. The method will return the average of the budget attribute from Movie objects directed by the director passed as parameter. If no movie with the given director is present in the array of movie objects, then the method should return 0.

getMovieByRatingBudget method: This method will take two int parameters rating and budget, along with the array of movie objects. The method will return the movie object, if the input parameters rating and budget, matches with the rating and budget attribute of the movie object respectively.

Also check if rating is a factor of budget (eg: 3 is a factor of 12 because 3 divides 12 without leaving a remainder). If any of the conditions are not met, then the method should return null.

Note : No two movie object would have the same value for rating and budget attributes. All the searches should be case insensitive. The budget mentioned are in crores and in INR.

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

For findAvgBudgetByDirector method - The main method should print the average budget as it is if the returned value is greater than 0, or it should print "Sorry - The given director has not yet directed any movie".

For getMovieByRatingBudget method - The main method should print the movieId of the returned movie object. If the returned value is null then it should print "Sorry - No movie is available with the specified rating and budget requirement".

Before calling these static methods in main, use Scanner object to read the values of four Movie objects referring attributes in the above mentioned attribute sequence. Next, read the value for director, rating and budget.
Input

1101
GVM
4
100
1201
Shankar
5
500
1301
Shankar
3
50
1401
GVM
5
300
GVM
5
300

Output

200
1401



code:

 

import java.util.Scanner;
public class Solution {
   public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        Movie[] movies = new Movie[4];
        for(int i=0;i<4;i++)
        {
            int movieId = sc.nextInt();sc.nextLine();
            String director = sc.nextLine();
            int rating = sc.nextInt();
            int budget = sc.nextInt();
            movies[i] = new Movie(movieId,director,rating,budget);
        }
        sc.nextLine();
        String searchDirector = sc.nextLine();
        int searchRating = sc.nextInt();
        int searchBudget = sc.nextInt();
        int avgBudget = findAvgBudgetByDirector(movies, searchDirector);
        if (avgBudget>0)
        System.out.println(avgBudget);
        else
        System.out.println("Sorry - The given director has not yet directed any movie");
        Movie resultMovie = getMovieByRatingBudget(movies, searchRating, searchBudget);
        if(resultMovie == null)
        System.out.println("Sorry - No movie is available with the specified rating and budget requirement");
        else
        System.out.println(resultMovie.getMovieId());

    }

    public static int findAvgBudgetByDirector (Movie[] movies, String searchDirector)
      {
          int avg,s=0,j=0;
          for(int i=0;i<4;i++)
          {              
              if(searchDirector.equalsIgnoreCase(movies[i].getDirector()))
              {
                  s = s+movies[i].getBudget();
                  j++;
              }
          }
              if(j>0)
              {
                  avg = s/j;
                  return avg;
              }          
              else
              return 0;
      }

    public static Movie getMovieByRatingBudget(Movie[] movies, int rating, int budget)
      {
          for(int i=0;i<4;i++)
          {
              if((rating == movies[i].getRating()) && (budget == movies[i].getBudget()) && (movies[i].getBudget() % movies[i].getRating() == 0))
              {
                  return movies[i];
              }
          }
          return null;   
          
      }
}

class Movie
{
    private int movieId,rating,budget;
    private String director;

    public int getMovieId() {
        return movieId;
    }

    public void setMovieId(int movieId) {
        this.movieId = movieId;
    }

    public int getRating() {
        return rating;
    }

    public void setRating(int rating) {
        this.rating = rating;
    }

    public int getBudget() {
        return budget;
    }

    public void setBudget(int budget) {
        this.budget = budget;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    Movie(int movieId, String director, int rating, int budget)
    {
        this.movieId = movieId;
        this.director = director;
        this.rating = rating;
        this.budget = budget;
    }
}

Comments

Popular posts from this blog

December 23 java coding question PRA solution

23 dec pra java coding question solution. Question: Create the class Student with below attributes. id-int name-String marks-int age-int write getters and setters and parametrized constructor in Student class. Create class Solution with main method. implement 2 static methods-findStudentWithMaximumAge and searchStudentById in Solution class. findStudentWithMaximumAge method:     This method will take the Array of the Students objects as input and  returns the Student object having maximum Age.   For this method,the main method should print the student object details with maximum age as it is.    searchStudentById method:     This method will take 2 input parameters.Array of the Students objects and an int value  id.and returns the Student object having the mentioned id  if found, else return null if not found.   For this method ,main method should print the details of Student objects  as it is,if the returned value is not null....

Travel_Agencies

  Problem Statement Create a class TravelAgencies with below attributes: regNo – int agencyName – String pakageType – String price – int flightFacility – boolean Write getters, setters for the above attributes . Create constructor which takes parameter in the above sequence. Create class Solution with main method. Implement two static methods – findAgencyWithHighestPackagePrice and agencyDetailsforGivenIdAndType in Solution class. findAgencyWithHighestPackagePrice method: This method will take array of TravelAgencies objects as an input parameter and return the highest package price from the given array of objects. agencyDetailsForGivenldAndType method: This method will take three input parameters -array of TravelAgencies objects, int parameter regNo and String parameter packageType. The method will return the TravelAgencies object based on below conditions. FlightFacility should be available. The input parameters(regNo and packageType) should matched with the re...

Solving TCS IPA java question.(class Player)

Question:  Create a Class Player with below attributes: id - int country - String side - String price - double Write getters, setters and parameterized constructor as required.  Create class Solution with main method.  Implement static method - searchPlayerForMatch in Solution class. This method will take a String parameter along with the other parameter as array of Player objects. The method will return array of Player where the String parameter appears in the side attribute (with case insensitive  search).   This method should be called from main method and display the id of returned objects in ascending order.  Before calling this method(searchPlayerForMatch) in the main method,  use Scanner object to read values for four Player objects referring the  attributes in the above sequence.  then, read the value for search parameter.  Next call the method searchPlayerForMatch, write the logic to sort the id in ascending order (i...