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 4 of 4

Thread: I have tried this many times and I don't get the correct code.

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I have tried this many times and I don't get the correct code.

    Here is our assingment.
    I'm in an introductory to Java class, and I have this assignment.

    "Go to Page 19 of Chapter 30 of the CCSU Course. Use the HelloObject class and the HelloTester Class you see on that page as the starting point of this Assignment. Make modifications such that when you run main, these four messages are created :
    Good Morning World
    Good Afternoon World
    Good Evening World
    Good Night World
    Replace the name HelloTester by Assign4_{your last name }.
    Note that main should be creating 4 Objects. An object stores one greeting message."

    this is the HelloObject and HelloTester this assignment is talking about.

    class HelloObject
    {
    String greeting;

    HelloObject( String st )
    {
    greeting = st;
    }

    void speak()
    {
    System.out.println( greeting );
    }
    }

    class HelloTester
    {
    public static void main ( String[] args )
    {
    HelloObject anObject = new HelloObject("A Greeting!");
    anObject.speak();
    }
    }

    I'm honestly not sure where to go from here when making the multiple objects.
    Can someone show me the correct code? I try and I get so many errors.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have tried this many times and I don't get the correct code.

    Also posted at: I have tried this many times and I don't get the correct code.

    This forum has the same policy and some of the same people as the other.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I have tried this many times and I don't get the correct code.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

    Please edit your post to post the code correctly.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    My Mood
    Fine
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I have tried this many times and I don't get the correct code.

    You need to create objects within the HelloTester class. The code you submitted:

    class HelloTester
    {
            public static void main ( String[] args )
            {
                  HelloObject anObject = new HelloObject("A Greeting!");
                  anObject.speak();
             }
    }

    creates 1 object referenced by 'anObject'. You can create the remaining objects, either by creating more objects as above, but changing the variable name that references the object. i.e. 'anotherObject', or if you have been taught it yet, use loop conditions. i.e for loop.

    if you are unsure of creating the objects, within your main() method:

    class HelloTester
    {
            public static void main ( String[] args )
            {
                  HelloObject anObject = new HelloObject("A Greeting!");
                  anObject.speak();
                  HelloObject anotherObject = new HelloObject("Another Greeting!");
                  anotherObject.speak();
                  // the remaining objects would be put underneath. 
             }
    }

Similar Threads

  1. [SOLVED] Don't understand why it loops through multiple times...
    By Discoveringmypath in forum Loops & Control Statements
    Replies: 4
    Last Post: January 26th, 2013, 09:00 PM
  2. Using the same scanner multiple times in code help
    By theostorm in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 16th, 2012, 11:30 PM
  3. Can anybody correct the code?
    By ur2cdanger in forum JDBC & Databases
    Replies: 1
    Last Post: October 17th, 2011, 07:07 PM
  4. while(true){ section of code runs 200 times}
    By jack_nutt in forum Java Theory & Questions
    Replies: 7
    Last Post: June 23rd, 2011, 07:06 AM
  5. Replies: 27
    Last Post: February 17th, 2011, 05:42 PM