CSE1IOO/CSE4IOO – Lab 3 Sample Solution PDF

Title CSE1IOO/CSE4IOO – Lab 3 Sample Solution
Author Cường Nguyễn
Course Intermediate Object-Oriented Programming
Institution La Trobe University
Pages 8
File Size 129.4 KB
File Type PDF
Total Downloads 1
Total Views 749

Summary

Laboratory 03: Abstract classes and methods// Laboratory 03 Question 1 public class Fish { private String species; private char temperature; // T Tropical, C Cold private char salinity; // F Fresh, S Saltpublic Fish(String species, char temperature, char salinity) { this = species; this = salinity; ...


Description

Laboratory 03: Abstract classes and methods // Laboratory 03 Question 1 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; } public String getSpecies() { return species; } public boolean waterSuitable(char temperature, char salinity) { return this.temperature == temperature && this.salinity == salinity; } public String toString() { String description = getClass().getName() + "[" + " species: " + species + ", temperature: " + temperature + ", salinity: " + salinity + " ]"; return description; } } // Laboratory 03 Question 2 public abstract class Tank { private String id; private char tankTemperature; private char tankSalinity; public Tank(String id, char tankTemperature, char tankSalinity) { this.id = id; this.tankTemperature = tankTemperature; this.tankSalinity = tankSalinity; } public String getId() { return id; } public char getTankTemperature() { return tankTemperature;

} public char getTankSalinity() { return tankSalinity; } public public public public public

abstract abstract abstract abstract abstract

void display(); boolean canAddFish(Fish theFish); boolean addFish(Fish theFish); boolean canRemoveFish(String species); Fish removeFish(String species);

} // Laboratory 03 Question 3 public class OneSpeciesTank extends Tank { private static final int MAX_FISH = 6; private Fish[] fish; private int numberOfFish; public OneSpeciesTank(String id, char tankTemperature, char tankSalinity) { super(id, tankTemperature, tankSalinity); fish = new Fish[MAX_FISH]; numberOfFish = 0; } public void display() { System.out.println(getClass().getName() + ": tank id " + getId()); for (int i = 0; i < numberOfFish; ++i) { System.out.println(fish[i]); } } public boolean canAddFish(Fish theFish) { // checks the water is ok for the fish and // the number of fish is ok (there is room) and // the species is ok (fish to add is the same species) boolean waterOk = theFish.waterSuitable(getTankTemperature(), getTankSalinity()); boolean roomOk = numberOfFish < MAX_FISH; boolean speciesOk = numberOfFish == 0 || fish[0].getSpecies().equalsIgnoreCase(theFish.getSpecies()); return waterOk && roomOk && speciesOk; } public boolean addFish(Fish theFish) { // checks if adding the fish is ok if (canAddFish(theFish)) { fish[numberOfFish] = theFish; numberOfFish++; return true;

} return false; } public boolean canRemoveFish(String species) { // checks if there are fish and if they are the required species return numberOfFish > 0 && fish[0].getSpecies().equalsIgnoreCase(species); } public Fish removeFish(String species) { // checks if removing the fish is ok // If so, removes the last one Fish removedFish = null; if (canRemoveFish(species)) { removedFish = fish[numberOfFish - 1]; numberOfFish--; } return removedFish; } } // Laboratory 03 Question 4 public class ManySpeciesTank extends Tank { private static final int SMALL = 3; private static final int MEDIUM = 6; private static final int LARGE = 10; private Fish[] fish; private int numberOfFish; private int maxFish; public ManySpeciesTank(String id, char tankTemperature, char tankSalinity, char size) { super(id, tankTemperature, tankSalinity); switch(size) { case 'S': fish = new Fish[SMALL]; maxFish = SMALL; break; case 'M': fish = new Fish[MEDIUM]; maxFish = MEDIUM; break; case 'L': fish = new Fish[LARGE]; maxFish = LARGE; break; default : System.out.println("Invalid tank size!"); } numberOfFish = 0; }

public void display() { System.out.println(getClass().getName() + ": tank id " + getId()); for (int i = 0; i < numberOfFish; ++i) { System.out.println(fish[i]); } }

public boolean canAddFish(Fish theFish) { // checks the water is ok for the fish and // the number of fish is ok (there is room) boolean waterOk = theFish.waterSuitable(getTankTemperature(), getTankSalinity()); boolean roomOk = numberOfFish < maxFish; return waterOk && roomOk; } public boolean addFish(Fish theFish) { // checks if adding the fish is ok if (canAddFish(theFish)) { fish[numberOfFish] = theFish; numberOfFish++; return true; } return false; } public boolean canRemoveFish(String species) { // checks if there are fish and if they are the required species for (int i = 0; i < numberOfFish; ++i) { if (fish[i].getSpecies().equalsIgnoreCase(species)) { return true; } } return false; } public Fish removeFish(String species) { // checks if fish of the species needed exist // If found, removes the first one Fish removedFish = null; if (canRemoveFish(species)) { boolean done = false; int i = 0; while (!done && i < numberOfFish) { if (fish[i].getSpecies().equalsIgnoreCase(species)) { done = true; removedFish = fish[i]; fish[i] = fish[numberOfFish - 1]; // close gap in array, // swap with last numberOfFish--; } else { i++; } } }

return removedFish; } }

// Laboratory 03 Question 5 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 aquarium[0] = new OneSpeciesTank("tank1", 'T', 'S'); aquarium[1] = new ManySpeciesTank("tank2", 'T', 'F', 'S'); aquarium[2] = new ManySpeciesTank("tank3", 'T', 'S', 'M'); // 2. Put some fish in each tank aquarium[0].addFish(new Fish("clownfish", aquarium[0].addFish(new Fish("clownfish", aquarium[0].addFish(new Fish("clownfish", aquarium[0].addFish(new Fish("clownfish",

'T', 'T', 'T', 'T',

'S')); 'S')); 'S')); 'S'));

aquarium[1].addFish(new Fish("guppy", 'T', 'F')); aquarium[1].addFish(new Fish("guppy", 'T', 'F')); aquarium[2].addFish(new aquarium[2].addFish(new aquarium[2].addFish(new aquarium[2].addFish(new aquarium[2].addFish(new

Fish("clownfish", Fish("clownfish", Fish("clownfish", Fish("clownfish", Fish("clownfish",

'T', 'T', 'T', 'T', 'T',

'S')); 'S')); 'S')); 'S')); 'S'));

} public void displayTanks() { // complete code to display fish in all tanks // compile and run your program now - before coding transferFish() for (int i = 0; i < MAX_TANKS; ++i) { aquarium[i].display(); } } public void transferFish() { System.out.println("\nTransferring Fish..."); System.out.println("Please enter tank id from which to remove fish: "); String fromTank = keyboard.nextLine(); System.out.println("Please enter tank id to which to add fish: "); String toTank = keyboard.nextLine(); int fromTankIndex = findTank(fromTank); int toTankIndex = findTank(toTank); if (fromTankIndex == -1 || toTankIndex == -1) { System.out.println("From or to tank does not exist!"); return; } System.out.print("Please enter species to transfer: ");

String species = keyboard.nextLine(); boolean canTake = aquarium[fromTankIndex].canRemoveFish(species); if (!canTake) { System.out.println("Cannot remove species from tank!"); return; } Fish transferFish = aquarium[fromTankIndex].removeFish(species); boolean canAdd = aquarium[toTankIndex].canAddFish(transferFish); if (!canAdd) { // return fish to "from tank" aquarium[fromTankIndex].addFish(transferFish); System.out.println("Cannot add species to tank!"); return; } aquarium[toTankIndex].addFish(transferFish); } // helper method for transferFish private int findTank(String tankId) { for (int i = 0; i < MAX_TANKS; ++i) { if (aquarium[i].getId().equals(tankId)) { return i; } } return -1; } public static void main(String[] args) { Aquarium petShop = new Aquarium(); petShop.displayTanks(); petShop.transferFish(); petShop.displayTanks(); } }...


Similar Free PDFs