• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiplication and ArrayList  RSS feed

 
Stacy Beery
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently working on a very basic program (created so I could use as I learn new things) which just prints a basic recipe. My goal at the moment is to take the size of the ingredients, which are stored in an arraylist, and multiply them by the servings. This way I change change the number of servings and it will update the amount of each ingredient needed. But I am having problem writing and implementing the correct method to make this work.

My basic recipe class with possible method:



And this is my recipe test class:



My recipe prints out just fine, I just don't understand how write the method in my recipe class or to make it work in the test program. I am looking for something that will point me in the right direction.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stacy,
Each recipe usually has a serving amount that it comes with maybe 4 or 2 or whatever, so you need a multiplier for the basic ingredient portions. Let's say your recipe makes 4 servings and you need 2, as you can easily see you need to half that, so you get a basic formula as (Needed Servings)/(recipe servings). This will give you what you need, but remember to use numbers that are not integer, as you have integer truncation problems and you'll only get whole numbers.
Les

Stacy Beery wrote:My recipe prints out just fine, I just don't understand how write the method in my recipe class or to make it work in the test program. I am looking for something that will point me in the right direction.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what you want is to be able to change the servings, and then it will multiply each necessary ingredient by the amount you want? The way I would do this is, use the servings instance variable, which can be set with a method, and then use a getIngredients method to return a list of ingredients with the desired amount needed.

Psuedocode:

This has the added benefit of the user not being able to mess with the original ArrayList. There are a lot more possible implementations but this seems like a good way.

Some feedback on your class design:

I think you're doing way too much in your test class, and not enough in your Recipe class.

This code, for example, should be in a toString method in the Recipe class.


Instantiation of fields should be done inside the class, not outside. For example, in Recipe:


Some sort of non-default constructor would be nice. Maybe a constructor to set the title at least.

All your instance variables should be private. The user should not be directly modifying them (see: encapsulation)
Instead, any instance fields that the user needs to access should have a getter method. Any that they need to directly modify should have a setter method. There are several advantages to this type of design:
1) You decide what information gets in/out, so you can do input validation before saving it/returning it.
2) This is expected Java standard. When writing code for others in the future, people expect getters/setters (not direct access to fields)
3) You control the setting/getting implementation. Sometimes you may want to just set/return the instance field, but sometimes you'll want to do something more with it. Input validation is an example of this, but definitely not the only one.

Another piece of advice, perhaps for after you get this working. You're using 2 ArrayLists, one for ingredient name, one for ingredient size. This seems like it's begging to be extracted to its own Ingredient class, with a field of size and name.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would start by implementing an Ingredient class that has quantity, units, and description.
Your Recipe class would have a List<Ingredient>.

For your multiplication, I would implement it inside a copy constructor. Both Ingredient and Recipe would need to implement a copy constructor that includes a multiplier.

What I'm suggesting here is that at no time will you change an existing recipe, but that you can create a new one from an existing one and a multiplier.


 
Stacy Beery
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for all the suggestions and advice. I know that getters, setters, and constructors are coming up next, but I haven't learned about them yet. As I learn about them, I am planning on changing the code to include. I have also considered creating an ingredient class especially since my goal is to be able to add new recipes to a "cookbook." I think all the suggestions will really help when I get that far, but I do have one more question concerning multiplying and ArrayList.

I understand that what is in my Arraylist is really an object. I have used a wrapper to make my list take an "int." I know that I need to write it so it unwraps before I can multiply it. I just don't understand how to write it to make it do so. Basically, in using Zachary's suggestion, I am taking one object at a time out of the list, unwrapping it so it becomes an int, multiply to get the new amount, and then placing it in a new list. Do I understand that right? And how do you write that so it will do it. I keep trying different things, but always run into having different types. I can't figure out how to write it so they have the same type and be able to do the math. I know that it has the ability to do autoboxing, if I write it the correct way.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer is a wrapper of int, but you don't need to worry about boxing/unboxing. Java 7 does that automatically for you, so something like myList.add(3); is fine, even though you're technically adding an int to an Integer list.
 
Stacy Beery
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but I am still lost at how to write the method for multiplying it.

I know that, for two int, I can do something like:



but I don't understand how to change that for the arraylist. I have seen several different examples such as using the for loop for arraylist:


This works for looping through, which I know I am going to have to do. But the int num is of course the wrong type to be able to write it.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can effectively treat Integers and ints the same when you're using lists like this. You can multiply an Integer and an int together, and then use the result as either an Integer or an int.

So when you're multiplying them, forget about the whole wrapper thing (at least for now).

Let's say we have a List of Integers to multiply by an int and then save in a List of integers. This line would be perfectly valid, for example:


(edit) If you want to see how autoboxing works for yourself, try out this code and see if it compiles:
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!