public class BirthdaySong { // Part of Step 2 // Add a private String instance variable myName private String myName; public BirthdaySong() { // expect to see myName = .. ^^^ // needs to be initialized in the constructor // it is setup so it needs to be used by the methods } //Add a constructor so that its parameter is String name // and assign name to myName in the constructor. // BirthdaySong song1 = new BirthdaySong(); // Song1.sing(); (IN CLASS NOTES) // BirthdaySong song2 = new BirthdaySong ("Grace"); public void sing() { // expect to see myName being used here System.out.println("Happy birthday to you"); System.out.println("Happy birthday to you"); System.out.println("Happy birthday dear "); System.out.println("Happy birthday to you"); // Add a method setName that changes myName to a given name from the client. // The method stub would look like this: // song1.setName("Bob") public void setName(String name) { // Your code here //expect to see myName being used here } }public class BirthdaySongTest { public static void main(String[] args) { // 1. Test the BirthdaySong class. // * Instantiate a BirthdaySong object named b1. // * Have b1 print the song BirthdaySong b1 = new BirthdaySong(); b1.sing(); // To add the name "Alice" in the bday song - // - Instance field: stores info in an object. // - instance fields include Year, Month, Day (in Project 1) // - public void set Year (int Year) { // } // public int getYear() { // return 0 // - Needs "access specifier", needs type, needs a name // Instantiate a BirthdaySong object named b2 using the name // "Grace" to sing to. // Have b2 sing. BirthdaySong b2 = new BirthdaySong("Grace"); b2.sing(); } }
Im confused on what to do next, these are some of the instructions i still need:
Modify the BirthdaySong class so that it sings the song to a person whose name is supplied by the client.
Add a private String instance variable myName.
Modify the constructor so that its parameter is String name and assign name to myName
in the constructor.
Modify the sing method so that it uses myName.
Happy birthday to you
Happy birthday to you
Happy birthday dear <myName here> !!!
Happy birthday to you
Don't forget to print the !!!
Add a method setName that changes myName to a given name from the client. The method stub would look like this:
public void setName(String name) {
// Your code here
}
Test your modifications.
Instantiate a BirthdaySong object named b2 using the name "Grace" to sing to.
Have b2 sing.
Set the name for b2 to "Blaise" using the setName method.
Have b2 sing again.
Set the name for b2 to "Alan" using the setName method.
Have b2 sing again.