Results 1 to 19 of 19
  1. #1
    AntivanCrow is offline Member
    Join Date
    Feb 2014
    Location
    MS
    Posts
    13
    Rep Power
    0

    Default Math Integration across multiple arrays.

    I am trying to complete an assignment and I am stuck on a key point. I must print the total inventory value across each of these arrays, as it prints for each "list". To do this, I understand that I need some form of correlation between my variables "total" and "Price" between each array. My quandary lies in the way that I have designed this code to begin with. I am not sure where to add the math section of code that is needed, nor am I fluent enough with Java to know how to call all of the arrays as it prints. Listed below is my code, when you compile it the total inventory value is 0 for each "list". Thank you in advance.
    Java Code:
    package inventory;
    
    
    
    public class Inventory {
    
    
     
        public static void
             main (String[] args) {
            List[]list = new List[5];
                              
            list[0] = new List(123,"Dell CPU", "Back-up for Line",6, 525.23);      
            System.out.println(list);
              
            list[1]  = new List (124,"Box of Paper","8.5x11", 8, 14.75);         
            System.out.println(list);
              
            list[2] = new List (125,"Dell Venue","8.5in Tablet",3,206.75);
            System.out.println(list);
              
            list[3] = new List (126,"Box of Paper Clips","Standard Paper Clips", 4, 3.15);
            System.out.println(list);
            
            int total=0;
            for (int i=0 ;i<list.length;i++ ) {
                System.out.println("The Inventory ID is: " + list[i].getinvItem());
                System.out.println("The Item's Name is: " + list[i].getinvName());
                System.out.println("The Item's Description is:" + list[i].getinvDescription());
                System.out.println("The Current stock quantity is: " + list[i].getinvStock());
                System.out.println("The value of the inventory is $" + list[i].value()); 
                total+=list[i].getTotal();
                System.out.println("The total value of the inventory is $"+list[i].total);
            }
        }
    }
        class List{
        int invItem;
        String invName;
        String invDescription;
        int invStock;
        double invPrice;
        double total;
          
        public List(int Item,String Title,String Description, int Stock, double Price) 
        {
            invItem  = Item;
            invName= Title;
            invDescription = Description;
            invStock = Stock;
            invPrice = Price;
        
            
        } //end Five-argument constructor
         public void setinvItem(int Item) 
        {
            invItem = Item;
        } //end method setinvItem
      
        //return invItem
        public int getinvItem() 
        {
            return invItem;
        } //end  method getinvItem
      
        //calculate inventory value
        public void setinvName(String Title) 
        {
           invName= Title;
        } //end method setinvName
        public String getinvName() 
        {
            return invName;
        } //end method getinvName
      
        // set inventory Description
        public void setinvDescription(String Description) 
        {
            invDescription = Description;
        } //end method  setinvDescription
      
        //return invDescription
        public String getinvDescription() 
        {
            return invDescription;
        } //end method getinvDescription
      
        //set  invStock
        public void setinvStock(int Stock) 
        {
            invStock = Stock;
        } //end method setinvStock
      
        //return invStock
        public int getinvStock() 
        {
            return invStock;
        } //end method getDvdStock
         //set invPrice
        public void setinvPrice(double Price) 
        {
            invPrice = Price;
        } //end method setinvPrice
      
        //return invPrice
        public double getinvPrice() 
        {
            return invPrice;
        } //end method getinvPrice
        //set invItem
      
        public double value() 
        {
            return invPrice * invStock;
        } //end method value
        
        public double getTotal()
        {
            return total;
        }//end method to return totalValue
    
    }// end class List

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Math Integration across multiple arrays.

    You've never calculated total for your lists. What's the difference between value() and total()?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    AntivanCrow is offline Member
    Join Date
    Feb 2014
    Location
    MS
    Posts
    13
    Rep Power
    0

    Default Re: Math Integration across multiple arrays.

    Quote Originally Posted by PhHein View Post
    You've never calculated total for your lists. What's the difference between value() and total()?
    True. The difference between value() and total() is simple. Value is the representation of the (Item Price * Stock Quantity). Total is what the issue is. I am not sure how to calculate this in a loop when all of the instances have yet to take place in the programming. Total should equal (price.list0+price.list1 +price.list2+price.list3). I need each array to display the total inventory quantity, when it prints out in the loop. My issue is that while total=0 in the program, I am unsure how to calculate this in List.list (0) for all "prices" after it. It would execute after I need the information, which is cause for concern. The equal to 0 came about, just to see if it could be done but it is not an accurate representation of what I am trying to accomplish. If boolean were to take place it would have issues with some of my declarations, correct? Boolean does not like to mix double and int. So I am stuck at the actual code level for the math portion of this issue.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Math Integration across multiple arrays.

    Huh?
    Java Code:
            double total=0; //<-------------------------- sum
            for (int i=0 ;i<list.length;i++ ) {
                System.out.println("The Inventory ID is: " + list[i].getinvItem());
                System.out.println("The Item's Name is: " + list[i].getinvName());
                System.out.println("The Item's Description is:" + list[i].getinvDescription());
                System.out.println("The Current stock quantity is: " + list[i].getinvStock());
                System.out.println("The value of the inventory is $" + list[i].value());
                total+=list[i].value; //<----------------------------  add up the values
            }
             System.out.println("The total value of the inventory is $"+total); // <------------- print total
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,004
    Rep Power
    6

    Default Re: Math Integration across multiple arrays.

    The only thing I see is a crash because list[4] is not initialized. How can you "see" that total equals 0?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    AntivanCrow is offline Member
    Join Date
    Feb 2014
    Location
    MS
    Posts
    13
    Rep Power
    0

    Default Re: Math Integration across multiple arrays.

    Quote Originally Posted by gimbal2 View Post
    The only thing I see is a crash because list[4] is not initialized. How can you "see" that total equals 0?
    In the NetBeans IDE. It displays zero on all 4 lists after:"The total inventory value is $". I know it represents zero. It was a test because I am unsure of the math that is required in java programming language. This is mentioned and explained in the previous post.

  7. #7
    AntivanCrow is offline Member
    Join Date
    Feb 2014
    Location
    MS
    Posts
    13
    Rep Power
    0

    Default Re: Math Integration across multiple arrays.

    The representation for total should take into account a sum of all inventory values within the four arrays. In my mind, the arrays need to be generated and a separate loop needs to be produced to get the correct value for "total".

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,004
    Rep Power
    6

    Default Re: Math Integration across multiple arrays.

    Hm right, the loop also prints. So this is what you get:

    Java Code:
    The Inventory ID is: 123
    The Item's Name is: Dell CPU
    The Item's Description is:Back-up for Line
    The Current stock quantity is: 6
    The value of the inventory is $3151.38
    The total value of the inventory is $0.0
    The Inventory ID is: 124
    The Item's Name is: Box of Paper
    The Item's Description is:8.5x11
    The Current stock quantity is: 8
    The value of the inventory is $118.0
    The total value of the inventory is $0.0
    The Inventory ID is: 125
    The Item's Name is: Dell Venue
    The Item's Description is:8.5in Tablet
    The Current stock quantity is: 3
    The value of the inventory is $620.25
    The total value of the inventory is $0.0
    The Inventory ID is: 126
    The Item's Name is: Box of Paper Clips
    The Item's Description is:Standard Paper Clips
    The Current stock quantity is: 4
    The value of the inventory is $12.6
    The total value of the inventory is $0.0
    Exception in thread "main" java.lang.NullPointerException   <--- crash because list[4] is null
            at Test2.main(Test2.java:24)
    In any case check PhHein's response. I didn't see it myself initially but there are some advised modifications in there.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    AntivanCrow is offline Member
    Join Date
    Feb 2014
    Location
    MS
    Posts
    13
    Rep Power
    0

    Default Re: Math Integration across multiple arrays.

    Ok. Sorry I have not replied back in a while. I had to work 80+ hours last week. I will post an updated code section in a bit. I am working on it now.

  10. #10
    AntivanCrow is offline Member
    Join Date
    Feb 2014
    Location
    MS
    Posts
    13
    Rep Power
    0

    Default Re: Math Integration across multiple arrays.

    This is what my Teacher sent me. Evidently I was supposed to sort the array as well.
    Java Code:
    public double calTotalInventory (Guitars [] play)   //In my case "calTotalInventory" should be something else. I have "getTotal" declared in Class "List"
            {
               double totalinventory = 0.0;  // Sum of inventory
               
               for (int i = 0; i < play.length; i++)   // "play.length" should be "list.length"
               { 
                   totalinventory = totalinventory + play[i].getTotalValue(); // Function to calculate the sum. totalinventory=totalinventory+ list[i].getTotalValue();
               }
               
               return totalinventory;//Execute function then print it.
            }
    
    Sort an array of objects -
    	private static void sortProducts(ProductInfo[] productArray) {//Unsure of how this plays into the schematic. Where does this go? I think after my main method
    		int length = productArray.length;
    		ProductInfo obj1 = null;
    		ProductInfo obj2 = null;
    		ProductInfo temp = null;
    		for (int i = 1; i < length; i++) {
    			for (int j = 0; j < length - i; j++) {
    				obj1 = productArray[j];
    Last edited by AntivanCrow; 02-19-2014 at 08:17 AM.

  11. #11
    AntivanCrow is offline Member
    Join Date
    Feb 2014
    Location
    MS
    Posts
    13
    Rep Power
    0

    Default Re: Math Integration across multiple arrays.

    Java Code:
        double total=0;
            for (int i=0 ;i<list.length;i++ ) {
                System.out.println("The Inventory ID is: " + list[i].getinvItem());
                System.out.println("The Item's Name is: " + list[i].getinvName());
                System.out.println("The Item's Description is:" + list[i].getinvDescription());
                System.out.println("The Current stock quantity is: " + list[i].getinvStock());
                System.out.println("The value of the inventory is $" + list[i].value()); 
                total+=list[i].value;
            }
                System.out.println("The total value of the inventory is $"+total);
                System.out.println();
                System.out.println();
            //End of Print Method
        }//End of Static Main
    }//End of Class Inventory
    It pops up an error if I do it this way. Value is not an object that NetBeans understands, even though "value" and "price" are referenced in Class "List".

  12. #12
    AntivanCrow is offline Member
    Join Date
    Feb 2014
    Location
    MS
    Posts
    13
    Rep Power
    0

    Default Re: Math Integration across multiple arrays.

    Java Code:
    package inventory;
    
    
    
    public class Inventory {
    
    
     
        public static void main (String[] args) {
            List[]list = new List[5];
                              
            list[0] = new List(123,"Dell CPU", "Back-up for Line",6, 525.23);      
            System.out.println(list);
              
            list[1]  = new List (124,"Box of Paper","8.5x11", 8, 14.75);         
            System.out.println(list);
              
            list[2] = new List (125,"Dell Venue","8.5in Tablet",3,206.75);
            System.out.println(list);
              
            list[3] = new List (126,"Box of Paper Clips","Standard Paper Clips", 4, 3.15);
            System.out.println(list);
            
            double total=0;
            for (int i=0 ;i<list.length;i++ ) {
                System.out.println("The Inventory ID is: " + list[i].getinvItem());
                System.out.println("The Item's Name is: " + list[i].getinvName());
                System.out.println("The Item's Description is:" + list[i].getinvDescription());
                System.out.println("The Current stock quantity is: " + list[i].getinvStock());
                System.out.println("The value of the inventory is $" + list[i].value()); 
                total+=list[i].value();
                System.out.println("The total value of the inventory is $"+total);//Prints a Running total now. I think this is acceptable. Thanks.
                System.out.println();
                System.out.println();
            } //End of Print Method
                private static void sortProducts(ProductInfo[] productArray) {
    		int length = productArray.length;
    		ProductInfo obj1 = null;
    		ProductInfo obj2 = null;
    		ProductInfo temp = null;
    		for (int i = 1; i < length; i++) {
    			for (int j = 0; j < length - i; j++) {
    				obj1 = productArray[j];       // I think this is where my teacher's code goes. But I am unsure of how to institute a sort method 
                                                                          //with my variables.
        }//End of Static Main
    }//End of Class Inventory
        class List{
        int invItem;
        String invName;
        String invDescription;
        int invStock;
        double invPrice;
        double total;
          
        public List(int Item,String Title,String Description, int Stock, double Price) 
        {
            invItem  = Item;
            invName= Title;
            invDescription = Description;
            invStock = Stock;
            invPrice = Price;
        
            
        } //end Five-argument constructor
         public void setinvItem(int Item) 
        {
            invItem = Item;
        } //end method setinvItem
      
        //return invItem
        public int getinvItem() 
        {
            return invItem;
        } //end  method getinvItem
      
        //calculate inventory value
        public void setinvName(String Title) 
        {
           invName= Title;
        } //end method setinvName
        public String getinvName() 
        {
            return invName;
        } //end method getinvName
      
        // set inventory Description
        public void setinvDescription(String Description) 
        {
            invDescription = Description;
        } //end method  setinvDescription
      
        //return invDescription
        public String getinvDescription() 
        {
            return invDescription;
        } //end method getinvDescription
      
        //set  invStock
        public void setinvStock(int Stock) 
        {
            invStock = Stock;
        } //end method setinvStock
      
        //return invStock
        public int getinvStock() 
        {
            return invStock;
        } //end method getDvdStock
         //set invPrice
        public void setinvPrice(double Price) 
        {
            invPrice = Price;
        } //end method setinvPrice
      
        //return invPrice
        public double getinvPrice() 
        {
            return invPrice;
        } //end method getinvPrice
       
      
        public double value() 
        {
            return invPrice * invStock;
        } //end method value
    
      
        public double getTotal()
        {
            return total;
        }//end method to return totalValue
    
    }// end class List
    Last edited by AntivanCrow; 02-19-2014 at 08:45 AM.

  13. #13
    AntivanCrow is offline Member
    Join Date
    Feb 2014
    Location
    MS
    Posts
    13
    Rep Power
    0

    Default Re: Math Integration across multiple arrays.

    I need to also create a sub-class for a restocking fee, that needs to be inserted into the print method for this on-going assignment. I have written some of that but I am still in the dark on a sort method and how to implement the sub-class over the Super class, in certain instances. Below is my updated code with my attempt at a sub-class. It has one error in the IDE, I'll mark it in the code and state the error message.
    Java Code:
    
    package inventory;
    
    
    
    public class Inventory {
    
    
     
        public static void main (String[] args) {
            
            List[]list = new List[5];
             //Construct Restocking fee.                 
            list[0] = new List(123,"Dell CPU", "Back-up for Line",6, 525.23);      
            System.out.println(list);
              
            list[1]  = new List (124,"Box of Paper","8.5x11", 8, 14.75);         
            System.out.println(list);
              
            list[2] = new List (125,"Dell Venue","8.5in Tablet",3,206.75);
            System.out.println(list);
              
            list[3] = new List (126,"Box of Paper Clips","Standard Paper Clips", 4, 3.15);
            System.out.println(list);
            
            double total=0;
            for (int i=0 ;i<list.length;i++ ) {
                System.out.println("The Inventory ID is: " + list[i].getinvItem());
                System.out.println("The Item's Name is: " + list[i].getinvName());
                System.out.println("The Item's Description is:" + list[i].getinvDescription());
                System.out.println("The Current stock quantity is: " + list[i].getinvStock());
                System.out.println("The value of the inventory is $" + list[i].value()); 
                total+=list[i].value();
                System.out.println("The total value of the inventory is $"+total);
                System.out.println();
                System.out.println();
            } //End of Print Method
        }//End of Static Main
    }//End of Class Inventory
        class List{
        int invItem;
        String invName;
        String invDescription;
        int invStock;
        double invPrice;
        double total;
          
        public List(int Item,String Title,String Description, int Stock, double Price) 
        {
            invItem  = Item;
            invName= Title;
            invDescription = Description;
            invStock = Stock;
            invPrice = Price;
        
            
        } //end Five-argument constructor
         public void setinvItem(int Item) 
        {
            invItem = Item;
        } //end method setinvItem
      
        //return invItem
        public int getinvItem() 
        {
            return invItem;
        } //end  method getinvItem
      
        //calculate inventory value
        public void setinvName(String Title) 
        {
           invName= Title;
        } //end method setinvName
        public String getinvName() 
        {
            return invName;
        } //end method getinvName
      
        // set inventory Description
        public void setinvDescription(String Description) 
        {
            invDescription = Description;
        } //end method  setinvDescription
      
        //return invDescription
        public String getinvDescription() 
        {
            return invDescription;
        } //end method getinvDescription
      
        //set  invStock
        public void setinvStock(int Stock) 
        {
            invStock = Stock;
        } //end method setinvStock
      
        //return invStock
        public int getinvStock() 
        {
            return invStock;
        } //end method getDvdStock
         //set invPrice
        public void setinvPrice(double Price) 
        {
            invPrice = Price;
        } //end method setinvPrice
      
        //return invPrice
        public double getinvPrice() 
        {
            return invPrice;
        } //end method getinvPrice
       
      
        public double value() 
        {
            return invPrice * invStock;
        } //end method value
    
      
        public double getTotal()
        {
            return total;
        }//end method to return totalValue
      private double f;
        
    }// end class List
    class restockFee extends List{
        //**int Item
        //**String Title
        //**String Description
        //**int Stock
        //**double Price
        public restockFee(int Item, String Title, String Description, int Stock, double Price)
        {
            super(Item, Title, Description, Stock, Price);
            f=0;
        }
        public double getValue()
        {
            double restockFee=super.invPrice +super.invStock;
            return (restockFee *f);
        }
        public void setRestockFee(double f);  //missing method body or declare abstract. Should be eliminated if this is implemented into the Method at the top.
        {
            f=.5;
        }
        private double f;
        
    }

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,004
    Rep Power
    6

    Default Re: Math Integration across multiple arrays.

    Take two seconds more to look at the line with the error please. Don't abuse this thread to get answers to ALL your problems, you have to do it yourself or you won't learn anything. Believe me: the error is incredibly trivial.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    AntivanCrow is offline Member
    Join Date
    Feb 2014
    Location
    MS
    Posts
    13
    Rep Power
    0

    Default Re: Math Integration across multiple arrays.

    Quote Originally Posted by gimbal2 View Post
    Take two seconds more to look at the line with the error please. Don't abuse this thread to get answers to ALL your problems, you have to do it yourself or you won't learn anything. Believe me: the error is incredibly trivial.
    True. There is not a main method. The reason why, I understand. The "how to fix", not so much. I need to declare the sub class in the main method. To do so, I need to re-write part of the code to insert the "restocking fee". I'm not completely ignorant, I just can't do Java programming like I could in C++. They may "act" the same but they are totally different. I'm not even really sure where I need to interject the sort part of the method. This is a legit question, not an abuse of the thread. The book that I am using is vague. As in, create all of your classes in separate files and "call them" in the main method type of vague.

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,004
    Rep Power
    6

    Default Re: Math Integration across multiple arrays.

    Quote Originally Posted by AntivanCrow View Post
    I need to declare the sub class in the main method.
    No you don't, you don't declare classes inside methods unless you want to create an anonymous inner class. You don't want to do that.

    The book that I am using is vague. As in, create all of your classes in separate files and "call them" in the main method type of vague.
    You want to blame your book, but I am going to assume you are trying to burn through it all as quickly as possible without really trying to understand what the book is telling you. What you are describing is the best advice any book can give; don't make your life difficult, if you want to work with different classes then put those classes in different source files. That's about as basic as you can make it, I would find it hard to believe that even a "dummies" book would explain that poorly, I hope you have a better one than that.

    But okay, if your book sucks then try a google for "java working with multiple classes".
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    AntivanCrow is offline Member
    Join Date
    Feb 2014
    Location
    MS
    Posts
    13
    Rep Power
    0

    Default Re: Math Integration across multiple arrays.

    Quote Originally Posted by gimbal2 View Post
    No you don't, you don't declare classes inside methods unless you want to create an anonymous inner class. You don't want to do that.


    You want to blame your book, but I am going to assume you are trying to burn through it all as quickly as possible without really trying to understand what the book is telling you. What you are describing is the best advice any book can give; don't make your life difficult, if you want to work with different classes then put those classes in different source files. That's about as basic as you can make it, I would find it hard to believe that even a "dummies" book would explain that poorly, I hope you have a better one than that.

    But okay, if your book sucks then try a google for "java working with multiple classes".
    I'm just saying, the book is splitting the difference between Regular Employees and Managers with a bonus. Class Employee has a sub-class of Manager. Manager takes the place of where my "list[0]" would go. It essentially changes the enitre array because it has instances throughout where it "@override". If I did that, the "value" would have to be the only thing over written, and the println() signifying the change in price. Using a separate source for a class, tidy's the code a bit but if you are learning/implementing it as a beginner it is not like C++. The main method is usually all you see unless you flip 3-4 Chapters back. Just saying. Where do you think my iteration came from? From inference, and inference only with the help that I have gotten so far on here. But to further your Google argument, multiple classes will generate a set of circumstances that you must account for. I guess I need to refine my questions. I understand more than you think. The problem is that I don't have a "validation" of what my assumptions are. The code in the book is for a different type of program that I am adapting to this and there isn't a bridge between the two.

  18. #18
    AntivanCrow is offline Member
    Join Date
    Feb 2014
    Location
    MS
    Posts
    13
    Rep Power
    0

    Default Re: Math Integration across multiple arrays.

    If you would like, I'll post the code from the book....

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,004
    Rep Power
    6

    Default Re: Math Integration across multiple arrays.

    Don't bother, I'm going to shut up. I hope when I do, you will stop posting here and you will keep working on your own problem and knowledge gap.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Printing multiple arrays help...
    By Toffer_15 in forum New To Java
    Replies: 8
    Last Post: 01-24-2013, 09:35 PM
  2. Printing multiple arrays in 1 dialog box
    By turbopenguin in forum New To Java
    Replies: 9
    Last Post: 04-19-2012, 08:18 AM
  3. Storing Multiple 2d Arrays in a file.
    By onifiro in forum New To Java
    Replies: 1
    Last Post: 01-11-2011, 12:39 AM
  4. How to use arrays with Math.random
    By LennyKosmos in forum New To Java
    Replies: 2
    Last Post: 09-20-2010, 04:44 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
  •