Question
Write the printItem() method for the base class. Sample output for below program: Last name: Smith First and last name: Bill Jones // ===== Code
Write the printItem() method for the base class. Sample output for below program:
Last name: Smith First and last name: Bill Jones
// ===== Code from file BaseItem.java ===== public class BaseItem { protected String lastName;
public void setLastName(String providedName) { lastName = providedName; return; }
// FIXME: Define printItem() method
/* Your solution goes here */
} // ===== end =====
// ===== Code from file DerivedItem.java ===== public class DerivedItem extends BaseItem { private String firstName;
public void setFirstName(String providedName) { firstName = providedName; return; }
@Override public void printItem() { System.out.print("First and last name: "); System.out.println(firstName + " " + lastName); return; } } // ===== end =====
// ===== Code from file ItemInventory.java ===== import java.util.ArrayList;
public class ItemInventory { public static void main (String [] args) { BaseItem baseItemPtr; DerivedItem derivedItemPtr; ArrayList itemList = new ArrayList(); int i = 0;
baseItemPtr = new BaseItem(); baseItemPtr.setLastName("Smith");
derivedItemPtr = new DerivedItem(); derivedItemPtr.setLastName("Jones"); derivedItemPtr.setFirstName("Bill");
itemList.add(baseItemPtr); itemList.add(derivedItemPtr);
for (i = 0; i < itemList.size(); ++i) { itemList.get(i).printItem(); }
return; } } // ===== end =====
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started