need help with very very basic java

Reply

Join Date: Jul 2008
Posts: 8
Reputation: d4n0wnz is an unknown quantity at this point 
Solved Threads: 0
d4n0wnz d4n0wnz is offline Offline
Newbie Poster

need help with very very basic java

 
0
  #1
Jul 10th, 2008
Hi, I am taking a course that is Java based over the summer and the prerequisite class which I took a year ago was in c++ and has recently been switched over to java. So now the class expects me to have a good background in java but there is my problem. I took a java course in high school... which was about 3 years ago and I have forgotten the very basics of writing a simple program in java.

My assignment is very simple, I would be able to do it in 5 mins back when I was in high school but I am extremely rusty and do not have a textbook to refer to at the moment.

The assignment is to create a function that will take a real number such as 3.0 from the user,
then take an exponent such as 3,
then give an answer which would be 27.0 and would do the math recursively.

Honestly I do not remember how to program such a simple task and would appreciate any help greatly
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 8
Reputation: d4n0wnz is an unknown quantity at this point 
Solved Threads: 0
d4n0wnz d4n0wnz is offline Offline
Newbie Poster

Re: need help with very very basic java

 
0
  #2
Jul 10th, 2008
I dont remember how to take user input(a double) and assign it to a variable.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: need help with very very basic java

 
2
  #3
Jul 10th, 2008
Originally Posted by d4n0wnz View Post
Hi, I am taking a course that is Java based over the summer and the prerequisite class which I took a year ago was in c++ and has recently been switched over to java. So now the class expects me to have a good background in java but there is my problem. I took a java course in high school... which was about 3 years ago and I have forgotten the very basics of writing a simple program in java.

My assignment is very simple, I would be able to do it in 5 mins back when I was in high school but I am extremely rusty and do not have a textbook to refer to at the moment.

The assignment is to create a function that will take a real number such as 3.0 from the user,
then take an exponent such as 3,
then give an answer which would be 27.0 and would do the math recursively.

Honestly I do not remember how to program such a simple task and would appreciate any help greatly
Let's break down the problem into steps...

->Accept user input
->Perform the desired operation / use recursion
->Prove that the operation has been done (by a display)

... these steps are in no particular order. This will help you get an idea of what to look up.

I can give you a few hints.

"Accept user input" - try using the following syntax--

  1.  
  2. import java.util.Scanner; // import the Scanner class file above the class declaration/definition
  3.  
  4. public class <your class name> ...

  1.  
  2. // Creates a scanner that reads information from the standard input stream in class System
  3. Scanner keyboard = new Scanner(System.in);
  4.  
  5. // Display statement - does nothing special but allow user/you to be aware of something
  6. System.out.println("Enter a word");
  7.  
  8. String a = keyboard.nextLine();
  9.  
  10. System.out.println("You entered: " + a);

You need to create the function that performs the operation. Keep in mind that in order to power something you need to understand how power works.

3 to the power of 3 is pretty easy to evaluate in our heads. We typically think of the number in terms of previous experiences of getting the answer through the calculator or by seeing the answer done. Rarely do we look at the mechanics of how the powering is done.

If we were to break down 3 to the power of 3, we could say that 3 to the power of 3 is just 3 times itself 3 times. For example--

(Warning, the below is not expressed in binary manipulation but in terms of regular calculator-based equations!)

3 ^ 3 = 3 * 3 * 3

and likewise

2 ^ 3 = 2 * 2 * 2;

This is easily iterable--

  1.  
  2. public double power(double value, int exp){
  3. double result = 1;
  4.  
  5. for(int i = 0; i < exp; i++){
  6. result = value * result;
  7. }
  8. return result;
  9. }

The above works for integer exponent values 0 and greater. If you need to do values below zero you'll have to do some research.

Even with this much done, there's still two more steps. We have to use recursion to solve the above and also we still have to display the value to the user.

Recursion implies that the function will call itself and break up the task into smaller tasks each time it is called. For example...

  1.  
  2. public static void recursivePrint(int times, String word){
  3. if(times < 0){
  4. recursivePrint(-times, word);
  5. return;
  6. }
  7. else if(times == 0)
  8. return;
  9.  
  10. System.out.println(word + " value times is " + times);
  11. recursivePrint(times - 1, word);
  12. }

Recursive print calls itself two additional times after the method is first invoked. This is because of the line--

recursivePrint(times - 1, word);
--which is perfectly legal in java.

The question is "why do this? Why not use iteration?" The answer is that some things are easier to express using recursion instead of iteration. Also some patterns/equations or events are easier to express through recursion where you only really need to know how the process is broken down in comparison to how to do the process.

For example, if I were to write the Fibonacci sequence using iteration, I'd need to know what the starting point is before progressing to the finish point.

Fibonacci sequences are 0 at 0 and 1 at 1. At 2 the result is 1, at position 3 the result is 3, at pos 4 the result is 5... so on and so forth.

I'd then have to progress forward with the sequence by constantly evaluating the last two numbers, add them together then store the new number in some data type and do another comparison depending on what position the user wants to stop at in the Fib sequence.

In recursion you don't have to do that! You only need to know 2 things--

The pattern for the numbers and, the stopping case(s) [the sentinels].

Using iteration...
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class Number_Manip{
  5.  
  6. public static void main(String[] args){
  7. System.out.println(Number_Manip.fibonacci(3));
  8. }
  9.  
  10. public static int fibonacci(int num){
  11. int first = 0, second = 1, result = 0; // rather annoying
  12.  
  13. for(int i = 0; i < num + 1; i++){ // even more annoying
  14. result = first + second;
  15. second = first;
  16. first = result;
  17. }
  18. return result;
  19. }
  20. }

And then using recursion--

  1.  
  2. import java.util.Scanner;
  3.  
  4. public class Number_Manip{
  5.  
  6. public static void main(String[] args) throws Exception{
  7. System.out.println(Number_Manip.fibonacci(5));
  8. }
  9.  
  10. public static int fibonacci(int num) throws Exception{
  11. if(num < 0)
  12. throw new Exception();
  13. if(num == 0)
  14. return 1;
  15. else if(num == 1)
  16. return 1;
  17. else return fibonacci(num - 2) + fibonacci(num - 1); // much cleaner
  18. }
  19. }

The above example breaks down the process, but not that the functions have to keep calling themselves until a sentinel is met in which they can stop.

Also note that the initial call of the function must wait, that is it must evaluate the two functions at the last line of the method before it can return anything.

Actually, each function, since they're nearly the same as the initial-invoking function, must wait until the function they call has reached an evaluation.

The sentinels num == 0 and num == 1 force the method call of fibonacci(1) to return 1, and likewise fibonacci(0) also returns 1. This example is slightly different from the for loop example since the first starting numbers were 0 and 1 and now we use 1 and 1. Really it's a preference of what you want to be the "first number" in the sequence - count 0 or not.

You may not see much of a different, but consider this - if you needed two functions to cooperate with each other in terms of breaking down a process, how would you do it using pure iteration? The answer is that it would be incredibly difficult (but not impossible) with a lot of complexity to the code. Recursivity allows one to break down a process into smaller processes - basically you're delegating small jobs to each function call instead of trying to do everything in one step like you would using iteration.

Recursivity doesn't just apply to functions, but for now experiment with recursion to see if you can understand it. Remember that when the function calls itself and when the sentinel(s) is met are probably the two most important things to note when using recursion.

Also note that anything done recursively can be done using a loop. I'm pretty sure the reverse is true, so try making the far-above function of the for loop into a recursive method that returns the power of a number with a given exponent.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Solved Threads: 1
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Re: need help with very very basic java

 
0
  #4
Jul 10th, 2008
I don't get why it has to be done recursively. I am pretty sure just a normal, exponential way is much easy for exponents.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: need help with very very basic java

 
0
  #5
Jul 10th, 2008
Originally Posted by bloody_ninja View Post
I don't get why it has to be done recursively. I am pretty sure just a normal, exponential way is much easy for exponents.
It's part of the requirements--

My assignment is very simple, I would be able to do it in 5 mins back when I was in high school but I am extremely rusty and do not have a textbook to refer to at the moment.

The assignment is to create a function that will take a real number such as 3.0 from the user,
then take an exponent such as 3,
then give an answer which would be 27.0 and would do the math recursively.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 408
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: need help with very very basic java

 
0
  #6
Jul 10th, 2008
thinking about how i would do this recursively, a number to a power, is the same as the number times the number to the power-1, until the power is zero, if it is zero the exponentiation is always 1, no matter the number, granted this approach only does work if the power is an integer:

  1. public static double myPow(double x, int y) throws IllegalArgumentException{
  2. if(y<0){
  3. throw new IllegalArgumentException();//i don't normally throw exceptions so i don't know if this is right
  4. }
  5. if(y==0){
  6. return 1;
  7. }
  8. return x * myPow(x,y-1);
  9. }
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: need help with very very basic java

 
0
  #7
Jul 10th, 2008
Originally Posted by sciwizeh View Post
thinking about how i would do this recursively, a number to a power, is the same as the number times the number to the power-1, until the power is zero, if it is zero the exponentiation is always 1, no matter the number, granted this approach only does work if the power is an integer:

  1. public static double myPow(double x, int y) throws IllegalArgumentException{
  2. if(y<0){
  3. throw new IllegalArgumentException();//i don't normally throw exceptions so i don't know if this is right
  4. }
  5. if(y==0){
  6. return 1;
  7. }
  8. return x * myPow(x,y-1);
  9. }
I would've posted the answer myself but I wanted to see if he could use the logic to get it on his own lol.

oh well =P
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: need help with very very basic java

 
0
  #8
Jul 11th, 2008
Originally Posted by d4n0wnz View Post
I dont remember how to take user input(a double) and assign it to a variable.
  1. import java.util.Scanner;
  2.  
  3. public class Test_Class_9{
  4. public static void main(String[] args){
  5. System.out.println("Enter a double");
  6. Scanner kb = new Scanner(System.in);
  7. double myDouble = kb.nextDouble();
  8. System.out.println("You entered: " + myDouble);
  9. }
  10. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 408
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: need help with very very basic java

 
0
  #9
Jul 11th, 2008
funny you should mention that i thought about posting the info without the code, but i figured it would end up in the thread anyway in one form or another sorry
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 40
Reputation: rapperhuj is an unknown quantity at this point 
Solved Threads: 0
rapperhuj rapperhuj is offline Offline
Light Poster

Re: need help with very very basic java

 
-1
  #10
Jul 11th, 2008
can u run a simple Hello World output??

if not.. u didn't installed properly you JDK or Java Development Kit.. and you are not implement the Environment variable path..

otherwise what is your editor?
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