CS303E Homework 7

Instructor: Dr. Bill Young
Due Date: Wednesday, October 15, 2025 at 11:59pm

Copyright © William D. Young. All rights reserved.

Driving a Toy Car

This assignment asks you to simulate driving a simple toy car around a grid. This requires you to implement a ToyCar class with specified attributes and methods. You will also write at least one functions outside the class that use the class to implement more complicated behaviors of the car.

The ToyCar class

Here are the specific requirements for your ToyCar class:
  1. Implement your toy car as a class, with the following three private data members: x coordinate of the location, the y coordinate of the location, the direction the car is heading. The x and y coordinates should default to 0. (Note that I may refer to the location as (x, y), but you shouldn't implement location as a pair; store x and y as separate data members.) The direction is represented as a number which must be in [0, 90, 180, 270], representing east, north, west and south, respectively. (The number is the angle of rotation counter-clockwise from the x axis.) Direction should default to 0 (east) when you create a ToyCar object.

  2. You should have at least the following methods in your class:
Of course, the definitions of these will all need the self parameter. See the Program Structure section below.

Notice that commands that change the location or direction of the car must update the class data members appropriately to represent the new state.

Other Functions:

In addition to the class described above, you will define at least one other function that makes use of the class. These should be defined outside of the class definition, but in the same file. Feel free to define auxiliary functions if you like, but you must have: goto( car, x, y ): This function drives the specified car to location (x, y). Notice that our cars can only travel in cardinal directions. I.e., they can't go diagonally on the grid. So you'll potentially have to set direction east or west and move in the X dimension, and then set direction north or south and move in the Y dimension. Be sure to handle the cases where you're already on proper coordinate(s). (For example, if you're at (0, 0) and you need to move to (0, 100), you don't have to move east or west, only north.) You can assume that x and y are integers.

Notice that you'd ordinarily have to print out the car location and direction ( print( car ) should work to do that) after every move to see if you're doing things right. To make the output more understandable, I added some debugging print statements to the commands that change the car location or direction. You'll see those in the sample output. I following the useful convention of prefixing each of those with "DEBUG" to make them easy to find. I would typically remove or comment those out for the "production" version of the software. You should just leave them in.

Notice also the use of print statements on the car object. This only works after you've defined __str__() for the class.

Possible Program Structure

Below is a possible structure for your program. You don't have to follow this exactly. But it's probably a good model.
# Program header and description here.

# Any imports you need.

# Constants:
EAST = 0
NORTH = 90
...

class ToyCar:

    def __init__( self, x = 0, ... ):
        # Command should fail and print "ERROR: Illegal direction entered." 
        # if the provided direction is invalid.  You can assume x and
        # y are integers.
        ...

    def __str__( self ):
        """ Generate a string containing information on 
        the class object. """
        ...

    def turnLeft( self ):
        ...

    # the other ToyCar methods

# The other functions you'll need to supply.  You can have more, but
# must have at least this one:

def goto( car, x, y ):
    ...

# You're welcome to add a main() function and a call to main() to 
# include some test code.  But you can also test it from the 
# interactive loop.  Both are illustrated below.

Expected Output:

Below is some sample output for this program. Note that this is run in the Python interactive loop started from the command line. You can run yours from your IDE, but the TAs will run their own "driver" program to test your class and functions.

> python
Python 3.8.10 (default, Feb  4 2025, 15:02:54) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ToyCar import *
>>> c1 = ToyCar( 100, -100, SOUTH )
>>> print( c1 )
Your car is at location (100, -100), heading South
>>> c2 = ToyCar()
>>> print( c2 )
Your car is at location (0, 0), heading East
>>> c3 = ToyCar( y = -50, d = 90 )
>>> print( c3 )
Your car is at location (0, -50), heading North
>>> c = ToyCar( d = NORTH )
>>> print( c )
Your car is at location (0, 0), heading North
>>> c.getDir()
90
>>> c.setDir( 45 )
ERROR: Illegal direction entered.
>>> print( c )
Your car is at location (0, 0), heading North
>>> c.forward( 100 )
DEBUG: moving forward 100
>>> print( c )
Your car is at location (0, 100), heading North
>>> c.turnLeft()
DEBUG: turning West
>>> print( c )
Your car is at location (0, 100), heading West
>>> c.forward( -50 )
ERROR: Illegal distance entered.
>>> c.forward( 50 )
DEBUG: moving forward 50
>>> print( c )
Your car is at location (-50, 100), heading West
>>> c.setDir( SOUTH )
DEBUG: setting direction South
>>> print( c )
Your car is at location (-50, 100), heading South
>>> c.turnRight()
DEBUG: turning West
>>> print( c )
Your car is at location (-50, 100), heading West
>>> c.forward( 25 )
DEBUG: moving forward 25
>>> print( c )
Your car is at location (-75, 100), heading West
>>> goto( c, 0, 0 )
DEBUG: setting direction East
DEBUG: moving forward 75
DEBUG: setting direction South
DEBUG: moving forward 100
>>> print( c )
Your car is at location (0, 0), heading South
>>> goto( c, 100, 100 )
DEBUG: setting direction East
DEBUG: moving forward 100
DEBUG: setting direction North
DEBUG: moving forward 100
>>> print( c )
Your car is at location (100, 100), heading North
>>> c.getX()
100
>>> c.getY()
100
>>> 

The above shows running the program interactively in the Python loop. You could also add a main function in your ToyCar.py file that includes the same commands. This is a nice way to debug without having to enter all the commands in the interactive loop. That would look as follows:

def main():
    c1 = ToyCar( 100, -100, SOUTH )     # Create car c1
    print( c1 )                         # and show its state

    c2 = ToyCar()                       # create car c2
    print( c2 )                         # and show its state

    c3 = ToyCar( y = -50, d = 90 )      # create car c3
    print( c3 )

    c = ToyCar( d = NORTH )             # create car c
    print( c )

    print( c.getDir() )                 # where is c headed?

    c.setDir( 45 )                      # this should fail
    print( c )

    c.forward( 100 )                    # move c forward 100
    print( c )

    c.turnLeft()                        # turn c left
    print( c )

    c.forward( -50 )                    # this should fail

    c.forward( 50 )                     # move c forward 50
    print( c )

    c.setDir( SOUTH )                   # turn c toward the south
    print( c )

    c.turnRight()                       # turn c right (West) 
    print( c )

    c.forward( 25 )                     # move c forward 25
    print( c )

    goto( c, 0, 0 )                     # call your external function 
    print( c )                          # to go to (0, 0)

    goto( c, 100, 100 )                 # now goto (100, 100)
    print( c )

    print( c.getX() )                   # get the current X
    print( c.getY() )                   # get the current Y

main()
You would then call the program like any other. Please comment out the call to main() before you submit.

Turning in the Assignment:

The program should be in a file named ToyCar.py. Submit the file via Canvas before the deadline shown at the top of this page. Submit it to the assignment hw7 under the assignments sections by uploading your Python file. Make sure that you following good coding style and use comments.

Your file must compile and run before submission. It must also contain a header with the following format:

# File: ToyCar.py
# Student: 
# UT EID:
# Course Name: CS303E
# 
# Date: 
# Description of Program: 

Programming Tips:

Program incrementally: Writing any complex program should be done in pieces. It's crazy to do the entire thing and then try to debug it. Yet that's what I frequently see students do. Instead, write the simplest version first and test it before you move on. For example, write a simple version of your ToyCar class, with the init and str functions. Get that working. Then start adding in the other methods and testing them. The extra time will more than pay off in reduced frustration!

Use print for debugging: If you don't quite see what's going wrong at any point, add print statements. Often seeing what values are being generated as you go is enough to understand your error. Just remember to comment out or remove the extra print statement before you submit.

Use Functions: Functions provide a powerful mechanism for abstraction. Once you've defined a function, it's as if you had an additional primitive in the programming language. For example, once we defined isPrime(n), you can treat that as it it were native to the language. Henceforth, whenever you need to check whether an integer is prime, you only need import that function from the appropriate library and call it. As long as you trust whomever programmed it, you really don't need to worry about how it works.

If you see that you're doing some computation multiple times write a function to handle that. Always test the function before you move on. Also, make your functions as robust as possible. For example, our isPrime function works for arbitrary integers, but will crash for other inputs. For a production version, it would be better to validate the arguments more thoroughly.

Functions provide all of the following benefits:

  1. they provide reusable functionality;
  2. they make your programs easier to read;
  3. you only need to debug the code once;
  4. they make your program easier to maintain;
  5. they facilitate writing large programs;
  6. they support collaboration with a programming team.