944,100 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2153
  • Java RSS
Nov 28th, 2005
0

Project Help: Growth of a Function

Expand Post »
I really need help. I did the code, but I know it is all wrong. I am not sure how to fix it. This program has boggled me for a week.
I am watching the growth of the function. First I have to calculate a function after entering the value of x from a prompt. Then I have to use printstars to print the relative size for values from 1 to 10.
I am a bad programmer. I already know this.
Does anyone see anything wrong with my code?

:cry:
Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.math.*;
  3.  
  4.  
  5. public class GrowthofaFunction {
  6.  
  7. /**
  8. * @param args
  9. */
  10. public static void main(String[] args) {
  11. double x;
  12. int y;
  13.  
  14. System.out.println("Enter value for x: ");
  15. public int calculateFunction(int x);
  16. {
  17. y = x + (x*x) + Math.pow(2,x);
  18. return y;
  19. }
  20.  
  21. //printStars has input x
  22. for (int y = 0; x > y; x++)
  23. {
  24. x = y/10;
  25. }
  26. System.out.print("*");
  27.  
  28. }
  29. return;
  30. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blackbabydoll is offline Offline
22 posts
since Dec 2004
Nov 28th, 2005
0

Re: Project Help: Growth of a Function

I don't see any code taking input. Look into the bufferedreader.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Nov 28th, 2005
0

Re: Project Help: Growth of a Function

Quote originally posted by server_crash ...
I don't see any code taking input. Look into the bufferedreader.
Yeah that would be a solution. I knew something important was missing like that. :o

I will post the new code and hope that someone else could tell me what i did wrong. I don't think I did like horribly bad. But I know those printStars are wrongggggg. I never got increments and decrements even when I took C++.

Sad huh? :eek:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blackbabydoll is offline Offline
22 posts
since Dec 2004
Nov 28th, 2005
0

Re: Project Help: Growth of a Function

Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.math.*;
  3. import java.util.*;
  4.  
  5.  
  6. public class GrowthofaFunction
  7. {
  8.  
  9. /**
  10. * @param args
  11. */
  12. public static void main(String[] args) {
  13.  
  14. public void calculateFunction(int x)
  15. {
  16. final int y1;
  17. final int y2;
  18. final int y;
  19.  
  20. Scanner stdin = new Scanner(System.in);
  21.  
  22. System.out.print("Enter value for x: ");
  23. x = stdin.nextInt();
  24.  
  25. y1 = x + (x*x);
  26. y2 =(int) (Math.pow(2,x));
  27. y = y1 + y2;
  28. return;
  29.  
  30.  
  31. // printStars has input x
  32.  
  33. for (int i = 0; i > 0; i++)
  34. {
  35. x = y/10;
  36. }
  37. System.out.print("*");
  38. return;
  39.  
  40.  
  41. }
  42.  
  43. }


This is the updated code. I just accomplished. I hope this is right. Anyone see anything worth checking? I finally figured it out. Thank God.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blackbabydoll is offline Offline
22 posts
since Dec 2004
Nov 28th, 2005
0

Re: Project Help: Growth of a Function

Yeah, you've got a problem with the loop. Fix that, and then I'll take a look at the rest.
Java Syntax (Toggle Plain Text)
  1. for (int i = 0; i < 10; i++)
  2. {
  3. x = y/10;
  4. System.out.print("*");
  5. }

You want to increment while a condition is true. For instance, in this case we start with:
i = 0;
which is our couting variable.

The second thing is the condition. The condition here is:
i<10;

That means we will keep looping as long as 'i' is less than 10.

The third thing in the loop, is the increment statement. In this example:

i++

means that for each round in the loop, 'i' will be incremented by one. The first time you program runs the looping looks like this;


i=0
i IS less than 10
i is incremented by one(now it is 1)
a start is printed

Second time:
i=1 //from the first increment
i is still less than 10
i is incremented //now equals 2
Another star is printed.


It goes on and on until the looping condition is not true(i is greater than or equal to 10).
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Nov 28th, 2005
0

Re: Project Help: Growth of a Function

Opps, you might want to take the x = y/10 out of the loop. There's no reason to do that 10 times!
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Nov 28th, 2005
0

Re: Project Help: Growth of a Function

Quote originally posted by server_crash ...
Opps, you might want to take the x = y/10 out of the loop. There's no reason to do that 10 times!
See apparently I am suppose to take the answer of the function and then print the stars.........say for instance the answer is 62 then divide it by 10 and i will get 6 stars because you know it is truncated.

So now I am guess, I am doing that wrong.

Java Syntax (Toggle Plain Text)
  1.  
  2. for (int i = 0; i <= 0; i++)
  3. {
  4. y = stdin.nextInt();
  5. i = y/10;
  6. }
  7. System.out.print("*");
  8. return;

Ok I changed it up a little bit. I am trying here. There is nothing better than that right. :o
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blackbabydoll is offline Offline
22 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Socket help needed: How to listen without blocking program
Next Thread in Java Forum Timeline: Nim Strategy





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC