-
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 .
-
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[i].getValue() > max){
max = myObjects.getValue();
}
}
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks