Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

stuck on assignment. any help

 
farrukh zaheen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there...this is my first time posting here


I am stuck on a little part of my assignment.

I have an array cars[i] which stores info about cars using NewCar(String model, int price, String color, int year) class and UsedCar(String model, int year, int price, boolean rusty) class.

Now i have another class called CarDealerShip .....in which i need to implement a method that takes in parameter color for cars[] and returns all cars that have a particular color (as stated int he parameter) here is the method below.....i am unable to implement it:

public Car[] getCars(String color)






How will i do that?? can anyone help me??
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Well, one way to do it would be to create a collection -- say a java.util.ArrayList -- and then write a for loop whiich iterates over the array of Cars and stores each one of the approriate color into the collection. Then at the end, you can use the collection's "toArray()" method to create an array contining just those Cars ( you'll have to pass a new Car[] as an argument to toArray, or it will return an Object[].

Good luck!
 
farrukh zaheen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply :-)


Yes i can use the for loop that iterates over the length of the array :

for (int i=0; i< cars.length; i++)


but what i dont understand is....how will i compare the color in the IF statement and collect all cars with the same color.

here is what i have so far
http://www.geocities.com/tauseef81/TauseefAssignment.zip (if u want to look at it...right click and save target as)


....i am done with the asignment...its just that i cant figure out this last method implementation. Any help will be appreciated.
Thank you.
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by farrukh zaheen:
...but what i dont understand is....how will i compare the color in the IF statement and collect all cars with the same color.


The color is represented by a String, so in this case, you will be comparing Strings. Remember, you do not want to use the == operator to do this, you'll want to look at the equals() method.

Now, to continue using the ArrayList scheme, you can create a temporary ArrayList which will represent all cars found to be the same color (as you find a car which matches the color, add it to the ArrayList). Once your loop is finished, you return this temporary ArrayList.

Good Luck.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic