Sunday, April 14, 2013

Power program

This is a Power program. This program is able to calculate the result of a value raised by its power. You will learn the followings from the program:
-taking data from args parameter of the main( String[] args) method,
-alternatively, reading data input from keyboard by using nextDouble() method of the Scanner class, and 
-writing a recursive method to calculate the result of a value raised by its power.

Power source code:

1 import java.util.Scanner;
2 public class Power
3          public static void main(String[] args)
                        //initialize local variables
                        double val=0.0;
                        double power=0.0;
                        Scanner sc=new Scanner(; //create Scanner object sc
                        if(args.length<=1) //reading data by using Scanner class
                                                System.out.print("Enter a value:");
                                                System.out.print("Enter its power:");
                                                System.out.format("%.3f raised by %.3f is %.3f",val,power,calPower
                        else //reading data from args
                                                System.out.format("%.3f raised by %.3f is %.3f",val,power,calPower
                 }catch(Exception ei){System.out.println("Error in calcuation!");}

4          public static double calPower(double value,double power){
                        if(power>=0){ //positive power
                                    if(power==0) return 1;
                                    else return (value*calPower(value,power-1));
                        else{ //negative power
                                    if(power==-1) return(1/value);
                                    else return((1/value)*calPower(value,power+1));


Program Output:

Power program in Java

Code Explanation:

1 Introduce the Scanner class to the program so that its nextDouble method can be used to receive values input from keyboard.

2 The Power is the main class of Power program.

3 The main method of the program is where the program begins. The main method has array parameter args which stores a collection of values supplied by the user. In the Power program, there are two values: value and its power that the program requires. To check whether the two values were input, the length property of the parameter args is used (args.length<=1). If the result returned by the length property is less than or equals to 1, it means that either the value or its power was not input (from the command line). Thus, alternatively, we use the nextDouble method  of the Scanner class to allow the user to enter the two values. Then the calculation can be made by invoking the calPower method.

4 The calPower(value, power) method implements a recursive concept to calculate a value raised by its power. For the positive power, the base case result of this recursive method is 1 (power=0) and its general case result is value*calPower(value,power-1). For the negative power, the base case result of this recursive method is 1/value  (power=-1) and its general case result is 1/value*calPower(value,power+1). 

Note: The double forward slashes (//) is used to make a comment for a single line. A comment can be used to explain the code. The code after the comment is not executed. If the text used for explanation expands many lines 
you can use the multi-line comment (/*...*/). 
/* here is text to explain code.
It can expand in many lines.
You can also use a comment to exclude code from being executed by placing the comment before the code if you use the single line comment or placing the code in multi-line comment block if you use the multi-line comment.  

No comments:

Post a Comment