Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Description: In object-oriented programming, aggregation is a relationship between two classes where objects of one class contain objects of the other. For our second

Java

image text in transcribed

image text in transcribed

image text in transcribed

Description: In object-oriented programming, aggregation is a relationship between two classes where objects of one class contain objects of the other. For our second lab, we will implement two such classes: Fish and School. A Fish object represents a single fish, while a School object represents a school of fish. Each School object contains an array of Fish objects. Class Diagram: Aggregation is represented in UML diagrams by a line with an empty diamond on one end that connects two classes. Objects of the class touching the diamond contain objects of the other class. The following is a class diagram for the Fish and School classes: Fish School -name: String -species: String -weight: double +Fish(name: String, species: String, weight: double) -fishArray: Fish[] -numFish: int -DEFAULT CAPACITY: int = 3 +School () +School(capacity: int) +getNumFish((): int +getCapacity(): int -doubleCapacity(): void +addFish(fish: Fish): void +removeFish(index: int): Fish +getName (): String +getSpecies (): String +getWeight(): double +tostring(): String +getFish(index: int): Fish +getFish(): Fish[] +getFish(species: String): Fish[] +getBiggestFish(): Fish +getweight(): double +tostring(): String Notice that the diamond touches the School class. Just like in nature, a School is an aggregation of Fish.1 Fish Class: Each Fish object has a name, species, and weight (in pounds). The toString method returns a String with the values of these fields separated by commas. For example, suppose that a Fish object is constructed with the following line: Fish fish = new Fish("Nemo", "clownfish", 0.2); Calling toString on this object returns the String "Nemo, clownfish, 0.2 lb". Note that the weight, which is a double value, is rounded to the tenths place. This can be done by using the format method of the String class as follows: String.format("%.1f", weight) The first argument is a format String that tells the method how many digits to keep after the decimal place. School Class: Each School object contains a reference to an array of Fish objects. The reference is stored in the field fishArray. When a School object is constructed, a new Fish array is also constructed. The array is initially empty, but Fish can be added by calling the addFish method. Each additional Fish is added to the next empty element in the array. In CS 1323/1324, we used the adjective "oversize" to describe arrays such as this, which have extra space for adding data. An oversize array has both a capacity and a size. The capacity is the length of the array. The size is the number of elements we treat as non-empty. (For instance, if an array has a capacity of 10 and a size of 6, we treat the first 6 elements as valid data and the last 4 as empty space.) In a School object, the size of fishArray is stored in the field numFish. The size of an oversize array cannot exceed its capacity. If addFish is called when fishArray is full, a new array must be constructed. The new array is made twice as long, and all the elements are copied from the old to the new array. The new Fish can then be added. Below are descriptions of all the methods of the School class. Most methods deal with adding, removing, or retrieving a particular Fish. A few of them, however, return properties of the School, such as the total weight or largest Fish. School(): Construct a School object with capacity equal to DEFAULT_CAPACITY. School(int capacity): Construct a School object with the given capacity. The smallest allowed capacity is 1. getNumFish: Return the number of non-empty elements in fishArray (i.e., numFish). getCapacity: Return the length of fishArray. doubleCapacity: Double the capacity of fishArray. That is, create a new array with twice the length, copy the elements of the old array into the new array (at the same indices), and assign the new array to fishArray. (The old array will be garbage collected.) addFish: Add the given fish to the next empty element in fishArray. (Since arrays are indexed from 0, note that the index of this element is equal to the number of fish.) If the array is full, double the capacity before adding the new Fish. removeFish: Remove and return the Fish with the given index in fishArray. Any Fish with a larger index is shifted down to the next-lowest index. (For example, suppose fishArray has two Fish. If the Fish at index 0 is removed, the Fish at index 1 is shifted down to index 0.) If the index is outside the non-empty part of fishArray, return null. getFish(int index): Return the Fish with the given index in fishArray. If the index is outside the non-empty part of fishArray, return null. getFish(): Return an array with all the Fish in the school. (The length of the array should be equal to numFish.) getFish(String species): Return an array with all the Fish of a given species. The order of the Fish should be the same as in fishArray. (The length of this array will be less than or equal to numFish.) getBiggestFish: Return the Fish with the largest weight. If the method is called on an empty School, return null. getWeight: Return the sum of the weights of all the Fish. toString: Return a description of the School. The String is formed by calling toString on each Fish and concatenating the output. The substrings are numbered and separated by newline characters

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

2nd Edition

0470624701, 978-0470624708

More Books

Students also viewed these Databases questions

Question

What is cultural tourism and why is it growing?

Answered: 1 week ago