• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Checking Code for accuracy

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Ya'll!!
I have an assignment listed below with my code.  It is fairly short . If you all could review that would be awesome ! Thanks in advance .


***** Assignment *****'
Purpose: To write an application that uses recursion to solve a problem.

Details: Create a class called Power that computes the value of base exponent.

The class should have one recursive method:

public static int power(int base, int exponent) – Which computes the value of base exponent using recursion, returning the result. The recursion step should use the following relationship
base exponent = base x base exponent - 1

and the termination condition should occur when exponent is equal to 1, because

base 1 = base

NOTE: For the power method, assume the exponent parameter value is always greater than 0.

Create a second class called PowerTest that contains the main method, and tests the Power.power method. The test class does not need to ask for input from the user of the class, nor does it need to do any additional error checking.

Ensure that your program is properly formatted and it follows all Java naming conventions.







*****Code ******






****Test***

 
Marshal
Posts: 76416
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 3 is incorrectly indented, line 4 should be removed, and it is probably best to reduce lines 5‑8, which are incorrectly formatted too, to return ... ? ... : ...;
Please read what you have been told to use as your base case (though I think your base case is better).
And what problems did you have with your test code?
There is an optimisation whereby you use exponent / 2 for even exponents and then square the result, but that may be more complicated than you want at this juncture.
 
Marshal
Posts: 27368
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marr Yenn wrote:Create a second class called PowerTest that contains the main method, and tests the Power.power method.



I have to say, I don't think your PowerTest class actually tests the Power.power method. All it does is to output the result of some test cases to the console.
 
Sheriff
Posts: 17086
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re not following the instructions you were given:

1. Instructions say to terminate recursion when exponent is 1. Your code terminates when exponent is 0. It’s not logically wrong but it’s not what the instructions say.

2. Instructions say to assume exponent is always greater than 0. Your first “test” uses 0 as the exponent.

3. Indentation is not proper.

Paying attention to details like that is important. While your solution is more complete than what the instructions tell you to write, you’re still not following instructions. Indentation is a no-brainer. If you’re using an IDE that can auto format your code, then it’s a simple thing to comply to the requirement to indent your code properly.
 
Junilu Lacar
Sheriff
Posts: 17086
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Paul is saying about your “test” code not actually doing any testing is correct.

This is what a proper test might look like:
 
If you're gonna buy things, buy this thing and I get a fat kickback:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic