Explanation

Discussion in 'Software' started by ll_llniQue, Mar 25, 2005.

  1. ll_llniQue

    ll_llniQue Private E-2

    Good Aternoon :D

    I have an assignment due Sunday ... The problem is that i don't understand the question cam someone explain it for me :eek:


    Question:

    Create a class Power that contains two methods:
    Write a method power(double a , int n) that calculates and displays the result of the double parameter a raised to the power n. For example

    power(3.0 , 2); display 3 to the power 2=9
    power(2.0 , 3); display 2 to the power 3=8

    Call it from the main method
     
  2. ll_llniQue

    ll_llniQue Private E-2

    Code:
    import javax.swing.JOptionPane;
    
    class Power
    {
    
       public static void main( String[] args )  
       { 
       
      int power;
      int result;
      
          // call power method with some test data  
          power(result, number);
          
          }
          
     
       public void power( double a, int n ) 
      {  
        
        // write some code which calculates a to the power of n   
        result = Math.pow(a,n);
        
        // now use System.out to write out the result
        System.out.println(Math.round(result);
         } 
     }

    :confused:
     
  3. goldfish

    goldfish Lt. Sushi.DC

    That seems about right to me. Does it work?
     
  4. ll_llniQue

    ll_llniQue Private E-2

    unfortunatly no :eek:
     
  5. noahawk

    noahawk Corporal

    Just some things I noticed in the code snippet..

    Where are the numbers to be multiplied coming from? (User input?)

    number was never defined in the main method. (int number; or something like that)

    Why is int power; in the main method - as it isn't being used (just to save space)

    result could be defined as double result;, as your compiler may be yelling at you that you can't pass an int as a double (but it may not)

    I think that's it...
     
  6. ll_llniQue

    ll_llniQue Private E-2

    :confused: i declared number ... and about the input

    This is the question

    Create a class Power that contains two methods:
    Write a method power(double a , int n) that calculates and displays the result of the double parameter a raised to the power n. For example

    power(3.0 , 2); display 3 to the power 2=9
    power(2.0 , 3); display 2 to the power 3=8

    Call it from the main method


    ^^^ Do you think it should be an input am abit confused :confused:


    Here is my modified code

    Code:
    class Power
    {
    
       public static void main( String[] args )  
       { 
       
      int power;
      int number;
      double result;
      
          // call power method with some test data  
          power(result, number);
          
          }
          
     
       public void power( double a, int n ) 
      {  
        
        // write some code which calculates a to the power of n   
        result = Math.pow(a,n);
        
        // now use System.out to write out the result
        System.out.println(Math.pow(result));
         } 
     }
     
  7. noahawk

    noahawk Corporal

    For inputs, I'm saying that since they aren't hard coded into the program, like: int number = 3; you aren't really passing a number for the math to be done on. The program you have modified is sending basically empty values to do math on. And since the description says for example, I doubt the teacher just wants 2 or 3 squared, so either some sort of user input or random number generator would be needed.

    Oh, and it looks like you switched from rounding to trying to exponent your result when trying to display the result.

    BTW, I think I'm done on the net for tonight, so I'll try to check on this tomorrow sometime after noon local time here.
     
    Last edited: Mar 25, 2005
  8. ll_llniQue

    ll_llniQue Private E-2

    :confused:

    Code:
     public static void main( String[] args )    
     { 
    power(3.0, 2);
    power(2.0, 3);
    power(5.0, 1);
    power(2.0, 0);
    power(3.75, 2);
    } 
    
    private static void power(double a, int n)   
    { 
    double result = Math.pow(a, n);
    System.out.println(String.valueOf(a) + " to the power of " + n +  " = " + result);   
    }
    }
     
  9. goldfish

    goldfish Lt. Sushi.DC

    I think you need to check the type of result. Does Math.pow() give a double? I think it gives a float..? In any case, you'd need to change that do that you can put it into a string... but I'm rusty at Java to say the least... somthing about string buffers seems familiar in this context.
     
  10. ll_llniQue

    ll_llniQue Private E-2

    Code:
    import javax.swing.JOptionPane;
    
    class Power
    {
    
       public static void main( String[] args )  
       { 
       double result;
       double a;
       int n, num1;
       String strBase, strPower;
       double num2;
       
       strBase = JOptionPane.showInputDialog("Enter the base: "); 
       num1 = Integer.parseInt(strBase); 
       
       strPower = JOptionPane.showInputDialog("Enter the power: "); 
       num2 = Integer.parseInt(strPower); 
       
       //power( num1, num2);
        
       }
    
       private static void power(double a, int n)
       {
           double result = Math.pow(a, n);
            
           
           JOptionPane.showMessageDialog(null,"" + num1 + " to the power of " + num2 +
                             " = " + result);
    
       }
    }


    :confused: my brain is locked .. i can't think anymore!!! please help
     
  11. noahawk

    noahawk Corporal

    Your call of power (num1,num2) needs to be uncommented and num1 would need to be double num1; (NOT int num1;).

    To accept the base, you'd probably need to use Double.parseDouble(strBase);

    If your compiler is giving an error message about the call to Math.pow, it's because for Java 2 SE 1.4.2, the method call is Math.pow(double, double); [so maybe a type cast for the int you're passing to it [but I'm not sure].

    And when you're trying to display the output, since num1 + num2 aren't instantiated in the power method, it won't work -- instead, use the variable a in place of num1 and n in place of num2.

    Here's the website where I double checked the documentation on Math.pow

    J2SE 1.4.2 documenation search If this is the version you're using, it'll help when looking for a method to do something you're trying to do.

    @ goldfish - I checked the Math class for J2SE 1.4.2 and Math.pow takes two doubles and returns another double.
     
  12. ll_llniQue

    ll_llniQue Private E-2

    i did this but i still have an error :eek:

    Code:
    import javax.swing.JOptionPane;
    
    class Power
    {
    
       public static void main( String[] args )  
       { 
       double result;
       double a;
       double num1;
       String strBase, strPower;
       double num2;
       int n;
       
       strBase = JOptionPane.showInputDialog("Enter the base: "); 
       num1 = Double.parseDouble(strBase); 
       
       strPower = JOptionPane.showInputDialog("Enter the power: "); 
       num2 = Double.parseDouble(strPower); 
       
       //power( num1, num2);
        
       }//end main
    
       private static double power(double a, int n)
       {
           double result = Math.pow(a, n);
            
           
           JOptionPane.showMessageDialog(null,"" + a + " to the power of " + n +
                             " = " + result);
       
       
       }
    }
    
    error >>> D:\Data\Desktop\BITE\Jumana\ITEC 257\Portfolio\Power.java:26: missing return statement
    {
    ^
    1 error
     
  13. noahawk

    noahawk Corporal

    Ok, ll_llniQue, the Math.pow method has a return type of double, the way you had it (void was ok for the power method you wrote). So just put void in place of double for your power method.

    Other than having the line //power(num1,num2); commented out, I think that the program will work. Just remove the // and check my other recommendation above, and that should be it!

    I'll check back later to see if we're up and running...
     
  14. ll_llniQue

    ll_llniQue Private E-2

    :D :D :D :D :D :D :D :D :D

    Thank you
    Thank you
    Thank you


    it's running

    :D :D :D :D :D :D :D :D :D

    Code:
    import javax.swing.JOptionPane;
    
    class Power
    {
    
       public static void main( String[] args )  
       { 
       double result;
       double a;
       double num1;
       String strBase, strPower;
       double num2;
       double n;
       
       strBase = JOptionPane.showInputDialog("Enter the base: "); 
       num1 = Double.parseDouble(strBase); 
       
       strPower = JOptionPane.showInputDialog("Enter the power: "); 
       num2 = Double.parseDouble(strPower); 
       
       power( num1, num2);
        
       }//end main
    
       private static void power(double a, double n)
       {
           double result = Math.pow(a, n);
            
           
           JOptionPane.showMessageDialog(null,"" + a + " to the power of " + n +
                             " = " + result);
       
       
       }
    }


    AM SO HAPPY!
     
  15. noahawk

    noahawk Corporal

    Good, good, excellent!! :D :D

    And you're welcome...

    Maybe you can help me now: How do you get the little insert with the code in? :confused: I think I may be needing that info sometime in the future.
     
  16. ll_llniQue

    ll_llniQue Private E-2

    You me the input!??!?!?
     
  17. QuickSilver

    QuickSilver Corporal

    noahawk - when you're replying there is an icon with a # symbol on... first select and copy what you're wanting to put in the code tags, click it, and then paste it in...
    Alternatively I suspect you can manually tag the data by writing CODE in square brackets ( [ and ] ) to start the tag and /CODE in square brackets again to end the code...

    Looks like I missed out on the rest of this problem...
     
  18. ll_llniQue

    ll_llniQue Private E-2

    QuickSilver >>> i would appreciate it if u comment on the code
     
  19. noahawk

    noahawk Corporal

    @quicksilver - Yeah, that's what I was talking about... I was wondering how it goes from the little one line box that pops up to a full program like that... That's pretty cool. I was trying to cover ya, and give ya some time off from java for the Easter weekend :D

    @ ll_llniQue - I guess I really need to be more specific when asking about code in the programming forum... Everyone's learning here! ;)
     
  20. Maxwell

    Maxwell Folgers

    You now need to put some data validation in, for example what happens if you do power (-0.5, -0.5) and similar things where exponentiation in the floating point domain is undefined or ill-conditioned?
     
  21. QuickSilver

    QuickSilver Corporal

    I just had a quick look over it and it looks fine - except I would be careful of one thing... From my time at University it seemed that tutors followed their questions to the letter...

    At the moment you are taking n as a double and passing it to your method as a double... something to consider doing would be taking it in as an int, passing it to your method as an int, and then in your method making the necessary cast to a double data type and calling the function then...
    Hehe - sorry to seem anal - I just remember feeling mortified when I was learning to get penalised for making such mistakes.

    Other than that it looks spot on... good work
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds