Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# help with this problem

jayson clark
Greenhorn
Posts: 14
I need write a method, called calculateFunction, that calculates the value of y for various values of x. The declaration of this method is below:

public int calculateFunction(int x);
The calculateFunction method calculates the function y = x + x2 + 2x. To calculate the values of this expression, you can use:

x2 = x * x
2x = Math.pow(2,x)

Note that because calculateFunction works in terms of int, and Math.pow returns a double, you must cast the value returned to an int, or Java will give you a compiler error about loss of precision.

Once the function has been calculated, print out the relative size of the result by calling the method printStars used in module 3 of the course modules. Calculate this function and print out its relative size for values from 1 to 10. Note that to make the number of stars reasonable, you should print out 1 star for each value of 10 returned (for example, if 62 is returned, you should print out 6 stars). You can do so by dividing by 10, noting that the value will be truncated to the next lowest int value.

Im very new and can anybody help be how to begin this problem..just the basic idea? plzzz...

Jeff Albertson
Ranch Hand
Posts: 1780
Perhaps if you posted your instructor's email account we could directly
email the solution to them, and that would save you even more time. :roll:

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24211
35
You also need to comply with the JavaRanch JavaRanch policy on display names before most folks will feel inclined to help you. You can change your name to a suitable firstname lastname combination by visiting here.

Mark Spritzler
ranger
Sheriff
Posts: 17278
6
JC, this is your second notice at least. If you don't comply with the naming policy in 24 hours, your account will be suspended.

Mark

jayson clark
Greenhorn
Posts: 14
well im not asking for the exact solution, im only asking for some hint or suggestion on how to start the program...

Junilu Lacar
Bartender
Posts: 7466
50
Well, if it's a hint you're looking for:

Mark Spritzler
ranger
Sheriff
Posts: 17278
6
Jayson, thanks very much for changing your display name, looks really good now.

Mark

Mark Spritzler
ranger
Sheriff
Posts: 17278
6
OK, so you have this

public int calculateFunction(int x) {
}

Inside it will calculate your function.

Do you understand this statement?
Note that because calculateFunction works in terms of int, and Math.pow returns a double, you must cast the value returned to an int, or Java will give you a compiler error about loss of precision.

Not sure if that is you posting or from your instructor's instructions.

After you get the result, you need to divide by 10, which of course there you should see a float/double, but I think to get the lower int value there is a method in Math that you can find.

If you haven't looked at the Java API Javadocs, I suggest you look there for what methods are available and what they do.

Here's a link to the main JavaDoc page, on the left you can scroll through the classes and find the Math class, click that link and on the right is the information you will need.

Good Luck

Mark