Ask Experts Questions for FREE Help !
Ask
    josh3012's Avatar
    josh3012 Posts: 9, Reputation: 1
    New Member
     
    #1

    Sep 16, 2014, 10:51 AM
    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.
    CravenMorhead's Avatar
    CravenMorhead Posts: 4,532, Reputation: 1065
    Adult Sexuality Expert
     
    #2

    Sep 16, 2014, 11:22 AM
    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.
    josh3012's Avatar
    josh3012 Posts: 9, Reputation: 1
    New Member
     
    #3

    Sep 17, 2014, 12:11 AM
    @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?
    josh3012's Avatar
    josh3012 Posts: 9, Reputation: 1
    New Member
     
    #4

    Sep 17, 2014, 07:14 AM
    How about wihout using "for loop"?
    CravenMorhead's Avatar
    CravenMorhead Posts: 4,532, Reputation: 1065
    Adult Sexuality Expert
     
    #5

    Sep 17, 2014, 07:18 AM
    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;
    }

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

How to use the contents of another class in java? [ 0 Answers ]

http://i.imgur.com/4G55Kcw.jpg For example, in this program I want to use the contents of the computerchoice.java and playerchoice.java. How would I do it?

Having trouble with DaoImplementation class Java [ 0 Answers ]

Hi, I have a DaoHibernateImpl class. I am getting error when I am running my webapplication and the error is this class. Here is the code for OfferBankDaoHibernateImpl.java package com.safeway.app.emom.dao.hibernate; ... public class OfferBankDaoHibernateImpl extends...

Decompling java .class [ 2 Answers ]

How can I get a .java file from .class file? Is it possible?

Java Virtual Machine Launcher - Could not find the main class [ 1 Answers ]

When I run a executable file, I am gettibg a error message. ' Java Virtual Machine Launcher ' - Could not find the main class.Program will exit. Can any provide me the solution what the problem maybe? Orelse should I install any software to rectify it?


View more questions Search