1 / 13

Java Unit 9: Arrays

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.

evette
Download Presentation

Java Unit 9: Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Unit 9: Arrays Arrays as Instance Variables

  2. 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’.

  3. Seed class public class Seed7 { private String seedColorName; public Seed7(String seedColorNameIn) {seedColorName = seedColorNameIn; } public String getSeedColorName() { return seedColorName; }}

  4. 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.

  5. Cup class public class Cup7 { private intseedCount; private intmaxSeedCount; private Seed7[] seedArray; public Cup7(intmaxSeedCountIn) {…} public intgetSeedCount {…} public booleanaddSeed(Seed7 seedIn) {…} public Seed7 removeSeed() {…}}

  6. Cup class • public Cup7(intmaxSeedCountIn) {seedCount = 0;maxSeedCount = maxSeedCountIn;seedArray = new Seed7[maxSeedCount];}public intgetSeedCount() { return seedCount;}

  7. Cup class • public booleanaddSeed(Seed7 seedIn) { if(seedCount < maxSeedCount) {seedArray[seedCount] = seedIn;seedCount++; return true; } else return false;}

  8. Cup class • public Seed7 removeSeed() { if (seedCount > 0) {seedCount--; Seed7 temp = seedArray[seedCount];seedArray[seedCount] = null; return temp; } else return null;}

  9. 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.

  10. 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.

  11. 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)… }}

  12. 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());}

  13. 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.

More Related