Concept explainers
19.9 (Package Inheritance Hierarchy) Package-delivery services, such as FedEx®, DHL® and UPS®, offer a number of different shipping options, each with specific costs associated. Create an inheritance hierarchy to represent various types of packages. Use class Package as the base class of the hierarchy, then include classes TwoDayPackage and OvernightPackage that derive from Package. Base class Package should include data members representing the name, address, city, state and ZIP code for both the sender and the recipient of the package, in addition to data members that store the weight (in ounces) and cost per ounce to ship the package. Package ‘5 constructor should initialize these data members. Ensure that the weight and cost per ounce contain positive values. Package should provide a public member function calculatecost that returns a double indicating the cost associated with shipping the package. Package ‘5 calculatecost function should determine the cost by multiplying the weight by the cost per ounce. Derived class TwoDayPackage should inherit the functionality of base class Package, but also include a data member that represents a flat fee that the shipping company charges for two-day-delivery service. TwoDayPackage’s constructor should receive a value to initialize this data member. TwoDayPackage should redefine member function calculatecost 50 that it computes the shipping cost by adding the flat fee to the weight-based cost calculated by base class Package ‘S calculateCost function. Class OvernightPackage should inherit directly from class Package and contain an additional data member representing an additional fee per ounce charged for overnight-delivery service. OvernightPackage should redefine member function calculatecost so that it adds the additional fee per ounce to the standard cost per ounce before calculating the shipping cost. Write a test program that creates objects of each type of Package and tests member function calculatecost.
Want to see the full answer?
Check out a sample textbook solutionChapter 19 Solutions
MYPROGRAMMINGLAB WITH PEARSON ETEXT
Additional Engineering Textbook Solutions
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Concepts of Programming Languages (11th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
Starting Out with Java: From Control Structures through Objects (6th Edition)
Java How To Program (Early Objects)
Computer Systems: A Programmer's Perspective (3rd Edition)
- T/F 4) Consider the following class hierarchy:Refer to Example Code Ch 09-1:A is a derived class of X.arrow_forwardT/F 3) If class AParentClass has a public instance data x, and AChildClass is a derived class of AParentClass, thenAChildClass can access x but can not redefine x to be a different type.arrow_forward1.1 Class definition Define a class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder’s name (string) Account number (int) Account type (string, check/savings/business) Balance (double) Interest rate (double) – store interest rate as a decimal number. Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. 1.2 Implement all appropriate member functions of a class. 1.3 write a program that illustrate how to use your class. Your program should have the following: Declare an array of 20 components of type bankAccount to process up to 20 customers. void menu() – helps the user to select if the customer is new or if they already exist. Furthermore, it prints the customer’s data or exits the program. Use a switch statement which uses the value from menu() as an expression to call the following user-defined functions: void…arrow_forward
- 13 (10 pts) Briefly explain what each of the following terms mean in the context of Java programming OR illustrate the term with one or more lines of code. Overloading hen a class has more than ane methd of the sm 0e and their paemeter elve dilferent. Instantiation It means to call the class Consructor a newobject. Constructor Attributearrow_forward3.2 Point Class (6¹) Complete the class Point to describe points on the plane and provide methods to move points and calculate the distance to another point A class Point (): def _init__(self, x-0, y-0): self.x = x self. y - y def move to(self, x, y): ***move a point to a new coordinate HHW def move by(self, dx, dy): ove a point with specified increment new xx+ dx new y = y + dy *** *** def distance to(self, p): ***calculate the distance to another point. p: another point www return dis def print info(self): return (s, %s) % (str(self.x), str(self.y)) #test p1 = Point (3, 5) Point () p2 p2.move_by(-1, 2) if p1.distance to (p2) !=5.: print('test failed') =arrow_forwardQ. No.1: a) Implement the following class hierarchy. TEACHER AUTHOR SCHOLAR 1) Add appropriate data member in all the classes a. Add name and age in both Teacher and Author classes 2) Add default and parameterized constructors and destructor in all the classes 3) Write appropriate getter, setter functions 4) In main program create object of scholar class and display all values inherited members from base classes and its own member. Also see the behavior and order in which constructors and destructors are called. b) Modification & Extension 1) Add a common base class Person 2) Remove name and age data members from Teacher and Author classes and add these data members in common base class Person 3) Also add constructor and destructor in Person class Page 1 of 2 4) Create object of Scholar class in main program. Display all data and see the behavior and order for constructors and destructors.arrow_forward
- T/F 23) Abstract classes are typically defined at the bottom of a class hierarchyarrow_forward1.1 Class definition Define a class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: • Account holder's name (string) • Account number (int) • Account type (string, check/savings/business) • Balance (double) • Interest rate (double) – store interest rate as a decimal number. • Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. 1.2 Implement all appropriate member functions of a class.arrow_forwardQ2\B\ True or false ( answer three) 1- Abstract data types cannot be implemented in procedural languages such as C and Pascal. 2- Although methods and fields can be static, constructors cannot be static. 3- One problem with OOP when create a Class it can never be changed. 4- polymorphysim is extensively used in implementing inheritance.arrow_forward
- Object Oriented Programming BS (2nd) Midterm Exam Date: 03-05-2021 Submit Till 03-05-2021 (4:00 PM) Q1. Write a class definition that creates a class called leverage with one private data member, crowbar, of type int and one public function whose declaration is void pry(). Q2. Write a class definition that creates a class called leverage with one private data member, crowbar, of type int and one public function whose declaration is void pry(). Write a member function called getcrow(). This function should return the value of the crowbar data. Assume the function is defined within the class definition. Q3. Write a constructor that initializes to 0 the crowbar data, a member of the leverage class. Assume that the constructor is defined within the class definition. Q4. Create a class called time that has separate int member data for hours, minutes, and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. Another member function…arrow_forward9.3 (Recommended: Using Composition Rather Than Inheritance) Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BasePlusCommissionEmployee (Fig. 9.11) of the CommissionEmployee—BasePlusCommissionEmployee hierarchy so that it contains a reference to a ConmissionEmployee object, rather than inheriting from class CommissionEmployee. Retest BasePlusConmissionEmployee to demonstrate that it still provides the same functionality.arrow_forwardC++arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education