Results 1 to 4 of 4
  1. #1
    gamecock is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Default Need help creating multiple objects

    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.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Need help creating multiple objects

    In Java, a class is just a "blueprint" for as many objects and you want. So you can simply use the same syntax:

    Java Code:
    public static void main(String[] args)
    {
    	HelloObject hello1 = new HelloObject("1 greeting");
    	HelloObject hello2 = new HelloObject("2 greeting");
    	HelloObject hello3 = new HelloObject("3 greeting");
    	HelloObject hello4 = new HelloObject("4 greeting");
    	
    	// Then, when you want to refer to that object, just use its name:
    	
    	hello1.speak();
    	
    	hello3.speak();
    	
    	//etc...
    	
    	// I don't know if you've learned about arrays yet. If not, you will eventually:
    	
    	HelloObject[] helloArray = new HelloObject[4];
    	helloArray[0] = new HelloObject("0 greeting");
    	helloArray[1] = new HelloObject("1 greeting");
    	helloArray[2] = new HelloObject("2 greeting");
    	helloArray[3] = new HelloObject("3 greeting");
    	
    	helloArray[0].speak();
    }

    Also, in the future, please surround your code with [code][/code] tags.

  3. #3
    gamecock is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Default Re: Need help creating multiple objects

    Quote Originally Posted by AndrewM16921 View Post
    In Java, a class is just a "blueprint" for as many objects and you want. So you can simply use the same syntax:

    Java Code:
    public static void main(String[] args)
    {
    	HelloObject hello1 = new HelloObject("1 greeting");
    	HelloObject hello2 = new HelloObject("2 greeting");
    	HelloObject hello3 = new HelloObject("3 greeting");
    	HelloObject hello4 = new HelloObject("4 greeting");
    	
    	// Then, when you want to refer to that object, just use its name:
    	
    	hello1.speak();
    	
    	hello3.speak();
    	
    	//etc...
    	
    	// I don't know if you've learned about arrays yet. If not, you will eventually:
    	
    	HelloObject[] helloArray = new HelloObject[4];
    	helloArray[0] = new HelloObject("0 greeting");
    	helloArray[1] = new HelloObject("1 greeting");
    	helloArray[2] = new HelloObject("2 greeting");
    	helloArray[3] = new HelloObject("3 greeting");
    	
    	helloArray[0].speak();
    }

    Also, in the future, please surround your code with [code][/code] tags.

    Thank you so much for your help! I apologize about not using the code tags, i'm brand new here.

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Need help creating multiple objects

    Not a probs, just letting you know. :) Glad I could help.

Similar Threads

  1. How to add Multiple Objects in Frame
    By AryanArs in forum New To Java
    Replies: 5
    Last Post: 02-26-2012, 08:05 PM
  2. Multiple objects in a class help please
    By patcc2 in forum New To Java
    Replies: 12
    Last Post: 01-11-2011, 02:35 PM
  3. do we need multiple connection objects
    By Pacerier in forum Java Servlet
    Replies: 4
    Last Post: 11-27-2010, 06:16 PM
  4. Multiple Graphics Objects?
    By MrFish in forum Java 2D
    Replies: 7
    Last Post: 10-29-2010, 07:37 PM
  5. Can I store multiple objects in an array
    By lareauk in forum New To Java
    Replies: 9
    Last Post: 05-29-2008, 03:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •