Category Archives: Content

Chapter 7: Pet Shop v1 – Final Version

Before turning in your work, review that you satisfied the requirements discussed in class:

1. DO NOT VIOLATE ENCAPSULATION
2. Use INHERITANCE CONCEPTS to re-use code and functionality.

Specifications:
You will create only one animal class and two of your classmates will share one of their own each for you to have a complete Pet Shop application.

The sample test below is incomplete and you need to add whatever os necessary to make it work.

…
PetSet myPetShop = new PetSet();
        
Animal tweety = new Bird(45.00, "I am a bird");
Animal duffy = new Bird(5.00, "I am a bird");
Animal nemo = new Fish(62.00, "I am a fish"); 
Animal bear = new Dog(1100.00, "I am a dog");
…
…
myPetShop.add(tweety);
myPetShop.add(duffy);
myPetShop.add(nemo);
myPetShop.add(bear);
…
System.out.println(myPetShop);


Chapter 7: Inheritance – Abstract Classes

Abstract Classes

  • It represents a concept on which other classes can build their definitions.An abstract class cannot be instantiated.
  • A class derived from an abstract parent must override all of its parent’s abstract methods, or the derived class will also be considered abstract.
  • An abstract class is similar to an interface in some ways.
  • However, unlike interfaces, an abstract class can contain methods that are not abstract.
  • An abstract class can also contain data declarations other than constants.
  • A class is declared as abstract by including the abstract modifier in the class header.
  • Any class that contains one or more abstract methods must be declared as abstract.
  • In abstract classes (unlike interfaces), the abstract modifier must be applied to each abstract method.
  • A class declared as abstract does not have to contain abstract methods.
  • Abstract classes serve as placeholders in a class hierarchy.
  • If a child of an abstract class does not give a definition for every abstract method that it inherits from its parent, then the child class is also considered abstract.
  • Note that it would be a contradiction for an abstract method to be modified as final or static.
  • Because abstract methods have no implementation, an abstract static method would make no sense.

Homework:
Read pages 382 through 419 and do exercises MC 7.1 through 7.10 and SA 7.3 and 7.4

giphy

All source: Java Software Solutions by Lewis, Loftus and Cocking

Chapter 6: ArrayLists a Java Standard Class

ArrayList

The ArrayList class is part of the java.util package of the Java standard class library.

It provides a service similar to an array in that it can store a list of values and reference them by an index. However, whereas an array remains a fixed size throughout its existence, an ArrayList object dynamically grows and shrinks as needed.

A data element can be inserted into or removed from any location (index) of an ArrayList object with a single method invocation.

The ArrayList class is part of the Collections API, a group of classes that serve to organize and manage other objects.

Unlike an array, an ArrayList is not declared to store a particular type.

An ArrayList object stores a list of references to the Object class.

A reference to any type of object can be added to an ArrayList object.

Because an ArrayList stores references, a primitive value must be stored in an appropriate wrapper class in order to be stored in an ArrayList. Figure 6.8 lists several methods of the ArrayList class.