Arrays as Instance Variables in Java Programming

java unit 9 arrays n.w
1 / 13
Embed
Share

Arrays can be utilized as instance variables in Java classes, such as in the example of a Cup class containing Seed objects. This concept is demonstrated through the implementation of Seed and Cup classes, where the Cup class manages an array of Seed objects based on a specified maximum seed count. By exploring this example, you can grasp the practical application of arrays as instance variables in Java programming.

  • Java Programming
  • Arrays
  • Instance Variables
  • Classes
  • Objects

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

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 int seedCount; private int maxSeedCount; private Seed7[] seedArray; public Cup7(int maxSeedCountIn) { } public int getSeedCount { } public boolean addSeed(Seed7 seedIn) { } public Seed7 removeSeed() { } }

  6. CUP CLASS public Cup7(int maxSeedCountIn) { seedCount = 0; maxSeedCount = maxSeedCountIn; seedArray = new Seed7[maxSeedCount]; } public int getSeedCount() { return seedCount; }

  7. CUP CLASS public boolean addSeed(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; int howManySeeds; boolean okToEnter = 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.

Related


More Related Content