Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Java questions. Implement a class, Numerical, to perform the computations described (https://www.askmehelpdesk.com/showthread.php?t=801390)

  • Sep 16, 2014, 10:51 AM
    josh3012
    Java questions. Implement a class, Numerical, to perform the computations described
    Java questions.




    Implement a class, Numerical, to perform the computations described below.
    • int sumTo(int m, int n), where m 6 n, to calculate m + (m + 1) + (m + 2) + · · · + n.


    e.g. sumTo(1, 10) =55,
    sumTo(9, 11) = 30.


    • int sumSquareTo(int n), to calculate 12 + 22 + 32 + · · · + n2.


    e.g.
    sumSquareTo(2) = 5,
    sumSquareTo(4) =30.


    • int pow(int n, int k), to calculate nk. Note that n0 = 1.


    e.g. pow(2, 10) = 1024,
    pow(3, 2) = 9,
    pow(3, 0) = 1.


    • int sumPowTo(int n, int k), to calculate 1k + 2k + 3k + · · · + nk.

    e.g. sumPowTo(4, 2)=30,
    sumPowTo(5, 3)=225.
  • Sep 16, 2014, 11:22 AM
    CravenMorhead
    This is a computer science exercise in "for" loops. Each one can be solved using a for loop. It is a matter of figuring out the range and what to do inside. I am not going to give you the answer, I could code this up in about 5 minutes, but all that you need is there. Research the for loop and you will be fine.
  • Sep 17, 2014, 12:11 AM
    josh3012
    @CravenMorhead as you said using the "for loop". But I'm still getting error on return statement. I'm new to this programing by the way. Do I only use on "for loops" in this exercise? Or do I need to use like "if", "else" etc?
  • Sep 17, 2014, 07:14 AM
    josh3012
    How about wihout using "for loop"?
  • Sep 17, 2014, 07:18 AM
    CravenMorhead
    This is a basic class that will multiple two numbers using a for loop. I am a mainly C++ programmer so this might be a little sparse or incorrect but the basic idea WRT java should be there. Techniquely if do your looks right, initialize everything in just the right way you shouldn't need conditionals. In this case the conditionals would ONLY be used to error check parameters. For example in the "Int pow" Function. IF you initialize it the right way you won't need a conditional, but if you put a "if(k==0)return 1;" at the beginning it will ensure that requirement is met, but if you initialize the returnValue to 1 and in each iteration of the loop you multiple the return value by n and then return the returnValue than you won't need it. This is because the for loop won't run if k is 0 so just the returnValue will be returned, if K is greater than one than it will be mutiplied accordingly.

    To make the code hardy you could use "If Else" to make sure the numbers are positive, or figure out what effect this would have on the mathematical operations. Good Luck.

    public class Mathstuff {

    public int multiplyTwoNumbers (int num1, int num2)
    {
    int returnValue = 0;
    int loopValue = 0;

    if(num2 >= 0)
    {
    for(loopValue = 0; loopValue < num2; loopValue++)
    {
    returnValue = returnValue + num1;
    }
    }
    else
    {for(loopValue = 0; loopValue > num2; loopValue--)
    {
    returnValue = returnValue - num1;
    }
    }
    return returnValue;
    }

  • All times are GMT -7. The time now is 09:16 PM.