Question: Purpose: Practice with conditionals; practice with variables and good program design. Degree of Di culty: Easy to Moderate For this problem, we have 3 balls

Purpose: Practice with conditionals; practice with variables and good program design. Degree of Di culty: Easy to Moderate

For this problem, we have 3 balls of di erent weights. Your job is to write a program that allows the user to place the balls on either side of a scale and reports which side of the scale is heavier

Figure 1: An example with all balls currently on the left-hand scale Here is a description of how your program should look:

Use a 400x300 canvas size

Colour half of the screen white and the other half black. Each half of the screen represents one side

of a scale.

Display 3 balls on the canvas. Each ball has a di erent weight (1kg, 2kg and 3kg). Each ball should be a di erent colour, and heavier balls should be bigger.

Displayatalltimesonthecanvasamessageindicatingwhetherthescaleisbalanced,orifnot,which side is heavier, based on the position of the balls.

Here is a description of how your program should behave

Initially, the balls can be anywhere on the screen. In the example above, they are all on the left-hand

side of the scale.

When the user presses 1: The 1kg ball should be moved to the location of the mouse.

When the user presses 2: The 2kg ball should be moved to the location of the mouse.

When the user presses 3: The 3kg ball should be moved to the location of the mouse.

Of course, the message concerning which side of the scale is heavier should be kept up to date as the balls move.

Here are some suggestions for how to design your program, especially with regard to the Model-View- Controller design pattern.

You will want to use at least 6 global variables in your program: these are the current x and y coor- dinates for each of the three balls. You may wish to use another 3 global variables for the weight of each ball as well, although since the weights do not change, this is not strictly necessary.

You will likely want your draw() function to take care of the following tasks: Drawing the 2 sides of the scale Drawing the 3 balls at their current positions Determining the correct message to display based on the positions and weights of the 3 balls.

You will likely want your keyPressed() function to take care of changing the positions of the balls by simply changing the global variables (i.e. the model) that represent each balls position

What To Hand In: Your Processing sketch folder (named cmpt140_a5q1) compressed as a zip le (.zip). This folder should contain your Processing program as a .pyde le with the same name as the folder (i.e.cmpt140_a5q1.pyde) and a sketch.properties project le.

Evaluation:

0 marks for submitting nothing and/or les that cannot be opened.

-1 mark for lack of identi cation in the solution.

2 marks for correctly displaying balls and the background

2 marks for correctly moving the balls on a keypress

3 marks for using conditional statements to determine which scale is heavier and displaying the cor- rect message

1 mark for documenting code using comments and docstrings.

1 mark for good Model-View-Controller style design

MUST USE PYTHON ! THANKS A LOT

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!