Skip to main content

Solving TCS IRA Java Question asked by my Subscriber. |TCS IRA | TCS PRA |TCS CPA |TCS IPA






Question: 

create the class Song with below attributes.


songId - int 

  title - String

 artist -  String

duration-double


The above attributes should be private, getters, setters and parameterized 

constructor as required. 


Create class MyClass with main method.


implement two static methods - findSongDurationForArtist and

getSongsInAscendingOrder in MyClass class.


findSongDurationForArtist method: This method will take two input 

parameters of Song objects and String parameter The method will return

 the sum of song duration from array of Song object for the giver

 artist (String parameter passed).


If no Song with the given artist is present in the array of Song objects,

  then the method should return zero.


getSongsInAscendingOrder method: This method will take input parameters

 array, of Song objects and String parameter.The method will return Song

 objects array in an ascending order of their duration, from the array of

 Song objects whose artist attribute matches with the input String parameter. 

If no Song with the given artist is present in the array of Song objects,

 then the method should return null


Note: No two Song object would have the same songid.


Combination of artist and duration should be unique.


All the searches should be case insensitive.


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


For getfindSongDuration ForArtist method - in method should print the sum 

of duration of artist as it is, if the returned value is greater than 0.

 or it should print "There are no songs with given artist".


For getSongsinAscendingOrder method - The main method should print the song

 id and title from the returned song object array if the returned value

 is not null. 

If the returned value is null then it should print 

"There are no songs with given artist".



For Example,

112

ABC

where 112 is song ld. ABC is song title.

Before calling these static methods in main, use. Scanner object to read

 the values of five Song objects referring attributes in the above

 mentioned attribute sequence. Next, read two String values for capturing

 artists.

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


Sample Input 1: 


2150

In time

Justin Timberlake

4

250

Cry Me

Justin Timberlake

3

1200

Mirrors

Justin Timberlake

5

1300

That's the way it is

celion dion

5

500

Ashes

celion dion

3

celion dion

Justin Timberlake



Sample output1:

8.0

250

Cry Me

2150

In time

1200

Mirrors




input 2:

2150

Cry Me

Justin Timberlake

3

1000

Why Not

Enrique Iglesius

5

1200

Mirrors

Justin Timberlake

5

1300

That's the way it is

Celion Dion

5

500

Ashes

celion Dion

3

Bryan Adams

Michael Larkson


output 2:

There are no songs with given artist

There are no songs with given artist




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



code :




import java.util.Scanner;

import java.util.Arrays;



public class Solution {


    public static void main(String[] args) {

       Scanner sc=new Scanner(System.in);

       Song[] songs=new Song[5];

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

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

            String b=sc.nextLine();

            String c=sc.nextLine();

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

             songs[i]  = new Song(a,b,c,d);

        }


        String input1=sc.nextLine();

        String input2=sc.nextLine();


       double ans1 =findSongDurationForArtist(songs,input1);

       if(ans1==0)

       {

           System.out.println( "There are no songs with given artist");

       }

       else

       {

           System.out.println(ans1);

       }


      Song[] ans2 =getSongsInAscendingOrder(songs,input2);

       if(ans2==null)

       {

           System.out.println("There are no songs with given artist");

       }

       else {

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

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

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

           }

       }


    }




   public static double findSongDurationForArtist(Song[] songs,String input1)

    {

      double sum=0;


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

           if(songs[i].getArtist().equalsIgnoreCase(input1))

           {

              sum+=songs[i].getDuration();

           }

        }


        if(sum>0)

        {

            return sum;

        }



        return 0;

    }


  public  static  Song[]  getSongsInAscendingOrder(Song[] songs,String input2)

    {



          Song[] help=new Song[0];

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

            if(songs[i].getArtist().equalsIgnoreCase(input2))

            {

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

              help[help.length-1]=songs[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())

                {

                   Song temp=help[j];

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

                   help[j+1]=temp;

                }

            }

        }


        if(help.length>0)

        {

            return help;

        }

        

        return null;

    }



}


class Song

{

   private int songId;

   private String title;

   private String artist;

   private double duration;


//constructor


    public Song(int songId, String title, String artist, double duration) {

        this.songId = songId;

        this.title = title;

        this.artist = artist;

        this.duration = duration;

    }


    //getter and setter



    public int getSongId() {

        return songId;

    }


    public void setSongId(int songId) {

        this.songId = songId;

    }


    public String getTitle() {

        return title;

    }


    public void setTitle(String title) {

        this.title = title;

    }


    public String getArtist() {

        return artist;

    }


    public void setArtist(String artist) {

        this.artist = artist;

    }


    public double getDuration() {

        return duration;

    }


    public void setDuration(double duration) {

        this.duration = duration;

    }

}


Comments