Page 1 of 4 123 ... LastLast
Results 1 to 20 of 77
Like Tree1Likes

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

  1. #1
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

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

    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();
    }
    }


    Can anyone help me? I need the correct code.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    Please explain the problem(s) you are having by asking some specific questions.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

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

    public static void main(String[] args)
    {
    HelloObject hello1 = new HelloObject("Good Morning; everybody!");
    HelloObject hello2 = new HelloObject("Good Afternoon; everybody!");
    HelloObject hello3 = new HelloObject("Good Evening; everybody!");
    HelloObject hello4 = new HelloObject("Good Night; everybody!");



    hello1.speak();
    hello2.speak();
    hello3.speak();
    hello4.speak();



    HelloObject[] helloArray = new HelloObject[4];
    helloArray[1] = new HelloObject("Good Morning; everybody!");
    helloArray[2] = new HelloObject("Good Afternoon; everybody!");
    helloArray[3] = new HelloObject("Good Evening; everybody!");
    helloArray[4] = new HelloObject("Good Night; everybody!");

    helloArray[0].speak();
    }


    I don't even know if this works, I can't even compile it and I don't know where to put my last name.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    I can't even compile it
    Please copy the full text of the error messages and paste it here.


    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

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

    Can you not test it for me to see if it works? I don't know how to compile.

  6. #6
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

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

    This is the error code I get.
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:1: error: class, interface, or enum expected
    public static void main(String[] args)
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:4: error: class, interface, or enum expected
    HelloObject hello2 = new HelloObject("Good Afternoon; everybody!");
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:5: error: class, interface, or enum expected
    HelloObject hello3 = new HelloObject("Good Evening; everybody!");
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:6: error: class, interface, or enum expected
    HelloObject hello4 = new HelloObject("Good Night; everybody!");
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:10: error: class, interface, or enum expected
    hello1.speak();
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:11: error: class, interface, or enum expected
    hello2.speak();
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:12: error: class, interface, or enum expected
    hello3.speak();
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:13: error: class, interface, or enum expected
    hello4.speak();
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:17: error: class, interface, or enum expected
    HelloObject[] helloArray = new HelloObject[4];
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:18: error: class, interface, or enum expected
    helloArray[1] = new HelloObject("Good Morning; everybody!");
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:19: error: class, interface, or enum expected
    helloArray[2] = new HelloObject("Good Afternoon; everybody!");
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:20: error: class, interface, or enum expected
    helloArray[3] = new HelloObject("Good Evening; everybody!");
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:21: error: class, interface, or enum expected
    helloArray[4] = new HelloObject("Good Night; everybody!");
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:23: error: class, interface, or enum expected
    helloArray[0].speak();
    ^
    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:24: error: class, interface, or enum expected
    }
    ^
    15 errors

    Tool completed with exit code 1

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    I don't know how to compile.
    That will make it impossible for you to program in java.

    First you need to download the JDK and install it.
    Then try writing the HelloWorld program described in the tutorial:
    Lesson: The "Hello World!" Application (The Java™ Tutorials > Getting Started)
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    All java code must be inside a class, interface, or enum. See the HelloObject class definition in post#1 for an example of a class.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

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

    I need someone to help me with my coding... You can't help?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    The main() method you are coding needs to be inside a class definition. See the main() method in the HelloTester class in post#1
    Also look at your class notes and the tutorial for examples: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

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

    I am so lost.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    Start with the simpler code posted in the first post. Compile and execute that.

    When that works make one small change to it, compile and execute that. etc
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

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

    Is there anyway we can talk via Skype, TeamSpeak, Or Ventrillo? I am taking this class online and our instructor doesn't help us at all.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    Sorry, this is it.

    Copy the code in post#1 into a file named: HelloTester.java
    compile it with the javac command
    execute it with the java command.

    If there are error messages, copy the full contents of the command prompt window and paste it here.

    The tutorial has samples for how to do that.
    Lesson: The "Hello World!" Application (The Java™ Tutorials > Getting Started)
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

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

    I thought this was help? I see other people post their code and others help with the code. All I get is instructions?

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by Clemson View Post
    I thought this was help? I see other people post their code and others help with the code. All I get is instructions?
    Yes, instructions how to correct your code; if you were expecting us to deliver the correct code for you, so that you only had to copy and paste it, you're in the wrong forum.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

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

    Okay, I did this and compiled the first part and I got this:

    class HelloObject
    {
    String greeting;

    HelloObject( String st )
    {
    greeting = st;
    }
    public static void main(String[] args)
    {
    HelloObject hello1 = new HelloObject("Good Morning; everybody!");
    HelloObject hello2 = new HelloObject("Good Afternoon; everybody!");
    HelloObject hello3 = new HelloObject("Good Evening; everybody!");
    HelloObject hello4 = new HelloObject("Good Night; everybody!");

    C:\Users\Bradley Abbott\Documents\Assign4_abbott.java:15: error: reached end of file while parsing
    HelloObject hello4 = new HelloObject("Good Night; everybody!");

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    Have you tried copying exactly the code from post#1 and then compiling and executing it? What happened?

    When that works, make one small change to it, compile and execute it again.

    The code in post#17 looks like it has had many changes done to it.

    Try again and take smaller steps.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

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

    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 compiled this and got no errors.

Page 1 of 4 123 ... LastLast

Similar Threads

  1. hello need help to correct my code
    By yanalvine in forum New To Java
    Replies: 2
    Last Post: 10-18-2013, 11:20 AM
  2. Very new to Java. Please help me correct my code.
    By jpab29 in forum New To Java
    Replies: 5
    Last Post: 06-29-2012, 04:05 PM
  3. Please correct this JSP code
    By rajesh4851 in forum New To Java
    Replies: 5
    Last Post: 06-13-2011, 12:12 PM
  4. Replies: 10
    Last Post: 06-06-2010, 10:12 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
  •