Question

Part 1: Song Class Write the code for the Song class. 1. Instance variables: title:String, lyrics:String,...

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 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 SongTester 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() );

}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java code

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Song {
    /**
     *  Instance variables:
     *  title:String,
     *  lyrics:String,
     *  numAwards:int
     */
    private String title;
    private String lyrics;
    private int numAwards;
    /**
     * For the 2-arg constructor
     * (parameters are title and lyrics),
     * use the following default value: - numAwards: 3
     */
    public Song(String title,String lyrics)
    {
        this.title= title;
        this.lyrics = lyrics;
        numAwards =3;
    }
    /**
     *  Create the 3-argument constructor.
     */
    public Song(String title, String lyrics, int numAwards) {
        this.title = title;
        this.lyrics = lyrics;
        this.numAwards = numAwards;
    }
    /**
     * . Generate all getters and setters for instance variables
     */
    public String getTitle() {
        return title;
    }

    public String getLyrics() {
        return lyrics;
    }

    public int getNumAwards() {
        return numAwards;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setLyrics(String lyrics) {
        this.lyrics = lyrics;
    }

    public void setNumAwards(int numAwards) {
        this.numAwards = numAwards;
    }
    /**
     *  toString:
     *  Return a nicely formatted string describing this object
     */
    @Override
    public String toString() {
        return "Song Title: "+title+"\nLyrics"+lyrics+"\nNum of Awards: "+numAwards;

    }
    /**
     * 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.
     */
    public String toSummary()
    {
        if(lyrics.length()<10)
            return lyrics.substring(0,lyrics.length()-1);
        else
        return lyrics.substring(0,9);
    }
    /**
     * 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)
     */
    public boolean isGoodSong()
    {

        if(numAwards>3 && (title.toUpperCase().startsWith("FUN")||title.toUpperCase().startsWith("DANCE")))
            return true;
        else
            return false;
    }
    /**
     * countHappy: This instance method with no parameters will
     * return the number of times "happy"
     * appears anywhere in the lyrics. This comparison is case insensitive.
     */
    public int countHappy()
    {
        int count=0;
        Pattern pattern = Pattern.compile("happy");
        Matcher m =pattern.matcher(lyrics.toLowerCase());
       while (m.find())
           count++;

        return count;
    }
}
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");
        }
        return countCorrect/4.0*100;
    }

    /**
     * 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.
     *
     */
    private static int sumAwards(ArrayList<Song> songs)
    {
        int sum=0;
        for (Song s:songs
             ) {
            sum +=s.getNumAwards();
        }
        return sum;
    }

    public static void main(String[] args)
    {
        System.out.println("Percent correct for isGoodSong: " + testisGoodSong() );
        System.out.println("Percent correct for countHappy: " + testcountHappy() );
        /**
         *  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.
         */
        ArrayList<Song> songAL= new ArrayList<>();
        Song song1 = new Song("SONG1","Happy we are happy",4);
        Song song2 = new Song("SONG2","Na-na-na-boo-boo",5);
        Song song3 = new Song("SONG3","We are going alone",3);
        Song song4 = new Song("SONG4","Fun is the funny thing",5);
        Song song5 = new Song("SONG5","Distnace in nothing more...",7);
        //Add to array list
        songAL.add(song1);
        songAL.add(song2);
        songAL.add(song3);
        songAL.add(song4);
        songAL.add(song5);
        /*Test your toSummary method using the songAL ArrayList.*/
        for (Song song:songAL
             ) {
            System.out.println(song.toSummary());
        }
        /*Call the sumAwards method using the songAL as a parameter.*/
        System.out.println("Total number of awards: "+sumAwards(songAL));
    }
}

//Output

//if you need any help regarding this solution............... please leave a comment ........... thanks

Add a comment
Know the answer?
Add Answer to:
Part 1: Song Class Write the code for the Song class. 1. Instance variables: title:String, lyrics:String,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the code for the Song class. 1. Instance variables: title:String, lyrics:String, numAwards:int 2. For the...

    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...

  • You may not add any instance variables to any class, though you may create local variables...

    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...

    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...

    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...

    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...

    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...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    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 a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    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...

    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;...

  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT