Question

Python: Design a Ship class that has the following members: A member variable for the name...

Python: Design a Ship class that has the following members: A member variable for the name of the ship. A member variable for the year that the ship was built. An init method and appropriate accessors and mutators. A print function that displays the ship’s name and the year it was built. Design a CruiseShip class that is derived from the Ship class. The CruiseShip class should have the following members: A member variable for the maximum number of passengers. An init method and appropriate accessors and mutators. A print function that overrides the print function in the base class. The CruiseShip class’s print function should display only the ship’s name and the maximum number of passengers. Design a CargoShip class that is derived from the Ship class. The CargoShip class should have the following members: A member variable for the cargo capacity in tonnage. An init method and appropriate accessors and mutators. A print function that overrides the print function in the base class. The CargoShip class’s print function should display only the ship’s name and the ship’s cargo capacity. Demonstrate the classes in a program that has a list of Ships. The list elements should be initialized with Ship, CruiseShip, and CargoShip objects. The program should then step through the list, calling each object’s print function. Use the Python issinstance() function to determine which object type each element holds.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change or if you get any indentation issues. If you are satisfied with the solution, please rate the answer. Thanks

#code

#Ship class
class Ship:
    #constructor with default values for name and year (not important to pass values)
   
def __init__(self,name='',year=1900):
        #assigning values to name and year fields
       
self.__name=name
        self.__year=year
  
    #getters and setters for all attributes
   
   
def getName(self):
        return self.__name

    def setName(self,name):
        self.__name=name

    def getYear(self):
        return self.__year

    def setYear(self,year):
        self.__year=year
   
    #print method to print the ship name and year
   
def print(self):
        print('Name:',self.__name,',Year:',self.__year)


#CruiseShip class
class CruiseShip(Ship):
    #constructor taking default values for name and max passengers
   
def __init__(self,name='',max_passengers=0):
        #setting __maximum_passengers
       
self.__maximum_passengers=max_passengers
        #setting name using mutator for name
       
self.setName(name)
   
    #getter and setter
   
   
def getMaximumPassengers(self):
        return self.__maximum_passengers

    def setMaximumPassengers(self, passengers):
        self.__maximum_passengers=passengers
   
    #prints the ship name and maximum passengers
   
def print(self):
        print('Name:', self.getName(), ',Maximum number of passengers:', self.__maximum_passengers)

#CargoShip class
class CargoShip(Ship):
    # constructor taking default values for name and max capacity
   
def __init__(self,name='',capacity=0):
        self.__capacity=0
        self.setName(name)

    def getCapacity(self):
        return self.__capacity

    def setCapacity(self,capacity):
        self.__capacity=capacity

    # prints the ship name and maximum capaity
   
def print(self):
        print('Name:', self.getName(), ',Maximum capacity (tonnage):', self.__capacity)


#main program for testing
if __name__ == '__main__':
    #creating an empty list
   
ships_list=[]
    #adding instances of Ship, CruiseShip and CargoShip
   
ships_list.append(Ship('Queen',1983))
    ships_list.append(Ship('ABC-1',2000))
    ships_list.append(CruiseShip('Gambit',600))
    ships_list.append(CruiseShip('Octavia',799))
    ships_list.append(CargoShip('Omega',450000))
    ships_list.append(CargoShip('Gladiator',125000))
    #looping through ships_list
   
for i in ships_list:
        #calling print method
       
i.print()
        #finding type using isinstance, and displaying it too
       
print('(Type: ',end='')
        if isinstance(i,CruiseShip):
            print('CruiseShip)')
        elif isinstance(i,CargoShip):
            print('CargoShip)')
        else:
            print('Ship)')

#output

Name: Queen ,Year: 1983

(Type: Ship)

Name: ABC-1 ,Year: 2000

(Type: Ship)

Name: Gambit ,Maximum number of passengers: 600

(Type: CruiseShip)

Name: Octavia ,Maximum number of passengers: 799

(Type: CruiseShip)

Name: Omega ,Maximum capacity (tonnage): 0

(Type: CargoShip)

Name: Gladiator ,Maximum capacity (tonnage): 0

(Type: CargoShip)

Add a comment
Know the answer?
Add Answer to:
Python: Design a Ship class that has the following members: A member variable for the name...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program Challenge 10 Design a Ship class that the following members: ·         A field for the...

    Program Challenge 10 Design a Ship class that the following members: ·         A field for the name of the ship (a string). ·         A field for the year that the ship was built (a string). ·         A constructor and appropriate accessors and mutators. ·         A toString method that displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: ·         A field for the...

  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...

  • Design a Ship class that has the following class members: A field for the name of the ship...

    Design a Ship class that has the following class members: A field for the name of the ship A field for the year the ship was built A constructor Appropriate accessors and mutators toString method that displays the ships name and the year it was built Design a CruiseShip class the inherits from the Ship class. Include the following members: A field for the max number of passengers A constructor Appropriate accessors and mutators toString method that overrides the Ship toString method. The method...

  • Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design...

    Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design a Ship class that has the following members: - A member variable for the name of the ship (a string) - A member variable for the year that the ship was built (a string) - A contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

  • Help needed on C++ program: Program Description: Complete the Ship, CruiseShip, and CargoShip program (#12 in...

    Help needed on C++ program: Program Description: Complete the Ship, CruiseShip, and CargoShip program (#12 in the 9th edition of the text). Read the specific method requirements in the text. Specific Requirements: • Create the Ship class. • Create CruiseShip and CargoShip classes that are derived from Ship. • Create a small tester cpp file that has an array of Ship pointers (one each of Ship, CruiseShip, and CargoShip). The program steps through the array, calling each object’s print method....

  • Write the declaration for class Building. The class’s members should be: doors, an integer. This variable...

    Write the declaration for class Building. The class’s members should be: doors, an integer. This variable should not be accessible to code outside the class or to member functions in any class derived from class Building. steps, an integer. This variable should not be accessible to code outside the class, but should be accessible to member functions in any class derived from class Building. setDoors, getDoors, setSteps, and getSteps. These are the set and get functions for the member variables...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • Design a class for python named PersonData with the following member variables: lastName firstName address city...

    Design a class for python named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool . It will be...

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT