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

Thread: simple java help needed

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default simple java help needed

    I've tried these equations but my solutions always come out wrong. I feel like I'm just making simple mistakes, such as using void instead of double, but I just don't know.

    #1: Write a public method called average, which is passed three ints, and returns their average as a double.
    Example: (5, 9, 6) --> 6.66666666666666

    #2: Write a public method called doubleWord, which is passed a String as a parameter and which returns the input parameter concatenated with itself-- a String value.

    Example: "boo" --> "booboo"

    #3: In the box below, enter the code that will print the pattern "01" 100 times. Use a for loop to do this.


  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: simple java help needed

    Do you have any specific questions?

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: simple java help needed

    Those are specific questions.

    for example,

    for number two I have, but it's wrong:
    public char doubleWord(String x)
    {
    x="boo";
    x=x+x;
    Return(x);
    }
    Last edited by miss confused; June 26th, 2010 at 11:31 PM.

  4. #4
    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: simple java help needed

    but it's wrong:
    Please explain. Can you show what is wrong?

  5. #5
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: simple java help needed

    I think what norms trying to say is that you seem to have missed sort of fundamental information about methods, and if you're ever going to get better this is some really important stuff to know.
    I think you've missunderstood the assignment btw. You dont want a method that declares boo and then creates booboo. You want a method that takes in a string, and returns (string + string). If your method would work, and be passed the value feet, it should return feetfeet. But yours would return booboo.

    Pro tips: Download a nice IDE if youre not using one already. I recommend netbeans or eclipse. It'll help you lots and lots.
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  6. #6
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: simple java help needed

    Quote Originally Posted by miss confused View Post
    Those are specific questions.

    for example,

    for number two I have, but it's wrong:
    public char doubleWord(String x)
    {
    x="boo";
    x=x+x;
    Return(x);
    }
    Hello Miss Concfused

    Here is an example of what I like posts requesting help to look like.

    First post your code, either the whole thing, or the specific problem area. You did so in this case, you just need to use the code tags.
    public char doubleWord(String x){
        x="boo";
        x=x+x;
        Return(x);
    }

    Then state the result of you code. Will it compile. If it will, then what does it result in, and why is that unexpected to you.
    In this case:
    My code wont compile, and when I got it to compile it returned "booboo" rather than the input concatenated input string.

    As for you method, there are a couple if things wrong.
    -Your return type is char. I cant see why you have chosen that, it should be String.
    -return is a keyword in java, and it is case sensitive. Return != return. It should be return here. Also the item returned does not go in brackets.
    The logical problem in the method:
    -Your local variable x of type String is overwritten by x="boo". So no matter what you chose as input the result will be "boo" + "boo" = "booboo".
    The concatenation works as it should, so with above fixes it should be fine.

  7. #7
    Junior Member
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: simple java help needed

    Took a few tries, but I finally got it. Thanks!

Similar Threads

  1. Java object help(should be simple)
    By Mirak in forum Object Oriented Programming
    Replies: 5
    Last Post: May 8th, 2010, 09:43 PM
  2. Help needed on java array
    By rossfally in forum Collections and Generics
    Replies: 2
    Last Post: March 4th, 2010, 08:49 PM
  3. Urgent Help needed with java codes
    By makarov in forum Java Theory & Questions
    Replies: 0
    Last Post: November 13th, 2009, 07:23 AM
  4. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM
  5. Java NullPointer Exception in Server chat program
    By Valtros in forum Exceptions
    Replies: 1
    Last Post: May 8th, 2009, 05:06 AM

Tags for this Thread