Skip to main content

june 12 tcs cpa java coding question.

Create a class song with the below attributes.
songId=int
title=String
artist=String
duration=double

the above attributes should be private  write getter and setter
and parametrized constructor as required .

create class cpa12june with main method.

Implement two static methods -
 findSongDurationForArtist and
getSongsInAscendingOrder in cpa12june class.

findSongDurationForArtist method- this method will take two
input parameters -array of Song objects and String parameter.
The method will  return the sum of the song duration from the
array of the Song objects for the given artists(Sttring 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 2 input parameters-array of the  Song
objects and the String parameter.
this method will return Song object 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 the Song objects, then the method should return null.

for findSongDurationForArtist method- The main method should
print  the sum of the 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 songId
And title of each Song object 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".



input1:
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

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.Arrays;
import java.util.Scanner;

public class cpa12june {
    public static void main(String[] args) {
        Song[] songs=new Song[5];
        Scanner sc=new Scanner(System.in);
        for (int i = 0; i <songs.length ; 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 is 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].songId);
                  System.out.println(ans2[i].title);
              }
          }

    }


    public static double findSongDurationForArtist(Song[] songs,String input1)
    {
        double sum=0;
        int count=0;
        for (int i = 0; i <songs.length ; i++) {
            if(songs[i].artist.equalsIgnoreCase(input1))
            {
                count=count+1;
                sum=sum+songs[i].duration;
            }
        }
        if(count>0)
        {
            return sum;
        }
    return 0;


    }





    public static Song[] getSongsInAscendingOrder(Song[] songs,String input2)
    {
        Song[] help=new Song[0];
        for (int i = 0; i <songs.length ; i++) {
            if(songs[i].artist.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].duration>help[j+1].duration)
                {
                    Song temp=help[j];
                    help[j]=help[j+1];
                    help[j+1]=temp;
                }
            }
        }

        if(help.length>0)
        {
            return help;
        }

        return null;
    }


}




class Song
{
    int songId;
    String title;
    String artist;
    double duration;

    //parametrized constructor


    public Song(int songId, String title, String artist, double duration) {
        this.songId = songId;
        this.title = title;
        this.artist = artist;
        this.duration = duration;
    }
}

 

 

 

Comments

Popular posts from this blog

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

HTML5 Semantics | Fresco Play | Hands On 6 | Video.

   Question: In this blog Post I am going to explain you how we can solve the below handson. Embed a video in your HTML5 page with a width of '320' and height of '200' with controls displayed. Now, try to do the following: Creating 3 videos Auto-reply the first video preload the second video Display image while the third video loads. Question is provided in the snip below: ......................................................................................................................................... Answer: Code  inside the index.html is provided below : <!DOCTYPE html> <html> <head>   <link rel="icon" href="favicon.png" type="image/png">   <title>Destiny</title>   <link href="mystyle.css" rel="stylesheet" type="text/css"> </head> <body> <video controls autoplay> <source src="video.mp4"> </video>   <video controls prel...