CIT020 Index > Alice Assignment: Classes, Objects, Methods, and Parameters

Alice Assignment: Classes, Objects, Methods, and Parameters

Read everything before doing anything.

Part 1

Do exercise 2 on page 89 (Lock Combination). Then create world with a TurningComboLock object in it. Your world.myFirstMethod will do the following (presuming that the lock starts at zero). In this assignment, we will define left and right as from our point of view, not the lock’s point of view. If the combination were 35-9-25, your program would do the following:

Your program must work on any three (different) numbers; it must not be designed to do only 35-9-25. This means you will have to do some arithmetic. Presume that zero will not be one of the numbers in the combination, and that no two consecutive numbers are alike. Thus, there cannot be a combination like 12-0-22 or 30-20-20. (This actually makes the program easier to write; you don’t have to look for those special cases.)

Notice that the leftOne and rightOne methods should move the combination lock only one digit to the left or right. Make the duration for these methods 0.1 seconds; that will make the animation run at a reasonable pace.

Save this world in a file whose name is in the form lastname_firstname_lock.a2w. You will have already stored TurningComboLock in an .a2c file, as it is a new class.

Part 2

Redo the previous exercise, using methods named turnLeft and turnRight. Each of these methods will take two numeric parameters. The first parameter is the starting number, and the second parameter is the ending number. So, for example, if you wanted to turn the dial to the left from 22 to 17, you would use 22 as the first parameter and 17 as the second parameter. Rename TurningComboLock as SmartComboLock and save it as a new class.

Do not use a while or a loop of any kind for the leftTo and rightTo methods. Instead, given the current number and the number to move to, determine how far you have to twist the dial clockwise or counterclockwise, and roll the dial that far. I want you to do this because:

  1. The animation is smoother and faster if you do it all in one “motion.”
  2. I want to know that you have done the planning to figure out the math for twisting the dial.

Hint: since there are 40 numbers, twisting the dial 1/40 of a revolution will move it one number.

Programming note: When we get to C++, we will discuss the concept of “magic numbers”—numbers that have meaning for your program, but not outside of it. The number of positions on the lock is a “magic number,” and if you are being strict, you should make a variable named LOCK_POSITIONS (a constant) and set it to 40. That way, if we ever change the number of positions on the lock, we need to change only one line instead of having to look through the entire program for every place that 40 occurs. However, for this assignment, it is fine if you just put 40 directly into your formulas.

Save this world in a file whose name is in the form lastname_firstname_smartlock.a2w

When You Finish

Attach the .a2w files and the .a2c files to an email and send them to the instructor. You must put the words CIT020 Assignment 3 in the subject line of your email, or it will not be graded.