473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

method call format using arrays

sammyboy78
I was wondering about the format of calling methods using arrays. I know that to call a method when not using arrays it would be formatted in the first class like:

Expand|Select|Wrap|Line Numbers
  1. public void methodName( parameters )
  2. {
  3.        statements;
  4.  
  5. }// end methtod
and then the method call in the second class (the one created to use the methods of the first class) would be:

Expand|Select|Wrap|Line Numbers
  1. createdObject.methodName( arguments )

I need to create a method in the first class such as:

Expand|Select|Wrap|Line Numbers
  1. public void methodName( double array[ ] )
  2. {
  3.         statements;
  4.  
  5. } // end method
I'm not sure how I would call this method form the second class. What's the format?
Jun 21 '07 #1
7 2256
blazedaces
284 100+
I was wondering about the format of calling methods using arrays. I know that to call a method when not using arrays it would be formatted in the first class like:

Expand|Select|Wrap|Line Numbers
  1. public void methodName( parameters )
  2. {
  3.        statements;
  4.  
  5. }// end methtod
and then the method call in the second class (the one created to use the methods of the first class) would be:

Expand|Select|Wrap|Line Numbers
  1. createdObject.methodName( arguments )

I need to create a method in the first class such as:

Expand|Select|Wrap|Line Numbers
  1. public void methodName( double array[ ] )
  2. {
  3.         statements;
  4.  
  5. } // end method
I'm not sure how I would call this method form the second class. What's the format?
You seem to me to be phrasing your question clearly, but if I misunderstood the nature of your question please let me know.

Your parameters as you put it in your creation of the method also identify your method, so java, when it looks for the method must find one with the same name, and input parameters.

Another words, the arguments must match exactly the parameters. If your method was

Expand|Select|Wrap|Line Numbers
  1. public void methodName( double array[ ] )
  2. {
  3.         statements;
  4.  
  5. } // end method
then when calling it you would type

Expand|Select|Wrap|Line Numbers
  1. double[] testArray = new double[length whatever you want];
  2. testArray[0] = 0;
  3. testArray[1] = 1;
  4. .
  5. .
  6. .
  7.  
  8. createdObject.methodName( testArray );
  9.  
If you tried to call the method with an int[] array it would not work, java would spit you an error saying it could not find a method called methodName(int[]).

Also, java's object-orientedness in this case allows us to use what I stated above to our advantage. Overloading methods, as it is called, is when you write more then one method, but of different input parameters.

For example:

[code]
Expand|Select|Wrap|Line Numbers
  1. public void methodName( double array[ ] )
  2. {
  3.         statements with double[];
  4.  
  5. } // end method
  6.  
  7. public void methodName( int array[ ] )
  8. {
  9.         statements with int[];
  10.  
  11. } // end method
  12.  
Now, if I tried to input a double[] array it would call the first methodName, but if I used int[] array it would call the second methodName. This is very convenient for similar processes for different variables. Try it out, even look at some of the classes java has already made and see how often it is used...

An example of extremely similar coding for a variety of inputs is Math's Math.max(input 1, input 2). There is an overloaded method for inputs 1 and 2 being double, float, long, and int.

Hope this answered your question and good luck,
-blazed
Jun 22 '07 #2
yeah that's what I'm asking but I should've asked about using an array of objects. I'm creating an array of Compact Discs (CDs) and I'm trying to create a method to calculate the total value of the inventory of CDs (the array of CDs) and one to display the inventory ( all the CD objects in the array). These classes were previously used to successfully display one CD object and calculate it's inventory value. Now, I'm modifying these classes to use arrays. I'm a newbie at Java let alone the concept of using arrays and arrays and objects together.

Here's my code:

Expand|Select|Wrap|Line Numbers
  1. // Inventory2.java
  2. // Represents a compact disc object
  3.  
  4. public class Inventory2
  5. {
  6.     private String title; // CD title (name of product)
  7.     private String number; // CD product number
  8.     private double numStock; // CD stock number
  9.     private double price; // price of CD
  10.     private double inventoryValue; //number of units in stock times price of each unit
  11.     private double totalInventoryValue;
  12.  
  13.     // constructor initializes CD information
  14.     public Inventory2( String cdTitle, String productNumber, double numberInStock, double cdPrice )
  15.     {
  16.         title = cdTitle;
  17.         number = productNumber;
  18.         numStock = numberInStock;
  19.         price = cdPrice;
  20.  
  21.     } // end constructor
  22.  
  23.     public void calculateInventoryValue()
  24.         {
  25.  
  26.             inventoryValue = numStock * price;
  27.  
  28.         } //end calculateInventoryValue
  29.  
  30.     public double getInventoryValue()
  31.     {
  32.         return inventoryValue;
  33.  
  34.         } //end getInventoryValue
  35.  
  36.     public void displayInventory()
  37.     {
  38.         System.out.printf( "\n%s\n" ,"Inventory of CDs :" );
  39.         System.out.printf( "\n%s%35s\n%s%12s\n%s%9.2f\n%s%12s%.2f\n%s%5s%.2f\n\n" , "CD Title:", title, "Product Number:", number , "Number in Stock:", numStock , "CD Price:" , "$" , price , "Inventory Value:" , "$" , getInventoryValue() );
  40.  
  41.     } // end method
  42.  
  43.     public void calculateTotalInventory( Inventory2 completeCDInventory[] )
  44.     {
  45.         calculateInventoryValue();
  46.  
  47.         for ( int count = 0; count < completeCDInventory.length; count++ )
  48.         {
  49.              totalInventoryValue += getInventoryValue();
  50.  
  51.         } // end for
  52.  
  53.     } // end calculateTotalInventory
  54.  
  55.     public double getTotalInventoryValue()
  56.     {
  57.         return totalInventoryValue;
  58.  
  59.     } // end getTotalInventory
  60.  
  61.     public void displayTotalInventory( Inventory2 completeCDInventory[] )
  62.     {
  63.  
  64.                 for ( int count = 0; count < completeCDInventory.length; count++ )
  65.         {
  66.             System.out.printf( "%s%i", "Item# ", count + 1 );
  67.             displayInventory();
  68.  
  69.         }// end for
  70.  
  71.     }// end displayTotalInventory
  72.  
  73. } // end class CD
and the class that uses Inventory2:

Expand|Select|Wrap|Line Numbers
  1. // InventoryTest2.java
  2. // creates and inventory object
  3.  
  4. public class InventoryTest2
  5. {
  6.  
  7.     // executes application
  8.     public static void main( String args[] )
  9.     {
  10.  
  11.         Inventory2 completeCDInventory[] = new Inventory2[ 5 ]; // creates a new 5 element array
  12.  
  13.         // populates array with objects that implement Inventory
  14.         completeCDInventory[ 0 ] = new Inventory2( "Sixpence None the Richer" , "D121401" , 12 , 11.99 );
  15.         completeCDInventory[ 1 ] = new Inventory2( "Clear" , "D126413" , 10 , 10.99 );
  16.         completeCDInventory[ 2 ] = new Inventory2( "NewsBoys: Love Liberty Disco" , "2438-51720-2" , 10 , 12.99 );
  17.         completeCDInventory[ 3 ] = new Inventory2( "Skillet: Hey You, I Love Your Soul" , "D122966" , 9 , 9.99 );
  18.         completeCDInventory[ 4 ] = new Inventory2( "Michael Sweet: Real" , "020831-1376-204" , 15 , 12.99 );
  19.  
  20.         completeCDInventory.calculateTotalInventory( completeCDInventory ); //calls Inventory's calculate method
  21.  
  22.         completeCDInventory.displayTotalInventory( completeCDInventory ); //calls Inventory's display method
  23.  
  24.  
  25.  
  26.     } // end main
  27.  
  28. } // end class
The Inventory2 class compiles but when I try the InventoryTest class it gives me this error message:

C:\Documents and Settings\Sam\InventoryTest2.java:20: cannot find symbol
symbol : method calculateTotalInventory(Inventory2[])
location: class Inventory2[]
completeCDInventory.calculateTotalInventory( completeCDInventory ); //calls Inventory's calculate method
^
C:\Documents and Settings\Sam\InventoryTest2.java:22: cannot find symbol
symbol : method displayTotalInventory(Inventory2[])
location: class Inventory2[]
completeCDInventory.displayTotalInventory( completeCDInventory ); //calls Inventory's display method
^
2 errors
Jun 22 '07 #3
blazedaces
284 100+
Well, I'm not really sure why it does that since the code looks alright to me, maybe someone else might spot the problem

I do suggest trying to use arraylists or vectors just to see if you get the same error.

Good luck,
-blazed
Jun 22 '07 #4
sumittyagi
202 Expert 100+
I am making changes to your code. It will run now.

Expand|Select|Wrap|Line Numbers
  1. // Inventory2.java
  2. // Represents a compact disc object
  3.  
  4. class Inventory2
  5. {
  6.     private String title; // CD title (name of product)
  7.     private String number; // CD product number
  8.     private double numStock; // CD stock number
  9.     private double price; // price of CD
  10.     private double inventoryValue; //number of units in stock times price of each unit
  11.     //private double totalInventoryValue; 
  12.  
  13.     // constructor initializes CD information
  14.     public Inventory2( String cdTitle, String productNumber, double numberInStock, double cdPrice )
  15.     {
  16.         title = cdTitle;
  17.         number = productNumber;
  18.         numStock = numberInStock;
  19.         price = cdPrice;
  20.  
  21.     } // end constructor
  22.  
  23.     public double getInventoryValue()
  24.     {
  25.         return numStock * price;
  26.     } //end getInventoryValue
  27.  
  28.     public void displayInventory()
  29.     {
  30.         System.out.printf( "\n%s\n" ,"Inventory of CDs :" );
  31.         System.out.printf( "\n%s%35s\n%s%12s\n%s%9.2f\n%s%12s%.2f\n%s%5s%.2f\n\n" , "CD Title:", title, "Product Number:", number , "Number in Stock:", numStock , "CD Price:" , "$" , price , "Inventory Value:" , "$" , getInventoryValue() );
  32.  
  33.     } // end method
  34.  
  35.     public static double calculateTotalInventory( Inventory2 completeCDInventory[] )
  36.     {
  37.         double totalInventoryValue = 0;
  38.         for ( int count = 0; count < completeCDInventory.length; count++ )
  39.         {
  40.              totalInventoryValue += completeCDInventory[count].getInventoryValue();
  41.  
  42.         } // end for
  43.                                 return totalInventoryValue;
  44.  
  45.     } // end calculateTotalInventory
  46.  
  47.  
  48.     public static void displayTotalInventory( Inventory2 completeCDInventory[] )
  49.     {
  50.  
  51.                 for ( int count = 0; count < completeCDInventory.length; count++ )
  52.         {
  53.             System.out.printf( "%s%i", "Item# ", count + 1 );
  54.             completeCDInventory[count].displayInventory();
  55.  
  56.         }// end for
  57.  
  58.     }// end displayTotalInventory
  59.  
  60. } // end class CD
  61.  
  62. public class InventoryTest2
  63. {
  64.  
  65.     // executes application
  66.     public static void main( String args[] )
  67.     {
  68.  
  69.         Inventory2 completeCDInventory[] = new Inventory2[ 5 ]; // creates a new 5 element array
  70.  
  71.         // populates array with objects that implement Inventory
  72.         completeCDInventory[ 0 ] = new Inventory2( "Sixpence None the Richer" , "D121401" , 12 , 11.99 );
  73.         completeCDInventory[ 1 ] = new Inventory2( "Clear" , "D126413" , 10 , 10.99 );
  74.         completeCDInventory[ 2 ] = new Inventory2( "NewsBoys: Love Liberty Disco" , "2438-51720-2" , 10 , 12.99 );
  75.         completeCDInventory[ 3 ] = new Inventory2( "Skillet: Hey You, I Love Your Soul" , "D122966" , 9 , 9.99 );
  76.         completeCDInventory[ 4 ] = new Inventory2( "Michael Sweet: Real" , "020831-1376-204" , 15 , 12.99 );
  77.  
  78.         double totalInventoryValue = Inventory2.calculateTotalInventory( completeCDInventory ); 
  79.  
  80.         System.out.println("Total Inventory Value is: " + totalInventoryValue);
  81.         Inventory2.displayTotalInventory( completeCDInventory ); //calls Inventory's display method
  82.  
  83.  
  84.  
  85.     } // end main
  86.  
  87. } // end class
  88.  
Now if you have any queries about the changes I made, then you may ask.
Jun 22 '07 #5
I was wondering about the reason why the methods needed to be declared as static to work. I had a small section in my text on static methods but it didn't really make a situation like this clear.
Jun 22 '07 #6
sumittyagi
202 Expert 100+
I was wondering about the reason why the methods needed to be declared as static to work. I had a small section in my text on static methods but it didn't really make a situation like this clear.
It was not needed to make methods static, I could have solved a situation in the way U solved it(by just correcting the way you called the methods). But I did it intentionally.

Whenever you see any methods which essentially work as utility methods, and they don't seem to belong to a particular object, then it is a good idea to make them static.

your methods calculateTotalInventory and displayTotalInventory doesn't belong to any cd object, rather they work as utility methods which takes an array of cd objects and returns the total price of all the cd objects in the inventory.

But methods getInventoryValue and displayInventoryValue belong to a particular object(that's why I didn't made them static).

So its only the design decision. You may do it your way, but it will not be a good practice.
Jun 25 '07 #7
***************************************thanks!
Jun 26 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: A.M-SG | last post by:
Hi, I have a web service with two soap extensions enabled on it. When I run the default IIS method invoke page, the invoke button bypasses all my soap extensions. But when I call the...
1
by: Frank [GOD] | last post by:
Let me set this up for y'all... I have 8 mySQL databases with over 100K records, which include a phone number field. I call these the storage tanks. They're labeled db1 - db8. Then I have 1...
11
by: MurdockSE | last post by:
Greetings. My Situation: // I have an .xml file that I am reading into a dataset in the following code - DataSet ds = new DataSet("MyDataset"); ds.ReadXml(@"c:\data\"+cmyxmlfilename);
6
by: LordHog | last post by:
Hello all, I recently ran into a strange behavior which I don't understand. I have two 'Add' method which a slightly different signature which they look like public void Add( string varName,...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
4
by: =?Utf-8?B?R3JlZw==?= | last post by:
I am a newbie to WCF so please forgive if this is an obvious question. I have the following. (Service contract) IEmployee public interface IEmployee { List<EmployeeGetEmployeeByID(string...
2
ADezii
by: ADezii | last post by:
This week's Tip is basically geared to Power Access Users and Gurus who demand the ultimate in efficiency within their Applications. It involves an undocumented feature of Jet 4, and is a technique...
0
ADezii
by: ADezii | last post by:
In last week's Tip, I showed you how to use the ISAMStats Method of the DBEngine (DAO) to return vital statistics concerning Query executions such as: Disk Reads and Writes, Cache Reads and Writes,...
9
by: Hans-Jürgen Philippi | last post by:
Hi group, let's say I have a 'Person' class with properties like 'FirstName', 'LastName', 'Birthday' and so on. Now I overload the 'Person' ToString() method with an implementation...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.