This week's book giveaway is in the Agile and other Processes forum.We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login Win a copy of The Mikado Method this week in the Agile and other Processes forum!

filling an array list

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i have my array list and i am trying to use my method and the other two array list to make calculations and fill another array list. basically what i am trying to do is take my two array lists, plug them both into my method, and then return a value.

i got further instruction on the assignment and realized that the reason my previous tries to get things to work were because they werent supposed to work that way.

Catapult.getDistance(speed, angle) to populate the table.

Use int arrays for storing speed {20, 25, 30, 35, 40, 45, 50} angle {25, 30, 35, 40, 45, 50}

Create a double [][] array for distance whose dimensions are [speed.lenght][angle.length]

Create a catapult object and use a nested for loop to calculate and print the table

distance[m][n] = catapult.getDistance(speed[m], angle[n])

print distance[m][n]

You do not need to store multiple Catapults in an arrayList, just use the getDistance(speed[m], angle[n]) method to calculate distance

im trying to follow these suggestions, i have gotten this far

my problem is that when i try to compile i get that my non static method distanceFeet() cannot be dereferenced from a static context
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708

4
That is because you have that code in the main method. You should pass the two arrays to the constructor, then set up the arrays in the Catapult object, then use them inside that object.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i have been looking at the suggestions that were given. i dont really understand where a new object comes into play, but i have been trying everything else. ive got this, but im getting a compiler error.

it says that an ")" is expected. im trying to figure out what its looking for, im not even sure this is able to be done, but its what i was suggested.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708

4
You could give the Catapult class an array for angles of attack and an array for speeds, or even an array of arrays for both.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
ive got my arrays, now im just trying to find a way to calculate the results for each angle vs. speed. i cant tell if what i have coded works because im getting the ")" expected error.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
wait nvm last post. i try to compile and i get array required, but arraylist found
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
ive gotten the code up to this, but im still having the same problem

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695

19

You are trying to dereference the speed and angle variables as if they are arrays -- they are not. Those variables are Lists, not arrays. To access a specific element, you need to use the get() method.

Henry

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i have been working really hard and have almost gotten perfect results. i dont really understand why this is happening with my output though. im getting two of each row of output.

ex:
Projectile Distance (feet)
MPH 25 deg 30 deg 35 deg 40 deg 45 deg 50 deg 55 deg
===============================================================================================================

20 102.55615401756114 102.55615401756114 115.94136018012075 115.94136018012075 125.80374678276648 125.80374678276648 131.8436501991854 131.8436501991854 133.87755102040816 133.87755102040816 131.8436501991854 131.8436501991854 125.8037467827665 125.8037467827665
25 160.24399065243927 160.24399065243927 181.15837528143868 181.15837528143868 196.56835434807263 196.56835434807263 206.00570343622715 206.00570343622715 209.1836734693877 209.1836734693877 206.00570343622715 206.00570343622715 196.56835434807263 196.56835434807263
30 230.75134653951253 230.75134653951253 260.86806040527165 260.86806040527165 283.0584302612246 283.0584302612246 296.6482129481671 296.6482129481671 301.2244897959183 301.2244897959183 296.6482129481671 296.6482129481671 283.05843026122466 283.05843026122466
35 314.07822167878095 314.07822167878095 355.07041555161976 355.07041555161976 385.2739745222224 385.2739745222224 403.7711787350052 403.7711787350052 409.99999999999994 409.99999999999994 403.7711787350052 403.7711787350052 385.2739745222224 385.2739745222224
40 410.22461607024456 410.22461607024456 463.765440720483 463.765440720483 503.2149871310659 503.2149871310659 527.3746007967416 527.3746007967416 535.5102040816327 535.5102040816327 527.3746007967416 527.3746007967416 503.214987131066 503.214987131066
45 519.1905297139032 519.1905297139032 586.9531359118613 586.9531359118613 636.8814680877553 636.8814680877553 667.458479133376 667.458479133376 677.7551020408163 677.7551020408163 667.458479133376 667.458479133376 636.8814680877554 636.8814680877554
50 640.9759626097571 640.9759626097571 724.6335011257547 724.6335011257547 786.2734173922905 786.2734173922905 824.0228137449086 824.0228137449086 836.7346938775509 836.7346938775509 824.0228137449086 824.0228137449086 786.2734173922905 786.2734173922905

i dont see what line could be causing this to happen, but if you guys see anything that could cause this problem it would help alot.

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
wait, that was an easy fix. i got it cleared up as soon as i posted. i had two print statements included. sorry about that.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i have one final question and then i can post my results.

im trying to format the output to only print so many decimal places.

i have used this before, but now i get an error that says ")" expected.

John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

This is a common problem with many sets of parentheses. Try adding one for each open paren, and subtracting one for each close paren. It should add up to zero if you've got your parens right.

John.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708

4
Count the ( and the ) in that line of code.

Get a decent text editor (eg NotePad++, Notepad2, jEdit) which supports bracket highlighting, and that sort of error will be much easier to spot.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
once again it was a simple over sight. sometimes that happens. happens alot with me. thank you guys for the help. here is the finished code.

I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.

subject: filling an array list