This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes stuck on assignment. any help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Java in General
Bookmark "stuck on assignment. any help" Watch "stuck on assignment. any help" New topic

stuck on assignment. any help

farrukh zaheen

Joined: Jul 01, 2004
Posts: 8
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 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

Joined: Jul 08, 2003
Posts: 24189


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!

[Jess in Action][AskingGoodQuestions]
farrukh zaheen

Joined: Jul 01, 2004
Posts: 8
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 will i compare the color in the IF statement and collect all cars with the same color.

here is what i have so far (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

Joined: Apr 16, 2004
Posts: 305
Originally posted by farrukh zaheen:
...but what i dont understand 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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: stuck on assignment. any help
It's not a secret anymore!