140 likes | 302 Vues
In this Java tutorial, we explore how to use arrays as instance variables within class definitions, using the Cup and Seed classes as examples. We present a simple model where a Cup can hold multiple Seeds. Each Seed has a color represented by a string. The Cup class manages an array of Seed instances while keeping track of the number of seeds it contains. We demonstrate the implementation with a short program showcasing the addition and removal of seeds, emphasizing the relationship between the Cup and Seed classes through instance variables.
E N D
Java Unit 9: Arrays Arrays as Instance Variables
Arrays as Instance Variables • Arrays can be used as instance variables in the class definitions. • There’s nothing new in the syntax for doing so. • We can present this idea by thinking of a ‘Cup’ that contains ‘Seeds’.
Seed class public class Seed7 { private String seedColorName; public Seed7(String seedColorNameIn) {seedColorName = seedColorNameIn; } public String getSeedColorName() { return seedColorName; }}
Seed class • It is given the number 7 so that its name is consistent with the cup class. • This class only contains one instance variable. • The overall example is not graphical in nature, so the color of a seed is simply recorded as a string containing the name of the color.
Cup class public class Cup7 { private intseedCount; private intmaxSeedCount; private Seed7[] seedArray; public Cup7(intmaxSeedCountIn) {…} public intgetSeedCount {…} public booleanaddSeed(Seed7 seedIn) {…} public Seed7 removeSeed() {…}}
Cup class • public Cup7(intmaxSeedCountIn) {seedCount = 0;maxSeedCount = maxSeedCountIn;seedArray = new Seed7[maxSeedCount];}public intgetSeedCount() { return seedCount;}
Cup class • public booleanaddSeed(Seed7 seedIn) { if(seedCount < maxSeedCount) {seedArray[seedCount] = seedIn;seedCount++; return true; } else return false;}
Cup class • public Seed7 removeSeed() { if (seedCount > 0) {seedCount--; Seed7 temp = seedArray[seedCount];seedArray[seedCount] = null; return temp; } else return null;}
Cup class • The main point of interest: • Private Seed7[] seedArray; • This array contains references to Seeds. • Notice that when constructing a cup, the maximum number of seeds that it can hold has to be specified. This input parameter becomes the length of the array. • The seedCount is still maintained, and this instance variable is used to make sure that a user cannot enter more seeds than the cup can contain.
Cup and Seed • Here is a short program that makes use of the Cup7 and Seed7 classes. It does not do anything unusual. It provides a simple review of while and for loops.
Cup and Seed public class TestCup7AndSeed7 { public static void main(String[] args) { Cup7 myCup = new Cup7(5); Seed7 aSeed;inthowManySeeds;booleanokToEnter = true; ...(cont. on next slide)… }}
Cup and Seed …while(okToEnter) {aSeed = new Seed7(“blue”);okToEnter = myCup.addSeed(aSeed);}howManySeeds = myCup.getSeedCount();for(int i = 0; I < howManySeeds; i++) {aSeed = myCup.removeSeed();System.out.println(aSeed.getSeedColorName());}
Cup and Seed • The idea that a cup has seeds in it is now more closely reflected by the code that has been written. • When the test program runs and a cup object is created, that cup object has instances of seeds in it, contained in its array instance variable. • A simple UML diagram showing these relationships is given below.