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
Post a Comment