Write the code for the Song class. 1. Instance variables: title:String, lyrics:String, numAwards:int 2. For the 2-arg constructor (parameters are title and lyrics), use the following default value: - numAwards: 3 3. Create the 3-argument constructor. 4. No additionalconstructors. 5. Generate all getters and setters for instance variables 6. toString: Return a nicely formatted string describing this object 7. toSummary: This instance method will return a String that contains up to the first 10 characters of the lyrics. Be sure to handle the case where the lyrics are shorter than 10 chars. No explicit parameters. 8. isGoodSong: This instance method with no parameters will return true if BOTH of the following are true: - Number of awards > 3 AND - Title starts with “Fun” or “Dance” (case insensitive) 9. countHappy: This instance method with no parameters will return the number of times "happy" appears anywhere in the lyrics. This comparison is case insensitive. Part 2: SongTester 10. Download the starter file for SongTester.java from the D2L dropbox. 11. Run the existing SongTester.java to test out your countHappy and isWinningSong methods. Fix any problems indicated by these test cases 12. sumAwards: In SongTester.java, create a static method called sumAwards. It should have an ArrayList as a parameter. The method should return the total number of awards (an int) all of the songs in the list have won. 13. In the main method: - Create at least 5 Song objects - Place the objects in an ArrayList of Songs named songAL. - Test your toSummary method using the songAL ArrayList. Fix if needed. - For the song at index 1, replace the lyrics with “Na-na-na-boo-boo”. - Call the sumAwards method using the songAL as a parameter. SOP the result.
This is the Tester method:
public class SongTester
{
/**
* ****DO NOT MODIFY this method****
* This method returns the percentage of test cases
* that the isGoodSong method has succesfully passed
* @return percentage of correct test cases
*/
public static double testisGoodSong()
{
Song song1 = new Song("", "");
int countCorrect = 0;
song1.setNumAwards(5);
song1.setTitle("Dance revolution");
if ( song1.isGoodSong())
{
countCorrect++;
}
else
{
System.err.println("isGoodSong case1 failed");
}
song1.setNumAwards(1);
song1.setTitle("Fun in the sun");
if ( !song1.isGoodSong())
{
countCorrect++;
}
else
{
System.err.println("isGoodSong case2 failed");
}
song1.setNumAwards(0);
song1.setTitle("Fun in the sun");
if ( !song1.isGoodSong())
{
countCorrect++;
}
else
{
System.err.println("isGoodSong case3 failed");
}
song1.setNumAwards(2);
song1.setTitle("Not winning");
if ( !song1.isGoodSong())
{
countCorrect++;
}
else
{
System.err.println("isGoodSong case4 failed");
}
return countCorrect/4.0*100;
}
/**
* ****DO NOT MODIFY this method****
* This method returns the percentage of test cases
* that the countHappy method has succesfully passed
* @return percentage of correct test cases
*/
public static double testcountHappy()
{
Song song1 = new Song("Default", "Short one");
int countCorrect = 0;
if ( song1.countHappy() == 0)
{
countCorrect++;
}
else
{
System.err.println("countHappy case1 failed");
}
song1.setLyrics("Happy haPPy");
if ( song1.countHappy() == 2)
{
countCorrect++;
}
else
{
System.err.println("countHappy case2 failed");
}
song1.setLyrics("Lots of lyrics. Example of long lyric with
HAPPY!!!!!! in middle");
if ( song1.countHappy() == 1)
{
countCorrect++;
}
else
{
System.err.println("countHappy case3 failed");
}
song1.setLyrics("My happy song. My happyhappyhappy song. Is happily
happy song. happy");
if ( song1.countHappy() == 6)
{
countCorrect++;
}
else
{
System.err.println("countHappy case4 failed");
}
return countCorrect/4.0*100;
}
public static void main(String[] args)
{
System.out.println("Percent correct for isGoodSong: " +
testisGoodSong() );
System.out.println("Percent correct for countHappy: " +
testcountHappy() );
}
}
Below is the code for Part 1. It is well explained inside the code using comments.
//Song.java
// Song class
class Song {
// Instance variables
private String
title; // title of the
Song
private String
lyrics; // lyrics of the Song
private int
numAwards; // number of
awards
// Constructor
public Song(String title, String lyrics) {
this.title =
title;
this.lyrics =
lyrics;
numAwards = 3;
}
// constructor
public Song(String title, String lyrics, int
numAwards) {
this.title =
title;
this.lyrics =
lyrics;
this.numAwards =
numAwards;
}
// returns the title
public String getTitle() {
return title;
}
// returns the lyrics
public String getLyrics() {
return lyrics;
}
// returns the number of awards
public int getNumAwards() {
return numAwards;
}
// sets the title
public void setTitle(String title) {
this.title =
title;
}
// sets the lyrics
public void setLyrics(String lyrics) {
this.lyrics =
lyrics;
}
// sets the number of awards
public void setNumAwards(int numAwards) {
this.numAwards =
numAwards;
}
// returns the Song object in String
format
@Override
public String toString() {
return "Song, Title: " +
title + "\nLyrics: " + lyrics + ". It has won " + numAwards + "
awards.";
}
// this method will return a String
that contains up to the first 10 characters of the lyrics
public String toSummary() {
// is length of lyrics
is more than 10, return first 10 characters
if (lyrics.length() >
10) {
return lyrics.substring(0, 10);
} else {
return lyrics;
}
}
// returns true if Number of awards
> 3 AND - Title starts with "Fun" or "Dance"
public boolean isGoodSong() {
return numAwards > 3
&& (title.toLowerCase().startsWith("fun") ||
title.toLowerCase().startsWith("dance"));
}
// this method will return the number
of times "happy" appears anywhere in the lyrics
public int countHappy() {
int count = 0; // to
store happy countts
int pos = 0; // to store
position to check for happy in lyrics
String lyricsSmall =
lyrics.toLowerCase(); // lyrics in lower case to compare with
happy
int happyLength =
5; // length of word happy
// keep
checking until it doesn't find happy in lyrics
while (pos != -1)
{
// get position of happy
pos = lyricsSmall.indexOf("happy", pos);
// if found happy
if(pos != -1){
// increase the counter
count += 1;
// increase the position
pos += happyLength;
}
}
// return the
happy count
return count;
}
}
Below is the output with the given main class:
Below is the code for Part 2, changes are highlighted in bold:
// SongTester.java
import java.util.ArrayList;
public class SongTester {
/**
* ****DO NOT MODIFY this method**** This
method returns the percentage of
* test cases that the isGoodSong method
has succesfully passed
*
* @return percentage of correct test
cases
*/
public static double testisGoodSong() {
Song song1 = new
Song("", "");
int countCorrect =
0;
song1.setNumAwards(5);
song1.setTitle("Dance
revolution");
if (song1.isGoodSong())
{
countCorrect++;
} else {
System.err.println("isGoodSong case1 failed");
}
song1.setNumAwards(1);
song1.setTitle("Fun in
the sun");
if (!song1.isGoodSong())
{
countCorrect++;
} else {
System.err.println("isGoodSong case2 failed");
}
song1.setNumAwards(0);
song1.setTitle("Fun in
the sun");
if (!song1.isGoodSong())
{
countCorrect++;
} else {
System.err.println("isGoodSong case3 failed");
}
song1.setNumAwards(2);
song1.setTitle("Not
winning");
if (!song1.isGoodSong())
{
countCorrect++;
} else {
System.err.println("isGoodSong case4 failed");
}
return countCorrect /
4.0 * 100;
}
/**
* ****DO NOT MODIFY this method**** This
method returns the percentage of
* test cases that the countHappy method
has succesfully passed
*
* @return percentage of correct test
cases
*/
public static double testcountHappy() {
Song song1 = new
Song("Default", "Short one");
int countCorrect =
0;
if (song1.countHappy()
== 0) {
countCorrect++;
} else {
System.err.println("countHappy case1 failed");
}
song1.setLyrics("Happy
haPPy");
if (song1.countHappy()
== 2) {
countCorrect++;
} else {
System.err.println("countHappy case2 failed");
}
song1.setLyrics("Lots of
lyrics. Example of long lyric with HAPPY!!!!!! in middle");
if (song1.countHappy()
== 1) {
countCorrect++;
} else {
System.err.println("countHappy case3 failed");
}
song1.setLyrics("My
happy song. My happyhappyhappy song. Is happily happy song.
happy");
if (song1.countHappy()
== 6) {
countCorrect++;
} else {
System.err.println("countHappy case4 failed");
}
// create
ArrayList of songs
ArrayList<Song>
songAL = new ArrayList<>();
// add 5 song
objects
songAL.add(new
Song("Skinny Love", "Come on skinny love just last the year. Pour a
little salt we were never here", 2));
songAL.add(new
Song("Beautiful in white", "Not sure if you know this But when we
first met I got so nervous", 1));
songAL.add(new Song("In
My Feelings", "Trap, TrapMoneyBenny Trap, TrapMoneyBenny Trap,
TrapMoneyBenny", 5));
songAL.add(new
Song("Mercy", "Why you gotta show up lookin so good"));
songAL.add(new
Song("Girls like you", "Spent 24 hours, I need more hours with
you", 3));
// display
Summary
for (Song song : songAL)
{
System.out.println(song.toSummary());
}
// For the
song at index 1, replace the lyrics with “Na-na-na-boo-boo”.
Song song =
songAL.get(1);
song.setLyrics("Na-na-na-boo-boo");
songAL.set(1,
song);
// display songs list
after update
System.out.println("\nAfter updating song list:");
// display Summary
for (Song s : songAL)
{
System.out.println(s.toSummary());
}
// call sum awards and
display the total awards
System.out.println("\nTotal Awards: " +
sumAwards(songAL));
return countCorrect /
4.0 * 100;
}
public static void main(String[] args) {
System.out.println("Percent correct for isGoodSong: " +
testisGoodSong());
System.out.println("Percent correct for countHappy: " +
testcountHappy());
}
// returns the total number of awards
all of the songs in the list have
won
public static int
sumAwards(ArrayList<Song> songs) {
int total =
0; // to store total awards
count
// iterate
through all songs and count awards
for (Song song : songs)
{
total += song.getNumAwards();
}
// return
total awards
return total;
}
}
Below is the sample output:
This completes the requirement. Let me know if you have any questions.
Thanks!
Write the code for the Song class. 1. Instance variables: title:String, lyrics:String, numAwards:int 2. For the...
Part 1: Song Class Write the code for the Song class. 1. Instance variables: title:String, lyrics:String, numAwards:int 2. For the 2-arg constructor (parameters are title and lyrics), use the following default value: - numAwards: 3 3. Create the 3-argument constructor. 4. No additionalconstructors. 5. Generate all getters and setters for instance variables 6. toString: Return a nicely formatted string describing this object 7. toSummary: This instance method will return a String that contains up to the first 10 characters of...
You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here. Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [ ] Name: courseCreditArray Access...
You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here. (I need step 2 please.) Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [...
Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name Access modifier: private Data Type: String Name: currentWeight Access modifier: private Data Type: double Name: maximumWeight Access modifier: private Data Type: double Constructors Name: Bag Access modifier: public Parameters: none (default constructor) Task: sets the value of the instance variable name to the empty string sets the value of the instance variable currentWeight to 0.0 sets the value of the instance variable maximumWeight to...
Programming assignment for Java: Do not add any other instance variables to any class, but you can create local variables in a method to accomplish tasks. Do not create any methods other than the ones listed below. Step 1 Develop the following class: Class Name: College Degree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String Name: courseCreditArray Access modifier:...
Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [ ] Name: courseCreditArray Access modifier: private Data type: int [ ] Static variables Name: maximumNumberOfCourses Access modifier: private Data type: int Initial Value: 40 Constructor Name: CollegeDegree Access modifier: public Parameters: none (default constructor) Task: sets major to the empty string...
In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...
complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */ public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null;...
JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet { /** * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList * containing all the indexes of Strings in arr that match String s. For this method, two Strings, * p and q, match when p.equals(q) returns true or if both of the compared references are null. * * @param s The string...
In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...