Project Help: Growth of a Function

Reply

Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Project Help: Growth of a Function

 
0
  #1
Nov 28th, 2005
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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Project Help: Growth of a Function

 
0
  #2
Nov 28th, 2005
I don't see any code taking input. Look into the bufferedreader.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Project Help: Growth of a Function

 
0
  #3
Nov 28th, 2005
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:
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Project Help: Growth of a Function

 
0
  #4
Nov 28th, 2005
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Project Help: Growth of a Function

 
0
  #5
Nov 28th, 2005
Yeah, you've got a problem with the loop. Fix that, and then I'll take a look at the rest.
  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).
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Project Help: Growth of a Function

 
0
  #6
Nov 28th, 2005
Opps, you might want to take the x = y/10 out of the loop. There's no reason to do that 10 times!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Project Help: Growth of a Function

 
0
  #7
Nov 28th, 2005
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.

  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC