rename equals
These labs are due Sunday, November 12th, at noon. Please submit ForEachFun.java, Song.java, PlayList.java and PlayListDriver.java.
You can see a video version of this lesson here: https://www.youtube.com/watch?v=Mjn9k5Zh1FA and https://www.youtube.com/watch?v=q8IsgvlgEmE
For every assignment, you may use a regular for loop or a for each loop. I STRONGLY recommend trying the for-each loop several times so you can get practice with the syntax. It is pretty slick! Be careful with the for loop removal issue any time you are using .remove!
Note that some assignments here use arrays, and some will use ArrayList objects. Read carefully!
1. ForEachFun.java – this program will be a group of static methods, not an object, about for each loops and the for loop removal issue. Every method will be static. You should write a main to test your other methods, but I will be looking for the following methods:
public static int positives(int[] numbers) – this function will input an array of ints, print out each of the positive numbers, and then return the amount of positive numbers. So if you send it the array {3,-0,4,5,-6,-7,0} it should return 3 because there are three positive numbers (note that 0 is not a positive number).
sumAll – this method should input an array of ints and return an int (not an Integer) representing the sum of all of the elements in the array. So if you send it the array {3,4,5,-6,-7,0} it should return -1.
sumAll – this should input an ArrayList of type <Integer> (so this method is overloaded) and return an int (not an Integer) representing the sum of all of the elements in the ArrayList.
sumPositives – this method should input a 2-d array of ints (int[][]) and return an int representing the sum of all of the positive elements in the entire 2-d array. It should ignore any negative entries and not add them to the total. So if you send it the array {{3,4,5},{-6,-7,0},{2,3,1}} it should return 18. Use a nested for each loop!!
NOTE: since the next two methods ask you to remove items from a list, you will need to use a traditional for loop, NOT a for each loop! Trying to remove items with a for each loop can cause a ConcurrentModificationException runtime error.
removeSub – this method will input an ArrayList of Strings and a String, and then remove any String in the arraylist that contains that 2nd string as a substring and return the ArrayList. For example, if I have an ArrayList named words containing [happy, sad, baddy, cool, advertisement], and I called removeSub(words, "ad"), this function should return the arrayList containing [happy,cool] because the others have an "ad" in them. Be careful!!!
removeThrees - this method will input an ArrayList of Integer variables, and remove all numbers that are equal to 3 OR -3. It should then return the updated ArrayList of Integers.
countGames - this method should input an ArrayList of String objects and return an int. It should count how many of the Strings end in "game" as the last four spots. If any of the inputs have fewer than 4 letters, there should not be a runtime error. For example, if the input list is ["squidgame", "fun game", "fame", "me", "ballgame"] it should return 3.
countSuccessors - this method should input an ArrayList of String objects and return an int. It should count how many items in the list are the same String (use .equals) as the one that follows it in the list. For example, if your list is ["a","b","b","c","a","a","a"], it should return 3, because the b in spot 1 is followed by another b and the a's in spots 4 and 5 are also followed by another "a"
2. Write a class called Song.java. Your class should have:
-a constructor that inputs a String for the artist, a String for the song name, and a double for the length in minutes (in that order).
-accessor methods for each of the three instance variables – getArtist, getName, and getLength
-a toString method (you may implement this however you want, you may just want to have it be something like "Metallica – Master of Puppets" and not include the length)
-an equals method – This method will input another Song object and return a boolean representing whether the songs are equal. Two songs are equal if they have the same artist and name.
3. Write a class named PlayList.java [notice that the L is capitalized]. This PlayList will contain one piece of private instance data – an ArrayList of Songs (use the class you just wrote) called tunes (or whatever you like, I will call it tunes in my descriptions of the methods). Write the following methods:
two overloaded constructors:
- a constructor with no inputs, this just sets up tunes to be an empty ArrayList of songs
-a constructor that inputs an ArrayList of Songs that sets tunes equal to the ArrayList that is inputted
other methods:
add – this void method inputs a Song (so the header should be: public void add(Song s) ) and adds it to the end of the playlist
numberOfSongs – this method takes no inputs and returns an int representing how many songs are in the PlayList - the size of tunes.
totalLength – this method takes no inputs and returns a double representing the sum of all of the lengths of each song in tunes.
playSong – this method takes no inputs and returns a Song object. It should return the Song in location 0 in tunes and remove it from the tunes ArrayList. (be careful with implementing this method)
gatherArtist - this method inputs a String and returns an ArrayList of Song objects. The method should create and return a new ArrayList that should contain all the songs from tunes with the given artist. If you call gatherArtist("Metallica"), it should return all the songs with "Metallica" as their artist. The tunes instance variable should not change.
removeArtist – this void method inputs a String, then removes all songs in tunes which have that String as the artist.
removeLength – this void method inputs a double, then removes all songs in tunes which have a length greater than or equal to the double that was sent to this method.
longestSong - this method takes no inputs an returns a Song. It should return the Song in tunes with the biggest length. If multiple songs are tied, any of them can be returned.
shuffle() – this void method takes no inputs, but just shuffles the order of the songs in tunes. Hint: this is going to be a difficult method to write. One possible strategy is to create a new ArrayList, and randomly move songs over one at a time from tunes into the new ArrayList (pick a random song from tunes, add it to the end of the second list and remove it from tunes), and then set tunes to be the new one. Test this thoroughly and make sure it shuffles differently each time.
toString() – this method should just call and return the toString method for tunes (remember that ArrayList already has toString written for it)
This is actually pretty close to how a playlist works on iTunes or Spotify, for example.
4. To test your code, create a PlayListDriver class, and in main, write code similar to this:
PlayList mysongs = new PlayList();
mysongs.add(new Song("Metallica", "Master of Puppets", 8.23));
mysongs.add(new Song("Pantera", "Floods", 5));
mysongs.add(new Song("Eminem", "Almost Famous", 4.5) );
System.out.println(mysongs);
mysongs.shuffle();
System.out.println(mysongs); //shuffle and test it out
[test out other methods as well]
Once you have tested this, submit ForEachFun.java, Song.java, PlayList.java and PlayListDriver.java.
AFTER YOU ARE DONE/EXTRA CREDIT:
First, check over all of your code, make sure your method names and function inputs are OK. Then you can either work on your review or go to codingbat.com, the Array-3 section and complete canBalance. Make sure that you have put me (gborish (at) hartdistrict.org) in the teacher share part of your preferences, then send me an email saying you did them and which ones you did.