Results 1 to 20 of 70
Thread: Array question
- 08-02-2013, 12:02 AM #1
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
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;
Textbook newTextbook = new Textbook(001, "Big Java", What to put here(?? authors[i] = Cay Horstmann ??), "John Wiley & Sons, Inc.", 129.99, 1)
- 08-02-2013, 01:32 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-02-2013, 01:49 AM #3
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
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.
- 08-02-2013, 01:57 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-02-2013, 02:18 AM #5
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
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]
- 08-02-2013, 02:59 AM #6
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.
- 08-02-2013, 03:03 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Array question
Yeah, is Cay Horstman a single name or two last names?
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-02-2013, 07:27 AM #8
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
Re: Array question
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.
- 08-02-2013, 08:52 AM #9
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
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>();
- 08-02-2013, 10:58 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 08-02-2013, 11:00 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
- 08-02-2013, 05:26 PM #12
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
- 08-02-2013, 05:43 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 08-02-2013, 05:49 PM #14
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
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.
- 08-02-2013, 09:41 PM #15
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
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; }
- 08-02-2013, 10:14 PM #16
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-02-2013, 11:06 PM #17
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
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;
}
- 08-02-2013, 11:10 PM #18
- 08-02-2013, 11:14 PM #19
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
- 08-03-2013, 02:02 AM #20
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
Re: Array question
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.
Similar Threads
-
Question about a 2d array example
By silverglade in forum New To JavaReplies: 8Last Post: 06-07-2011, 12:56 AM -
Array question..
By gerarda in forum New To JavaReplies: 14Last Post: 03-02-2011, 02:42 AM -
Question about array
By hei1233212000 in forum New To JavaReplies: 2Last Post: 09-18-2010, 04:55 PM -
Array question
By TaxpayersMoney in forum New To JavaReplies: 5Last Post: 06-11-2010, 02:41 AM -
Array question
By McChill in forum New To JavaReplies: 5Last Post: 02-20-2009, 03:18 AM
Bookmarks