This repository is a source of practice for the Java concepts introduced in the Java II Codeup curriculum.
- read through all instructions in this readme
- fork, clone, and open this repository in your preferred IDE for Java
- create and checkout to a new branch called
my-solution
- follow the steps below to build a karaoke program
The KaraokeApp
class included with this project contains a commented out main method that can be used to test your app after you complete it. Add other main methods to any class as needed for simpler testing as you build the app.
-
add a
Song
class with the following...- private string properties for
title
,artist
, and a private list of strings forlyrics
- a constructor to assign values to all instance properties
- getters and setters for all instance properties
- a static method
parseLyrics
that takes in a single string input of possibly several words and returns a list of strings that are the output of splitting all the words in the input string on a single space character" "
- private string properties for
-
add a
Album
class with the following...- a private string property for the album
name
- a private property for a list of song objects called
songs
- a constructor that sets both instance properties
- getters and setters for both instance properties
- a public method with a void return type called
printTrackListings
that will iterate through all songs for a given album and print output like the following (90s Hits is the example album name):
90S HITS track listing... 1. Every Day is a Winding Road by Sheryl Crow 2. Ready to Go by Republica 3. Airbag by Radiohead
- a private string property for the album
-
add a
Playable
interface with the following...- a method called
play
that takes in an album instance with a void return type - a method called
play
that takes in a song instance with a void return type
- a method called
-
add an abstract
Player
class that implements thePlayable
interface (not it's methods, however) and include the following...- an int constant called
WORD_CADENCE
set to a value of 600 - an int constant called
INTRO_PAUSE
set to a value of 7000
- an int constant called
-
add a class
MusicPlayer
that extends thePlayer
abstract class and include the following...- a private instance string property called
voice
set to an empty string by default - an empty constructor
- a constructor that sets the value of the
voice
property - a getter and setter for the
voice
property - an implmentation of the
play
method that takes in a song instance and does the following...- this method will create a runtime instance with the following code
Runtime r = Runtime.getRuntime();
- a command can be issued from Java to the terminal using
r.exec()
and passing in a string that will be executed by the terminal (this will require handling a checked exception; please use a try-catch for this) - you can make your MacBook say text aloud by simply running a command in the terminal using the
say
command. For example, you can make your computer say "hello world" by typing into the terminalsay hello world
- using the knowledge in the previous bullet points, this method should issue a say command to the terminal for each lyric in the list of song
lyrics
- to avoid saying multiple words at once, issue include a pause after saying "now playing...SONG_TITLE by SONG_ARTIST" using the
sleep()
method and passing in theINTRO_PAUSE
constant (this is the number of milliseconds to wait) and include asleep()
command after each lyric reading command is issued, passing in the constantWORD_CADENCE
- for each say command, you can also pass in a
-v
flag and include the name of the computer voice you would like the say command to be spoken with (you can use thevoice
property for this) - if you get stuck on these steps, an example of a possible implementation of the
play(Song s)
method is further below
- this method will create a runtime instance with the following code
- an implementation of the
play
method that takes in an album instance and invokes theplay(Song s)
method for each song in the album
- a private instance string property called
-
add a
LyricsPlayer
class that extendsPlayer
and includes the following...-
add the following static constants:
public static final String RESET = "\u001B[0m"; public static final String RED = "\u001B[31m"; public static final String GREEN = "\u001B[32m"; public static final String YELLOW = "\u001B[33m"; public static final String BLUE = "\u001B[34m"; public static final String PURPLE = "\u001B[35m"; public static final String CYAN = "\u001B[36m";
-
a private string instance property for
textColor
set to the value"default"
-
an empty constructor
-
a constructor that sets the
textColor
value -
getter and setter for
textColor
-
copy and paste the following
displayText
method:private void displayText(String string) { String colorCode = RESET; switch(textColor.toLowerCase()) { case "red": colorCode = RED; break; case "green": colorCode = GREEN; break; case "yellow": colorCode = YELLOW; break; case "blue": colorCode = BLUE; break; case "purple": colorCode = PURPLE; break; case "cyan": colorCode = CYAN; break; } System.out.printf("%s%s", colorCode, string); }
-
using the
play
method implementations in theMusicPlayer
class as a reference, implement theplay
methods in theLyricsPlayer
class to behave similarly but instead of saying the lyrics, it should print them out in a timed way. First, sout outRESET + "Now playing... SONG-TITLE by SONG-ARTIST"
message before each song and using thedisplayText
method to display each lyric. Remember to use thesleep
method and theINTRO_PAUSE
andWORD_CADENCE
constants to handle timing.
-
In total, you should have the following files inside your src
folder upon completion:
Album
KaraokeApp
LyricsPlayer
MusicPlayer
Playable
Player
Song
Skip over the following code example to avoid seeing an example solution of the play(Song s) method from the MusicPlayer class
Here is what a possible play(Song s)
might look like in the MusicPlayer
class...
@Override
public void play(Song s) {
Runtime r = Runtime.getRuntime();
try {
r.exec("say " + "now playing..." + s.getTitle() + " by " + s.getArtist());
sleep(INTRO_PAUSE);
for (String lyric : s.getLyrics()) {
if (voice.isEmpty()) {
r.exec("say " + lyric);
} else {
r.exec("say -v " + voice + " " + lyric);
}
sleep(WORD_CADENCE);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
While it is preferable to build your own working solution of this app first, at any point, you can checkout to the solution
branch to review an existing solution.