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

Thread: A Few Quick, Easy Questions

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A Few Quick, Easy Questions

    Hello! I'm completely new to Java, and I have small questions I must answer weekly for my class. Here are the questions, along with my answers. Can you please tell me if I did them correctly, and if I didn't, what I did wrong and how to fix them? I tried my best :X I don't want to submit them unless they're right b/c I obviously I won't get any credit if they're wrong.

    1. Write a public method called largerStr, which is passed two Strings, s1 and s2, and returns true if s1 is strictly longer than s2, false otherwise.

    Example: largerStr("here", "there") --> false
    Example: largerStr("there", "here") --> true
    Example: largerStr("hear", "here") --> false

    A:
    I'm pretty sure this one is incorrect.
    public bool largerStr(string s1, string s2)
    {
    return (s1.length>s2.length);
    }

    2. Write a public method called signFlip, which is passed a double and returns that value with the opposite sign.

    Example1: 4.0 --> -4.0
    Example2: -3.14 --> 3.14

    Note that in this example you must write a complete method, including the method header line.

    A:

    public double signFlip(double x)
    {
    return (x * -1.0)
    }

    3. Write a public method called doubleOut, which is passed a String as a parameter, and which prints to the console that String concatenated with itself. Note that this method does not return anything.

    Example: "boo" --> prints out booboo

    Note that in this example you must write a complete method, including the method header line.

    A:
    I'm pretty sure I didn't do this one correctly either.
    public void doubleOut(string x)
    {
    System.out.println(x + x);
    }

    4. Write a public method called sumNumbers, which is passed a positive int as a parameter and returns the sum of the positive numbers that are less than or equal to the supplied int.

    Example: 5 --> 15 since (5 + 4 + 3 + 2 + 1) = 15
    Example: 3 --> 6 since (3 + 2 + 1) = 6

    Note that in this example you must write a complete method, including the method header line.

    A:

    public int sumNumbers(int x)
    {
    int sum=0;
    for (int i=x; i>0; i--)
    {sum+=i;}
    return sum;
    }

    Thanks for you help!!!

    P.S. Sorry if I posted this in the wrong forum :X


  2. #2
    Member
    Join Date
    Feb 2010
    Location
    Auburn, AL
    Posts
    31
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: A Few Quick, Easy Questions

    All of those look right to me. Why do you think they're wrong?

    1. The only things that could be wrong here are your use of "bool" and "string" (in Java, these might be "Boolean" and "String", so if your code fails to compile you can try that... I'm sure about String, not immediately so about bool/Boolean).

    2. This should be fine. If you're trying to test it from within the class in which it is defined, you'll need to make it static to test it with the main method. Otherwise, this looks gold.

    3. Looks good, but see previous comments.

    4. This looks like it should work fine. No problems. You could make it more efficient by using the closed-form solution to the summation, but your algorithm is correct.

    What problems are you having? If you're new to Java and you got all that, you should be proud of yourself. I don't see anything wrong with it at all... outside a couple of those possible reasons for its not compiling.
    Let me know what you think of my website:
    http://www.auburn.edu/~carpept
    Comments or suggestions are appreciated!

Similar Threads

  1. Need some serious quick help. :/
    By aanders5 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 23rd, 2010, 06:05 PM
  2. Just Need Some quick help with code
    By mulkman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2010, 11:11 AM
  3. String + Compare // Might be too easy for ya
    By Jangan in forum Java Theory & Questions
    Replies: 1
    Last Post: October 18th, 2009, 05:40 PM
  4. Replies: 6
    Last Post: April 14th, 2009, 08:02 AM
  5. JAVA Image Icon and JButton resizing problem
    By antitru5t in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2009, 04:39 AM