Write the printItem() method for the base class. Sample output for below program:

Write the printItem() method for the base class. Sample output for below program:

Last name: Smith First and last name: Bill Jones

  1. // ===== Code from file BaseItem.java =====
  2. public class BaseItem {
  3. protected String lastName;
  4. public void setLastName(String providedName) {
  5. lastName = providedName;
  6. return;
  7. }
  8. // FIXME: Define printItem() method
  9. /* Your solution goes here */
  10. }
  11. // ===== end =====
  12. // ===== Code from file DerivedItem.java =====
  13. public class DerivedItem extends BaseItem {
  14. private String firstName;
  15. public void setFirstName(String providedName) {
  16. firstName = providedName;
  17. return;
  18. }
  19. @Override
  20. public void printItem() {
  21. System.out.print("First and last name: ");
  22. System.out.println(firstName + " " + lastName);
  23. return;
  24. }
  25. }
  26. // ===== end =====
  27. // ===== Code from file ItemInventory.java =====
  28. import java.util.ArrayList;
  29. public class ItemInventory {
  30. public static void main (String [] args) {
  31. BaseItem baseItemPtr;
  32. DerivedItem derivedItemPtr;
  33. ArrayList<BaseItem> itemList = new ArrayList<BaseItem>();
  34. int i = 0;
  35. baseItemPtr = new BaseItem();
  36. baseItemPtr.setLastName("Smith");
  37. derivedItemPtr = new DerivedItem();
  38. derivedItemPtr.setLastName("Jones");
  39. derivedItemPtr.setFirstName("Bill");
  40. itemList.add(baseItemPtr);
  41. itemList.add(derivedItemPtr);
  42. for (i = 0; i < itemList.size(); ++i) {
  43. itemList.get(i).printItem();
  44. }
  45. return;
  46. }
  47. }
  48. // ===== end =====

Answer

  1. / ===== Code from file BaseItem.java =====
  2. public class BaseItem {
  3. protected String lastName;
  4. public void setLastName(String providedName) {
  5. lastName = providedName;
  6. return;
  7. }
  8. // FIXME: Define printItem() method
  9. /* Your solution goes here */
  10. public void printItem() {
  11. // TODO Auto-generated method stub
  12. System.out.println("Last name: "+lastName);
  13. }
  14. }
  15. // ===== end =====
  16. // ===== Code from file DerivedItem.java =====
  17. public class DerivedItem extends BaseItem {
  18. private String firstName;
  19. public void setFirstName(String providedName) {
  20. firstName = providedName;
  21. return;
  22. }
  23. @Override
  24. public void printItem() {
  25. System.out.print("First and last name: ");
  26. System.out.println(firstName + " " + lastName);
  27. return;
  28. }
  29. }
  30. // ===== end =====
  31. // ===== Code from file ItemInventory.java =====
  32. import java.util.ArrayList;
  33. public class ItemInventory {
  34. public static void main (String [] args) {
  35. BaseItem baseItemPtr;
  36. DerivedItem derivedItemPtr;
  37. ArrayList<BaseItem> itemList = new ArrayList<BaseItem>();
  38. int i = 0;
  39. baseItemPtr = new BaseItem();
  40. baseItemPtr.setLastName("Smith");
  41. derivedItemPtr = new DerivedItem();
  42. derivedItemPtr.setLastName("Jones");
  43. derivedItemPtr.setFirstName("Bill");
  44. itemList.add(baseItemPtr);
  45. itemList.add(derivedItemPtr);
  46. for (i = 0; i < itemList.size(); ++i) {
  47. itemList.get(i).printItem();
  48. }
  49. return;
  50. }
  51. }
  52. // ===== end =====

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts