• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem involving Arraylists

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have been stuck for a while, cant seem to get what to do.

introduce a Bin class. Each object of this class represents
a bin in the warehouse. It has two instance variables: a String that contains
the bin name and an ArrayList<BinItem> that contains a BinItem for each
of the SKUs stored in the bin. It also has a method making it possible for us
to add a new SKU to the bin. Implement the Bin class by completing the definition.

Here' what I had, think my Arraylist<BinItem> is incomplete and incorrect so...

 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Noah, welcome to JavaRanch.

In the future, please UseCodeTags when posting code. I've added them for you this time.

In the getContents() method, can you describe in words what you want it to do? I think you've got the right idea there, but several details are off.
 
Noah Kim
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response. I don't really understand what your asking, sorry I'm kind of a newbie to JAVA, just starting it as a freshman in high school. The main problem is I also don't know how to put the correct syntax in the add(BinItem B), my main problem.
 
Noah Kim
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh wait I get what you mean, it needs to contain a BinItem for each
of the SKUs stored in the bin. It also has a method making it possible for us
to add a new SKU to the bin. ( for the getContents method)

Sorry for the late response.
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't told us what a BinItem is. It seems to relate to a SKU, but how exactly? Leaving that aside for now, it seems to me the most obvious way to implement getContents() would be to return myContents. Instead you have a loop. I can think of a reason why you might want a loop there, but I'm curious to know your reason.

New programers tend to get too wrapped up by syntax. You need to be able to state in plain English the steps you need to follow to accomplish each task. Take your loop. In English, it might read: for each integer in the bin name, add the integer to list. Are there integers in the String myName? If there were, would you want to add them to list? What is list anyway? That's the only time in the code it's even mentioned.

Figure out what you want to do first, and then worry about how to code it.
 
Noah Kim
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, BinItem is are all the items in a bin that share the same SKU. SKU is the code for the song. (This code is databasing a library of songs) Also regarding the loop, I actually meant just to loop it I just added the previous syntax because the textbook example had one so i thought it was necessary. It also has to contain a binitem for every SKU that is added to bin.
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still don't understand BinItem. What data does it contain? SKU seems to be one piece, and also maybe a collection of ... what? Maybe it's a list of songs that share the same code. You also call them items though. Are you using songs and items to mean the same thing?

I can't parse the meaning out of this sentence:

Noah Kim wrote:I actually meant just to loop it I just added the previous syntax because the textbook example had one so i thought it was necessary.



It sounds like you are cutting-and-pasting code that you don't understand. I know from long, painful experience that's unlikely to work! It's not impossible, mind you, but unlikely. Your first step should be to understand the code you are copying. After that making it work for your case will be much easier.
 
Noah Kim
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah... hehehe. I think I worked it out.


import java.util.*;
public class Bin
{

private String myName;
private ArrayList<BinItem> myContents;
public Bin( String name )
{
myName=name;
}
public String getName()
{
return myName;
}
public ArrayList<BinItem> getContents()
{
return myContents;
}

public void add( BinItem b )
{
myContents.add( b );
}
public String toString()
{
String s = "Bin " + myName + ":\n";

for ( BinItem b : myContents )
s += b + "\n";
return s;
}
}

 
Noah Kim
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait I got it, nevermind. Thanks for the help sir.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Noah Kim wrote:. . . I think I worked it out. . . .

And I don’t.
You don’t appear to be using the List.
You would find out if you had used the List because of the NullPointerException.
You have some bad practice in the toString method. Avoid using the + operator on Strings in multiple statements. Use a StringBuilder instead. Despite what it shows in many books, you should avoid the \n and \r escape sequences. One solution is to use String.format and the %n tag.

You didn’t use the code tags correctly; it says here how they work. But since your code was not indented, they would have made little difference.
 
reply
    Bookmark Topic Watch Topic
  • New Topic