Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

User This assignment is a programming exercise and you will need to create file called assignment 2 . py Note that there are strict requirements

User
This assignment is a programming exercise and you will need to create file called assignment2.py
Note that there are strict requirements how class and methods should be named. If you fail to follow the requirement, your work will not be graded. Each task is worth 1 point.
You don't need to implement validation of the input parameters, we will always supply legitimate values in our testing scripts.
Make sure you implement each requested function as a class method! Unless it is explicitly asking for a static method, you must include self as a first special argument. For example, if asking to create a method foobar taking two arguments, you should write something like
class Assignment2:
def foobar(self, first_argument, second_argument):
# body of the method
Task 1(Constructor)
Implement a constructor that accepts one parameter: year (type int). This parameter should initialize the similarly named class instance variable (hint: these would start with self.)
Task 2(Age)
Implement method named tellAge. This method should accept a single argument: currentYear (type int).
The result of this method should be printing on the standard output the following line:
Your age is XXXX
Where XXXX is calculated birth year, based on the supplied currentYear argument and year from the class instance variable.
Task 3(List)
Write a method named listAnniversaries that doesn't accepts arguments.
This method should return a list (note, unlike task 2, not to print, but return a list!) containing all 10-year anniversaries, assuming today is year 2022. For example, when running the following code
a = Assignment2(2000)
ret = a.listAnniversaries()
print(ret)
The output should be [10,20].
And when running the following:
a = Assignment2(1991)
ret = a.listAnniversaries()
print(ret)
The output should be [10,20,30].
Task 4(String Manipulation)
Write a method named modifyYear with the following specification:
Input parameters:
variable named n, type int: number to adjust list size
Return value should be a string, containing:
n times the first two characters in the text representation of the year (e.g., if year 2000 and n=5, then this part should be "2020202020")
odd positioned characters of text representation of year multiplied by n (if year is 2000 and n=5, then text representation is "10000" and you seelct odd characters, which is "100"; if year 1782 and n=3, then text representation is "5346" and you select "54".
For example,
a = Assignment2(1782)
ret = a.modifyYear(3)
print(ret)
should print 17171754.
Task 5(Loop and Conditional statements)
Write a static method named checkGoodString with the following specification. Note that static methods don't have self but must be annotated with @staticmethod.
Input parameters:
variable named string, type str
Return Value:
type bool: True if string has the following requirements
Is at least 9 characters long
Starts with lower case letter a-z
Contains only one number 0-9
For example,
ret = Assignment2.checkGoodString("f1obar0more")
print(ret)
ret = Assignment2.checkGoodString("foobar0more")
print(ret)
should print
False
True
Task 6(Socket)
Write a static method named connectTcp that will create TCP IPv4 socket (AF_INET, SOCK_STREAM), connect to the specified host/port, closes the connection, and returns True if the connection was established correctly.
Input parameters:
variable named host: type str
variable named port: type int
Return value
type bool: True if TCP connection successfully established, otherwise False
You can test this method with any Internet service, for example:
retval = Assignment2.connectTcp("www.google.com", 80)
if retval:
print("Connection established correctly")
else:
print("Some error")
Note that Python 3 by default will throw an exception if you trying to connect and connection cannot be established. You will need to handle this case to return False as required by this task.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions

Question

What are the different techniques used in decision making?

Answered: 1 week ago