Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to take input dynamically and display?

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to take input dynamically and display?

    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.


  2. #2
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: Birthday Song Class

    BirthdaySong b2 = new BirthdaySong("Grace");
    b2.sing();
    Here creating BirthdaySong object you pass a string: "Grace"

    in BirthdaySong class you have a constructor that looks like this:
    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
    }

    but here you dont take any strings, so what you have to do is to create a new constructor that takes string variable which will be used as name:

    public BirthdaySong(String name) {
        this.myName = name; //now you change myName variable to be the same as the name that was given (f.e.: "Grace")
    }

    I hope this helped,
    Dal.

Similar Threads

  1. Replies: 4
    Last Post: June 5th, 2009, 06:11 AM
  2. HAPPY BIRTHDAY JAVA PROGRAMMING FORUMS!
    By JavaPF in forum The Cafe
    Replies: 30
    Last Post: June 4th, 2009, 08:32 AM