Results 1 to 17 of 17
  1. #1
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default help..stuck w/ a java programming assignment

    kinda confused on if I am doing the assignment correctly...these are my instructions and i will post my code under and the link

    also here is the link to the chapter 30
    CHAPTER 30 — Defining Your Own Classes

    Click the title of the assignment to see Due Date and submission features.
    Modify the class HelloObject and the class HelloTester discussed in Chapter 30 of CCSU; such that when you run main, two messages are created, a morning greeting and an evening greeting. Use two methods in HelloObject, one for each greeting. Replace the name HelloTester by Assign4_{your last name }. When running the program, output would look something like this :
    Good Morning World !
    Good Evening World !


    Java Code:
    class HelloObject 
    {
    String greeting;
    
    HelloObject( String st )
    {
    greeting = st;
    }
    
    void speak() 
    { 
    System.out.println( "Good Morning World" );
    }
    }
    
    class Assign4_Roberts
    {
    public static void main ( String[] args ) 
    {
    HelloObject anObject = new HelloObject("Good Evening World"); 
    anObject.speak();
    }
    }
    whenever i run it my output is:
    Good Morning World

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    what does your speak() method do?

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    You passed your HelloObject constructor a String, you stored the String in greeting, have you printed it out?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Use two methods in HelloObject, one for each greeting.
    It helps if you actually follow instructions.

  5. #5
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    i updated it to this but don't believe its correct

    Java Code:
    
    class HelloObject                                  
    {
      String greeting;
    
      HelloObject( String st )
      {
        greeting = st;
      }
    
      void speak()                                     
      { 
        System.out.println( "Good Morning World" );
        System.out.println( "Good Evening World");
      }
    }
    
    class Assign4_Roberts
    {
      public static void main ( String[] args )        
      {
        HelloObject anObject = new HelloObject("Good Evening World"); 
        anObject.speak();
      }
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Of course it is not correct. Can you read? If so I draw your attention once again to your instructions.

    Use TWO methods in HelloObject, one for each greeting.

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    It is not, like 1st your assignment told you and then 2nd Junky told you it requires "two methods in HelloObject, one for each greeting". One method is already there called speak().

    What is the value of String greeting when you run this code in main?
    Java Code:
     HelloObject anObject = new HelloObject("Good Evening World");
    Test it out with some println's, poke around the code make it jump don't be afraid of it.

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    I can't help commenting that there is apparently a Java text out there that has 29 chapters before it gets around to Defining Your Own Classes. Ugh.

    -Gary-

  9. #9
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    Look at your speak method. Is this what is it supposed to print? The assignment says to use 2 methods, right now you are using only 1.
    Last edited by rp181; 02-24-2011 at 03:30 AM.

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by gcalvin View Post
    I can't help commenting that there is apparently a Java text out there that has 29 chapters before it gets around to Defining Your Own Classes. Ugh.

    -Gary-
    You have heard of Java in two semesters well this is Java in two lifetimes...

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by rp181 View Post
    Look at your speak method. Is this what is it supposed to print? Where is your greeting String, set in your constructor, used? Where should it be used? *nudge nudge* :)
    His speak method is fine...

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    I think that was the point, a hint that they need to do something almost the same.

  13. #13
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    Dear god you people reply fast. I reread it and just edited my post =p

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Java Code:
    HelloObject anObject = new HelloObject("Good Evening World");
    Another example of not reading the instructions fully. I cannot see where it mentions passing a parameter to the constructor.

  15. #15
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    updated code

    Java Code:
      HelloObject( String st )
      {
        greeting = st;
      }
    
      void morning()                                     
      { 
        System.out.println( "Good Morning World!" );
      }
      void evening()
      {
        System.out.println( "Good Evening World!");
      }
    }
    
    class Assign4_Roberts
    {
    	public static void main ( String[] args ) 
    	{
    	HelloObject anObject = new HelloObject("nothing"); 
    	anObject.morning();
    	anObject.evening();
    	}
    
    	}

  16. #16
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    You do not have to have a constructor parameter, or a constructor at all in this case. You can simply have a method:

    Java Code:
     HelloObject(){
      }
    Because you don't do anything in it, you can simply omit it, and the default constructor is used.

    Which is instantiatied by:

    Java Code:
    HelloObject obj = new HelloObject();
    obj.morning();
    obj.evening();

  17. #17
    supershayd24 is offline Member
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Default Re: help..stuck w/ a java programming assignment

    im new to java to and working on the same project what was missing on his script?
    would it be the same if i had to use
    good morning; everyone !
    good afternoon; everybody !
    and so on?
    Last edited by supershayd24; 02-19-2014 at 04:19 AM.

Similar Threads

  1. First Assignment and already stuck....Newbie Question
    By Danieldcc in forum New To Java
    Replies: 5
    Last Post: 09-24-2010, 11:44 PM
  2. Stuck new to Java
    By Jaguar1998 in forum New To Java
    Replies: 3
    Last Post: 04-09-2010, 12:02 PM
  3. Replies: 1
    Last Post: 10-02-2009, 01:35 AM
  4. Replies: 2
    Last Post: 12-10-2008, 04:40 PM
  5. stuck on an assignment
    By starchildren3317 in forum New To Java
    Replies: 11
    Last Post: 11-19-2008, 11:03 PM

Posting Permissions

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