Beginner - Need help getting array to work.

  • (8 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

115 Replies - 2441 Views - Last Post: 06 October 2011 - 09:12 PM Rate Topic: ***-- 2 Votes

#1 fired0g22  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 06-October 11

Beginner - Need help getting array to work.

Posted 06 October 2011 - 06:17 AM

I am new to java and this project needs an array. I have 3 different classes which are my main, a constructor, and an array class. The constructor and main work fine. The array is new to me. I am having trouble getting the input into the array and then having the data moved from the array to my main for output. Any help would be appreciated.

main
/* A application that calculates the inventory of an item, the value, and how many of that item is in stock */

import java.util.Scanner; // need to call the scanner before it can be used
public class InventoryProgram
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);


System.out.println("Inventory for cameras within the Electronics Department");
System.out.print("Please enter the name of a camera: ");
cam.setItem(input.nextLine());
string[] cname = new string [5];
for (string cname = ""; i < num.length; i++)


System.out.print("Please enter the product number: ");
cam.itemNumber(input.nextInt());

System.out.print("Please enter the price per single unit: ");
cam.price(input.nextDouble());

System.out.print("Please enter the number of cameras that are currently in stock: ");
cam.unitsAvail(input.nextInt());

System.out.println("Item Name: " + cam.displayItem());
System.out.println("Item Number: " + cam.itemNumber());
System.out.println("There are " + cam.unitsAvail() + " unit(s) available at $" + cam.price() + " each.");
System.out.println("The total value of this item in the " + cam.displayDept() + " department is $" + cam.inventValue());

} // end method main
} // end class InventoryProgram



Constructor class
class Camera extends InventoryProgram
{
private int item_number;
private int units_avail;
private double unit_price;
private double invent_value;
private String item_name;
public String dept_name;

//default constructor
public Camera()
{
item_number = 0;
units_avail = 0;
unit_price = 0;
invent_value = 0;
item_name = "";
dept_name = "Electronics";
} //end default constructor

//Parameterized Constructor
public Camera(int item_number, int units_avail, double unit_price, double invent_value, String item_name, String dept_name)
{
this.item_number = item_number;
this.units_avail = units_avail;
this.unit_price = unit_price;
this.invent_value = invent_value;
this.item_name = item_name;
this.dept_name = dept_name;
} //end constructor

public int itemNumber(int item_number)
{
this.item_number = item_number;
return item_number;
}

public int itemNumber()
{
return item_number;
}

public int unitsAvail(int units_avail)
{
this.units_avail = units_avail;
return units_avail;
}

public int unitsAvail()
{
return units_avail;
}

public double price (double unit_price)
{
this.unit_price=unit_price;
return unit_price;
}

public double price()
{
return unit_price;
}

public String setItem (String item_name)
{
this.item_name = item_name;
return item_name;
}

public String displayItem()
{
return item_name;
}

public String setDept (String dept_name)
{
this.dept_name = dept_name;
return dept_name;
}

public String displayDept()
{
return dept_name;
}

public double inventValue()
{
this.invent_value = (units_avail * unit_price);
return invent_value;
}

} // end of camera class 



Array class
public class Arrays extends InventoryProgram {
 
    Camera cam = new Camera();
          public Arrays(String[] args) {
          String[] product;              // declares an array of strings
 
          product = new String[6];      // allocates memory for 5 strings
             
          product[0] = cam.item_name; // initialize first element
          product[1] = cam.item_name; // initialize second element
          product[2] = cam.item_name; // etc.
          product[3] = cam.item_name;
          product[4] = cam.item_name;
          product[5] = cam.item_name;
 
          return product[0];
           
     }
}



I am just confused on how to get this to work. Examples would be helpful. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Beginner - Need help getting array to work.

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 06:53 AM

First of all why these two classes extends the main InventoryProgram class?
Second String is in capital S:
string[] cname = new string [5];
for (string cname = ""; i < num.length; i++)//what is this loop doing?

What the use of that loop?
Where did you declare cam in InventoryProgram class? cam.setItem(input.nextLine());
Those are among the error in main(). So the question is what were you trying to do here?
Was This Post Helpful? 1
  • +
  • -

#3 fired0g22  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 06-October 11

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 07:27 AM

View Postsmohd, on 06 October 2011 - 06:53 AM, said:

First of all why these two classes extends the main InventoryProgram class?
Second String is in capital S:
string[] cname = new string [5];
for (string cname = ""; i < num.length; i++)//what is this loop doing?

What the use of that loop?
Where did you declare cam in InventoryProgram class? cam.setItem(input.nextLine());
Those are among the error in main(). So the question is what were you trying to do here?



I do not know why I have both classes extending the main InventoryProgram. I thought that was how it was done. I guess I do not understand what "extends" means. I will remove those.
Second String is in capital S:
string[] cname = new string [5];
for (string cname = ""; i < num.length; i++)//what is this loop doing?

I think I left this in error. I was trying to get it to work myself and used an example from another code. That should all be removed I guess.
cam is declared in the Camera class, not the main. Does it need to be done in both?
Let me explain what the project needs to have done and we can go from there.

Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information about one product at a time, including the item number, the name of the product, the department of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.

Create a method to sort the array items by the name of the product.
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 07:32 AM

What he meant with String, is that it is a class and needs to be capitalized, should be String not string. Java is case sensitive. When you extend something that means that it is a sub class. What you are trying to to is go through inheritance so that you can create an object of one class in a different class. That is really easily done actually, as long as the classes are in the same folder or location, then you can create an object for it in a different class. You don't need extends.
Was This Post Helpful? 1
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 07:38 AM

First yes, they should not extends anything here.

Quote

cam is declared in the Camera class, not the main. Does it need to be done in both?

Not true, it is in Arrays class, and yes variables has their scopes.

Now according to your question, you are required to use array(and not necessarily have a class array) to allow the storage of many objects of Camera. So you will need an array to store objects of Camera(and not Constructor) created. macosxnerd101 has a good tutorial explain how to get what you need called moving away from parallel arrays. In this tut he shows how to store objects in array and retrieve their values.

Giving an idea: What you are going to do is having array of type Camera, then ask user for input(probably using loop since you will need many of items), then create an instance of the Camera according to inputted values and store it in array.
Was This Post Helpful? 1
  • +
  • -

#6 fired0g22  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 06-October 11

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 07:41 AM

View PostFuzzyness, on 06 October 2011 - 07:32 AM, said:

What he meant with String, is that it is a class and needs to be capitalized, should be String not string. Java is case sensitive. When you extend something that means that it is a sub class. What you are trying to to is go through inheritance so that you can create an object of one class in a different class. That is really easily done actually, as long as the classes are in the same folder or location, then you can create an object for it in a different class. You don't need extends.


Thank you Fuzzyness. Now I understand the "extends" and I will remove those now. I have read through so many forums and examples of how a array works and I get it. This is where I am getting stuck. I have my constructor with the variables in it that are entered via user input. I need my array to pull those variables in and store the data. Then at the end of the program once everything is entered, the array will spit out the stored data one item at a time. I am sooo confused.
Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 07:46 AM

As smohd said in his previous post, I think you may be confused by what she meant. You do not have to create an Arrays class, actually there is already one created in the API. What she meant is that you need to create an Array object. Camera[] camList = new Camera[length];
You will have it be a type of Camera and you will prompt the person for information, and using that information you will create a Camera object and add it to the Array that you created.

make sense?

This post has been edited by Fuzzyness: 06 October 2011 - 07:50 AM

Was This Post Helpful? 1
  • +
  • -

#8 fired0g22  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 06-October 11

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 07:51 AM

View Postsmohd, on 06 October 2011 - 07:38 AM, said:

First yes, they should not extends anything here.

Quote

cam is declared in the Camera class, not the main. Does it need to be done in both?

Not true, it is in Arrays class, and yes variables has their scopes.

Now according to your question, you are required to use array(and not necessarily have a class array) to allow the storage of many objects of Camera. So you will need an array to store objects of Camera(and not Constructor) created. macosxnerd101 has a good tutorial explain how to get what you need called moving away from parallel arrays. In this tut he shows how to store objects in array and retrieve their values.

Giving an idea: What you are going to do is having array of type Camera, then ask user for input(probably using loop since you will need many of items), then create an instance of the Camera according to inputted values and store it in array.


smohd, Thank you. So I am going to get rid of the constructor entirely? I was thinking I had to keep it in and build the array using the variables from the constructor. If I understand you correctly it makes more sense not to have it and have the information stored directly into the array. The last program we wrote had a loop and asked for user input but it only stored 1 value for the input and when the time came for output it gave the last users input. This one is much more complex since it stores multiple values of the same input and then we have to output all of it. This once has been a major challenge but I think I might have been the problem, over-thinking it. I will review those examples you told me to and see if I can learn anything there. Thank you so much.
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 07:57 AM

No, an array is not going to save one item in each(like name, price....) but it is going to save the complete object with its instance variables in one index of an array. That is why it should be of type Camera(read that tutorial, and you will get the concept).

So after asking all user inputs, you create your Camera object using those inputs and store that object in an array. For example, I will have something like:
Camera[] myList = new Camera[SIZE];// this is the array to store those objects
//get user inputs;
//then create object and store it like
myList[index] = new Camera(........);
// and so you loop for all other objects

Was This Post Helpful? 1
  • +
  • -

#10 fired0g22  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 06-October 11

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 08:05 AM

View PostFuzzyness, on 06 October 2011 - 07:46 AM, said:

As smohd said in his previous post, I think you may be confused by what she meant. You do not have to create an Arrays class, actually there is already one created in the API. What she meant is that you need to create an Array object. Camera[] camList = new Camera[length];
You will have it be a type of Camera and you will prompt the person for information, and using that information you will create a Camera object and add it to the Array that you created.

make sense?


Fuzzyness,
Your name fits because it does but it is still a little "fuzzy". Would I build the array in the class that now holds the constructor or does the program now have one one main class and that's it? Does the main take the input, build the array and give the output?

View Postsmohd, on 06 October 2011 - 07:57 AM, said:

No, an array is not going to save one item in each(like name, price....) but it is going to save the complete object with its instance variables in one index of an array. That is why it should be of type Camera(read that tutorial, and you will get the concept).

So after asking all user inputs, you create your Camera object using those inputs and store that object in an array. For example, I will have something like:
Camera[] myList = new Camera[SIZE];// this is the array to store those objects
//get user inputs;
//then create object and store it like
myList[index] = new Camera(........);
// and so you loop for all other objects


Is this making an array or an array list? Just curious because in my readings they are two different things right?
Was This Post Helpful? 0
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 08:06 AM

View Postfired0g22, on 06 October 2011 - 08:36 PM, said:

smohd, Thank you. So I am going to get rid of the constructor entirely?

I mean the class you call it constructor is called Camera class. Depending on the design, what is important is that we will have an array of type Camera to store our data, then ask user for input like normal, what we did in previous, except now the created object will be stored in array.

Quote

As smohd said in his previous post, I think you may be confused by what she meant
@Fuzzyness: I think you mean "what he meant"
Was This Post Helpful? 1
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 08:10 AM

You will only use 2 classes, the Camera class and the InventoryProgram Class. You will create the array inside of the Inventory class. When you create the array, it will be an Array of Camera objects.
Camera[] camList = new Camera[5];
That is what the Camera part of the statement means, giving the Array elements a type. You would then ask them for information needed to create a new Camera, since you have a constructor with no parameters I will use that as an example.
camList[0] = new Camera();
Normally, you would prompt them for the information and it would between the ( )
Now, when you print them, say you want to print the name of each, you would just create a loop and call the display_item() method. So would look like this:
for(int i = 0; i< camList.length; i++)
     // Print each item using the display_item method of camList[i]


Will iterate throughout the code and print each Camera's name with that method.

A bit clearer?

Edit -
@smohd, I assumed this was a school assignment and I meant that he might not have understood what his teacher wanted. I have a bad habit of referencing anyone I do not know as a female! ^^ My apologies if you thought I was calling you a female definitely didn't mean it like that!

This post has been edited by Fuzzyness: 06 October 2011 - 08:12 AM

Was This Post Helpful? 1
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 08:22 AM

Dont worry Fuzzyness :)

@fired0g22: May be you are taking the same class with the OP of this thread
Was This Post Helpful? 1
  • +
  • -

#14 fired0g22  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 06-October 11

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 08:24 AM

View PostFuzzyness, on 06 October 2011 - 08:10 AM, said:

You will only use 2 classes, the Camera class and the InventoryProgram Class. You will create the array inside of the Inventory class. When you create the array, it will be an Array of Camera objects.
Camera[] camList = new Camera[5];
That is what the Camera part of the statement means, giving the Array elements a type. You would then ask them for information needed to create a new Camera, since you have a constructor with no parameters I will use that as an example.
camList[0] = new Camera();
Normally, you would prompt them for the information and it would between the ( )
Now, when you print them, say you want to print the name of each, you would just create a loop and call the display_item() method. So would look like this:
for(int i = 0; i< camList.length; i++)
     // Print each item using the display_item method of camList[i]


Will iterate throughout the code and print each Camera's name with that method.

A bit clearer?

Edit -
@smohd, I assumed this was a school assignment and I meant that he might not have understood what his teacher wanted. I have a bad habit of referencing anyone I do not know as a female! ^^ My apologies if you thought I was calling you a female definitely didn't mean it like that!


Fuzzyness,
Yes that is a bit clearer thank you. Is there a way to display each item one at a time. I mean for example,

Item Name:
Item Number:
Units available:
<pause> - user needs to hit any key
Item Name:
Item Number:
Units available:
<pause> - user needs to hit any key
Item Name:
Item Number:
Units available:
<pause> - user needs to hit any key

??

View Postsmohd, on 06 October 2011 - 08:06 AM, said:

View Postfired0g22, on 06 October 2011 - 08:36 PM, said:

smohd, Thank you. So I am going to get rid of the constructor entirely?

I mean the class you call it constructor is called Camera class. Depending on the design, what is important is that we will have an array of type Camera to store our data, then ask user for input like normal, what we did in previous, except now the created object will be stored in array.

Quote

As smohd said in his previous post, I think you may be confused by what she meant
@Fuzzyness: I think you mean "what he meant"


Makes much more sense. Keeping the constructor is what was messing me up I think.
Was This Post Helpful? 0
  • +
  • -

#15 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Beginner - Need help getting array to work.

Posted 06 October 2011 - 08:26 AM

if you want to do that then you need to create what is called a toString method in the Camera class. basically what will happen is it will return a String, and you can have it be of all of the values. Something like this:
return "Item name: " + itemName + "\nItem Number: " + itemNumber + "\nUnits Available: " + units + "\n");
You create the format that you want it to be, and when you are in the for loop, instead of calling the display_item method you call the toString() method. Like: camList.toString();
Was This Post Helpful? 1
  • +
  • -

  • (8 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »