Question

In: Computer Science

You are given the following class definition (assume all methods are correctly implemented): public class Song...

You are given the following class definition (assume all methods are correctly implemented):

public class Song {
...
public Song(String name, String artist) { ... }
public String getName() { ... }
public String getArtist() { ... }
public String getGenre() { ... }
public int copiesSold() { ... }
}


Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will get max half credit):

An expression that can be passed as argument to the sort method of a List of songs, for sorting in ascending order of copies sold.

Java Lambda

Solutions

Expert Solution

package javaapplication620;

public class SongDriver {

public static void main(String[] args) {
Playlist one = new Playlist();

Song song1 = new Song("Hotline Bling", "Drake", "Hotline Bing - Single", 267000);
Song song2 = new Song("What Do You Mean?", "Justin Bieber", "What Do You Mean? - Single", 207000);
Song song3 = new Song("Watch Me", "Silento", "Watch Me (Whip / Nae Nae) - Single", 185000);
Song song4 = new Song("679", "Fetty Wap ft. Remy Boyz", "Fetty Wap", 185000);
Song song5 = new Song("Can't Feel My Face", "The Weeknd", "Beauty Behind the Madness", 213000);
Song song6 = new Song("Good for You", "Selena Gomez ft. A$AP Rocky", "Good for You - Single", 221000);
Song song7 = new Song("If You", "Big Bang", "MADE", 264000);

one.add(song1);
one.add(song2);
one.add(song3);
one.add(song4);
one.add(song5);
one.add(song6);
one.add(song7);

one.print();

one.remove("679");
one.remove("Watch Me");

one.print();
one.clear();
}

public static long HOURS = 60 * 60 * 1000;
public static long MINS = 60 * 1000;
public static long SECS = 1000;

public static class Playlist {

private Song[] songs;
private int count;
private String playlistName;

public Playlist() {
songs = new Song[12];
count = 0;
}

public String getPlaylistName() {
return playlistName;
}

public void setPlayListName() {
this.playlistName = playlistName;
}

public void add(Song a) {
if (count == songs.length) {
System.out.println("ERROR: Collection is full. Songs were not added to the Playlist.");
}
songs[count] = a;
count++;
}

public Song get(int i) {
if (count > i) {
return songs[i];
} else {
return null;
}
}

public Song remove(String name) {
boolean found = false;
int indexToRemove = 0;
while (indexToRemove < count && !found) {
if (songs[indexToRemove].getName().equals(name)) {
found = true;
} else {
indexToRemove++;
}
}
if (indexToRemove < count) {
for (int from = indexToRemove + 1; from < count; from++) {
songs[from - 1] = songs[from];
}
songs[count - 1] = null;
count--;
}
return null;
}

public void print() {
String result = "NumSongs = " + count
+ " / PlayList song limit = " + songs.length + "\n";

for (int i = 0; i < count; i++) {
result += ("songList[" + i + "] = <"
+ songs[i] + ">\n");
}
result += " / " + formattedTotalTime();
System.out.println(result.toString());
}

public int size() {
return count;
}

public int totalTime() {
int totalTime = 0;
for (int i = 0; i < count; i++) {
totalTime += songs[i].getLength();
}
return totalTime;
}

public String formattedTotalTime() {
return formatTime(totalTime());
}

public void clear() {
for (int i = 0; i < songs.length; i++) {
songs[i] = null;
count = 0;
}
return;
}
}

public static class Song {

public String name;
public String artist;
public String album;
public int length;

public Song(String songName, String artistName, String albumName, int trackLength) {
this.name = songName;
this.artist = artistName;
this.album = albumName;
this.length = trackLength;
}

public void setName(String songName) {
name = songName;
}

public String getName() {
return name;
}

public void setArtist(String artistName) {
artist = artistName;
}

public String getArtist() {
return artist;
}

public void setAlbum(String albumName) {
album = albumName;
}

public String getAlbum() {
return album;
}

public void setLength(int h, int m, int s) {
length = (h * 3600 + m * 60 + s);
if (h == 0) {
length = (m * 60 + s);
}
}

public int getLength() {
return length;
}

public String toString() {
return "Title: " + getName() + ", Artist: " + getArtist()
+ ", Album: " + getAlbum() + ", Track Length: " + formatTime(getLength());
}

}

public static String formatTime(long time) {
long overflow = time;
long h = time / HOURS;
overflow = time % HOURS;
long m = overflow / MINS;
overflow = time % MINS;
long s = overflow / SECS;
return String.format("%02d:%02d.%02d", h, m, s);

}
}

This test code currently prints out:

NumSongs = 7 / PlayList song limit = 12
songList[0] = <Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 00:04.27>
songList[1] = <Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 00:03.27>
songList[2] = <Title: Watch Me, Artist: Silento, Album: Watch Me (Whip / Nae Nae) - Single, Track Length: 00:03.05>
songList[3] = <Title: 679, Artist: Fetty Wap ft. Remy Boyz, Album: Fetty Wap, Track Length: 00:03.05>
songList[4] = <Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 00:03.33>
songList[5] = <Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 00:03.41>
songList[6] = <Title: If You, Artist: Big Bang, Album: MADE, Track Length: 00:04.24>
/ 00:25.42
NumSongs = 5 / PlayList song limit = 12
songList[0] = <Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 00:04.27>
songList[1] = <Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 00:03.27>
songList[2] = <Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 00:03.33>
songList[3] = <Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 00:03.41>
songList[4] = <Title: If You, Artist: Big Bang, Album: MADE, Track Length: 00:04.24>
/ 00:19.32


Related Solutions

You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
Assume you have created the following data definition class: public class Book {    private String title;...
Assume you have created the following data definition class: public class Book {    private String title;    private double cost;       public String getTitle() { return this.title; }    public double getCost() { return this.cost; }       public void setTitle(String title) {       this.title = title;    } // Mutator to return true/false instead of using exception handling public boolean setCost(double cost) {       if (cost >= 0 && cost < 100) {          this.cost = cost;          return true;       }       else {          return false;       }...
Assume you have created the following data definition class: public abstract class Organization { private String...
Assume you have created the following data definition class: public abstract class Organization { private String name;    private int numEmployees;    public static final double TAX_RATE = 0.01;    public String getName() { return this.name; }    public int getNumEmployees() { return this.numEmployees; }         public abstract double calculateTax() {       return this.numEmployees * TAX_RATE;    } } In your own words, briefly (1-2 sentences) explain why the data definition class will not compile. Then, write modified code that...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int m_numElements; // constructor // Do not make any changes to this method! public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! boolean isEmpty() { if (m_last == null) return true; else return false; } // return the size of the list (# of Song nodes) // Do...
A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
Create a class, called Song. Song will have the following fields:  artist (a string) ...
Create a class, called Song. Song will have the following fields:  artist (a string)  title (a string)  duration (an integer, recorded in seconds)  collectionName (a string) All fields, except for collectionName, will be unique for each song. The collectionName will have the same value for all songs. In addition to these four fields, you will also create a set of get/set methods and a constructor. The get/set methods must be present for artist, title, and duration....
JAVA In the last lesson, we wrote a class with methods to print out the song...
JAVA In the last lesson, we wrote a class with methods to print out the song The Ants Go Marching. Notice that this is a class where there are no instance variables and we don’t really need to generate multiple objects. With students or pets, it makes sense to have multiple objects. With the Song, we can just make the methods static and have just 1 copy of them. Copy in your class from the last lesson into this active...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT