QuestionQuestion

Transcribed TextTranscribed Text

Overview For this lab assignment, you will practice creating and designing your own Python data classes. Classes are a useful programming tool that allows you to better organize your code, particularly for data objects that have similar properties. For example, if you were collecting data in the field relating to soil profile data, you could create a "Soils" class that contains properties relating to the data (such as bulk density and soil texture) as well as methods (i.e. functions defined in a class) relating to the phenomenon (such as determining the soil classification based on the measured data). As another example, you could work on a project that involves surveyed data, and create a "People" class that contains info such as number of dogs, cats, etc. Open a new file and save your program as "geometry.py" [1] (20 Points) Create a generic base class named Geometry that will be used to organize any type of geometry object (such as point, line, or polygon) that you might create. Assume that all geometry objects should have a unique ID. Create a "Geometry" class that has the following attributes:  id = A unique ID for each Geometry object (starts at 0) The Geometry class should have the following methods:  A constructor (__init__) for when creating a new Geometry object The constructor of the Geometry class should automatically assign the ID of the Geometry object. The data type of the ID is an integer and starts at 0 (similar to the FID field in an ArcGIS shapefile). (See the mini-tutorial at the end of the lab for creating an automatic unique ID.) [2] Create a class named Point that stores and implements a 2-D geographic point. The Point class should inherit the Geometry class (i.e. be a subclass of Geometry). Create a "Point" class that has the following attributes:  id = Inherited from the Geometry class  x = The x coordinate of the Point  y = The y coordinate of the Point The Point class should have the following methods:  A constructor for when creating new Point objects - Creates a Point object having an ID inherited from the Geometry class as well as x and y coordinates  A string conversion method to return a string representation of the point in the following format (note the number of decimal places used), such that: >>> # Example Output >>> p1 = Point(1.216, 3.4582) >>> print p1 2 (1.22, 3.46)  An "equality" method to compare the x and y coordinates of the point to another point. This method should return a boolean value. If the points have the same x and y coordinates, return True, otherwise, return False  An "identify" method to compare the IDs of the point to another point. This method should return a boolean value. If the IDs of two points are the same, return True, otherwise, return False  A "distance" method that returns a floating point representing the straight line distance between the point and another point using the distance formula: 𝑑 = √(𝑥2 − 𝑥1 ) 2 + (𝑦2 − 𝑦1 ) 2  A "quadrant" method that returns a string depending on which quadrant it is located on the Cartesian coordinate plane. The output from "quadrant" is defined as: Condition Output x > 0 and y > 0 "Quad I" x < 0 and y > 0 "Quad II" x < 0 and y < 0 "Quad III" x > 0 and y < 0 "Quad IV" x = 0 "Y-axis" y = 0 "X-axis" x = 0 and y = 0 "Origin" >>> # Example Output >>> p1 = Point(-2,1) >>> p2 = Point(1,5) >>> print p1 (-2.00, 1.00) >>> print p2 (1.00, 5.00) >>> p1.equal(p2) False >>> p1.distance(p2) 5.0 >>> p1.quadrant() 'Quad II' >>> p2.quadrant() 'Quad I' 3 [3] Write a test program by creating a "test" function. This function should create three points, P1 = (0,3), P2 = (-3,7), and P3 = (-3,7), and performs the following tasks:  Prints the id and x,y coordinates of each point onto the screen  Prints where the coordinate is located on the Cartesian coordinate plane  Calculate and display the distance between each set of points  Test the equality method between each set of points  Test the identify method between each set of points # Example Output with P1 = (-3,-3) and P2 = (0,0) (Note: You don't have to match this exactly.) P1) ID = 0, Coordinates = (-3.00, -3.00), Location = Quad III P2) ID = 1, Coordinates = (0.00, 0.00), Location = Origin Distance between (-3.00, -3.00) and (0.00, 0.00) is 4.24 P1 == P2? False P1 same as P2? False [4] Assume you are given three points, P1, P2, and P3. One way to test if all three points are in the same line is by calculating the slope between each pair of points and comparing them. The slope (m) of a line between two points is given by the equation: 𝑚 = (𝑦2 − 𝑦1 ) (𝑥2 − 𝑥1 ) If the slope between P1 and P2 equals the slope between P2 and P3, the points are collinear. Otherwise, they are not. (This is not a perfect definition, but it will do for this lab.) Write a function called "collinear" (not a method) that has three input parameters, representing three points, and returns True if they are collinear (all fall on the same line), False otherwise. Note: This equation doesn't work too well for cases where (x2 - x1) = 0 because of the "Division by Zero" error. You can ignore these cases (i.e. don't worry about it). >>> # Example Output >>> p1 = Point(0,0) >>> p2 = Point(3,3) >>> p3 = Point(2,4) >>> print "Are P1, P2, and P3 on the same line?", collinear(p1,p2,p3) Are P1, P2, and P3 on the same line? False >>> p4 = Point(0,0) >>> p5 = Point(3,3) >>> p6 = Point(5,5) >>> print "Are P4, P5, and P6 on the same line?", collinear(p4,p5,p6) Are P4, P5, and P6 on the same line? True 4 What to Submit Submit a python file, "geometry.py" which includes all classes and functions you created. Note: You must include appropriate comments in your code to receive full points. Mini-tutorial - How to generate the unique ID for Geometry objects? First, let’s see how to assign a specific ID to an example object called "Land_Parcel". The following snippet is a constructor, "__init__", to create a Land_Parcel object by assigning the value of a parameter pid to the instance variable id. class Land_Parcel(object): def __init__(self,pid): self.id = pid self.area = 0 # Floating point variable representing the area in acres self.price = 0 # Integer variable representing price in thousands of dollars self.owner = "" # String variable representing the current owner def test(): p1 = Land_Parcel(1) p2 = Land_Parcel(2) p3 = Land_Parcel(2) print p1.id print p2.id print p3.id >>> test() 1 2 2 Using the class name with parameters like a function tells Python find the class and build one instance of it. Here, Land_Parcel requires a parameter, pid when generates a new Land_Parcel object. (It is named pid to differentiate the id of Land_Parcel from the id passed into __init__ as an parameter.) p1 = Land_Parcel(1) The variable p1 refers to a Land_Parcel object that has an instance variable id. The specialized function __init__ is automatically called when an object is created by using the name of the class like in "p1 = Land_Parcel(1)". Then, the first parameter (self) in __init__ refers to "p1" and the second parameter (1) is passed to "pid". The sentence "self.id = pid" in "__init__(self, pid)" assigns 1 to the id of p1. 5 An instance variable, id can be accessed using dot notation: print p1.id # Prints 1 on the screen Can we generate the unique IDs of Land_Parcel objects when you run the above snippet? No, the test() program generates two Land_Parcel objects: p2 and p3 having the same id (2). If we allow a user to assign a specific ID, it does not guarantee that every object has the unique ID. So, now how can we generate a unique ID? We should not allow a user assign the ID of Land_Parcel objects and automatically generate the id of Land_Parcel objects inside of the __init__ function. Suppose that ID must be an integer and starts with 1. To generate the unique integer ID starting from 1, we could automatically increase the id of the object by 1 whenever a new Land_Parcel object is created. Here, the important thing you have to consider is that the variable holding the current id should be initialized one time at first and accessible over the entire program. class Land_Parcel(object): def __init__(self): self.id = ?? # How? There are several ways to implement the unique id by increasing the id of the object by 1 whenever new Land_Parcel object is created. We'll discuss two methods here. Method 1 - Global Variables (Not Recommended) We can use a global variable that can be accessed by the entire scope of the program. The variables in a function are local to that function. Global variables are declared at the top of a program, and are visible in the entire program. It is usually not recommended to use global variables because it can lead to confusing code, with global variables overriding local variables. Example using Global Variables: first = 1 def one(): global first # Before using a global variable inside of a function, you specify it first += 3 result = first * 2 return result >>> print one() # first = 1 8 >>> print one() # first = 4 14 >>> print one() # first = 7 20 6 To generate a unique ID for the Land_Parcel object: # (1) Define a global variable outside of the Land_Parcel class, for example, "uniqueID" class Land_Parcel(object): def __init__(self): # (2) To access "uniqueID", specify that it is a global variable # (3) Increase "uniqueID" by 1 # (4) Assign "uniqueID" to "self.id" Here, we generate the ID starting from 1. How can we generate the ID so it starts from 0? Method 2 - Member Variables (Recommended) We can use a member variable of the Land_Parcel class that we create. We can declare a class member variable inside of a class. The member variable can be accessed in class methods using dot notation through the name of the class. For more information on member variables see the slides on this topic in the lecture notes for this week. class Land_Parcel(object): # (1) Define a member variable in the class called "uniqueID" with an initial value def __init__(self): # To access a member variable, use dot notation with the name of the class # The print command is shown here as an example reference print Land_Parcel.uniqueID # (2) Increase the uniqueID by 1 # (3) Assign the uniqueID to self.id Example using Member Variables: class Circle(object): pi = 3.14159 # Member variable def __init__(self,radius): self.r = radius def getArea(self): return Circle.pi * self.r ** 2 >>> c1 = Circle(5) # radius = 5 >>> c1.getArea() 78.53975

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import math

# [1] Create a generic base class named Geometry that will be used to organize any
# type of geometry object (such as point, line, or polygon) that we might create. Assume that all
# geometry objects should have a unique ID
class Geometry:

    # id = A unique ID for each Geometry object (starts at 0)
    # The constructor of the Geometry class should automatically assign the ID of the Geometry
    # object. The data type of the ID is an integer and starts at 0
    uniqueID = 0

    # A constructor (__init__) for when creating a new Geometry object
    def __init__(self):
       self.id = self.uniqueID
       self.__class__.uniqueID += 1

# [2] Create a class named Point that stores and implements a 2-D geographic point.
# The Point class should inherit the Geometry class (i.e. be a subclass of Geometry).
class Point(Geometry):

    # Create a "Point" class that has the following attributes:
    # id = Inherited from the Geometry class
    # x = The x coordinate of the Point
    # y = The y coordinate of the Point
    x = 0.0
    y = 0.0

    # A constructor for when creating new Point objects - Creates a Point object
    # having an ID inherited from the Geometry class as well as x and y coordinates
    def __init__(self, x, y):
       Geometry.__init__(self)
       self.x = x
       self.y = y...

By purchasing this solution you'll be able to access the following files:
Solution.py.

$73.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats