Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,365 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,434 people online right now. Registration is fast and FREE... Join Now!




Different arrays or an array of objects

 

Different arrays or an array of objects

crazygrandpa

3 Jul, 2009 - 07:44 AM
Post #1

New D.I.C Head
*

Joined: 6 Jun, 2009
Posts: 6

i would like to ask about
if once i stored the item into an array,
how do i take out the item that has been stored into the array and modify the details of the item
for example,

my array has the item of math book,
details is item number=A12, description = math,price=12.00,quantity=12
for above details was been stored into the array already,
now the problem is if i wan to amend the description to become history
how do i do?
can someone provide an example for take a look ?
thank

This post has been edited by pbl: 8 Jul, 2009 - 03:30 PM

User is offlineProfile CardPM
+Quote Post


ayman_mastermind

RE: Different Arrays Or An Array Of Objects

3 Jul, 2009 - 08:03 AM
Post #2

human.setType("geek");
Group Icon

Joined: 12 Dec, 2008
Posts: 1,807



Thanked: 92 times
Dream Kudos: 525
My Contributions
You have all those mentioned items in one array? I mean you cant have in the same array values of different types, such String(A12 and math), then price(double) and finally quantity(int). Maybe you mean an array for item numbers, an array for description, an array of prices etc... were the values at each index are respective to each other.

Furthermore to change the value of the item that has been stored in array for example the array is called items[]. let's say you want to replace the value of item number one that is at index 0 , simply what you do is the following:
CODE

items[0] = A14; // the previous value was A12,

This way the value is replaced directly...

If you want to replace the new value and store the old one you will have to store the old one into a new array(lets call it old[]) then we have:
CODE

old[0] = items[0]; // stores the old value in another array A12
items[0] = A14; // gives a new value to the item which is A14 instead of A12


I hope that was what you are looking for, hope this helps, good luck smile.gif

This post has been edited by ayman_mastermind: 3 Jul, 2009 - 08:04 AM
User is offlineProfile CardPM
+Quote Post

crazygrandpa

RE: Different Arrays Or An Array Of Objects

3 Jul, 2009 - 10:12 AM
Post #3

New D.I.C Head
*

Joined: 6 Jun, 2009
Posts: 6

QUOTE(ayman_mastermind @ 3 Jul, 2009 - 08:03 AM) *

You have all those mentioned items in one array? I mean you cant have in the same array values of different types, such String(A12 and math), then price(double) and finally quantity(int). Maybe you mean an array for item numbers, an array for description, an array of prices etc... were the values at each index are respective to each other.

Furthermore to change the value of the item that has been stored in array for example the array is called items[]. let's say you want to replace the value of item number one that is at index 0 , simply what you do is the following:
CODE

items[0] = A14; // the previous value was A12,

This way the value is replaced directly...

If you want to replace the new value and store the old one you will have to store the old one into a new array(lets call it old[]) then we have:
CODE

old[0] = items[0]; // stores the old value in another array A12
items[0] = A14; // gives a new value to the item which is A14 instead of A12


I hope that was what you are looking for, hope this helps, good luck smile.gif


by the way,i m using a class file to test the program
for example,inside my class file
i declared the array is Item[] itemArray = new Item[10];

the following is my class file:



public class Item {
private String itemNumber;
private String itemDescription;
private double sellPrice;
private int quantityOnhand;

Item() {
}

public Item(String code, String descript, double price, int quantity){
itemNumber = code;
itemDescription = descript;
sellPrice = price;
quantityOnhand = quantity;
}

public void setItemNumber(String code){
itemNumber = code;
}
public void setItemDescription(String descript){
itemDescription = descript;
}
public void setSellPrice(double price){
sellPrice = price;
}
public void setQuantityOnhand(int quantity){
quantityOnhand = quantity;
}

public String getItemNumber(){
return itemNumber;
}
public String getItemDescription(){
return itemDescription;
}
public double getsellPrice(){
return sellPrice;
}
public int getQuantityOnhand(){
return quantityOnhand;
}
}
somemore,the question is following below:

(a) Define the class Item to store the details of an item sold in the bookshop. Each object of Item should store the following information:
• Item number
• Item description
• Selling price
• Quantity on hand

Besides the set and get methods, Item class should also include:
• A no-argument constructor
• A constructor with parameters
• The method equals that returns true if two objects contain the same information.

(cool.gif Write a driver program / main method to test the various
operations of the class Item.


Create an application program that declares an array of 100 components of type Item. Your program should provide the following operations which the user may invoke from a menu:
• Add a new item into the array (duplicate entries are not allowed).
• Amend the details of an item.
• Search for an item by item number.
• Enter a sales transaction (the quantity on hand has to be reduced accordingly).
• Input stock received (the quantity on hand has to be updated accordingly).
• List details of all items – one item’s detail per line, with line numbering.

furthermore,can you help me to interpret it?
i m quite confused about the question..

This post has been edited by crazygrandpa: 3 Jul, 2009 - 10:15 AM
User is offlineProfile CardPM
+Quote Post

nick2price

RE: Different Arrays Or An Array Of Objects

3 Jul, 2009 - 11:28 AM
Post #4

D.I.C Lover
*****

Joined: 23 Nov, 2007
Posts: 1,296



Thanked: 119 times
My Contributions
Please edit your post so that your code is in code tags. Has your tutor really thought this one through?
QUOTE
• Add a new item into the array (duplicate entries are not allowed).

So he wants 100 individual items to be added to the Array through user input? Because the Array size is
QUOTE
an array of 100 components of type Item


And if you dont completely fill it up, you will just get a null pointer exception for any other operation you try.
User is offlineProfile CardPM
+Quote Post

Fuzzyness

RE: Different Arrays Or An Array Of Objects

3 Jul, 2009 - 11:30 AM
Post #5

Comp Sci Student
Group Icon

Joined: 6 Mar, 2009
Posts: 1,145



Thanked: 172 times
Dream Kudos: 150
My Contributions
Tell your teacher the most efficient way is to use an arraylist because you need to store objects of unknown Quantities.

ArrayList<Item> cart = new ArayList<Item>();

Hope this helps!
User is offlineProfile CardPM
+Quote Post

pbl

RE: Different Arrays Or An Array Of Objects

3 Jul, 2009 - 01:12 PM
Post #6

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,534



Thanked: 1125 times
Dream Kudos: 450
My Contributions
Please use a descriptive topic name (this is rule #5 of this forum) 90% of the posts here are about Java Problem
Also rule #4 says: code.gif

Both these rules were showned to you way you inserted a post/reply
User is online!Profile CardPM
+Quote Post

crazygrandpa

RE: Different Arrays Or An Array Of Objects

8 Jul, 2009 - 05:58 AM
Post #7

New D.I.C Head
*

Joined: 6 Jun, 2009
Posts: 6

QUOTE(pbl @ 3 Jul, 2009 - 01:12 PM) *

Please use a descriptive topic name (this is rule #5 of this forum) 90% of the posts here are about Java Problem
Also rule #4 says: code.gif

Both these rules were showned to you way you inserted a post/reply

thx for informing.
last time i didnt how to put the tag
now i got it
smile.gif
User is offlineProfile CardPM
+Quote Post

pbl

RE: Different Arrays Or An Array Of Objects

8 Jul, 2009 - 03:29 PM
Post #8

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,534



Thanked: 1125 times
Dream Kudos: 450
My Contributions
Please have more meaninfull topic name
Topic name renamed
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:44PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month