9 Replies - 10769 Views - Last Post: 14 June 2009 - 09:57 AM Rate Topic: -----

#1 tralioc81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 13-June 09

how do I pass an array of objects to a method?

Posted 13 June 2009 - 05:10 PM

I am trying to pass an array of objects to a method to make the main body of code look cleaner and also because the assignment says I have to use a method. The problem I am having is when I try to access the array movie from the method calculateGrandTotal it tells me movie cannot be resolved. Is there a way this will work, or am I just going about things all wrong? If you can't tell I am very new to programming.

Here is the code for Product, which the class Inventory that I having trouble with is referencing..
public class Product {				//class definition
	
	private int itemNumber;				//Declare variables of DVDs
	private String productName;
	private int stockUnits;
	private double unitPrice;
	
	
	public Product(int dvdNumber, String dvdName, int dvdUnits,  //Class constructor
			double dvdPrice)
	{
		itemNumber = dvdNumber;
		productName = dvdName;
		stockUnits = dvdUnits;
		unitPrice = dvdPrice;
	}
	
	public void setDvdNumber(int dvdNumber)
	{
		itemNumber = dvdNumber;
	}
	
	public int getDvdNumber()
	{
		return itemNumber;
	}
	
	public void setDvdName(String dvdName)
	{
		productName = dvdName;
	}
	
	public String getDvdName()
	{
		return productName;
	}
	
	public void setDvdUnits(int dvdUnits)
	{
		stockUnits = dvdUnits;
	}
	
	public int getDvdUnits()
	{
		return stockUnits;
	}
	
	public void setDvdPrice(double dvdPrice)
	{
		unitPrice = dvdPrice;
	}
	
	public double getDvdPrice()
	{
		return unitPrice;
	}
	
	static void printHeadings()
	{
		System.out.printf("Item #\tDVD Title		  # of Units   Unit Price   Item Total\n");
	}
	
	public void printItems()
	{
		System.out.printf("%6s\t%-20s%9s%13.2f%13.2f\n", itemNumber, productName, stockUnits, unitPrice, itemTotal());
	}
	
	public double itemTotal()
	{
		return stockUnits * unitPrice;
	}
	

} //end Product class



And here is the code for class Inventory...
public class Inventory {
	
	static double grandTotal;
	
	public static void main(String args[])
	{
		Product movie[] = new Product[3];
		
		movie[0] = new Product(1, "Inspector Gadget", 25, 9.50);
		
		movie[1] = new Product(2, "Constantine", 5, 10.00);
		
		movie[2] = new Product(3, "Goldmember", 7, 11.25);
		
		
		
		
		Product.printHeadings();
		
		for(int counter = 0; counter < movie.length; counter++)
		{
			movie[counter].printItems();
		}
		
		calculateGrandTotal();
		
		System.out.printf("\n\nAnd the total value of the inventory is %23.2f", grandTotal);		
	}
	
	public static void calculateGrandTotal()
	{
		for(int counter = 0; counter < movie.length; counter++)
		{
			grandTotal += movie[counter].itemTotal();
		
		}
		
	}
	
}



Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: how do I pass an array of objects to a method?

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: how do I pass an array of objects to a method?

Posted 13 June 2009 - 05:32 PM

Where are you trying to send your Array to a method? The problem you will have here is that you have created your Array in your main method. This means it is only available to that method. Make your Array a class variable, and you do this by moving it from where it is now to underneath where you declared grandTotal
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how do I pass an array of objects to a method?

Posted 13 June 2009 - 07:40 PM

As parameter

class Animal {
....
}

...
Animal[] animal = new Animal[100];
...

print(animal);	 // calls method prinmt that receives an array of Animal
....

void print(Animal[] array) {	// recives an array of Animal as parameter
	for(int i = 0; i < array.length; i++) {
	   System.out.println(array[i].getName();	// assuming Animal has a getName() method
	}
}


Was This Post Helpful? 1
  • +
  • -

#4 paser  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 135
  • Joined: 30-May 09

Re: how do I pass an array of objects to a method?

Posted 13 June 2009 - 09:43 PM

ugh my bad, was in wrong forum =D

This post has been edited by paser: 14 June 2009 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how do I pass an array of objects to a method?

Posted 13 June 2009 - 09:47 PM

View Postpaser, on 13 Jun, 2009 - 08:43 PM, said:

you can also pass in a pointer to the first element and use pointer arithmatic to find which ever one ur looking for.

Hey.. we are in Java here not in C++
you cannot pass anything else than a pointer to the array
and Java does not allow aritmetic on pointers .... as it hides them from the user

This post has been edited by pbl: 13 June 2009 - 09:48 PM

Was This Post Helpful? 0
  • +
  • -

#6 tralioc81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 13-June 09

Re: how do I pass an array of objects to a method?

Posted 13 June 2009 - 10:08 PM

View Postpbl, on 13 Jun, 2009 - 06:40 PM, said:

As parameter

class Animal {
....
}

...
Animal[] animal = new Animal[100];
...

print(animal);	 // calls method prinmt that receives an array of Animal
....

void print(Animal[] array) {	// recives an array of Animal as parameter
	for(int i = 0; i < array.length; i++) {
	   System.out.println(array[i].getName();	// assuming Animal has a getName() method
	}
}



I don't seem to be able to pass it as a parameter because the movie array is an array of product objects?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how do I pass an array of objects to a method?

Posted 13 June 2009 - 10:14 PM

View Posttralioc81, on 13 Jun, 2009 - 09:08 PM, said:

I don't seem to be able to pass it as a parameter because the movie array is an array of product objects?

And which methd is the one that receices as parameter an array of movies or even product ?
Was This Post Helpful? 0
  • +
  • -

#8 tralioc81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 13-June 09

Re: how do I pass an array of objects to a method?

Posted 13 June 2009 - 10:38 PM

class Product contains a constructor to set up product objects. I then create an array named movie of those objects in class Inventory. This movie array is what I am trying to pass to a method, also in class Inventory. I seem to be able to use my product objects just fine from within the Inventory class, but I just wanted to split the different tasks into methods in that class.

This is how I originally had the Inventory class..before I decided I wanted to move things to methods within it.


public class Inventory {
	
	static double grandTotal;

	public static void main(String args[])
	{
		Product.printHeadings();
		
		Product movie[] = new Product[3];
		
		movie[0] = new Product(1, "Inspector Gadget", 25, 9.50);
				
		movie[1] = new Product(2, "Constantine", 5, 10.00);
				
		movie[2] = new Product(3, "Goldmember", 7, 11.25);
		
		for(int counter = 0; counter < movie.length; counter++)
		{
			movie[counter].printItems();
		}
		
		for(int counter = 0; counter <  movie.length; counter++)
		{
			grandTotal += movie[counter].itemTotal();		
		}
	
		System.out.printf("\n\nAnd the total value of the inventory is %23.2f", grandTotal);
	}
}



Everything compiled and functioned at least this way. The bad part is the assignment specifically says to create a method for the grand total, and then another to sort the items in the array by name. I haven't gotten to the sorting part because I got hung up on the method.
Was This Post Helpful? 0
  • +
  • -

#9 tralioc81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 13-June 09

Re: how do I pass an array of objects to a method?

Posted 14 June 2009 - 09:19 AM

ok so I did it a little different. I now have a class Product to construct the objects, a class Inventory to perform all the actions on the objects, and then a class InventoryTest to call the methods of Inventory. Everything seemed ok until I go to try and run the program. For the two methods I am trying to run that use the 'movie' array I am getting null pointer exceptions. I just am not having much luck passing arrays.

Here is the code for each class I am currently using.
public class Product {				//class definition
	
	private int itemNumber;				//Declare variables of DVDs
	private String productName;
	private int stockUnits;
	private double unitPrice;
	
	
	public Product(int dvdNumber, String dvdName, int dvdUnits,  //Class constructor
			double dvdPrice)
	{
		itemNumber = dvdNumber;
		productName = dvdName;
		stockUnits = dvdUnits;
		unitPrice = dvdPrice;
	}
	
	static void printHeadings()
	{
		System.out.printf("Item #\tDVD Title		  # of Units   Unit Price   Item Total\n");
	}
	
	public void printItems()
	{
		System.out.printf("%6s\t%-20s%9s%13.2f%13.2f\n", itemNumber, productName, stockUnits, unitPrice, itemTotal());
	}
	
	public double itemTotal()
	{
		return stockUnits * unitPrice;
	}	

} //end Product class

public class Inventory {
	
	static float grandTotal = 0;
	static Product movie[];
	
	static void initArray()
	{
		Product movie[] = new Product[3];
		
		movie[0] = new Product(1, "Inspector Gadget", 25, 9.50);
				
		movie[1] = new Product(2, "Constantine", 5, 10.00);
				
		movie[2] = new Product(3, "Goldmember", 7, 11.25);
	}
	
	static void printItems(Product movie[])
	{
		for(int counter = 0; counter < movie.length; counter++)
		{
			movie[counter].printItems();
		}
	}
	
	static void calculateGrandTotal(Product movie[])
	{
		for(int counter = 0; counter <  movie.length; counter++)
		{
			grandTotal += movie[counter].itemTotal();		
		}

		System.out.printf("\n\nAnd the total value of the inventory is %23.2f", grandTotal);
	}
}


public class InventoryTest {
	
	public static void main(String args[])
	{
		Product.printHeadings();
		Inventory.initArray();
		Inventory.printItems(Inventory.movie);
		Inventory.calculateGrandTotal(Inventory.movie);
	}

}


Was This Post Helpful? 0
  • +
  • -

#10 tralioc81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 13-June 09

Re: how do I pass an array of objects to a method?

Posted 14 June 2009 - 09:57 AM

Wohoo it works now. I moved the array initialization outside of a method in the Inventory class and I also needed to create an Inventory object in Inventory test before I could use its methods. Thanks guys for the pointers I do not think I would have headed toward the answer without your help

Here is how I changed Inventory and InventoryTest to get it going.
public class Inventory {
	
	
	static float grandTotal = 0;
	Product movie[] = new Product[3];
	
	public void fillArray()
	{		
		movie[0] = new Product(1, "Inspector Gadget", 25, 9.50);
				
		movie[1] = new Product(2, "Constantine", 5, 10.00);
				
		movie[2] = new Product(3, "Goldmember", 7, 11.25);
	}
	
	public void printItems()
	{
		for(int counter = 0; counter < movie.length; counter++)
		{
			movie[counter].printItems();
		}
	}
	
	public void calculateGrandTotal()
	{
		for(int counter = 0; counter <  movie.length; counter++)
		{
			grandTotal += movie[counter].itemTotal();		
		}

		System.out.printf("\n\nAnd the total value of the inventory is %23.2f", grandTotal);
	}
	
	
}


public class InventoryTest {
	
	public static void main(String args[])
	{
		Inventory dvd = new Inventory();
		
		Product.printHeadings();
		dvd.fillArray();
		dvd.printItems();
		dvd.calculateGrandTotal();
	}

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1