Page 1 of 4 123 ... LastLast
Results 1 to 20 of 70
Like Tree2Likes

Thread: Array question

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

    Default Array question

    OK Final project for the class, and of course it ask something of us that has not been covered in class.

    Passing an array as a parameter of a constructor.

    Here is my constructor:

    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;
    I am not sure what else to do here. I am a little confused on how I would add a author in my tester to put it into the next slot of the array?

    Textbook newTextbook = new Textbook(001, "Big Java", What to put here(?? authors[i] = Cay Horstmann ??), "John Wiley & Sons, Inc.", 129.99, 1)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Array question

    To pass an array just pass the name of the array, no brackets. If the array has not been declares you can pass it like this (assuming an array of Strings):

    new Textbook(001, "Big Java", new String[]{"Name1", "Name2", "Name3"}, "John Wiley & Sons, Inc.", 129.99.1}

    Since you are passing to an array you need to do it like that even if you only have one name.

    Regards,
    Jim
    jwood likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Array question

    this is the instance variable:

    private String[] authors;

    so like this:

    Java Code:
    public Textbook(String aId, String aTitle, String authors , String aPublisher, double aPrice, int aStatus)
            {
                id = aId;
                title = aTitle;
                publisher = aPublisher;
                price= aPrice;
          }

    and then

    Textbook newTextbook = new Textbook(001, "Big Java", new String[]{ Cay Horstmann}, "John Wiley & Sons, Inc.", 129.99, 1)
    Last edited by jwood; 08-02-2013 at 01:51 AM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Array question

    Not quite. In the method signature,you need to show you are passing an array so the brackets stay in the parameter. And your dynamic array is correct except the array elements need "" around them.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Array question

    Is this the correct ? So I need nothing about the array in the constructor body?

    Java Code:
    public Textbook(String aId, String aTitle, String[] authors , String aPublisher, double aPrice, int aStatus)
            {
                id = aId;
                title = aTitle;
                publisher = aPublisher;
                price= aPrice;
          }

    and then

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

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Array question

    That will give you an array with a single item. Is that what you want? If not you need to separate the names like the example in reply #2.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Array question

    Yeah, is Cay Horstman a single name or two last names?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    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
    Yeah, is Cay Horstman a single name or two last names?

    Regards,
    Jim
    That is a single name. The array stores the author of each book, and only the author...I don't know why, but that is directions, so it is for a single entry each time....some of these directions are very hard to follow.

    1) Develop a simple class that models textbooks. Each textbook is described by several instance fields:
    a. an ID as a String to identify the textbook,
    b. a title represented as a String to store the title of the textbook,
    c. a list of authors represented as an array of Strings to store author information,
    d. a publisher represented as a String to store the publisher information,
    e. a price represented as a double to store the price of a book,
    f. a status represented as an integer to store if the book is in INSTOCK or ORDERED.

    In addition to these fields, the class has the following constructors and methods:
    a. A parameterized constructor that initializes the attributes of a textbook.
    b. setPrice(double price) to change the price of a textbook.
    c. changeStatus(int newStatus) to change the status of a textbook to either INSTOCK or ORDERED. If an invalid status is passed as a parameter, the method prints an error message to the screen and sets the status to UNKNOWN.
    d. Accessor methods for all instance fields.
    e. toString() to return a neatly formatted string that contains all the information stored in the instance fields.

    For getAuthors(), the method returns a string of comma-separated author names.

    The class also includes three constants to set status values:
    a. INSTOCK has value 1 and indicates that the book is in stock.
    b. ORDERED has value 2 and indicates that the book has been ordered.
    c. UNKNOWN has value 0 and indicates that the book may be ordered or in stock.

    I think I understand this one for the most part, but the next one....
    1) Develop a simple class that models apparels including t-shirts, sweatshirts, and hats. The class has several instance fields:
    a. an ID as a String to identify the specific item,
    b. a category as a String to store the specific type of apparel,
    c. a name as a String to store the name of the apparel item,
    d. a size as a String to store the size of the apparel,
    e. a price represented as a double to store the price of the apparel item,
    f. a status represented as an integer to store if the item is in INSTOCK or ORDERED.
    Valid values for the size include "S", "M", "L", "XL", and "XXL" for shirts. Hats come in "S" and "M". Valid values for category is "T-Shirt", "Sweatshirt", and "Hat". If invalid values are entered, an error message must be printed and the size instance field must be set to "UNKNOWN". The same accounts for category information.

    In addition to these attributes, the class has the following constructors and methods:
    a. A parameterized constructor that initializes the attributes of an apparel item.
    b. setPrice(double price) to change the price of the apparel.
    c. changeStatus(int newStatus) to change the status of an apparel item to either INSTOCK or ORDERED. If an invalid status is passed as a parameter, the method prints an error message to the screen and sets the status to UNKNOWN.
    d. Accessor methods for all instance fields.
    e. toString() to return a neatly formatted string that contains all the information stored in the instance fields.

    The class also maintains three constants to change status values:
    a. INSTOCK has value 1 and indicates that the book is in stock.
    b. ORDERED has value 2 and indicates that the book has been ordered.
    c. UNKNOWN has value 0 and indicates that the book may be ordered or in stock.
    I am not exactly sure where the part in red would go....or when it should activate....my thought was in the constructor but it has me confused....

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

    Default Re: Array question

    And another question.... -textbooks : ArrayList<Textbook> how does that translate to an instance variable?

    because this does not seem right to me:

    private ArrayList<Textbook>textbook= new ArrayList<Textbook>();

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Array question

    In your constructor you need to assign the 'authors' parameter to the 'authors' attribute.
    Since they both have the same name then you need to use 'this.authors = authors' so the compiler knows you are assigning the parameter to the attribute.

    And it's an array of authors because a book can have multiple authors. Just in the example you've given there happens to be only the one.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Array question

    Quote Originally Posted by jwood View Post
    And another question.... -textbooks : ArrayList<Textbook> how does that translate to an instance variable?

    because this does not seem right to me:

    private ArrayList<Textbook>textbook= new ArrayList<Textbook>();
    Why does that not seem right?
    You've just declared an ArrayList of Textbooks...which is presumably what the model has asked for.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Array question

    Quote Originally Posted by Tolls View Post
    Why does that not seem right?
    You've just declared an ArrayList of Textbooks...which is presumably what the model has asked for.
    It just seemed to much for an instance variable ...IDK....I thought instance variables were "simpler" only including what would be on the left of an equals sign similar to declaring an array:


    private String[] authors;

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Array question

    I have in front of me at the moment a College class that contains a List<Candidates> representing the candidates applying for that college.

    How else would you represent that model? Presuming that list of text books is the stock for the book shop or library or whatever.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Array question

    Con someone explain this to me in really dumbed down terms? We have done nothing like this in class....I do not even know where to begin.

    a. getTextbooks() returns an ArrayList<Textbook> of all the textbook inventory (in-stock and ordered). This method must create a separate copy of the ArrayList before it returns the list. If there are no textbooks in the Bookstore, an empty list is returned.

    b. getTextbooks (String name) returns a list of Textbook objects whose title matches the specified name. For example, if called with "Java Programming" the method returns all textbooks with the title "Java Programming" as a new list. This method must create a new copy of an ArrayList that stores all the matched Textbook objects. If no books in the bookstore match the given name, an empty list is returned.

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

    Default Re: Array question

    Am I even close on what is being asked in the part in red?

    ) Develop a simple class that models apparels including t-shirts, sweatshirts, and hats. The class has several instance fields:
    a. an ID as a String to identify the specific item,
    b. a category as a String to store the specific type of apparel,
    c. a name as a String to store the name of the apparel item,
    d. a size as a String to store the size of the apparel,
    e. a price represented as a double to store the price of the apparel item,
    f. a status represented as an integer to store if the item is in INSTOCK or ORDERED.
    Valid values for the size include "S", "M", "L", "XL", and "XXL" for shirts. Hats come in "S" and "M". Valid values for category is "T-Shirt", "Sweatshirt", and "Hat". If invalid values are entered, an error message must be printed and the size instance field must be set to "UNKNOWN". The same accounts for category information.

    In addition to these attributes, the class has the following constructors and methods:
    a. A parameterized constructor that initializes the attributes of an apparel item.
    b. setPrice(double price) to change the price of the apparel.
    c. changeStatus(int newStatus) to change the status of an apparel item to either INSTOCK or ORDERED. If an invalid status is passed as a parameter, the method prints an error message to the screen and sets the status to UNKNOWN.
    d. Accessor methods for all instance fields.
    e. toString() to return a neatly formatted string that contains all the information stored in the instance fields.


    Java Code:
    /**			Parametized constructor
    			@param aId apparel id
    			@param aCategory category of apparel
    			@param aName name of apparel
    			@param aSize size of apparel
    			@param aPrice price of apparel
    			@param aStatus status of apparel
    			(Precondition: aCategory.equalsIgnoreCase("T-Shirt") || aCategory.equalsIgnoreCase("Sweatshirt")||
    								aCategory.equalsIgnoreCase("Hat"))
    			(Precondition: aCategory.equalsIgnoreCase("Hat") && aSize.equalsIgnoreCase("S") || aSize.equalsIgnoreCase("M"))
    			(Precondition: aCategory.equalsIgnoreCase("T-Shirt") || aCategory.equalsIgnoreCase("Sweatshirt")&&
    								aSize.equalsIgnoreCase("S") || aSize.equalsIgnoreCase("M")|| aSize.equalsIgnoreCase("L") ||
    								aSize.equalsIgnoreCase("XL") || aSize.equalsIgnoreCase("XXL"))
    		*/		
    		public Apparel(String aId, String aCategory, String aName, String aSize, double aPrice, int aStatus)
    		{
    			id = aId;
    			category = aCategory;
    			name = aName;
    			size = aSize; 
    			price = aPrice;
    			status = aStatus;		
    		}

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Array question

    In both cases the return type is ArrayList<Textbook>.

    In the first method (a) method simply create a copy of the current List and return that.

    The next method (b) is similar except you return a new list of which the contents are only those that have the exact title passed as an argument. If no title is found, simply return the empty list.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Array question

    so for method a

    public ArrayList<Textbook> getTextbooks()


    And how do you make a copy of an ArrayList...I have looked all through the API and can find no copyOf for ArrayList, only Arrays.....so I am at a loss or is it as simple as
    in the method body...
    {
    textbook = new ArrayList<Textbook>();
    return textbook;
    }

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Array question

    Quote Originally Posted by jwood View Post
    I have looked all through the API
    Not possible. Not in one lifetime anyways.

    Check out java.util.Collections

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Array question

    Quote Originally Posted by DarrylBurke View Post
    Not possible. Not in one lifetime anyways.

    Check out java.util.Collections

    db

    I specifically looked under ArrayList and Array

    and I said I looked all through, not through all lol :-)

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

    Default Re: Array question

    Quote Originally Posted by jwood View Post
    Can someone explain this to me in really dumbed down terms? We have done nothing like this in class....I do not even know where to begin.

    a. getTextbooks() returns an ArrayList<Textbook> of all the textbook inventory (in-stock and ordered). This method must create a separate copy of the ArrayList before it returns the list. If there are no textbooks in the Bookstore, an empty list is returned.

    b. getTextbooks (String name) returns a list of Textbook objects whose title matches the specified name. For example, if called with "Java Programming" the method returns all textbooks with the title "Java Programming" as a new list. This method must create a new copy of an ArrayList that stores all the matched Textbook objects. If no books in the bookstore match the given name, an empty list is returned.

    I have no idea if I am doing this right or not

    Java Code:
             public ArrayList<Textbook> getTextbooks()
            {
                textbook = new ArrayList<Textbook>();
                return textbook;
            }
            
            /**
            */
            public ArrayList<Textbook> getTextbooks(String name)
            {
                textbooks = new ArrayList<Textbook>();
                for(int i = 0; i < textbook.size(); i++)
                {
                    if(textbooks.getTitle(i).equals(name))
                        {
                            return textbooks.getTitle(i);
                        }
                }    
                return null;
            }
    Last edited by jwood; 08-03-2013 at 08:03 PM.

Page 1 of 4 123 ... LastLast

Similar Threads

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