Skip to main content

28 february IRA JAVA solution.

Question: 


Create a class Music with below attributes:

playListNo - int

type -String

count - int

duration(minutes) -double.


The above attributes should be private.Write Getter and Setter and 

parametrized constructor as required.

Create class Solution with main method.

****************************************************************************

Implement two static methods: findAvgOfCount and sortTypeByDuration in 

Solution Class.


findAvgOfCount Method:

   This method will take an array of Music objects and an int value as input

parameters.The method will find out Average (as int) of count for those 

objects whose count is more than the given Count(int parameter passed).This 

method will return average if found.If there is no Type that matches then 

the method should return 0.


for this method- main method should print average ,if the returned value

is not 0.If the returned value is 0 then main method should print 

"No playlist found".





sortTypeByDuration method:

   This method will take an array of Music objects and an int value as input 

parameters.This method should return an array of Music objects in an 

ascending order of their duration which are more than the given duration

(int parameter passed) value.

   If there are no such objects ,then the method should return null.


for this method-The main method should print the type from the returned

Music object array if the returned value is not null.If the returned value 

is null then the main method should print "No playlist found with 

mentioned attribute".

****************************************************************************


Consider the below input and output:


input1:


111

WorkOut

10

15.2

321

Dance Party

20

55.500

721

Childhood Jams

6

50.60

521

Chill

30

78.89

15

20


output1:

25

Childhood Jams

Dance Party

Chill




input2:

111

Oldies but Goodies

17

55

321

Guilty Pleasures

27

27

721

night drive

21

345

521

Rainy day

34

21

50

5000


output 2:

No playlist found.

No playlist found with mentioned attribute.




*************************************************************************


code:






import java.util.Arrays;

import java.util.Scanner;



public class Solution {

    public static void main(String[] args) {

     Scanner sc=new Scanner(System.in);

     Music[] musics=new Music[4];

        for (int i = 0; i <4 ; i++) {

            int a=sc.nextInt();sc.nextLine();

            String b=sc.nextLine();

            int c=sc.nextInt();sc.nextLine();

            double d=sc.nextDouble();sc.nextLine();

           musics[i] =new Music(a,b,c,d);

        }


       int  input1=sc.nextInt();sc.nextLine();

          int  input2=sc.nextInt();sc.nextLine();


       int ans1 =findAvgOfCount(musics,input1);

       if(ans1==0)

       {

           System.out.println("No playlist found");

       }

       else

       {

           System.out.println(ans1);

       }


       Music[] ans2= sortTypeByDuration(musics,input2);

       if(ans2==null)

       {

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

       }

       else

       {

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

               System.out.println(ans2[i].getType());

           }

       }


    }


   public static int  findAvgOfCount(Music[] musics,int input1)

    {

        int count=0;

        int sum=0;

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

            if(musics[i].getCount()>input1)

            {

                count+=1;

              sum+=musics[i].getCount();

            }

        }



        if(count>0)

        {

            return sum/count;

        }


        return 0;

    }


   public static Music[] sortTypeByDuration(Music[] musics,int input2)

    {



      Music[]  help=new Music[0];

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

            if(musics[i].getDuration()>input2)

            {

                help=Arrays.copyOf(help,help.length+1);

                help[help.length-1]=musics[i];

            }

        }


        //bubble sort


        for (int i = 0; i < help.length-1; i++) {

            for (int j = 0; j <help.length-i-1 ; j++) {

                if(help[j].getDuration()>help[j+1].getDuration())

                {

                    Music temp=help[j+1];

                    help[j+1]=help[j];

                    help[j]=temp;

                }

            }

        }


        if(help.length>0)

        {

            return help;

        }


        return null;

    }




}



class Music

{

 private int playlistNo;

private String type;

private int count;

private double duration;


//parametrized constructor



    public Music(int playlistNo, String type, int count, double duration) {

        this.playlistNo = playlistNo;

        this.type = type;

        this.count = count;

        this.duration = duration;

    }


    //getter and setter



    public int getPlaylistNo() {

        return playlistNo;

    }


    public void setPlaylistNo(int playlistNo) {

        this.playlistNo = playlistNo;

    }


    public String getType() {

        return type;

    }


    public void setType(String type) {

        this.type = type;

    }


    public int getCount() {

        return count;

    }


    public void setCount(int count) {

        this.count = count;

    }


    public double getDuration() {

        return duration;

    }


    public void setDuration(double duration) {

        this.duration = duration;

    }

}


*****************************************************************************

Comments

Popular posts from this blog

How to Introduce Yourself to project team and client when you join a new project as fresher in service based company ?

Question:     How to Introduce Yourself to project team /client  when you join a new project as fresher ? Answer:  Hii Everyone,    Today I will be talking about how we can introduce ourselves to a team  when we join a new project in any servicebased company as a fresher.     After I completed my Engineering ,I got placed into TCS.I got allocated  into a project.I introduced myself to a team.So I will be telling about how   exactly I introduced myself to the team.The impressive introduction is  necessary if you want team members to treat you better in this work from  home period.If you fail to impress team members ,If your introduction is not  impressive ,then probably your team members will ignore you.    Before I tell you about how exactly I introduced myself,I would like  to talk about the points that you should remember before giving your  introduction. .start your introduction with Hii Ev...

TCS IRA 13 DECEMBER JAVA CODING QUESTION SOLUTION.

Create a class newspaper with below attributes. regNo-int name-String publicationYear-int price-int Write parametrised constructor as required. Create class Solution with main method. Implement 2 static methods findTotalPriceByPublicationYear and  searchNewspaperByName in solution class. findTotalPriceByPublicationYear method:    This method will take array of the Newspaper objects and int parameter type.This method will return the total price of newspapers from array of  Newspaper objects,If the publication year attribute matches with the int  parameter passed .If no Newspaper with the given publication year is present  in the array ,then the method should return 0. The main method should print total price if the returned value is greater  than 0.Else it should print "No Newspaper found with the mentioned attribute". searchNewspaperByName method :    This method will take the array of the Newspaper objects and the String  parameter type...

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....