Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 31

Thread: Math.pow

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Math.pow

    I'm new to java and trying to be self taught which is proving more and more difficult.

    This is the question,

    Add an extra CASE that will raise the inputted value to the inputted exponent, using Math.pow (Case E is my extra case)
    I truly don't know what its asking for

    import java.util.Scanner;
     
    public class Arithmetic{
      public static void main(String[] args) {
     
    System.out.println("Make your arithmetic selection from the choices below:\n");
     
    System.out.println(" A. Addition");
    System.out.println(" S. Subtraction");
    System.out.println(" M. Multiplication");
    System.out.println(" D. Division\n");
     
    System.out.print(" Your choice? ");
     
    Scanner kbReader = new Scanner(System.in);
    String choice = kbReader.nextLine( );
    //char ch = choice; //You would think this would work…but it doesn’t.
    char ch = choice.charAt(0); //you just learned another String method.
     
    System.out.print("\nEnter first operand. " );
    double op1 = kbReader.nextDouble( );
    System.out.print("\nEnter second operand." );
    double op2 = kbReader.nextDouble( );
     
    System.out.println(" ");
     
    switch (ch)
    {
    	case 'A': //addition
    	case 'a': //Notice we are providing for both capital A and little a.
    	   System.out.println(op1 + " plus " + op2 + " = " + (op1 + op2) );
               break;
    	case 'S': //subtraction
    	case 's':
    	    System.out.println(op1 + " minus " + op2 + " = " + (op1 - op2) );
                break;
    	case 'M': //multiplication
    	case 'm':
    	    System.out.println(op1 + " times " + op2 + " = " + (op1 * op2) );
                break;
    	case 'D': //division
    	case 'd':
    	    System.out.println(op1 + " divided by " + op2 + " = " + (op1 / op2) );
                break;
            case 'E': // Math.pow
            case 'e':
                System.out.println("pow(" + op1 + ", " + op2 + ") is " + Math.pow(op1, op2));
                break;
            default:
    	    System.out.println("Hey dummy, enter only a A, S, M, or D!");
      }
     }
    }
    Last edited by britton33; July 1st, 2012 at 12:00 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Math.pow

    I truly don't know what its asking for
    Can you explain the problem? Are you getting error messages? Please copy and paste the full text here.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.pow

    No errors, I just guessed by putting in the Math.pow. Its not giving me anything when I run the program...acts like its not even there.

    case 'E': // Math.pow
    case 'e':
    System.out.println("pow(" + op1 + ", " + op2 + ") is " + Math.pow(op1, op2));

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Math.pow

    Its not giving me anything when I run the program.
    Can you post the contents of the console window that shows what you are talking about?

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.pow

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.



    C:\myJava>javac Arithmetic.java

    C:\myJava>java Arithmetic
    Make your arithmetic selection from the choices below:

    A. Addition
    S. Subtraction
    M. Multiplication
    D. Division

    Your choice? a

    Enter first operand. 2

    Enter second operand.2

    2.0 plus 2.0 = 4.0

    C:\myJava>

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Math.pow

    Can you explain what is wrong with what the program did when you executed it?
    The output looks like the program did what it was supposed to do.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.pow

    If I take out case E, I get the same thing

    I'm not getting this at all,
    Add an extra CASE that will raise the inputted value to the inputted exponent, using Math.pow

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Math.pow

    If you take out the case for M, S or D you will also get the same thing when you enter an 'a'. The code for those other letters is not being used when you enter an 'a'.

    Try entering an e (instead of the a) when the program asks you for your selection. The program will not use the case 'E' unless you enter an 'e'.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.pow

    Ahh, your awesome...Thats what I needed to know. I have one more simple question that I'm having a problem with.
    import java.util.Scanner;
     
    public class Password{
      public static void main(String[] args)  {
     
      Scanner kbReader = new Scanner(System.in);
      System.out.println("Enter your password. ");
     [B] int thePassword = kbReader.nextInt();  //  sure this is wrong
    [/B]
      if (thePassword = XRay)
      System.out.println("Password entered successfully.");
     
      else
      System.out.println("Incorrect password.");
      }
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Math.pow

    Can you explain the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    britton33 (July 1st, 2012)

  12. #11
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.pow

    This is what I'm getting. This is from the Blue Pelican java book online and since I didn't buy the answers, I have no way of checking to see what's wrong.

    C:\myJava>javac Password.java
    Password.java:17: error: cannot find symbol
    if (thePassword = XRay)
    ^
    symbol: variable XRay
    location: class Password
    Password.java:17: error: incompatible types
    if (thePassword = XRay)
    ^
    required: boolean
    found: int
    2 errors

    C:\myJava>

    I'm sure its looking for integers (int)
    Last edited by britton33; July 1st, 2012 at 12:46 PM.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Math.pow

    You are using the assignment operator (=) instead of the comparsion operator (==).
    The value returned by an assignment is what is to the left of the =.
    An if statement requires a boolean value, not an int
    required: boolean
    found: int
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.pow

    How would I write that? I'm new to boolean

  15. #14

  16. #15
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.pow

    Read those and changed code to:

    import java.util.Scanner;
     
        public class Password{
          public static void main(String[] args)  {
     
          Scanner kbReader = new Scanner(System.in);
          System.out.println("Enter your password. ");
          boolean thePassword = kbReader.nextBoolean();  //  sure this is wrong
     
          if (thePassword == XRay)
          System.out.println("Password entered successfully.");
     
          else
          System.out.println("Incorrect password.");
          }
        }

    Here is my error

    C:\myJava>javac Password.java
    Password.java:17: error: cannot find symbol
    if (thePassword == XRay)
    ^
    symbol: variable XRay
    location: class Password
    1 error

    C:\myJava>

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Math.pow

    Is Xray the name of a variable? If so you need to define it and give it a value.
    Or did you want it to be a String? If so enclose it in "s.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.pow

    I hate to sound completely ignorant but could you write an example code?

    How do you define XRay or
    how to you enclose it in "s".

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Math.pow

    The last line of your post encloses the s in "s "s"

    This line defines the variable: thePassword as a boolean variable
    boolean thePassword

    What text are you using to learn java? These questions should be covered in the first chapter.

    Take a look through the tutorial:
    http://docs.oracle.com/javase/tutorial/java/TOC.html
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.pow

    Nothing I'm doing is fixing it...still getting the same error.

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Math.pow

    still getting the same error.
    If you had made either of the changes you should get a different error.
    Please post the new code and the full text of the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.pow

      import java.util.Scanner;
     
        public class Password{
            public static void main(String[] args) {
     
          Scanner kbReader = new Scanner(System.in);
          System.out.println("Enter your password. ");
          String s = kbReader.nextLine();  
     
          if ( s == XRay )
          System.out.println("Password entered successfully.");
     
          else
          System.out.println("Incorrect password.");
          }
        }


    C:\myJava>javac Password.java
    Password.java:17: error: cannot find symbol
    if ( s == XRay )
    ^
    symbol: variable XRay
    location: class Password
    1 error

    C:\myJava>

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Math.pow

    Where is the variable named: Xray defined? The compiler can not find its definition.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #23
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.pow

      import java.util.Scanner;
     
        public class Password{
            public static void main(String[] args) {
     
          Scanner kbReader = new Scanner(System.in);
          System.out.println("Enter your password. ");
          String s = kbReader.nextLine();  
     
          if ( s == XRay )
          System.out.println("Password entered successfully.");
     
          else
          System.out.println("Incorrect password.");
          }
        }
    C:\myJava>javac Password.java
    Password.java:17: error: cannot find symbol
    if ( s == XRay )
    ^
    symbol: variable XRay
    location: class Password
    1 error

    C:\myJava>

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Math.pow

    Where is the variable named: Xray defined? The compiler can not find its definition.
    Also see post #16
    If you don't understand my answer, don't ignore it, ask a question.

  26. #25
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.pow

    Think I'm making this way more difficult than it should be.

Page 1 of 2 12 LastLast

Similar Threads

  1. If-Else Statement and Math
    By mikecancelosi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2012, 12:31 AM
  2. Math.Random()
    By xionyus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2011, 10:22 PM
  3. For the Math Majors
    By mszyndlar in forum Java Theory & Questions
    Replies: 0
    Last Post: October 18th, 2011, 04:40 PM
  4. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM
  5. Math in Java?
    By [Kyle] in forum Java Theory & Questions
    Replies: 3
    Last Post: September 23rd, 2009, 12:21 PM