CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Help with Java

  1. #1
    Join Date
    Jan 2010
    Posts
    1

    Help with Java

    Ok so I started Java a week ago and have done some pretty basic stuff but I am having a problem getting the code straight in my brain and lack confidence in my coding knowledge as of now to be sure I am doing the right thing.

    I am going to type out the code that I have and see what everyone thinks and please give feedback I am not asking for answers or anything just want a little boost in confidence is all and maybe some pointers on how to better myself as a coder here goes nothing.

    The assignment is to create an application where the main() method holds two integer variables. I have to assign values to both. I need to pass both variables to methods named
    sum() and difference(). These methods have to compute the sum of and difference between the values of two arguments. I also have to add another method called product() and it should do all the above but instead of displaying the answers it has to return the answer to the calling method, which displays the answer. This is what I have:

    public class Numbers
    {
    public static void main(String[] args)
    {
    int firstNum = 40; ( these are the values I assigned)
    int secondNum = 132;(see above)
    computeSum(firstNum, secondNum);
    computeDiff(firstNum, secondNum);
    computeProduct(firstNum, secondNum);
    }
    public static void computeSum(int firstNum, int secondNum)
    {
    double sum;
    sum = firstNum + secondNum;
    System.out.println("\nThe sum of " + firstNum +
    " plus " + secondNum + " is " + sum);
    }
    public static void computeDiff(int firstNum, int secondNum)
    {
    double diff;
    diff = firstNum - secondNum;
    System.out.println("\nThe difference of " + firstNum
    " minus " + secondNum + " is " + diff);
    }
    public static void computeProduct(int firstNum, secondNum)
    {
    double product;
    product = firstNum * secondNum;
    System.out.println("\The product of " + firstNum +
    " times " + secondNum + " is " + product);
    return product;
    }
    }

    So that is what I have done and spent the last 2 hours trying to figure out, the part I am really confused on is the return product; part is that what the question is asking? I get lost when it talks about calling a method and what not any help would be awsome and if it is correct even better just thought I would get on and ask some people with more knowledge than myself.

    -Andrew-

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help with Java

    Well it's a pretty good effort for a first time programmer.

    Some comments:
    Coding comments start with // or are wrapped in /* ... */ and are not in in brackets

    Why are you using a double to store the result of integer math. If you are worried about overflowing an integer (which is a realistic problem for sum and product operations) then use a long.

    Your product method is returning the result but is declared with a return type of void (ie not returning anything). You need to change the method declarartion.

    Your product method is not supposed to be printing out anything but contains a print method.

    The code in your main method that calls the product method needs to assign the returned value to a variable and then you need to print it out.

    As for whether it's correct or not the sure fire way to tell is to compile it (which it won't!!) and run it with several test cases and see if it does what it's supposed to.

    BTW When posting code please use code tags

  3. #3
    Join Date
    Jul 2009
    Posts
    105

    Re: Help with Java

    Well, i can give you a tip regarding the way it is organized. When you are doing a System.out.println(""); you can keep all of the text on one line so that you dont use up alot of lines.
    Also with println(""); it will automatically make the next thing you send out for the application to see on a new line. for instance
    Code:
    System.out.println("Hello my name is: ");
    System.out.println("Alex!");
    will display
    Code:
    Hello my name is:
    Alex
    So there is no need for the "\n" on every println

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help with Java

    When you are doing a System.out.println(""); you can keep all of the text on one line so that you dont use up alot of lines.
    I suspect the OP is aware of that and he/she has probably split it across lines so they can see all of the code in their editor without having to scroll left and right.
    Also with println(""); it will automatically make the next thing you send out for the application to see on a new line
    Given the OP is already using println one would suspect they are also aware of this and just wanted a blank line before the text. Of course they would have been better off using system.println() to get a blank line rather than embedding a control character but this is a minor issue.

  5. #5
    Join Date
    Jul 2009
    Posts
    105

    Re: Help with Java

    Quote Originally Posted by keang View Post
    I suspect the OP is aware of that and he/she has probably split it across lines so they can see all of the code in their editor without having to scroll left and right.
    Given the OP is already using println one would suspect they are also aware of this and just wanted a blank line before the text. Of course they would have been better off using system.println() to get a blank line rather than embedding a control character but this is a minor issue.
    Yea i am aware, he was asking for some ways to make him a better coder, i was just helping him with making it easier to read and understand what was going on in coding.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured