谁能帮我做这个啊
CP2004 Assignment 2
due:week 12, friday at 5 pm
In Assignment 1 you wrote a program to simulate the shooting of a projectile straight up into the air. Most canon, however, don’t fire their shells straight up but at an angle. In Assignment 2 we will simulate the firing of the canon at an angle. This will involve altering the code we wrote in Assignment 1. Remember that in Assignment 1 we were mostly concerned with how the data was to be modelled. Since Assignment 2 includes a Graphical User Interface we talk about writing code to view the data.
Task
Write a Java program that simulates the shooting of a projectile at an angle up into the air, and a GUI that allows us to see the path of the projectile. First, let’s find a way to simulate the calculation of the position of the projectile, taking the starting angle of the canon into account.
If the starting velocity is v and the starting angle is a then the velocity is a vector with the components:
vx = v * cos(a) and vy = v * sin(a).
We can say that in the x (horizontal) axis the velocity of the projectile does not change. However, in the y (vertical) axis gravity plays its part and drags the projectile back to earth. So we need to calculate the x and y components of the location and velocity of the projectile separately.
We can now repeat the simulation from Assignment 1 but this time we update the x and y components separately. The steps we need to take are:
a) Rewrite the constructor for the Projectile class so that it takes both the initialVelocity and the initialAngle in Radians. The constructor needs to initialise both the xPosition and the yPosition of the projectile to 0. Then it should calculate the x component of the initial velocity by multiplying the cosine of the initial angle in Radians by the initialVelocity, and the y component of the initial velocity by multiplying the sine of the initial angle in Radians by the initialVelocity. (Give these variables suitable names.)
b) Rewrite the move() method in the Projectile class so that both the xPosition and the yPosition are calculated by adding each velocity component (x and y) multiplied by the time interval to the appropriate current position (so the x one gets added to the xPosition and the y one gets added to the yPosition).
c) Create a new variable that keeps count of the number of times the move method is used – call it numMoves. Increment this in the moves() method.
d) Write getters for each variable – there should be 5 variables:
a. 2 for the x and y components of the velocity
b. 2 for the x and y components of the position
c. and 1 for the number of moves.
e) Hard-code the following two variables:
private static final double GRAVITY = 9.81;
private static final double TIME_INTERVAL = 0.01;
Next we need to write the GUI. This is made up of two classes:
1) MainFrame, which extends JFrame and also contains the public static void main method. MainFrame is 800 pixels wide and 400 deep. Its title is “Trajectory”. Make sure that you set the default close operation to something appropriate. MainFrame adds an instance of the next class to its content pane.
2) ProjectileComponent, which extends JComponent. ProjectileComponent has the following methods:
a. A constructor which takes an instance of MainFrame as a parameter and sets two variables (X_MAX and Y_Max) to be the x and y dimensions of the MainFrame instance. It also initialises X_MIN and Y_MIN to 0.
The constructor also uses two JOptionPanes to get the initial velocity and the initial angle as user input, the latter using Math.toRadians to convert the parsed input to radians.
b. An overridden version of paintComponent(Graphics g) which allows us to draw the projectile every n moves (try with n set to 10 perhaps). You get the number of moves from the getNumMoves() method which is in the Projectile class. Then, while the yPosition of the projectile is > 0 calculate the pixel positions of the projectile and draw a small circle at that point every n moves. Consider using Graphics2G to allow the easy drawing of small circles to represent the projectile.
c. Use the following code to help convert the user position to a pixel position:
/**
* Converts from user coordinates to pixel coordinates.
*
* @param xUser an x-value in user coordinates
* @return the corresponding value in pixel coordinates
*/
public double xPixel(double xUser) {
return (xUser - X_MIN) * (getWidth() - 1) / (X_MAX - X_MIN);
}
/**
* Converts from user coordinates to pixel coordinates.
*
* @param yUser a y-value in user coordinates
* @return the corresponding value in pixel coordinates
*/
public double yPixel(double yUser) {
return (yUser - Y_MAX) * (getHeight() - 1) / (Y_MIN - Y_MAX);
}
You might require other methods – the list above is not exhaustive.
Each class must be appropriately documented, using javadoc and comments as necessary.
Sample output
Using an initial velocity of 100, an initial angle 60 degrees, a short time interval of 0.01 and drawing the projectile every 20 moves I get:
NB – these are pictures from an Apple Mac – if you use MSWindows yours will look a bit different.
Requirements
Students are required to produce their own solutions to this task by constructing classes as outlined above using JDK1.4.2. The new Projectile class should be in a package called au.edu.jcu.cp2004.model and the GUI classes should be in a package called au.edu.jcu.cp2004.view. In addition, please include a short document (< 0.5 pages) detailing the status of you code and documentation (how complete it is), any known bugs in your code, and any assumptions in your testing.
Reading
a) You are also encouraged to make full use of the api javadocs.
b) For further details of some ballistics formulae, have a look at Michel Grimmick’s site:
http://www.xs4all.nl/~mdgsoft/catapult/ballistics.html.
c) Have a look at http://www.javaworld.com/javaworld/jw-04-1998/jw-04-howto.html for a detailed description of models and views, but only if you have time and are very keen!
Submission
An electronic copy of your assignment must be emailed to your lecturer by the due date. Your lecturer will announce that date during semester.
Marking Scheme
The assignment is worth 20% of your overall CP2004 grade.
Execution: 14 marks
a. Re-implementation of Projectile.java
1. Constructor (2 marks)
2. move() (2 marks)
3. getters (2 marks)
b. MainFrame – 2 marks
c. ProjectileComponent.java
1. Constructor with 2 OptionPanes which return Strings (2 marks)
2. Overriding paintComponent(Graphics g) (4 marks)
Coding: 6 marks
- Appropriate code organisation
- Javadoc comments and ordinary comments
- Code formatting (eg correct indenting of methods, loops and control statements) and code style (eg use of camelCase in naming variables and methods), including appropriate names for variables and methods