Lab03-Abstract Classes (08-13) PDF

Title Lab03-Abstract Classes (08-13)
Course Intermediate Object-Oriented Programming
Institution La Trobe University
Pages 8
File Size 356.9 KB
File Type PDF
Total Downloads 32
Total Views 114

Summary

Intermediate Object-Oriented Programming Laboratory 03 (Week 04) Handout Introduction When using inheritance to implement an object-oriented design that solves a given problem, Java allows abstract classes. An abstract class can only be used as a base class to derive other classes. We cannot instant...


Description

Intermediate Object-Oriented Programming

Laboratory 03 (Week 04) Handout

Introduction When using inheritance to implement an object-oriented design that solves a given problem, Java allows abstract classes. An abstract class can only be used as a base class to derive other classes. We cannot instantiate an object of such a base class but we can instantiate objects of its nonabstract derived classes. This lab will explore inheritance and abstract classes and abstract methods.

Objectives  

To design and implement abstract classes in Java To practise using abstract methods and abstract classes

Tank

Fish

*

OneSpeciesTank

1

ManySpeciesTank

1

SingleFishTank

A Collection of Fish Tanks in a Pet Shop In this lab, you are going to implement Java code that allows us to manage a collection of different fish tanks containing various fish.

Fish The water in which a fish can live is characterized by the temperature the fish requires and the salinity or salt level (Fresh water or Salt water). The water temperature can be Tropical or Cold, and the salinity can be Fresh water or Salt water. An individual fish is described by a species name, together with the water temperature and salinity it needs to live. A suitable class diagram for Fish follows. Fish - String species - char temperature // ‘T’ or ‘C’ - char salinity // ‘F’ or ‘S’ + Fish(String species, char temperature, char salinity) + String getSpecies() + boolean waterSuitable(char temperature, char salinity) + String toString()

Question 1 Copy the partially completed Fish class representing an individual fish from the library area into your current directory using the command: cp

/home/1st/csilib/cse1ioo/lab03/Fish.java .

(This file and others provided for this lab are also posted on LMS.) The source code is listed below for your convenience. public class Fish { private String species; private char temperature; private char salinity;

// T Tropical, C Cold // F Fresh, S Salt

public Fish(String species, char temperature, char salinity) { this.species = species; this.salinity = salinity; this.temperature = temperature; }

2

public String getSpecies() { return species; } public boolean waterSuitable(char temperature, char salinity) { // Method to complete } public String toString() { String description = getClass().getName() + "[" + " species: " + species + ", temperature: " + temperature + ", salinity: " + salinity + " ]"; return description; } }

Complete the method called waterSuitable() that takes temperature and salinity as arguments and returns a boolean that indicates whether the water environment described by the arguments is suitable for the fish. Test this class before moving on to the next question. A small test driver, FishTester.java can be copied from the library area. Look over it and run it to test your Fish class. cp /home/1st/csilib/cse1ioo/lab03/FishTester.java .

Fish Tank There are several kinds of fish tanks, for example, tanks for fish of the same species (“one species tank”), tanks that can accommodate fish of several species (“many species tank”). Every fish tank is identified by a unique id, and contains water described by tank temperature and tank salinity. Also, for every tank, there are two basic things that we would need to do: add fish to the tank and remove fish from the tank. These common features can be captured in a super class called Tank. This class is represented by the diagram on the next page.

Question 2 Write the Tank class. Besides the attributes, a constructor, and the get methods, the class also has the following methods that we cannot yet implement:

3



display() - a void method with no parameters that displays all fish in the tank



canAddFish() - takes a Fish parameter and returns a boolean indicating whether that fish is allowed to be placed in the tank



addFish() - takes a Fish parameter and adds it to the tank (if possible)



canRemoveFish() - takes a species name as parameter and returns true if there is a fish of that species in the tank, and false otherwise



removeFish() - takes a species name and removes one fish of that species name from the tank. It returns the Fish object that is removed or null if the specified fish could not be found.

We cannot implement the display(), canAddFish(), addFish(), canRemoveFish()or removeFish()methods as these methods will be different for the different types of fish tanks that we code later.

Tank - String id - char tankTemperature - char tankSalinity + Tank(String id, char tankTemperature, char tankSalinity) + String getId() + char getTankTemperature() + char getTankSalinity() + void display() + boolean canAddFish(Fish theFish) + boolean addFish(Fish theFish) + boolean canRemoveFish(String species) + Fish removeFish(String species)

4

Subclasses of Tank For now, suppose that Fish tanks come in one of two types:  One Species tanks  Many Species tanks

Question 3 One Species tanks can contain fish of one species only. A fish of any species can be added to an empty One Species tank. One Species tanks can contain up to 6 fish of the one species. Using the following design, code the derived class OneSpeciesTank1. OneSpeciesTank - final int MAX_FISH = 6 - Fish[ ] fish - int numberOfFish + OneSpeciesTank(String id, char tankTemperature, char tankSalinity) + void display() + boolean canAddFish(Fish theFish) + boolean addFish(Fish theFish) + boolean canRemoveFish(String species) + Fish removeFish(String species)

A small sample test driver, OSTankTester.java can be copied from the library area. cp /home/1st/csilib/cse1ioo/lab03/OSTankTester.java . Read the test driver to see what tests we have there and then run it to test your OneSpeciesTank class.

Question 4 Many Species tanks can contain fish of different species. The tanks come in three sizes: small, medium and large (denoted by the character ‘S’, ‘M’ or ‘L’). Small tanks can contain up to 3 fish, medium tanks can contain up to 6 fish, while large tanks can contain up to 10 fish. Using the design shown on the next page, code the derived concrete class ManySpeciesTank.

1

You may assume no attempt is made to add a Fish object to a tank when that particular object is already in the tank. As an extra question, Can we prevent the same fish from being added to a tank more than once?

5

ManySpeciesTank - final int SMALL = 3 - final int MEDIUM = 6 - final int LARGE = 10 - Fish[ ] fish - int numberOfFish - int maxFish + ManySpeciesTank(String id, char tankTemperature, char tankSalinity, char size) + void display() + boolean canAddFish(Fish theFish) + boolean addFish(Fish theFish) + boolean canRemoveFish(String species) + Fish removeFish(String species)

A sample test driver, MSTankTester.java can be copied from the library area. cp /home/1st/csilib/cse1ioo/lab03/MSTankTester.java .

Read it and run it to test your ManySpeciesTank class.

-----------------------------------------------------------------------------

Question 5 (Optional) The Pet Shop currently has 3 tanks: 1 OneSpeciesTank, 1 small size ManySpeciesTank and 1 medium size ManySpeciesTank. A skeleton of an application program in a file Aquarium.java is provided below and can be copied with command: cp /home/1st/csilib/cse1ioo/lab03/Aquarium.java .

Its purpose it to create the collection of 3 tanks via its constructor2 and experiment with some functionality provided by the Tank classes.

2

Note this approach is NOT a typical use of a constructor.

6

// Laboratory 03 Question 5 // Uncommented skeleton code import java.util.*; public class Aquarium { private static Scanner keyboard = new Scanner(System.in); private static final int MAX_TANKS = 3; private Tank[] aquarium; public Aquarium() { aquarium = new Tank[MAX_TANKS];

// array of Tank references

// 1. fill the Tank array elements with the pet shop’s 3 derived tanks // 2. Put some fish in each tank } public void displayTanks() { // complete code to display fish in all tanks // compile and run your program now – before coding transferFish() } public void transferFish() { System.out.println(″\nTransferring Fish...″); // Prompt for the identity number of the ″from tank″ to remove from and // the ″to tank″ to add to and check they both exist. // If they exist, prompt for the species to transfer and check if it // can be taken from the ″from tank″. // If ok, then check the species can be added to the ″to tank″ // If all checks are ok, transfer the fish, otherwise write a message // to screen indicating the problem. } public static void main(String[] args) { Aquarium petShop = new Aquarium(); petShop.displayTanks(); petShop.transferFish(); petShop.displayTanks(); } }

You may want to change the constructor to give you other fish populations in the tanks to experiment with your transferFish() method. -----------------------------------------------------------------------------

7

Question 6 (Optional) The pet shop has decided to expand its fish range to include aggressive fish that require a tank of their own (a SingleFishTank). 

Copy your files: Fish.java Tank.java OneSpeciesTank.java ManySpeciesTank.java Aquarium.java to a new directory.



Add another attribute and get method to your Fish class (in bold in the class diagram below) and update other methods in the Fish class as needed.

Fish - String species - char temperature - char salinity - boolean aggressive + Fish(String species, char temperature, char salinity) + String getSpecies() + boolean getAggressive() + boolean waterSuitable(char temperature, char salinity) + String toString()



Update your other already coded classes to cope with this additional Fish attribute.



Code the class SingleFishTank that is derived from Tank.



Now update Aquarium to include more than 3 tanks, by adding 1 or 2 SingleFishTank objects and experiment with your transferFish() method.



8...


Similar Free PDFs