jayson clark

Greenhorn

Posts: 14

posted 11 years ago

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...

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

posted 11 years ago

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.

jayson clark

Greenhorn

Posts: 14

posted 11 years ago

Well, if it's a hint you're looking for:

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

posted 11 years ago

OK, so you have this

public int calculateFunction(int x) {

}

Inside it will calculate your function.

Do you understand this statement?

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

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