Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# stars

Tammy Kline
Greenhorn
Posts: 20
This is what I'm supposed to do with my program:

In this project, we wish to see the growth of the function y = x + x^2 + 2^x. You should 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 + x^2 + 2^x. To calculate the values of this expression, you can use:

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

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.

This is what I've written:

It compiles correctly, but instead of giving me the following:

1
2 *
3 **
4 ***
5 ******
6 ************
7 ***************************
8 *****************************************************
9 *************************************************************************
****
10 ************************************************************************
************************************

It only gives me:

**********

Please take a look at my code and give me some hints as to what exactly I'm missing. Any help would be greatly appreciated!

Campbell Ritchie
Sheriff
Posts: 50258
79
Quite a lot of things going wrong, I am afraid. A few suggestions, but these don't sort everything out:

You can shorten your calculate funtion:Why are you declaring these methods static? Get rid of the static declarations. Move everything out of your main method into a new non-static method. Call it something like go. Then put as the entire text of your main method. A main method is not supposed to contain lots of code, but only to set the application going.

Alter the loop by adding a couple of check lines to it. That should allow you to see what is going wrong.

Tammy Kline
Greenhorn
Posts: 20
Originally posted by Campbell Ritchie:
Alter the loop by adding a couple of check lines to it. That should allow you to see what is going wrong.

First piece of advice I took from you was to do the check lines. I did not alter anything else because I wanted to see what it would do.

The check came back, and it told me that x = everything that i wanted it to ... so it looks like I'm doing the main wrong. One of the instructions from the prof was to call the other two methods from the main method ... which I thought I was doing (but obviously not). That's what I'm working on now.

Thanks for the help!

Garrett Rowe
Ranch Hand
Posts: 1296

First piece of advice I took from you was to do the check lines. I did not alter anything else because I wanted to see what it would do.

The check came back, and it told me that x = everything that i wanted it to ..

Maybe the first thing you should do is try to describe, in words, what the printStars() method should do.

Campbell Ritchie
Sheriff
Posts: 50258
79
Agree with Garrett Rowe. The idea of the two lines with //test at the end was to allow you to follow your loops.

Campbell Ritchie
Sheriff
Posts: 50258
79
Thank you for the PM; it would have been better to post it as a reply here.
posted April 22, 2007 07:05 PM Profile for Teresa Dize Ok. I can't figure it out. I thought that it would be something like this (if I'm understanding my book and the other stuff I've found):

code:

public static void main(String[] args) { //start main

y.calculateFunction();
numberofStars.printStars();
System.out.println(" ");

} //end main

But that's giving me an error saying it can't find the variable y or numberofStars. What am I missing?
I have gone back to your original posting, and run that. And like you, I got a printout of ten stars. Like this:-
**********
To get the thing to compile, suggest:-
• printStars method with the two //test lines in: no other change.
• Move the entire contents of your present main method into a new method, let's call it "private void go()"
• Put the following line into the main method: "new Stars().go();"
• Delete the import; you aren't using anything from java.util.
• You now have:Go through your coding very carefully and find how many times you use "numberofstars" and remember that capital letters mean something different from small letters in Java. Put "%n" at the start of the first //test line, as above.

I did all that lot and this is what I got:
[Campbell@dhcppc0 disk]\$ javac Stars.java
[Campbell@dhcppc0 disk]\$ java Stars

In loop: x = 1, numberOfStars = 0 , *
In loop: x = 2, numberOfStars = 1 , *
In loop: x = 3, numberOfStars = 2 , *
In loop: x = 4, numberOfStars = 3 , *
In loop: x = 5, numberOfStars = 6 , *
In loop: x = 6, numberOfStars = 10 , *
In loop: x = 7, numberOfStars = 18 , *
In loop: x = 8, numberOfStars = 32 , *
In loop: x = 9, numberOfStars = 60 , *
In loop: x = 10, numberOfStars = 113 , *
[Campbell@dhcppc0 disk]\$
Now go back to your "for" loop and work out what it supposed to do and what it is actually doing.

Campbell Ritchie
Sheriff
Posts: 50258
79
Actually, the bit about numberOfStars in the printing method might be my mistake. Make sure every reference to numberOfStars or numberofStars has exactly the same spelling, with all the same capital letters. numberOfStars is the preferred form.

Tammy Kline
Greenhorn
Posts: 20
This is what it's supposed to look like (but with stars):

[Campbell@dhcppc0 disk]\$ javac Stars.java
[Campbell@dhcppc0 disk]\$ java Stars

In loop: x = 1, numberOfStars = 0 , *
In loop: x = 2, numberOfStars = 1 , *
In loop: x = 3, numberOfStars = 2 , *
In loop: x = 4, numberOfStars = 3 , *
In loop: x = 5, numberOfStars = 6 , *
In loop: x = 6, numberOfStars = 10 , *
In loop: x = 7, numberOfStars = 18 , *
In loop: x = 8, numberOfStars = 32 , *
In loop: x = 9, numberOfStars = 60 , *
In loop: x = 10, numberOfStars = 113 , *
[Campbell@dhcppc0 disk]\$

I was getting that as well. That's what I'm supposed to get except instead of the words, just the numbers 1-10 and the numberofStars being actual stars instead of numbers!

I'm going to have to work more on this one later ... I'd definitely like to figure out exactly what I'm missing but right now I gotta move on to my next program. This class is quickly coming to a close (May 5 is my exam) and I've still got one more program to write <phew> ... this is a tough class!!

anyway, again, thank you for you help with this program ... if I figure out what my last bit is supposed to be to print it out correctly, I"ll post it on here.

I'm sure you'll be hearing from me soon ... that last program ... yeah, fun!

Campbell Ritchie
Sheriff
Posts: 50258
79
Follow Garrett Rowe's suggestion: go through the output, which you say is the same as what I got, and write

[LIST][*]The first time it was supposed to print 0 stars and printed 1 star.[*]The second time it was supposed to print 1 star and printed 1 star.