• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help: Iteratoring an Arraylist, Graphing profit.  RSS feed

 
Kai Viz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm trying to add a method to my code to iterate an arraylist for the make, model, and year of a car, then add that cars price to the profit. (This profit will be available in a graph format.)

I did the Import.java.util.Iterator; but it doesn't work. I get cannot find symbol - method Iterator()

Here is my code (so far)

 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


That is looking for a static iterator() method on the Cars class.
Presumably it doesn't have one.

I'm guessing you want to actually iterate over the carList, so you want to call the iterate() method on that instead.
 
Kai Viz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:

That is looking for a static iterator() method on the Cars class.
Presumably it doesn't have one.

I'm guessing you want to actually iterate over the carList, so you want to call the iterate() method on that instead.


Well. Don't I feel stupid now. Thank you much! Now to get back to work and add the profit and graphing parts. I'm sure I'll be back for more help!
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kai Viz wrote:Well. Don't I feel stupid now.

Don't worry we've all been there.
 
Liutauras Vilda
Marshal
Posts: 4657
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You still can improve a bit your variables naming.

1. Carlist should read as carList
2. Profit as profit
3. class name Cars should read as singular Car, as one instance refers to a one Car object.
4. All parameter variables in method addCar are also started with an upper case. Should start with lower.

Note: Class names - start with upper case. Variable names, methods with lower case - it is important.

5. Method name cardSoldout. The name does not make sense to me and does not reveal method purpose. Please try to come up with something better. And usually method should be verb, while class a noun.

6. Indentation is not consistent. You use two different styles:
Stick to a one and always follow that, unless you're told by a senior to use different one. In case of that, fully switch to that and forget previous one.
 
Liutauras Vilda
Marshal
Posts: 4657
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, why profit is int and where the variable Price comes from?

comment:
Have no idea what the lot is, most likely someone else won't know too. If when you write comments, spend decent amount of time and come up with clear comments so won't be more questions than answers. Actually questions shouldn't come up at all.
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks wrong
iterator<Cars> it = Cars.iterator();

Did you mean ?
iterator<Cars> it = Carlist.iterator();

Also, variable names, e.g. "Carlist", should begin with a lower case letter. And, "Cars" should be singular.
 
Kai Viz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Welcome to the Ranch.

You still can improve a bit your variables naming.

1. Carlist should read as carList
2. Profit as profit
3. class name Cars should read as singular Car, as one instance refers to a one Car object.
4. All parameter variables in method addCar are also started with an upper case. Should start with lower.

Note: Class names - start with upper case. Variable names, methods with lower case - it is important.

5. Method name cardSoldout. The name does not make sense to me and does not reveal method purpose. Please try to come up with something better. And usually method should be verb, while class a noun.

6. Indentation is not consistent. You use two different styles:
Stick to a one and always follow that, unless you're told by a senior to use different one. In case of that, fully switch to that and forget previous one.



I fixed my variables, that was definitely a mistake on my part and am grateful that you caught it for me!
5 - So, that one should actually be carSold. Another one is going to be "sold out". I want one to register as a sale, and then add profit to what I plan to make an ArrayList of profit to be graphed in a line graph
or histogram. Not sure which yet, that would be "carSold" . The carSoldOut will do the same thing, but also remove it from the ArrayList and replace it with a "sold out of this car" message.
6 - The indentation is done by my IDE (BlueJ) and is the way my instructor has it. This is my first class, so I honestly am not entirely sure what you mean by the indentation.

Liutauras Vilda wrote:Also, why profit is int and where the variable Price comes from?
comment:
Have no idea what the lot is, most likely someone else won't know too. If when you write comments, spend decent amount of time and come up with clear comments so won't be more questions than answers. Actually questions shouldn't come up at all.


profit is an int because it's going to be a number to be graphed. Price (now price) comes from the Car (formerly cars) class. If you look at the ArrayList carLot it's the 4th variable.
It's a lot of cars...it's called a car lot...I assumed this was obvious. Clearly I was mistaken and will have to fix this. Thanks!
 
Liutauras Vilda
Marshal
Posts: 4657
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you need to have a field in Car object which is called sold. So you could check if car is still on the market bugatti.isSold().
That method soldOut I still think is incorrect. Naming at least. Maybe sellCar, which would tell you the actual intense of the method.

I'd think, that your ArrayList is a market. Cars which are in that list are on the market. If you want to sell it, you check if car is sold, if not, then you look it up in ArrayList, find it, sell it (remove from the list, calculate profit), make car as sold out.

Remember, method should be small enough and do one and only one thing. If you need to calculate profit, consider of creating another method which does that.

What I was trying to say you about variables Profit, getModel, getYear - all these are undefined in class LotInventory, so you should be getting compilation error.
 
Kai Viz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Maybe you need to have a field in Car object which is called sold. So you could check if car is still on the market bugatti.isSold().
That method soldOut I still think is incorrect. Naming at least. Maybe sellCar, which would tell you the actual intense of the method.

I'd think, that your ArrayList is a market. Cars which are in that list are on the market. If you want to sell it, you check if car is sold, if not, then you look it up in ArrayList, find it, sell it (remove from the list, calculate profit), make car as sold out.

Remember, method should be small enough and do one and only one thing. If you need to calculate profit, consider of creating another method which does that.

What I was trying to say you about variables Profit, getModel, getYear - all these are undefined in class LotInventory, so you should be getting compilation error.


getModel() is a method in the Car class, and model is defined there. Same with getYear(). That is how I was taught to do it in the class I'm doing it in right now. If it's wrong, it's my teachers fault, and I'll have to fix it after this class (need to pass the class, after all!)

Profit will end up being defined I think in a third class, so I can use that to make another Arraylist for profit to graph. I just...need to figure out how to make a line graph to show it somehow....
 
Liutauras Vilda
Marshal
Posts: 4657
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kai Viz wrote:getModel() is a method in the Car class, and model is defined there.
Go back to your code and check how you wrote that method call there in your earlier post.
Kai Viz wrote:
You are missing () there. Beside that, you should invoke method on particular instance of the Car class - you failed to do that too. When you write code compile it often enough, every 5 or even 3 lines of code, so you'll catch errors earlier.

Kai Viz wrote:If it's wrong, it's my teachers fault
You are writing that code, so you are responsible for its correctness

Kai Viz wrote:I'll have to fix it after this class (need to pass the class, after all!)
I think you have to fix it before you pass. Currently your code doesn't even compile. Think about the class as about the place to grasp the topic very well, not about just passing it. You'll need that knowledge in your later modules and at work after all.

I'm sorry if I disappointed you, but I think you should step back a bit, and revise your code from the beginning. By following routine:
1. Create class
2. Create test class
3. Try instantiate created class in step 1
5. Add method to class from step 1
6. Call method in tester class, make sure it works and produces expected output.
7. Repeat from step 5 till you have all functionality done.
8. Go back to step 1 and create another class if needed and do all steps again.

Everything by little steps. And if you that way, better in my opinion is to get just a part of the program but correct, rather than "everything" but incorrect. Chin Up!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!