< Back

Test 1


If you do not currently have a directory for tests, create one in your cs170 directory, and also create a directory for test1

$ mkdir ~/cs170/tests
$ mkdir ~/cs170/tests/test1
$ cd ~/cs170/test/test1

This portion of the test is worth 30 total points. You may only access the Python documentation website and the tkinter documentation website. NO OTHER WEBSITES ARE PERMITTED. You are also not allowed to access any personal files in your CS account. As usual, you are not allowed to directly copy any code from the Internet or another individual. You should also follow all coding standards discussed thus far.


Test 1

Question 9

In a file called question_9.py, create a program that creates a 400 × 400 window, and fills it with a target seen below. This squared target is centered in the window, and each square is 150 pixels smaller than the enclosing square.

(10 points)

Question 10

In a file called questions_10_11.py, create a class called Simulator. This class should have attributes PC, R1, and R2. All three attributes should be integers, and should be defaulted to the value 0. This class should have methods called add and sub, which performs the specified operations on R1 and R2 (in that order, so R1 - R2), and stores the result in attribute R1. Each operation should increment the PC attribute by 1. You should also provide methods called set_R1 and set_R2, which allow the values of R1 and R2 to be set, respectively. Include a __str__(self) method which simply prints the values of all of the registers in table format.

  >>> my_sim = Simulator()
  >>> my_sim.set_R1(3)
  >>> my_sim.set_R2(4)
  >>> my_sim.add()
  >>> print(my_sim)
  R1 7
  R2 4
  >>> my_sim.sub()
  >>> print(my_sim)
  R1 3
  R2 4
  

(10 points)

Question 11

In the same file as above, create a class called AdvancedSimulator, which inherits your above definition of Simulator. This should add two additional attributes: R3 and R4. These should also be integers, but their values should be provdied to the constructor. You should define appropriate methods for setting these registers as well. You need to define methods to copy the value from R3 or R4 into R2. These methods should be named following the template copy_R#_to_R2. You also need to add two additional methods: mul and div, which computes the product and quotient (integer!) of R1 and R2 (respectively), and stores them in the R1 attribute.

Don't forget to override the __str__(self) method so that it can print all 4 registers!

  >>> my_adv_sim = AdvSimulator(1, 2)
  >>> my_adv_sim.set_R3(2)
  >>> my_adv_sim.set_R1(3)
  >>> my_adv_sim.set_R2(4)
  >>> my_adv_sim.add()
  >>> print(my_adv_sim)
  R1 7
  R2 4
  R3 2
  R4 2
  >>> my_adv_sim.sub()
  >>> print(my_adv_sim)
  R1 3
  R2 4
  R3 2
  R4 2
  >>> my_adv_sim.copy_R4_to_R2()
  >>> print(my_adv_sim)
  R1 3
  R2 2
  R3 2
  R4 2
  >>> my_adv_sim.mul()
  >>> print(my_adv_sim)
  R1 6
  R2 2
  R3 2
  R4 2
  >>> my_adv_sim.div()
  >>> print(my_adv_sim)
  R1 3
  R2 2
  R3 2
  R4 2
  

(10 points)

 



Submission

When you have finished, create a tar file of your test1 directory. To create a tar file, execute the following commands:

cd ~/cs170/tests
tar czvf test1.tgz test1/

To submit your activity, go to inquire.roanoke.edu. You should see an available assignment called Test 1. Make sure you include a header listing the authors of the file.