Xela
Xela

Reputation: 3

Using Math.pow in Java to determine surface area of a cube AND also a difficulty with 'skipping'

I'm writing a simple program to determine some math values and I'm stuck on a problem where it asks a user for a length of a side of a side of a cube, then to output the surface area (using Math.pow and the formula 6a^2). I get the formula and everything but maybe I'm entering this in wrong? I keep getting a different answer than what I'm supposed to...

System.out.print ("Please enter the length of a side of your cube: ");
a = in.nextDouble();
System.out.printf ("Surface area of your cube is : %.3f\n", Math.pow (6*( a,2 )));

Does this problem deal with spacing or what? It just keeps giving me expected error for the parenthesis but I'm sure everything looks okay... I also tried to shift things about such as (( a,2 )*6) and I would get the same wrong answer and expected error ):

~

Another thing I am running into is when I ask the user for two numbers separated by a space, to look for the larger one with Math.max, it works, but then it uses the same numbers to ask for the smaller number. I DO NOT want the user to use these same numbers as I want them to enter some other new numbers...

System.out.print ("Please enter two numbers separated by spaces: ");
a = in.nextDouble();
System.out.printf("Larger of the first two numbers is %.3f\n", Math.max ( a,a ) );

System.out.print ("Please enter two numbers separated by spaces: ");
a = in.nextDouble();
System.out.printf ("Smaller of the first two number is %.3f\n", Math.min ( a,a ) );

I am new to programming. Any help is greatly appreciated!!

Upvotes: 0

Views: 633

Answers (3)

kenshinji
kenshinji

Reputation: 2091

For the second question you ask, I suggest you to put the numbers that user input into a HashSet, and then use contains method to check if the numbers user input has been used previously. You can check here for reference of the HashSet.

Upvotes: 1

Am_I_Helpful
Am_I_Helpful

Reputation: 19168

Your code is incorrect, Math.pow(base, exponent) expects 2 parameters :-

System.out.printf ("Surface area of your cube is : %.3f\n", Math.pow (6*( a,2 )));

Should be :-

System.out.printf ("Surface area of your cube is : %.3f\n", 6 * Math.pow (a,2) );

And, for finding the square, you should stick to trivial concept, i.e., doing (a*a), rather than opting for Math.pow(base,exponent)

[Note -> Multiplication would cost easier than calling the pow() method].

when I ask the user for two numbers separated by a space, to look for the larger one with Math.max, it works, but then it uses the same numbers to ask for the smaller number. I DO NOT want the user to use these same numbers as I want them to enter some other new numbers...

Firstly, that should be a separate question. But, even then for achieving the same, you need the knowledge of loop so that you could run a task for pre-defined number of times.

As this problem is simple, you could simply ask for more doubles below, after calculating the max(). But, that'd be somewhat complex. I suggest you to tep ahead in a step by step approach. Follow the books/tutorials and keep in that order.

Upvotes: 2

user207421
user207421

Reputation: 310980

6a^2

This means six times the square of a.

Math.pow (6*( a,2 )

This means nothing. It's not even valid syntax. You need:

6*Math.pow (a,2 )

or more simply

6*a*a

Upvotes: 2

Related Questions