Urgent Java Question:firing a cannon...not a lot to do just a few things to add.

Dec 6th, 2014
SKTFaker
Category:
Computer Science
Price: $10 USD

Question description

Please help!

In this part of the assignment we will fire the cannon and do the animation. Make a boolean class variable called running which is initialized to false. At the beginning of the while loop, increment the time by 1 if running is true. Display the value of time along with the angle, target, and force. In the fire method, set running to true. The fire method is called when the Enter key is pushed. Test the program to make sure the time starts running when the Enter key is pushed.

Equations of motion: When the cannon is fired, it imposes a force (actually an impulse) on the cannonball. After that the cannon ball follows the Newtonian laws of motion. Under gravity, these are give by: x = x0 + vxt y = y0 + vyt - .5gt2 The vx and vy are the initial velocities in the x and y directions and g is the acceleration due to gravity. Define double class variables vx and vy to represent vc and vy. When we fire the cannon, set: vx = DrawingUtility.getTriangleX(force,cannonAngle) and vy = DrawingUtility.getTriangleY(force,cannonAngle). We will use -.2 for the value of g. (We use negative values for vy and g because y decreases as you go up.) Create int class variables, x0 and y0 to represent x0 and y0. Set these to the initial values of x and y. These are the values you previously used in drawCannonballAtTime, and must be changed whenever the cannon angle moves. Write methods getCircleX(double time) and getCircleY(double time) that calculate the coordinates of the center of the circle at a given time using the equations of motion. Modify drawCannonballAtTime to use use these methods to draw the circle. Test the program. Everything should be the same until the enter key is pushed. As before, old circles are displayed. To eliminate these, in the while loop, draw the circle in white before incrementing the time. Hitting the target: Write a method called checkForHitOrMiss() which returns -1 for a miss, 1 for a hit, or 0 if the circle has not gotten to the target yet. Report a hit if the value if the x value of the circle has gone far enough to reach the target and the y value of any part of the circle's bounding box is between the top and bottom of the target. Report a miss if the circle goes outside the panel. Otherwise, return 0. In the main while loop, call checkForHitOrMiss if running is true. If a hit or a miss occurs, set running to false, the time to 0, and (temporarily) print a message indicating whether there was a hit or a miss.

Multiple runs: Modify the main program so it keeps track of the number of hits. Run the program until 10 hits have occurred by changing the while condition. Also keep track of your score. You get 5 points each time you have a hit and -1 when you have a miss. Report the number of hits and the score along with the other data. The score should be in green, but change it to red when it is the final score (after 10 hits). Finally, where there is a hit, move the target to a random valid position. Keep the x coordinate of the target the same, just modify the top of the target. Make sure the entire target stays in the visible area.

History: Keep a history of the number of misses for each hit. Declare an array of 10 integers that is initialized to 0. The first entry will contain the number of misses before the first hit. The second will hold the number of misses between the first and second hit, etc. Display this information on the screen under your name when the h is held down. Erase it when the key is released. Only show the part of the array corresponding to the number of hits so far. Before the first hit, show only the first element of the array. After the list of misses, display the average number of misses per hit to two decimal places. The methods keyUp and keyDown can be used. When any letter key is pushed, keyDown is called and the parameter is the character corresponding to the key pushed. Similarly with keyUp when a key is released.

HERE IS MY WORKING CODE...

import java.awt.*;

public class AngryCircles {

public static final boolean runProgram = true;
public static final int PANEL_WIDTH = 300;
public static final int PANEL_HEIGHT = 300;
public static final int CANNON_BASEX = 0;
public static final int CANNON_BASEY = 300;
public static DrawingPanel panel;
public static Graphics g;
public static double cannonAngle;
public static Color black = Color.BLACK;
public static Color white = Color.WHITE;
public static Color red = Color.RED;
public static int intX = getTriangleX(30.0,cannonAngle);
public static int intY = getTriangleY(30.0,cannonAngle);
public static int targetTop;
public static int targetDown;
public static int force;
public static double time;
public static boolean running;

// Main  
public static void main(String[] args) {
panel = new DrawingPanel(PANEL_WIDTH,PANEL_HEIGHT);
g = panel.getGraphics();
new DrawingUtility(panel);
cannonAngle = 45.0;
targetTop = 75;
force = 10;
running = false;
while(runProgram){
if (running = true) {
time ++;
}
targetDown = targetTop + 50;
drawData();
drawCannon(black);
drawCannonball(getTriangleX(30.0,cannonAngle), 300 - getTriangleY(30.0, cannonAngle), black);
drawCannonballAtTime(time, red);
drawTarget(targetTop);
panel.sleep(50);
}
}
// prints information about the location of objects
public static void drawData() {
g.setColor(Color.red);
g.drawString("Angry Circles written by MyName", 10, 15);
g.setColor(Color.black);
g.drawString("Cannon Angle : " + cannonAngle,10, 30);
g.drawString("Target Location : ("+ targetTop +"," + targetDown +")",10, 45);
g.drawString("Force : " + force,10, 55);
}
// Draws cannon
public static void drawCannon(Color choice) {
int cannonNewX = getTriangleX(30.00, cannonAngle);
int cannonNewY = 300 - getTriangleY(30.00, cannonAngle);
double backupAngle;
time = 0;
backupAngle = 0 + cannonAngle;
g.setColor(choice);
if (choice == Color.black) {
g.drawLine(CANNON_BASEX, CANNON_BASEY, cannonNewX, cannonNewY);
} else if (choice == Color.white) {
g.drawLine(CANNON_BASEX, CANNON_BASEY, cannonNewX, cannonNewY);
}
}
// increases and decreases angle of the cannon
public static void angleUp() {
int cannonNewX = getTriangleX(30.00, cannonAngle);
int cannonNewY = 300 - getTriangleY(30.00, cannonAngle);
if (cannonAngle < 89 && cannonAngle >= 0) {
g.setColor(white);
g.drawLine(CANNON_BASEX, CANNON_BASEY, cannonNewX, cannonNewY);
g.drawString("Cannon Angle : " + cannonAngle,10, 30);
drawCannonballAtTime(time, white);
drawCannonball(cannonNewX, cannonNewY, white);
cannonAngle += 1.0;
}
}
public static void angleDown() {
int cannonNewX = getTriangleX(30.00, cannonAngle);
int cannonNewY = 300 - getTriangleY(30.00, cannonAngle);
if (cannonAngle < 89 && cannonAngle >= 0) {
g.setColor(white);
g.drawLine(CANNON_BASEX, CANNON_BASEY, cannonNewX, cannonNewY);
g.drawString("Cannon Angle : " + cannonAngle,10, 30);
drawCannonballAtTime(time, white);
drawCannonball(cannonNewX, cannonNewY, white);
cannonAngle -= 1.0;
}
}
//draws target for the program and changes location
public static void drawTarget(int Top) {
g.setColor(white);
g.drawLine(280,0,280,300);
g.setColor(black);
g.drawLine(280, Top, 280, targetDown);
}
public static void targetUp() {
targetDown = targetTop + 50;
g.setColor(white);
g.drawString("Target Location : ("+ targetTop +"," + targetDown +")",10, 45);
if (targetTop > 0) {
targetTop --;
} else {
targetTop ++;
}
}
public static void targetDown() {
targetDown = targetTop + 50;
g.setColor(white);
g.drawString("Target Location : ("+ targetTop +"," + targetDown +")",10, 45);
if (targetTop <= PANEL_HEIGHT - 50 ) {
targetTop ++;
} else {
targetTop --;
}
}
// Draws cannonball and the x and y
public static void drawCannonball(int x, int y, Color c) {
g.setColor(c);
x = x - 5;
y = y - 5;
g.drawOval(x, y,10,10);
}
public static void drawCannonballAtTime(double time, Color c) {
g.setColor(c);
g.fillOval(getTriangleX(30.0,cannonAngle) - 5, 300 - getTriangleY(30.0, cannonAngle) - 5, 10, 10);
}

 public static void forceLarger() {

    int cannonNewX = getTriangleX(30.00, cannonAngle);

        int cannonNewY = 300 - getTriangleY(30.00, cannonAngle);

        if (force < 99 && force >= 2) {

            g.setColor(white);

            g.drawLine(CANNON_BASEX, CANNON_BASEY, cannonNewX, cannonNewY);

            g.drawString("Force : " + force,10, 55);

            drawCannonballAtTime(time, white);

            drawCannonball(cannonNewX, cannonNewY, white);

            force += 1.0;

        }    }  

   public static void forceSmaller() {

   int cannonNewX = getTriangleX(30.00, cannonAngle);

        int cannonNewY = 300 - getTriangleY(30.00, cannonAngle);

        if (force < 99 && force >= 2) {

            g.setColor(white);

            g.drawLine(CANNON_BASEX, CANNON_BASEY, cannonNewX, cannonNewY);

            g.drawString("Force : " + force,10, 55);

            drawCannonballAtTime(time, white);

            drawCannonball(cannonNewX, cannonNewY, white);

            force -= 1.0;

        }}

public static void fire() {
}
public static void keyDown(char c) {
}
public static void keyUp(char c) {
}
public static int getTriangleX(double length, double angle) {
return (int)(length*Math.cos(angle*Math.PI/180)+.5);
}
public static int getTriangleY(double length, double angle) {
return (int)(length*Math.sin(angle*Math.PI/180)+.5);
}
}


Tutor Answer

(Top Tutor) Daniel C.
(997)
School: Cornell University
PREMIUM TUTOR

Studypool has helped 1,244,100 students

7 Reviews


Summary
Quality
Communication
On Time
Value
kpcutie
Dec 4th, 2016
" Excellent job "
Hemapathy
Nov 20th, 2016
" all I can say is wow very fast work, great work thanks "
BlueOcean
Nov 7th, 2016
" Awesome! Exactly what I wanted. "
kevin12622
Oct 29th, 2016
" Goes above and beyond expectations ! "
ashleyisgod
Oct 15th, 2016
" Top quality work from this guy! I'll be back! "
likeplum4
Oct 6th, 2016
" Excellent work as usual "
Molly_Moon
Sep 23rd, 2016
" AMAZING as always! "
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1821 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors