• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Checking Code for accuracy

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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: 66575
251
  • Mark post as helpful
  • send pies
  • 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.
 
Sheriff
Posts: 24753
59
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • 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:
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!