1

For my assignment I must write a program that simulates a catapults projectile launch distance and print it to a table. I have no understanding of 2-D arrays even after the class lesson and many other third party resources I have looked at. Can someone please give me tips and help on converting my program to use a 2-D array?

Class 1: tester class

public class CatapultTester{ public static void main(String[] args){ double [] velocity = {20,25,30,35,40,45,50}; double [] degrees = {20,25,30,35,40,45,50}; Catapult vars = new Catapult(velocity,degrees); double [] distance = vars.calcDistance(); System.out.println(" Projectile Distance (feet) "); System.out.printf("%s", "MPH"); for (int i = 0; i < degrees.length; i++){ System.out.printf("%5.0f %1s",degrees[i], "deg"); } System.out.print("\n"); System.out.println("=================================================================="); for(int i = 0; i < distance.length; i++) { System.out.printf( "%2.0f ", velocity[i]); for(int f = 0; f < distance.length; f++) { System.out.printf("%8.1f ", distance[i]); } System.out.printf("\n"); } } } 

Class 2:

public class Catapult extends CatapultTester { double [] velocity; double [] degrees; Catapult(double[] v, double[] d){ velocity = v; degrees = d; } public double [] calcDistance(){ double [] total = {0,0,0,0,0,0,0}; for (int i = 0; i < degrees.length; i++){ total[i] = (Math.pow(velocity[i]/2.237, 2) * Math.sin(2 * Math.toRadians(degrees[i]))/9.8); } return total; } } 

Overall I need to take the velocity and angle arrays and combine them into one 2-Dimensional array and they use the method calcDistance() to get a set of values and then use a loop to print them into a table as the output.

2
  • You didn't tell us what your program is supposed to do, whether it works as expected, and where you want to introduce a 2D array. So, what's the exact question you want us to answer? Commented Mar 16, 2019 at 21:05
  • I have edited my questions, thanks for asking for clarification Commented Mar 17, 2019 at 1:05

1 Answer 1

2

A 2d array is just a way to organize your data into a grid. It is an array of arrays. In your case, for every possible launch angle and velocity, you calculate the distance achieved. So if you used a two-dimensional array, you could, for example, treat one dimension as the angle and the other dimension as the velocity. Then store that particular computed distance there.

For example, if I made a tic-tac-toe game, I might represent the game board by a 3x3 array. And then I could indicate a player's move by doing something like board[row][column] = 'X'; where row and column are values in the range [0..2].

In your case, you may not want to index into the array with the actual velocity and angle values because you'd have sparse arrays; that is, to store the distance for velocity = 50, you'd need an array of length >= 50 and many of those array slots would not be used. Moreover, your velocities and angles happen to be integers, but they could instead be floating point values with fractional portions. So instead of doing distances[angle][velocity], you would do distances[i][j] where i and j are indices into your degrees and velocities tables, respectively.

Sign up to request clarification or add additional context in comments.

2 Comments

so does my combination of the two arrays (velocity and angle) need to be created as array[velocity][angle] = 'X'; ? and can you please help me on how to fill in the values for "x". Much appreciated
Yes; what you have here are several angles and several velocities; if you think of a grid where the angles increase along one axis and velocity increases on the other, every intersection of an angle value and a velocity value gives you a distance; populating these distance values for all combinations of angle and velocity would create a table, just like a multiplication table you'd see in grade school. You have the code to compute the distance for a given combination of angle and velocity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.