Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java inheritance exercise Pursose: This exercise allows you to implement inherit

ID: 3695695 • Letter: J

Question

Java inheritance exercise Pursose: This exercise allows you to implement inheritance and polymorphism. Description Write a bank a Bank Account project according to the following specifications: Bank Account: Every bank account has an identifier, a PIN, and a balance. Every bank account should have a way to deposit an amount, withdraw an amount(abstract method), and display the current balance. Ensure that any class that inherits from this class has a withdraw() method that overrides the abstract withdraw() method. Savings: The withdraw method will only let the owner withdraw if the funds are sufficient. If the owner attempts to withdraw an amount higher than the balance, print an error message and do not process the withdraw. Checking: The withdraw method should withdraw the amount only if the funds are availible, and apply a service fee of $0.25 for each withdrawal to the balance. If the owner. if the owner attempts to withdraw more than the balance of the account, print an error message and do not process the withdraw. CheckingPlus: The withdraw method will allow the owner to withdraw more than the balance up to a predefined limite(the limit is a field in the class, the numer I used was 1000) If the amount requested is approved the fee is applied. The fee is a percentage of the amount overdrawn(ex: 10%). If the limit is exceeded, throw and catch the error. Testing: In main() create an array of five BankAccounts objects and each account will have a unique ID. Then allow the user to enter the BankAccount ID and PIN to either deposit or withdraw an amount. After each transaction, display the current balance. if the account ID with the PIN number is wrong, print an error message to the user and ask for another ID and PIN.

Explanation / Answer

Let's consider several other examples of polymorphism. If class Rectangle is derived from class Quadrilateral, then a Rectangle object is a more specific version of a Quadrilateral object. Any operation (e.g., calculating the perimeter or the area) that can be performed on a Quadrilateral object can also be performed on a Rectangle object. These operations can also be performed on other Quadrilaterals, such as Squares, Parallelograms and Trapezoids. The polymorphism occurs when a program invokes a method through a superclass variable—at execution time, the correct subclass version of the method is called, based on the type of the reference stored in the superclass variable. You'll see a simple code example that illustrates this process in Section 10.3.

As another example, suppose we design a video game that manipulates objects of classes Martian, Venusian, Plutonian, SpaceShip and LaserBeam. Imagine that each class inherits from the common superclass called SpaceObject, which contains method draw. Each subclass implements this method. A screen-manager program maintains a collection (e.g., a SpaceObject array) of references to objects of the various classes. To refresh the screen, the screen manager periodically sends each object the same message—namely, draw. However, each object responds in a unique way. For example, a Martian object might draw itself in red with green eyes and the appropriate number of antennae. A SpaceShip object might draw itself as a bright silver flying saucer. A LaserBeam object might draw itself as a bright red beam across the screen. Again, the same message (in this case, draw) sent to a variety of objects has "many forms" of results.

A screen manager might use polymorphism to facilitate adding new classes to a system with minimal modifications to the system's code. Suppose that we want to add Mercurian objects to our video game. To do so, we must build a class Mercurian that extends SpaceObject and provides its own draw method implementation. When objects of class Mercurian appear in the SpaceObject collection, the screen manager code invokes method draw, exactly as it does for every other object in the collection, regardless of its type. So the new Mercurian objects simply "plug right in" without any modification of the screen manager code. Thus, without modifying the system (other than to build new classes and modify the code that creates new objects), programmers can use polymorphism to conveniently include additional types that were not envisioned when the system was created.

With polymorphism, the same method name and signature can be used to cause different actions to occur, depending on the type of object on which the method is invoked. This gives the programmer tremendous expressive capability

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote