Results 1 to 3 of 3
  1. #1
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default problem with Vectors and getTotal() function

    Hi all,
    I don't know how to use vectors to get the total number of amounts it should be in double.

    this method is in one of the two classes, and it should count the total amounts that are in the other class (Aggregation) using vectors and return the total in double.
    I don't really know how the total is done using vectors I just know how to do it using array lists.

    here is what I meant and my solution:(not all methods are there just the methods related to my problem are added)
    I hope you help me please.




    Java Code:
    class Coffee
    private String CoffeeName;//coffee name
    private Vector mybatch;//vector of batches
    	
      double getTotalStock()
    	{	//NOT SURE
    		//calculate the total amount
    
      for(int i =0;i<mybatch.size();i++) 
    Batch total += (Batch)mybatch.elementAt(i).getStock();
    //here I guess the type is a class and the way to get the total is wrong  how to get the value in double???
    }
    
    class Batch
    {	
    	
    	private double stock;
    public Batch(double stock)
    	{	//Done
    		//intialize new batch with stock 
    		stock = stock;
    		
    	}
    	public double getStock()
    	{
    	 return stock;	}
    }
    thank you

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    class Coffee
    {
        private String CoffeeName;  //coffee name
        private Vector mybatch;     //vector of batches
    
        double getTotalStock()
        {
            //calculate the total amount
            double total = 0;
            for(int i = 0; i < mybatch.size(); i++)
            {
                // You can do this in one line
                //total += ((Batch)mybatch.get(i)).getStock();
                // or you can always break it down
                Batch batch = (Batch)mybatch.elementAt(i);
                total += batch.getStock();
            }
            return total;
        }
    }
    
    class Batch
    {	
        private double stock;
    
        public Batch(double stock)
        {
            //intialize new batch with stock 
            stock = stock;
        }
    
        public double getStock()
        {
            return stock;
        }
    }

  3. #3
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

Similar Threads

  1. Sorting JTable (Vectors) Problem
    By ramapple in forum AWT / Swing
    Replies: 6
    Last Post: 07-07-2009, 12:15 AM
  2. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 03:57 PM
  3. Problem with vectors in java
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:56 AM
  4. Function declaration problem.
    By snooze-g in forum Advanced Java
    Replies: 3
    Last Post: 07-18-2007, 10:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •