search through objects of arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: search through objects of arrays

  1. #1
    Join Date
    May 2004
    Posts
    5

    search through objects of arrays

    Hi,
    I have an abstract class called Building, and two sub classes called Flat and House, and a class called BuildingSystem. Constructors, instance variables are all there. In this class, Building System I have an array called Building [] build and each subclass has its own getValue() method.
    In building System i have a method to add building to array

    public void addBuilding (Building leftover)
    {
    if (extra == null || numberofBuilids > maximum)
    {
    return;
    }
    build[numberOfBuilds] = extra;
    numberofBuilds++
    }


    then I create a new Building instance
    eg
    public Building createBuilding()
    {
    String name;
    double type;
    double insu;
    Building build;

    for (int i = 0; i < maximum; i ++)
    {
    name = Keyboard.readString "Name: ")
    insu = Keyboard.readDouble("Insurance ")
    type = Keyboard.readBuilding ("type")
    }
    if (type == 1)
    {
    build = new House(name, type)
    }
    if (type == 2)
    {
    buid = new Flat (name, type)
    }
    } return build;
    }

    Now in a method that has to be called
    public Building findHighestValue(int buildtype)
    I want it to search through the property array, and return the highest getValue method for each type of building eg
    If the buildtype was 1, i want to get the highest getValue() from the Flats and if it were 2 I want it to do the same except get highest of the House instances an for 0 i want t tot return highest of all. Any tips ? Thanks heaps .

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    something like this, will search for ONE highest value. you can use the idea, to expand to "if the building is a nhouse, change the int maxHouse else if its a Flat, change the int maxFlat)
    When you have a method that finds the highest of both.. you should then assess what the user asked for.. was it house only? return the house max you found, else, was it the flat only? then retun the flat max you found, else was it highest of all? compare the flat and house maxes and return the larger

    here is the generic process for finding one maximum:

    Code:
    //myObjects is an array of objects
    
    int max = -1;
    for(int i=0; i < myObjects.length; i++)
      //if the object value is bigger than the max, set a new max
      if( myObjects&#091;i&#093;.getValue() > max){
        max = myObjects.getValue();
      }
    }
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    May 2004
    Posts
    5
    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center