Page 2 of 4 FirstFirst 1234 LastLast
Results 21 to 40 of 70
Like Tree2Likes

Thread: Array question

  1. #21
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array question

    Your method claims to return an ArrayList<TextBook> but it returns a simple String in line #16 instead; your compiler is not going to like that ... and besides: an ArrayList doesn't have a geTitle(...) method. and besides that, variable 'textbooks' is an empty ArrayList (see line #11) so the for loop in line #12 won't loop at all.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  2. #22
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Here are my classess: Textbook, Apparel, Bookstore, and a Tester. Both arraylist in the Bookstore class are suppose to contain the information in the Textbook and Apparel classes. I thought, because the type was Textbook, that as I added textbooks in the Textbook class, the Textbook ArrayList would "pick them up".

  3. #23
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array question

    Quote Originally Posted by jwood View Post
    I thought, because the type was Textbook, that as I added textbooks in the Textbook class, the Textbook ArrayList would "pick them up".
    Not unless you add those Textbook objects to that ArrayList<Textbook> object explicitly yourself. Nothing happens automagically in Java.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #24
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Here are the directions for the rest of the methods in the class.

    a. getTextbook(String id) returns the Textbook that matches the specified id. If there is no Textbook matching the given id, null is returned.
    b. getApparel()returns an ArrayList<Apparel> of all the apparel inventory (in-stock and ordered). This method must create a separate copy of the ArrayList before it returns the list. If there are no apparel items in the Bookstore, an empty list is returned.
    c. getApparel (String name) returns a list of Apparel objects whose title matches the specified name. For example, if called with "Argo T-shirt" the method returns all Apparel objects with the name "Argo T-shirt" as a new list. This method must create a new copy of an ArrayList that stores all the matched Apparel objects. If no apparel items in the bookstore match the given name, an empty list is returned.
    d. getApparelByCategory(String name)returns a list of Apparel objects of a particular category. For example, if called with " Sweatshirt " the method returns all Apparel objects that are Sweatshirt by category in a new list. This method must create a new copy of an ArrayList that stores all the matched Apparel objects. If no apparel items in the Bookstore match the given category name, an empty list is returned.
    e. getApparel (String id) returns the apparel item that matches the specified id. If there is no item matching the given id, null is returned.
    f. addTextbook(Textbook) adds a new Textbook to the inventory of the bookstore.
    g. addApparel(Apparel) adds a new Apparel to the inventory of the bookstore.
    h. buyTextbook(String id) removes a Textbook object from the list of textbooks of the Bookstore. If the id does not match any Textbook object in the list, an error message is printed to the screen.
    i. buyApparel(String id) removes an Apparel object from the list of apparel items of the Bookstore. If the id does not match any Apparel object in the list, an error message is printed to the screen.
    In f and g, I believe is where I would add the the textbooks and apparel.

    something like this in the body of the addTextbook method?

    Textbook.add(Textbook newTextbook = new Textbook(001, "Big Java", new String[]{ "Cay Horstmann"}, "John Wiley & Sons, Inc.", 129.99, 1);

  5. #25
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Hmmm....the diagram confuses me:

    +addTextbook (Textbook)

    Java Code:
    public void addTextbook(Class Textbook)
            {
                Textbook newTextbook = new Textbook();
                textbook.add(newTextbook);
            }
    would this be right? We have not learned about adding a class as a method parameter in another class, so not sure how to do it exactly.

  6. #26
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Quote Originally Posted by JosAH View Post
    Your method claims to return an ArrayList<TextBook> but it returns a simple String in line #16 instead; your compiler is not going to like that ... and besides: an ArrayList doesn't have a geTitle(...) method. and besides that, variable 'textbooks' is an empty ArrayList (see line #11) so the for loop in line #12 won't loop at all.

    kind regards,

    Jos

    How do I make a copy of the array list set up in the instance variable for these methods?

    These are the directions I got for the GA

    "You need to create a new ArrayList (a new copy of the ArrayList class) and populate it with elements that match the criteria. You then return the new ArrayList. This is accomplished by looping through the existing ArrayList and adding any matching elements to the new ArrayList within each iteration."


    But what ArrayList class??

    Here are the "diagrams for what I am suppose to do:

    Textbook
    -id : String
    -title : String
    -authors : String[]
    -publisher : String
    -price : double
    -status : int
    +INSTOCK : int
    +ORDERED : int
    +UNKNOWN : int
    +TextBook (String, String, String[], String, double, int)
    +setPrice(double)
    +changeStatus(int)
    +getId() : String
    +getTitle() : String
    +getAuthors() : String
    +getPublisher() : String
    +getPrice() : dobule
    +getStatus() : int
    +toString() : String


    Apparel
    -id : String
    -category : String
    -name : String
    -size : String
    -price : double
    -status : int
    +STOCK : int
    +ORDERED : int
    +UNKNOWN : int
    +Apparel (String, String, String, String, double, int)
    +setPrice(double)
    +changeStatus(int)
    +getId() : String
    +getCategory() : String
    +getName() : String
    +getSize() : String
    +getPrice() : double
    +getStatus() : int
    +toString() : String

    Bookstore
    -textbooks : ArrayList<Textbook>
    -apparel : ArrayList<Apparel>
    -name : String
    +Bookstore (String)
    +getName() : String
    +getTextbooks() : ArrayList<Textbook>
    +getTextbooks (String name) : ArrayList<Textbook>
    +getTextbook (String id) : Textbook
    +getApparel() : ArrayList<Apparel>
    +getApparel (String name) : ArrayList<Apparel>
    +getApparelByCategory (String name) : Arraylist<Apparel>
    +getApparel (String id) : Apparel
    +addTextbook (Textbook)
    +addApparel (Apparel)
    +buyTextbook (String id)
    +buyApparel (String id)

  7. #27
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Array question

    No. Here is how I interpret the situation.

    1. You need to create a Bookstore class. This holds the books and the apparel you can add.
    2. You need a Textbook class and an Apparel class.
    3. The Bookstore class has add (and other) methods for both text books and apparel.
    4. Your tester class should create a bookstore class and then exercise the various methods to add apparel and textbooks and so forth.

    So let's assume the bookstore also sells watches and you have a Watch class.

    Java Code:
    Bookstore bookstore = new Bookstore();
    Watch watch = new Watch( /* constructor params here */ );
    bookstore.addWatch(watch);
    In bookstore:
    Java Code:
    public void addWatch(Watch watch) {
          merchandise.add(watch); // this is up to you.
    }
    How you store watches (or text books or apparel) is up to you. Single lists, mutliple lists, etc. It all depends on how you want to design your Bookstore class and, more importantly, what the instructor expects. If the s/he says use an ArrayList then you must do so. But all this is invisible (or should be) to anyone who would use the Bookstore class. These are fundamental concepts in OO programming and I would hope that the instructor would have covered them.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #28
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    So something like this to add a textbook to the ArrayList in the bookstore?

    Java Code:
    private ArrayList<Textbook> textbook;
    
    public Bookstore(String aName)
    		{
    			name = aName;
    			textbook = new ArrayList<Textbook>();
    			apparel = new ArrayList<Apparel>();
    		}
    
    
    
    
             public void addTextbook(Class Textbook)
    		{
    			Textbook newTextbook = new Textbook();
    			textbook.add(newTextbook);
    		}

  9. #29
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    What has been covered in this class and what is expected on these projects are not the same thing. For array list we learned basic syntax, .add, .remove, .set, and .size. We did not learn anything about on copying ArrayList, using ArrayList as a parameter, and adding one Class to the ArrayList in another class. Chapters 1-8 of a 23 chapter book....and to tell you just how backward it is put together...chapter 8 is about discovering classes.....so I am not sure at this point if I have been taught the fundamentals since the semester ends with learning about classes and packages....kinda think that should have been at the beginning considering the projects we have been assigned.....

  10. #30
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Hmm...in my tester I get 11 errors in the param of the textbook class call.....I don't think I understand it.....

    Java Code:
    public class BookStoreTester
    	{
    		public static void main(String[] args)
    		{
    			Bookstore bookstore = new Bookstore("UWF Bookstore");
    			Textbook newTextbook = new Textbook(String aId, String aTitle, String[] authors , String aPublisher, double aPrice, int aStatus);
    			bookstore.addTextbook(001, "Big Java", new String[]{ "Cay Horstmann"}, "John Wiley & Sons, Inc.", 129.99, 1);
    			
    		}
    	}
    and I am also getting an error on textbook.add(newTextbook); saying it cannot find symbol....so I seem to be calling the array list wrong as well....

  11. #31
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    I also tried this in the tester

    Java Code:
        public class BookStoreTester
        {
            public static void main(String[] args)
            {
                Bookstore bookstore = new Bookstore("UWF Bookstore");
                Textbook newTextbook = new Textbook(001, "Big Java", new String[]{ "Cay Horstmann"}, "John Wiley & Sons, Inc.", 129.99, 1);
                bookstore.addTextbook(newTextbook);
                
    
    
            }
        }
    only to get this:

    BookStoreTester.java:17: error: no suitable constructor found for Textbook(int,String,String[],String,double,int) ERROR LINE 6
    Textbook newTextbook = new Textbook(001, "Big Java", new String[]{ "Cay Horstmann"}, "John Wiley & Sons, Inc.", 129.99, 1);
    ^
    constructor Textbook.Textbook(String,String,String[],String,double,int) is not applicable
    (actual argument int cannot be converted to String by method invocation conversion)
    constructor Textbook.Textbook() is not applicable

  12. #32
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Array question

    The addTextbook method is wrong. Did you see the Watch example I provided?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #33
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Array question

    Do you know what that means? Compare the types of the constructor to what you are passing. See any problems?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #34
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Quote Originally Posted by jim829 View Post
    Do you know what that means? Compare the types of the constructor to what you are passing. See any problems?

    Regards,
    Jim

    Not really


    (String aId, String aTitle, String[] authors , String aPublisher, double aPrice, int aStatus)

    (001, "Big Java", String[]{ "Cay Horstmann"}, "John Wiley & Sons, Inc.", 129.99, 1)



    Ohhhhhhh.....just saw String when it was suppose to be int for aId!

    Was that it?

  15. #35
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Array question

    Write the types of the constructor (i.e. String, int, etc) above the types of the passed arguments to the constructor and see if they are the same. If the aren't (and they aren't) make the appropriate correction(s).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #36
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    OK addTextbook...I think I got it now, at least it compiles

    Java Code:
    public void addTextbook(Textbook newTextbook)
    		{
    			textbook.add(newTextbook);
    		}

    Textbook newTextbook = new Textbook("TB1", "Big Java", String[]{ "Cay Horstmann"}, "John Wiley & Sons, Inc.", 129.99, 1);

    public Textbook(String aId, String aTitle, String[] authors , String aPublisher, double aPrice, int aStatus)

    I am not sure where it is wrong?

  17. #37
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    I am thinking I do not know how to do the whole array in a parameter thing...bleh.....

    Java Code:
    public Textbook(String aId, String aTitle, String[] authors , String aPublisher, double aPrice, int aStatus)
            {
                id = aId;
                title = aTitle;
                publisher = aPublisher;
                price = aPrice;
                status = aStatus;
                            
            }
    
    
    
    Textbook newTextbook = new Textbook("TB1", "Big Java", String[]{ "Cay Horstmann"}, "John Wiley & Sons, Inc.", 129.99, 1);
    +TextBook (String, String, String[], String, double, int)


    everything after Textbook newTextbook = new Textbook("TB1", "Big Java", String[] has an error
    Last edited by jwood; 08-04-2013 at 02:25 AM.

  18. #38
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Array question

    The first error was that you were passing 001 as a String. You have corrected that. The error above was that you forgot the new keyword in from of the String[] (but only in the instantiation, not the constructor).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  19. #39
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    Quote Originally Posted by jim829 View Post
    The first error was that you were passing 001 as a String. You have corrected that. The error above was that you forgot the new keyword in from of the String[] (but only in the instantiation, not the constructor).

    Regards,
    Jim
    so that will add it to the array in the textbook class, not create a new array right?

  20. #40
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Array question

    How would I add a second book to the ArrayList in the tester...just repeat?

    I thought something like this would work...

    bookstore.addTextbook(newTextbook("TB2", "Natural-Born Cyborgs", new String[]{"Andy Clark"}, "Oxford University Press", 59.99, 1));

    or this

    bookstore.addTextbook("TB2", "Natural-Born Cyborgs", new String[]{"Andy Clark"}, "Oxford University Press", 59.99, 1);

    but neither work

Page 2 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. Question about a 2d array example
    By silverglade in forum New To Java
    Replies: 8
    Last Post: 06-06-2011, 11:56 PM
  2. Array question..
    By gerarda in forum New To Java
    Replies: 14
    Last Post: 03-02-2011, 01:42 AM
  3. Question about array
    By hei1233212000 in forum New To Java
    Replies: 2
    Last Post: 09-18-2010, 03:55 PM
  4. Array question
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 06-11-2010, 01:41 AM
  5. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 02:18 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
  •