Results 1 to 10 of 10
  1. #1
    Cian is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default calling a class from another class

    i know this is a basic enough question but i just cant seem to get it to work! ok the diagram for my project is attached.

    i need to fill an item(object?) list in class StorageSystem. this is my StorageSystem code:


    Java Code:
    public class StorageSystem
    {
        static  Item[] itemList;
        public static int totalStored = 0;
           int totalTime;
              int choosenTime;
        public StorageSystem(int totalIn)
        {
            totalStored = totalIn;
            itemList = new Item[totalStored];
        }
        
        public void addItem(Item itemListIn)
        {
            itemList[totalStored] = itemListIn;
            totalStored += 1;
        }
        
        public int calcPartyTime()
        {
         
            for(int i=0; i<totalStored; i++)
            {
                totalTime += itemList[i].playingTime;
            }
            
            return totalTime; 
        }
        
        public int calcChoosenItemTime()
        {
         
            for(int i=0; i<totalStored; i++)
            {
                if(itemList[i].gotIt == true)
                 choosenTime += itemList[i].playingTime;
                
            }
            return choosenTime;
        }
        
        public void printList()
        {
            for(int i=0; i<totalStored; i++)
            {
                System.out.println(itemList[i].title);
            }
        }
    }

    so basic enough. here is the code for Item class


    Java Code:
    public abstract class Item
    {
        String title, comment;
        int playingTime;
        boolean gotIt = false;
        float price;
        
        public Item(String titleIn, int playingTimeIn)
        {
            title = titleIn;
            playingTime = playingTimeIn;
        }
        
        public void setComment(String commentIn)
        {
            comment = commentIn;
        }
        
        public String getComment()
        {
            return comment;
        }
        
        public void setOwn(boolean gotItIn)
        {
            gotIt = gotItIn = true;
        }
        
        public boolean getOwn()
        {
            return gotIt;
        }
        
        public void setPrice(float priceIn)
        {
            price = priceIn;
        }
        
        public float getPrice()
        {
            return price;
        }
        
        public void print()
        {
            System.out.println(title);
            System.out.println(comment);
            System.out.println(playingTime);
            System.out.println(gotIt);
            System.out.println(price);
        }
    }
    now item is abstract, so i have a CD class and a DVD class, i'm starting with the CD class which is here:

    Java Code:
    public class CD extends Item
    {
        String artist;
        int numOfTracks;
        
        public CD(String artistIn, String titleIn, int playingTimeIn, int tracksIn)
        {
            super(titleIn, playingTimeIn);
            artist = artistIn;
            numOfTracks = tracksIn;
        }
        
        public void print()
        {
            System.out.println("name" + artist);
            System.out.println("title" + title);
            System.out.println("playingTime" + playingTime);
            System.out.println("numOfTracks" + numOfTracks);
        }
    }

    now i have a TestMethod class in which im getting the user to enter in the data to fill the Item[] array found in StorageSystem, but i just cant seem to get this to work!! so... here is my TestMethod class!!


    Java Code:
    public class Test
    {
        public static void main(String args[])
        {
            int select;
            String title, artist;
            int playTime, tracks, count;
            System.out.println("enter 1 for add new CD");
            System.out.println("enter 2 for add new DVD");
            System.out.println("enter 3 to calculate party time");
            System.out.println("enter 4 to calculate chosen time");
            System.out.println("enter 5 to print item list");
            System.out.println("enter 6 to quit");
            select = EasyIn.getInt();
            CD newcd = new CD(artist,title,playTime,tracks);
              switch (select) 
              {
                  
                case 1:
                //StorageSystem.itemList[StorageSystem.totalStored] = EasyIn.getString()
                System.out.println("enter title");
                title = EasyIn.getString();
                System.out.println("enter artist");
                artist = EasyIn.getString();            
                System.out.println("enter play time");
                playTime = EasyIn.getInt();
                System.out.println("enter number of tracks");
                tracks = EasyIn.getInt();
              //  StorageSystem.itemList[StorageSystem.totalStored].CD(artist,title,playTime,tracks);
                System.out.println("result");
              //  for(int i=0; i<StorageSystem.totalStored; i++)
                //StorageSystem.addItem(Item.Item(artist, title));
               StorageSystem.itemList[count]
                StorageSystem.printList();
                break;
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                break;
            }
        }  
    }
    any help here would be greatly appreciated, i know theres a lot of code there. also i have tried to create an item list (CD newcd = new CD()) and it says cannot find constructor, where as if you look in CD class, the constructor is there!

    thanks in advance for any help received :)

    Moderator Edit: Code tags added
    Last edited by Fubarable; 11-27-2010 at 02:55 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think that your problem may be that you need to create a StorageSystem object in the Test main method, and call methods on it rather than try to call methods on the StorageSystem class as your comments show you're trying to do.

    Question for you: did you create the StorageSystem class, or was that given to you by your instructor? If you did it, are you absolutely sure that the itemList and totalStored variables should be static? Because that smells very funny to me.

    Also, I added code tags to your post above, and you'll want to learn to do this to. To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck and welcome to the forum!

  3. #3
    Cian is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    cheers for the code tags tip, i wasnt sure how to do it. :) i wrote the SystemStorage alright, the reason i put it as static was because i was getting an error in my testmothod. i'll try what you said to do about creating a SystemStorageItem. thanks for the quick reply! :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Cian View Post
    i wrote the SystemStorage alright, the reason i put it as static was because i was getting an error in my testmothod.
    You fixed the wrong problem then. The solution is not to make anything static but to call SystemStorage methods on an object. So please get rid of the statics in that class.

    Luck!

  5. #5
    Cian is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    cheers for your help Fubarable. i removed static without any problems and created systemStorage object. inside in SystemStorage i removed item list and created a dvdList and cdList as both are seperate classes. now i have no syntax error but i do have a compiling error of -
    java.lang.NullPointerException
    at Test.main(Test.java:38)

    i cant understand the problem. it seems to me to be an out of bounds array error?? but my arrays have room in them so i cant seem to figure out what the real problem is. here is my updated code for testMethod -
    Java Code:
     public static void main(String args[])
        {
            int select;
            String title, artist;
            int playTime, tracks,count = 0;
            
            System.out.println("enter 1 for add new CD");
            System.out.println("enter 2 for add new DVD");
            System.out.println("enter 3 to calculate party time");
            System.out.println("enter 4 to calculate chosen time");
            System.out.println("enter 5 to print item list");
            System.out.println("enter 6 to quit");
            select = EasyIn.getInt();
            StorageSystem storage = new StorageSystem(10);
              switch (select) 
              {
                  
                case 1:
                //StorageSystem.itemList[StorageSystem.totalStored] = EasyIn.getString()
                System.out.println("enter title");
                title = EasyIn.getString();
                System.out.println("enter artist");
                artist = EasyIn.getString();            
                System.out.println("enter play time");
                playTime = EasyIn.getInt();
                System.out.println("enter number of tracks");
                tracks = EasyIn.getInt();
              //  StorageSystem.itemList[StorageSystem.totalStored].CD(artist,title,playTime,tracks); 
               System.out.println("result: ");
               //storage.StorageSystem(10);
               storage.cdList[count].title = title; 
               storage.cdList[count].artist = artist; 
               storage.cdList[count].playingTime = playTime;
               storage.cdList[count].numOfTracks = tracks;
               count =+ 1;
               storage.printList();
                break;
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                break;
            }
        }
    updated SystemStorage goes as follows
    Java Code:
    public class StorageSystem
    {
        public Video[] videoList;
    
        public int totalStored = 0;
        public CD[] cdList = new CD[10];
        int totalTime;
        int choosenTime;
        public StorageSystem(int totalIn)
        {
            totalStored = totalIn;
            videoList = new Video[totalStored];
       //     cdList = new CD[totalStored];
        }
        
        public void addCd(CD cdListIn)
        {
            cdList[totalStored] = cdListIn;
            totalStored += 1;
        }
        
        public void addVideo(Video videoListIn)
        {
            videoList[totalStored] = videoListIn;
            totalStored += 1;
        }
        
        public int calcPartyTime()
        {
         
            for(int i=0; i<totalStored; i++)
            {
                totalTime += videoList[i].playingTime;
                totalTime += cdList[i].playingTime;
            }
            
            return totalTime;
        }
        
        public int calcChoosenItemTime()
        {
         
            for(int i=0; i<totalStored; i++)
            {
                if(cdList[i].gotIt == true)
                 choosenTime += cdList[i].playingTime;       
                 
                if(videoList[i].gotIt == true)
                 choosenTime += videoList[i].playingTime;       
            }
            return choosenTime;
        }
        
        public void printList()
        {
            for(int i=0; i<totalStored; i++)
            {
                System.out.println(cdList[i].title);
            }
        }
    }
    cheers again for all your help!! :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You are trying to access objects in an array before they've been initialized. Yes, the array itself has been initialized, but the object it holds have not. Think of an array of objects as a parking lot. You can't drive any cars in the lot (use any objects), until you first put cars in the lot.

    Also, I'm not sure that you want to create two different arrays in your storage class, and I'm pretty sure that you shouldn't be adding data to it the way you're trying to do. Rather I would imagine that you should be using your StorageSystem addItem method to add data.

  7. #7
    Cian is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    i thought i would need 2 different arrays because class CD and Class Video have different attributes? when i use just plain old item i get syntax errors saying for example variable artist does not exist, but it is part of CD class which extends class item. but your right - in the UML diagram there is only one 'itemList'. having said that, many of the diagrams we have been given by our professor have been WRONG! or at least missing information..

    could you give me an example of how to add an item to an array then if you reckon im doing it wrong? obviously im not asking you to do my assignment here ;) just to point me in the right direction when it comes to calling methods from another class :)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Cian View Post
    i thought i would need 2 different arrays because class CD and Class Video have different attributes?
    They are both descended from Item, right? So an Item array can hold them both happily.


    when i use just plain old item i get syntax errors saying for example variable artist does not exist, but it is part of CD class which extends class item.
    If you still have this error, show us the code causing the error and the exact error message.


    but your right - in the UML diagram there is only one 'itemList'. having said that, many of the diagrams we have been given by our professor have been WRONG! or at least missing information..
    I think he's right this time.


    could you give me an example of how to add an item to an array then if you reckon im doing it wrong? obviously im not asking you to do my assignment here ;) just to point me in the right direction when it comes to calling methods from another class :)
    You already have this code! The addItem method has all that is needed to do this. What you must do is create an Item (or CD or Video) object first, then pass this object into a call to addItem, and the object has been added to the array.

  9. #9
    Cian is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    I GOT IT TO WORK!!! :D haha, why is it always the most stupid errors in the end??? the problem was i was changing totalStored (my counter) in my constructor to whatever i was inputing so my array was then going out of bounds. ya that took a few hours only to realise i had to delete but one line...
    i also changed the way i input the data to use the methods like you said. :)
    thanks for all your help man. reps! :)

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Calling a class within another class
    By BeeGee in forum Advanced Java
    Replies: 39
    Last Post: 06-11-2010, 09:54 AM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 03:48 AM
  3. Calling a class method from another class
    By caro in forum New To Java
    Replies: 4
    Last Post: 06-10-2009, 02:12 AM
  4. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 08:35 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM

Posting Permissions

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