Question

In: Computer Science

05 Prepare : Checkpoint A Objective This assignment will give you practice writing event handling methods...

05 Prepare : Checkpoint A


Objective


This assignment will give you practice writing event handling methods to advance a ship in two dimensional space.


Overview


After completing (or while completing) the preparation material for this week, complete the following exercise.


For this exercise, you will create a class that models a ship flying around in a 2-D plain (containing an x and y coordinate). A game class is provided to you that will help you call methods to advance and "draw" it.


Instructions


Write a class for a ship that has integer x and y coordinates, and also stores the components of its velocity, dx and dy. (Please note that will will have similar elements in our weekly assignment, where we will break them into separate classes, but for this assignment, you can store all four as member variables of your ship class.


The ship should have two methods, one for advance, and one for draw. The advance method should move the ship forward one unit in time. This is accomplished by adding dx onto x, and dy onto y. This draw method should output, "Drawing the ship at (x, y)" where x and y represent the current location of the ship. The __init__ method for the ship should initialize x, y, dx, and dy to 0.


For this assignment, the provided main function will set the velocity of the ship when it is created, and the velocity will remain constant for the rest of that run.


The following UML class diagram defines your ship class:


Shipx : inty : intdx : intdy : int__init__()advance()draw()


To go with your ship class, you will be provided with a Game class and a main function. The Game class has a ship, and two important functions, on_draw() and update(). In your the on_draw function, you simply need to call the ship's draw function. In the update function, you simply need to call the ship's advance function. Nothing else in the Game class is required of you.


In addition, nothing is required from you in the main function. It can be left as provided. You can obtain the provided code from: /home/cs241/check05a.py with the following command:


cp /home/cs241/check05a.py .

Finally, for simplicity, for this assignment you may include all of your classes in the same file if you would like.


INSTRUCTIONS RECAP


Thus, in short, for this checkpoint, you need to do the following.


Create a Ship class


Create an __init__ method that sets x, y, dx, and dy to 0.


Create a draw method that prints the text, "Drawing ship at (x, y)".


Create an advance method that adds dx to x, and dy to y.


In the Game class on_draw method, call your ship's draw method.


In the Game class update method, call your ship's advance method.


Sample Output


The following is an example of output for this program:


Enter a random seed: cat Starting the ship with velocity (-1, 3) Drawing ship at (-1, 3) Drawing ship at (-2, 6) Drawing ship at (-3, 9) Drawing ship at (-4, 12) Drawing ship at (-5, 15) Drawing ship at (-6, 18) Drawing ship at (-7, 21) Drawing ship at (-8, 24) Drawing ship at (-9, 27) Drawing ship at (-10, 30) Drawing ship at (-11, 33) Drawing ship at (-12, 36) Drawing ship at (-13, 39) Drawing ship at (-14, 42) Drawing ship at (-15, 45) Drawing ship at (-16, 48) Drawing ship at (-17, 51) Drawing ship at (-18, 54) Drawing ship at (-19, 57) Drawing ship at (-20, 60)

Automatic Grading Script (TestBed)


This assignment is pass/fail. In order to receive credit, it must pass the auto-grading test script (TestBed). You can run this script as many times as you like while you are working on the assignment. The same test script will be run by the instructor for credit, so you should not be surprised at the result.


In addition, because the point of this exercise is to help you practice the use of classes. You must use classes to receive credit for this assignment.


Solutions

Expert Solution

# Python version : 3.6

class Ship:
  
   def __init__(self):
       ''' Constructor that initializes x, y, dx, dy to 0 '''
       self.x = 0
       self.y = 0
       self.dx = 0
       self.dy = 0
      
   def draw(self):
       '''
       Function that prints the text, "Drawing ship at (x, y)".
       Call this function within Game's on_draw function, by using the Ship class object and dot operator
       i.e ship.draw() where ship is the object of class Ship in class Game
       '''
       print('Drawing ship at (',x,', ',y,')')
      
   def advance(self):
       '''
       Function that adds dx to x, and dy to y.
       Call this function within Game's update function, by using the Ship class object and dot operator
       i.e ship.update() where ship is the object of class Ship in class Game
       '''
       x += dx
       y += dy

#end of Ship class

Code Screenshot:


Related Solutions

This assignment will give you practice in Handling Exceptions and using File I/O. Language for this...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this program is JAVA Part One A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Prompt the user for data to write the file. Part Two Write a program that...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please have a screenshot of output) In competitive diving, each diver makes dives of varying degrees of difficulty. Nine judges score each dive from 0 through 10 in steps of 0.5. The difficulty is a floating-point value between 1.0 and 3.0 that represents how complex the dive is to perform. The total score is obtained by discarding the lowest and highest of the judges’ scores,...
03 Prepare : Checkpoint B Objective Demonstrate basic class methods (member functions) in Python. After completing...
03 Prepare : Checkpoint B Objective Demonstrate basic class methods (member functions) in Python. After completing (or while completing) the preparation material for this week, complete the following exercise. Introduction Recall from your mathematics classes that a complex number is composed of two parts, a real part and an imaginary part. We could write a complex number in the form "3 + 4i" where 3 is the real part and 4 is the imaginary part. For this program, we will...
Murach Android tip calculator event handling assignment
Murach Android tip calculator event handling assignment
Question Objective: The objective of this lab exercise is to give you practice in programming with...
Question Objective: The objective of this lab exercise is to give you practice in programming with one of Python’s most widely used “container” data types -- the List (commonly called an “Array” in most other programming languages). More specifically you will demonstrate how to: Declare list objects Access a list for storing (i.e., writing) into a cell (a.k.a., element or component) and retrieving (i.e., reading) a value from a list cell/element/component Iterate through a list looking for specific values using...
Programming Assignment #3: SimpleFigure and CirclesProgram Description:This assignment will give you practice with value...
Programming Assignment #3: SimpleFigure and CirclesProgram Description:This assignment will give you practice with value parameters, using Java objects, and graphics. This assignment has 2 parts; therefore you should turn in two Java files.You will be using a special class called DrawingPanel written by the instructor, and classes called Graphics and Color that are part of the Java class libraries.Part 1 of 2 (4 points)Simple Figure, or your own drawingFor the first part of this assignment, turn in a file named...
Objective Of The Assignment To prepare a memorandum to the chief executive officer (CEO) as to...
Objective Of The Assignment To prepare a memorandum to the chief executive officer (CEO) as to why the accrual basis of accounting and recording financial transactions using other aspects of Generally Accepted Accounting Principles is preferred (not just mandatory) as opposed to the cash basis of accounting. In actual business, critical issues and problems are communicated in writing, particularly to create accountability for the positions taken and solutions arrived at. The communication includes relevant background information and rationale and other...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and if-else statements. Candy Calculator [50 points] The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you do no exercise. This is called your basal metabolic rate or BMR. The calories needed for a woman to maintain her weight is: BMR = 655 + (4.3 * weight in pounds) + (4.7 * height in inches) - (4.7 *...
Assignment Overview This assignment will give you practice with interactive programs and if/else statements. Part 1:...
Assignment Overview This assignment will give you practice with interactive programs and if/else statements. Part 1: User name Generator Write a program that prompts for and reads the user’s first and last name (separately). Then print a string composed of the first letter of the user’s first name, followed by the first five characters of the user’s last name, followed by a random number in the range 10 to 99. Assume that the last name is at least five letters...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is selected from the online contest problem archive (Links to an external site.), which is used mostly by college students world wide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest (Links to an external site.). For your convenience, I copied the description of the problem below with my note on the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT