Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import ou.*; /** * Class Rocket allows a representation of a rocket to be created in the Shapes * Window, and for it to move

import ou.*; /** * Class Rocket allows a representation of a rocket to be created in the Shapes * Window, and for it to move up the screen. * * This class is incomplete. * * @author (M250 Course Team) * @version (1.1) */ public class Rocket { private Triangle nose; // represents the rocket's nose cone private Square body; // represents the rocket's body private Circle jet; // represents the blast from the rocket's engine

/** * Constructor for objects of class Rocket */ public Rocket(Triangle t, Square s, Circle c) { //first, we store references to the workspace shape objects this.nose = t; this.body = s; this.jet = c;

//sets the initial positions of the nose. //The other parts need to be set relative to these positions. this.nose.setXPos(50); this.nose.setYPos(300);

//sets the body relative to the nose, using the helper methods this.body.setXPos(getBodyXPos()); this.body.setYPos(getBodyYPos());

//The jet is invisible to begin with, because it uses //the background colour of WHITE, but you can use BLACK //to help you see it while testing this.jet.setColour(OUColour.WHITE); this.jet.setDiameter(10);

//sets the jet position relative to the body, using the helper methods this.jet.setXPos(getJetXPos()); this.jet.setYPos(getJetYPos()); }

private int getBodyXPos() { //to be written in Q1(a)(i) return 0; }

private int getBodyYPos() { //to be written in Q1(a)(ii) return 0; } private int getJetXPos() { //to be written in Q1(a)(iii) return 0; }

private int getJetYPos() { //to be written in Q1(a)(iv) return 0; }

/** * Moves the nose of the receiver * by anInt units. * Moves other rocket components relative to the * position of the nose. */ public void moveRocketBy(int anInt) { //to be written in Q1(b) }

/** * Sets the diameter of the receiver's jet to * 6, alters its xPos so its centre aligns with the body centre * and sets its colour to red. */ public void pulse1() { //to be written in Q1(c)(i) }

/** * Sets the diameter of the receiver's jet to * 12, alters its xPos so its centre aligns with the body centre * and sets its colour to orange. */ public void pulse2() { //to be written in Q1(c)(ii) }

/** * Sets the diameter of the receiver's jet to * 24, alters its xPos so its centre aligns with the body centre * and sets its colour to red. */ public void pulse3() { //to be written in Q1(c)(iii) }

/** * Simulates the ignition of the rocket's jets */ public void ignition() { //to be written in Q1(d) }

/** * Moves the entire rocket in a loop repeated * 100 times, animating the jet as it goes, so the rocket * moves upwards by speed units every 4th iteration */ public void animateRocket(int speed) { //to be written in Q1(e) }

/** * Prompts the user to enter the number of units they want the * rocket to move upwards at a time (speed) * * If the number of units provided would eventually cause the tip of the * nose to go past the top of the Graphical Display, the user * is informed via a dialogue box that the rocket will not launch. * * Otherwise the rocket launches as required. */ public void launch() { //to be written in Q1(f) }

/** * Causes execution to pause by time number of milliseconds */ public void delay(int time) { try { Thread.sleep(time); } catch (Exception e) { System.out.println(e); } } }

  • To make the body and jet line up correctly with the nose, you need to complete the methods getBodyXPos(), getBodyYPos(), getJetXPos() and getPosJetYPos() in the Rocket class.

    When you complete the helper methods in i. to iv. below, your methods must not use fixed numbers for the positions or dimensions of the rocket parts. Rather, you must use appropriate methods from the classes Circle, Triangle and Square.

    • i.Complete the method getBodyXPos() so that it returns an appropriate value, relative to the position of the nose.

      (1 mark)

    • ii.Complete the method getBodyYPos() so that it returns an appropriate value, relative to the position of the nose.

      (2 marks)

    • iii.Complete the method getJetXPos() so that it returns an appropriate value, relative to the position of the body.

      (3 marks)

    • iv.Complete the method getJetYPos() so that it returns an appropriate value, relative to the position of the body.

      (2 marks)

    When you have correctly completed these private helper methods, the parts of the rocket should appear properly assembled in the launch position.

    However, because the colour of the jet is OUColour.WHITE it will not be visible in the Graphical Display. You can set it to OUColour.BLACK in order to see where it is, as has been done in Figure 3.

Figure 3 Correctly positioned parts of rocket

    • Triangle t = new Triangle();
    • Square s = new Square();
    • Circle c = new Circle();
    • c.setColour(OUColour.BLACK);
    • Rocket r = new Rocket(t, s, c);

TIP: To ensure that the OUWorkspace detects changes to your code, rebuild your project after editing. Use the Tools | Rebuild Package option in BlueJ.

  • b.Complete the method moveRocketBy(). The method should move the rocket upwardsby the amount given by the method's argument. In other words, the method needs to change the y-positions of the parts of the rocket.

    To do this, first set the nose position, and then set the body and jet positions relative to the nose and body, respectively. You must make use of the methods you wrote for part a., as well as other methods.

    Test your method moveRocketBy() in the OUWorkspace, using the test code included in the README.TXT file for this project, to check that the rocket moves up and down correctly.

    (3 marks)

  • c.Complete the methods pulse1(), pulse2() and pulse3(), as described below.
    • i.The pulse1() method should do the following:
    • set the diameter of jet to 6
    • reset the position of the jet (because its size is now different, and we want it to stay centred underneath the body)
    • set the colour of jet to red.
    • ii.The pulse2() method should do the following:
    • set the diameter of jet to 12
    • reset the position of the jet (because its size is now different, and we want it to stay centred underneath the body)
    • set the colour of jet to orange.
    • iii.The pulse3() method should do the following:
    • set the diameter of jet to 24
    • reset the position of the jet (because its size is now different, and we want it to stay centred underneath the body)
    • set the colour of jet to red.

    To test your completed methods, execute the statements in part a. to create an instance of Rocket.

    Execute the next three lines of code, which are included in the README.TXT file for this project, one line at a time observing the results in the Graphical Display:

    • r.pulse1();
    • r.pulse2();
    • r.pulse3();

    If your methods are correct, you should see the jet expanding and changing colour in the Graphical Display.

    (4 marks)

  • d.Complete the ignition() method. The method will simulate firing up the rocket's jet engines by repeatedly changing the size and colour of the jet using the methods pulse1(), pulse2() and pulse3()..

    Because the Graphical Display will change very quickly as your method goes through each expansion / colour change step, we have provided a method called delay() that you can use to pause execution for a number of milliseconds after each change in the colour and size of the jet. You do not have to know how this method works, all you need to do is to include the message-send

    this.delay(200);

    after each pulse. If this does not slow down the display enough for you to see the changes, try increasing the argument 200 to a bigger number until you can see the changes happening.

    Use a loop to repeat the following five times:

    • send a pulse1() message to the receiver
    • delay execution for 200 milliseconds
    • send a pulse2() message to the receiver
    • delay execution for 200 milliseconds
    • send a pulse3() message to the receiver
    • delay execution for 200 milliseconds.

    To test your completed method, execute the statements in part a. to create an instance of Rocket.

    Then execute the following line of code, on its own, after the others.

    • r.ignition();

    If your method is correct, you should see in the Graphical Display, the jets expanding and changing colour a total of five times.

    (4 marks)

  • e.You will now complete the method animateRocket(int speed).

    This method animates the rocket, as follows.

    As we have a rocket movement method (part b.) and three pulse methods (part c.), we can perform four different animations in a loop.

    The first time the loop executes:

    • send a pulse1() message to the receiver
    • delay execution for 200 milliseconds.

    The second time the loop executes:

    • send a pulse2() message to the receiver
    • delay execution for 200 milliseconds.

    The third time the loop executes:

    • send a pulse3() message to the receiver
    • delay execution for 200 milliseconds.

    The fourth time the loop executes:

    • move the rocket by the specified number of units (speed) upwards in one step.

    Write a loop that repeats this sequence 100 times.

    Complete the animateRocket() method.

    (6 marks)

    Before proceeding you should test your animateRocket() method in the OUWorkspace using the test code included in the README.TXT file for this project.

  • f.In the final method launch() a dialogue box is displayed to prompt the user to enter a positive integer representing the speed (number of units) with which they want the rocket to move upwards.

    You may assume that the user enters characters that represent valid positive integers in the dialogue box.

    • You will need to convert the string returned by the dialogue box into an integer.
    • If the user has entered a number that would move the tip of the nose past the top of the Graphical Display (i.e. animating the rocket would cause the yPos of the nose to become less than zero at some stage), a dialogue box should alert the user that the rocket will not launch as the number entered is too large.
    • Otherwise the method should send an ignition() message to the receiver, followed by an animateRocket() message with the user-specified speed as the message's argument.

    (4 marks)

  • g.To facilitate testing, all of the methods you have written (apart from the helper methods you wrote in part a.) have had a public access modifier, however in the finished class it seems more appropriate that only one of these methods should be public. Write down the name of that method and explain why it should be public.

    (1 mark)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions