3 Replies - 6414 Views - Last Post: 05 December 2011 - 03:59 PM Rate Topic: -----

#1 defiance5050   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-August 09

Trouble Using Math.pow With Arrays

Posted 04 December 2011 - 08:58 PM

Hello guys, it has been a while since I have been in a Java class, and I am having some trouble right now. I need to create a cube for an assignment, but I am having trouble with one area right now. Trying to get an array to work with Math.pow. I have been trying to look online for examples of how people use arrays and Math classes, but I cannot find a basic program with a simple explanation. Most of them are way more advanced than what I have been going over (I am just taking an Intro to Java Course, so trying to keep things simple). Does anyone have a simple, explanation and sample code they can talk about? The most helpful link I have found so far is, http://mootools.net/forge/p/array_math .

Only problem is, I am having trouble trying to use it. I have posted my program below, but it is not finished yet. If I could just get a simple code on an array using the Math.pow class, with a simple explanation, I would greatly appreciate it!

public class CubeTest {
	public static void main(String[] args) {
		
		//Our array for our cube
		double[] cubeNumbers = {3.0, 4.0, 5.0};
		
		volumeOfCube1(cubeNumbers);
	}

	
	private static void volumeOfCube1(double[] methodCubeNumbers) {
		System.out.println("Test to find out what array currently is before Math.pow: ");
		double cubeCalculation = 0;

		//Testing to make sure array variables are showing up
		for (double test: methodCubeNumbers)
			System.out.println(test);

              System.out.println("Test to see the results of the Math.pow class on array")
		//Trying to get each variable in the array to be raised to the 3rd power
		for (int count = 0; count < methodCubeNumbers.length; count++)
			methodCubeNumbers[count].(double)pow(3);
				
		
		
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Trouble Using Math.pow With Arrays

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Trouble Using Math.pow With Arrays

Posted 04 December 2011 - 09:06 PM

Nice by why 2 for() loops ? And you don;t return or print any value, how can you test it works

public class CubeTest {
	public static void main(String[] args) {
		
		//Our array for our cube
		double[] cubeNumbers = {3.0, 4.0, 5.0};
		
		volumeOfCube1(cubeNumbers);
	}

	
	private static void volumeOfCube1(double[] methodCubeNumbers) {
		for (double test: methodCubeNumbers)
			System.out.println("Cube of " + test + " is " + Math.pow(test, 3);		
	}
}



Was This Post Helpful? 1
  • +
  • -

#3 defiance5050   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-August 09

Re: Trouble Using Math.pow With Arrays

Posted 05 December 2011 - 11:38 AM

I just used the first for loop to check and make sure my variables in the array were showing up in the method. I am very new to arrays, methods, and objects, so I added a few extra lines to make sure the array is actually showing up. I did not know I could actually just throw in the test variable in and then raise that variable by the power of 3. At first, I tried putting in the array into the Math.pow method instead of using the test and kept getting errors.

First try example
Math.pow(mehodCubeNumbers[test], 3)



This is why I tried using the

methodCubeNumbers.pow() 



Once I get off work, I will go home and test out the code with the new test variable in place, and then play around with mine and work on geting a working soltion. I hope it works, and I highly appreciate your assistance!
Was This Post Helpful? 0
  • +
  • -

#4 defiance5050   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-August 09

Re: Trouble Using Math.pow With Arrays

Posted 05 December 2011 - 03:59 PM

Thank you so much pbl!!!! Once I arrived home, I tried it in a test program and it is working like a charm! I am now also able to play around with other calculations with arrays also and working on other solutions in my code. You just saved me a lot of time, thank you again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1