# Math.pow

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• July 1st, 2012, 11:40 AM
britton33
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

Code java:

```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!"); } } }```
• July 1st, 2012, 11:44 AM
Norm
Re: Math.pow
Quote:

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.

[code=java]
[/code]
to get highlighting and preserve formatting.
• July 1st, 2012, 11:58 AM
britton33
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.

Code java:

```case 'E': // Math.pow case 'e': System.out.println("pow(" + op1 + ", " + op2 + ") is " + Math.pow(op1, op2));```
• July 1st, 2012, 12:02 PM
Norm
Re: Math.pow
Quote:

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.
• July 1st, 2012, 12:19 PM
britton33
Re: Math.pow
Microsoft Windows XP [Version 5.1.2600]

C:\myJava>javac Arithmetic.java

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

S. Subtraction
M. Multiplication
D. Division

Enter first operand. 2

Enter second operand.2

2.0 plus 2.0 = 4.0

C:\myJava>
• July 1st, 2012, 12:21 PM
Norm
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.
• July 1st, 2012, 12:23 PM
britton33
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
• July 1st, 2012, 12:30 PM
Norm
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'.
• July 1st, 2012, 12:34 PM
britton33
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.
Code java:

```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."); } }```
• July 1st, 2012, 12:40 PM
Norm
Re: Math.pow
Can you explain the problem?
• July 1st, 2012, 12:43 PM
britton33
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.

^
symbol: variable XRay
^
required: boolean
found: int
2 errors

C:\myJava>

I'm sure its looking for integers (int)
• July 1st, 2012, 12:48 PM
Norm
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
Quote:

required: boolean
found: int
• July 1st, 2012, 12:50 PM
britton33
Re: Math.pow
How would I write that? I'm new to boolean
• July 1st, 2012, 12:53 PM
Norm
Re: Math.pow
• July 1st, 2012, 01:04 PM
britton33
Re: Math.pow
Read those and changed code to:

Code java:

```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

^
symbol: variable XRay
1 error

C:\myJava>
• July 1st, 2012, 01:11 PM
Norm
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.
• July 1st, 2012, 01:13 PM
britton33
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".
• July 1st, 2012, 01:16 PM
Norm
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
Code :

`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
• July 1st, 2012, 01:19 PM
britton33
Re: Math.pow
Nothing I'm doing is fixing it...still getting the same error.
• July 1st, 2012, 01:21 PM
Norm
Re: Math.pow
Quote:

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.
• July 1st, 2012, 01:31 PM
britton33
Re: Math.pow
Code java:

``` 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."); } }```

if ( s == XRay )
^
symbol: variable XRay
1 error

C:\myJava>
• July 1st, 2012, 01:33 PM
Norm
Re: Math.pow
Where is the variable named: Xray defined? The compiler can not find its definition.
• July 1st, 2012, 01:35 PM
britton33
Re: Math.pow
Code java:

``` 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."); } }```
if ( s == XRay )
^
symbol: variable XRay
1 error

C:\myJava>
• July 1st, 2012, 01:37 PM
Norm
Re: Math.pow
Where is the variable named: Xray defined? The compiler can not find its definition.
Also see post #16
• July 1st, 2012, 01:38 PM
britton33
Re: Math.pow
Think I'm making this way more difficult than it should be.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last