Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default help to write a function

    Hello;
    in the beginning i'm sorry for my weak language !!1

    I have aquestion, I"m trying to write a program, but I have a problem with a method.
    the method should do:
    /**
    * This method creates and adds new Race Car to Race Cars array.
    * @param licensePlate
    * @param year
    * @param manufacturer
    * @param model
    * @param colour
    * @param numDoors
    * @param topSpeed
    * @param totalRaces
    * @return true if new car added successfully to race cars array,
    false if array has no room for more cars.
    */

    I wrote he method but It doesn't work correctly and doesn't add new RaceCar to the array, this is my code:

    PHP Code:
    public boolean addCarToArray(int licensePlate,int year, String 
    			manufacturer, String model,Color colour,int numDoors, double 
    			topSpeed, int totalRaces) {
    		if (carsArr.length<MAX_ARRAYS_LENGTH){
    			for (current_Length_Car_Array=0;current_Length_Car_Array<
    			MAX_ARRAYS_LENGTH;current_Length_Car_Array++){
    				if (carsArr[current_Length_Car_Array]==null){
    					carsArr[current_Length_Car_Array].getLicensePlate();
    					carsArr[current_Length_Car_Array].setLicensePlate(licensePlate);
    					carsArr[current_Length_Car_Array].getYear();
    					carsArr[current_Length_Car_Array].setYear(year);
    					carsArr[current_Length_Car_Array].getManufacture();
    					carsArr[current_Length_Car_Array].setManufacture(manufacturer);
    					carsArr[current_Length_Car_Array].getModel();
    					carsArr[current_Length_Car_Array].setModel(model);
    					carsArr[current_Length_Car_Array].getColor();
    					carsArr[current_Length_Car_Array].setColor(colour);
    					carsArr[current_Length_Car_Array].getNumDoors();
    					carsArr[current_Length_Car_Array].setNumDoors(numDoors);
    					carsArr[current_Length_Car_Array].getTopSpeed();
    					carsArr[current_Length_Car_Array].setTopSpeed(topSpeed);
    					return true;
    				}
    				else return false;
    			}			
    		}
    		return false;
    	 }

  2. #2
    sick_peng is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    im new to the forums but my suggestion here is to post your "whole" code and not just the method. i really cant tell whats going on with the program if i cant see the rest of it.

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    you're going about this all wrong. here are a few reasons why:

    1. you are looping through your whole array to find the first null object
    2. you are calling methods on an object that is null
    3. you are calling methods on an object and not doing anything with its values

    here are some tips to not do these things:

    1. have an instance variable that already knows the next empty index.
    2. don't call a method on a null object.
    3. if you call a method on an object, at least do something with it, or don't do it at all.

  4. #4
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    Thank you for your replying, my code contain 6 classes
    Last edited by alrebatsd; 04-20-2009 at 04:36 PM.

  5. #5
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    thank You emceenugget, but I doesn't understand you cleary, Can you give a simple example how it should be ?

    Thanks alot :)

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    without totally giving away what i was hinting at, say you have the following:

    Java Code:
    class x {
    
       object arr[];
       int next;
    
       add(object) {
          arr[next] = object;
       }
    }
    where somewhere you need to change the value of next to remember where you need to insert the next object instead of having to find it each time.

  7. #7
    sick_peng is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    in my experience in class what gets the job done works =) this may or may not help im not sure but right here.

    public boolean addCarToArray(int licensePlate,int year, String
    manufacturer, String model,Color colour,int numDoors, double
    topSpeed, int totalRaces)

    you have identified the object to have everything that you said plus the (int totalRaces) at the end. but in your addCartoArray function you end it with topSpeed.



    if (carsArr.length<MAX_ARRAYS_LENGTH){
    for (current_Length_Car_Array=0;current_Length_Car_Arr ay<
    MAX_ARRAYS_LENGTH;current_Length_Car_Array++){
    if (carsArr[current_Length_Car_Array]==null){
    carsArr[current_Length_Car_Array].getLicensePlate();
    carsArr[current_Length_Car_Array].setLicensePlate
    (licensePlate);
    carsArr[current_Length_Car_Array].getYear();
    carsArr[current_Length_Car_Array].setYear
    (year);
    carsArr[current_Length_Car_Array].getManufacture();
    carsArr[current_Length_Car_Array].setManufacture
    (manufacturer);
    carsArr[current_Length_Car_Array].getModel();
    carsArr[current_Length_Car_Array].setModel
    (model);
    carsArr[current_Length_Car_Array].getColor();
    carsArr[current_Length_Car_Array].setColor
    (colour);
    carsArr[current_Length_Car_Array].getNumDoors();
    carsArr[current_Length_Car_Array].setNumDoors
    (numDoors);
    carsArr[current_Length_Car_Array].getTopSpeed();
    carsArr[current_Length_Car_Array].setTopSpeed
    (topSpeed);
    return true;

    }
    else return false;
    }


    my best guess to make this code work without changing everything is to just add a

    carsArr[current_Length_Car_Array].totalRaces();
    carsArr[current_Length_Car_Array].totalRaces
    (totalRaces);

    or something of that nature... it seems that your loop is not even getting to the end because the system is looking to add one more item in order to totally identify it in the array and because of that it isn't adding it to the array.

    hope this helps

  8. #8
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    You mean that the fun shouldn't loop on the whole array ?
    The name of the object is RaceCar, so I should add this rows? :

    PHP Code:
    RaceCar arr[];
    		int next;
    		add (RaceCar){
    			arr[next]=RaceCar;
    		}
    Sorry My friend, if I doesn't mean What you want to say :(

  9. #9
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    sick_peng thank you, I added the:
    carsArr[current_Length_Car_Array].getRaces();
    carsArr[current_Length_Car_Array].setRaces
    (totalRaces);


    but it doesn't work too :(

    and how it loop without end if i limited it to loop 50 times (MAX_ARRAYS_LENGTH =50, it is a final variable )

  10. #10
    sick_peng is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    IMO what he is saying is all about efficiency and the normal way to go about it. i would say you need to try and fix your code as it stands. obviously if your addRaceCarDriver method is working and your addRaceCar method isn't but they are wrote the same way then there is a logical error somewhere. try what i replied in an earlier post and see if that works. afterwords do what he is saying. his answer did not seem to correct your code at all he just said your doing it wrong... lol well duh =) thats why your here. idk if what i said was correct because i cant compile your program w/o your input.txt but dont worry about it.. if what i said earlier works great you got you HW done !!! =) afterwords you can worry about making it efficient.

    but for future projects that involve you doing the same thing, take his advice. i think you have way to much written on this code to go back and redo everything. depending on the due date....

  11. #11
    sick_peng is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by alrebatsd View Post
    sick_peng thank you, I added the:
    carsArr[current_Length_Car_Array].getRaces();
    carsArr[current_Length_Car_Array].setRaces
    (totalRaces);


    but it doesn't work too :(

    and how it loop without end if i limited it to loop 50 times (MAX_ARRAYS_LENGTH =50, it is a final variable )
    or the loop isn't ending and it is just geting the wrong inpute..

    ie. system is looking for an input (int)
    you give it the input (string)

    return false;

    restarts the loop and finds everything after that false.

    *shrugs shoulders*

    also is your input.txt in the correct format??? does it have ur totalRaces in the right spot?
    Last edited by sick_peng; 04-15-2009 at 08:07 PM.

  12. #12
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    Hello,

    obviously if your addRaceCarDriver method is working and your addRaceCar method isn't
    The addRaceCarDriver method too doesn't work correctl, but I ask about one method to solve the problem in the two methods.

    the input.txt is in he coorect format, and here the input:

    addRaceDriverToArray A22XY234 Jenson Button British 19/01/1980 BrawnGP 0 242 2
    addRaceDriverToArray B33ZB435 Rubens Barrichello Brazilian 23/05/1972 BrawnGP 2 538 9
    addRaceDriverToArray C44WE765 Lewis Hamilton British 07/01/1985 McLaren 1 213 9
    addRaceDriverToArray D55FG556 Timo Glock German 18/03/1982 Toyota 0 32 2
    addRaceDriverToArray E66VB958 Jarno Trulli Italian 13/07/1974 Toyota 1 214 3
    addRaceDriverToArray F77HJ567 Kimi Rהikkצnen Finnish 17/10/1979 Ferrari 3 531 17
    addRaceDriverToArray G88LK545 Felipe Massa Brazilian 25/04/1981 Ferrari 0 298 11
    addRaceDriverToArray H99KU865 Sebastian Vettel German 03/07/1987 RedBull 2 538 9
    addRaceDriverToArray I10UT546 Nick Heidfeld German 10/05/1977 BMWSauber 0 200 7
    addRaceDriverToArray J20DF763 Giancarlo Fisichella Italian 14/01/1973 ForceIndia 0 267 3
    addRaceDriverToArray K30SD544 Nelsinho Piquet Brazilian 25/07/1985 Renault 0 19 1
    addRaceDriverToArray L40AS457 Adrian Sutil German 11/01/1983 ForceIndia 0 36 1
    addRaceCarToArray 1123408 2008 Mercedes-Benz FO108W 0 0 0 2 250 24
    addRaceCarToArray 2265607 2007 Mercedes-Benz FO107W 0 0 0 2 255 17
    addRaceCarToArray 3368706 2006 Mercedes-Benz FA109K 0 255 0 3 300 20
    addRaceCarToArray 4467207 2007 Toyota RVX-07 255 255 255 3 270 12
    addRaceCarToArray 5543307 2007 Toyota RVY-07 0 0 255 2 360 34
    addRaceCarToArray 6643309 2009 Ferrari Type-056 0 255 0 2 370 10
    addRaceCarToArray 7711108 2008 Ferrari Type-024 0 0 255 2 290 17
    addRaceCarToArray 8822205 2005 Renault RS27 255 255 255 2 280 50
    addRaceCarToArray 9933306 2006 BMW P86/9 0 255 0 3 310 42
    addRaceCarToArray 1044407 2007 Mercedes-Benz FE1043L 0 0 255 2 270 11
    addRaceCarToArray 2055508 2008 Renault RK21 0 0 0 3 380 27
    addRaceCarToArray 3066605 2007 Mercedes-Benz VJM02 255 255 255 2 320 12
    addRaceCarToDriver A22XY234 1123408
    addRaceCarToDriver B33ZB435 2265607
    addRaceCarToDriver C44WE765 3368706
    addRaceCarToDriver D55FG556 4467207
    addRaceCarToDriver E66VB958 5543307
    addRaceCarToDriver F77HJ567 6643309
    addRaceCarToDriver G88LK545 7711108
    addRaceCarToDriver H99KU865 8822205
    addRaceCarToDriver I10UT546 9933306
    addRaceCarToDriver J20DF763 1044407
    addRaceCarToDriver K30SD544 2055508
    addRaceCarToDriver P30SD544 2055508
    addRaceCarToDriver I10UT546 8933306
    getDriverRaceCar A22XY234
    getDriverRaceCar K30SD544
    getDriverRaceCar L40AS457
    getDriverRaceCar K40AS457
    findTheBestRaceDriver
    getSortedRaceCars

    and this is the output.txt :

    addRaceCarToDriver returns: Failed to add car 1123408 to driver A22XY234
    addRaceCarToDriver returns: Failed to add car 2265607 to driver B33ZB435
    addRaceCarToDriver returns: Failed to add car 3368706 to driver C44WE765
    addRaceCarToDriver returns: Failed to add car 4467207 to driver D55FG556
    addRaceCarToDriver returns: Failed to add car 5543307 to driver E66VB958
    addRaceCarToDriver returns: Failed to add car 6643309 to driver F77HJ567
    addRaceCarToDriver returns: Failed to add car 7711108 to driver G88LK545
    addRaceCarToDriver returns: Failed to add car 8822205 to driver H99KU865
    addRaceCarToDriver returns: Failed to add car 9933306 to driver I10UT546
    addRaceCarToDriver returns: Failed to add car 1044407 to driver J20DF763
    addRaceCarToDriver returns: Failed to add car 2055508 to driver K30SD544
    addRaceCarToDriver returns: Failed to add car 2055508 to driver P30SD544
    addRaceCarToDriver returns: Failed to add car 8933306 to driver I10UT546

    getDriverRaceCar returns:
    driver A22XY234 don't have a race car yet!

    getDriverRaceCar returns:
    driver K30SD544 don't have a race car yet!

    getDriverRaceCar returns:
    driver L40AS457 don't have a race car yet!

    getDriverRaceCar returns:
    driver K40AS457 don't have a race car yet!

    findTheBestRaceDriver returns:
    null

  13. #13
    sick_peng is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    XD woulda been nice to know that its a problem with both haha now i feel dumb aight ima see if i can try anything.

  14. #14
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    I'm sorry for this feeling :cool: , And I should thank you for your help trying :)

  15. #15
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    There's no one have suggestion how to solute this problem ?! :(

  16. #16
    sick_peng is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by alrebatsd View Post

    addRaceCarToDriver returns: Failed to add car 1123408 to driver A22XY234
    addRaceCarToDriver returns: Failed to add car 2265607 to driver B33ZB435
    addRaceCarToDriver returns: Failed to add car 3368706 to driver C44WE765
    addRaceCarToDriver returns: Failed to add car 4467207 to driver D55FG556
    addRaceCarToDriver returns: Failed to add car 5543307 to driver E66VB958
    addRaceCarToDriver returns: Failed to add car 6643309 to driver F77HJ567
    addRaceCarToDriver returns: Failed to add car 7711108 to driver G88LK545
    addRaceCarToDriver returns: Failed to add car 8822205 to driver H99KU865
    addRaceCarToDriver returns: Failed to add car 9933306 to driver I10UT546
    addRaceCarToDriver returns: Failed to add car 1044407 to driver J20DF763
    addRaceCarToDriver returns: Failed to add car 2055508 to driver K30SD544
    addRaceCarToDriver returns: Failed to add car 2055508 to driver P30SD544
    addRaceCarToDriver returns: Failed to add car 8933306 to driver I10UT546

    getDriverRaceCar returns:
    driver A22XY234 don't have a race car yet!

    getDriverRaceCar returns:
    driver K30SD544 don't have a race car yet!

    getDriverRaceCar returns:
    driver L40AS457 don't have a race car yet!

    getDriverRaceCar returns:
    driver K40AS457 don't have a race car yet!

    findTheBestRaceDriver returns:
    null
    at first glace your addRaceCarToDriver is the problem but i don't think that that is it...

    your array's have 8 params , so when you try to add the car to the driver it doesn't work because it does not know what you are trying to create... basically you have made the race driver class but it is never used and this is were you are trying to use it...

    it seems you need to set the output of the addracecartodriver as a RaceDriver() if you do it right you will have all of the correct params. now that you are actually using your racedriver class you probably need to set this into its own array, and set the get race car driver to look at that array....

    this is the way i think that it would be handled with your current code....

    the only problem is..... how is ur array using the method's if it isn't classified as that class.... idk... i set your input file into my src and it didn't read it soo i can't exactly see what you are seeing... i know what i said won't work but.... food for thought?? mabye if you think along these lines???

  17. #17
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    we also get a ready class that we should add it, the class name is vehicle.jar, I import it, and I attached it for you to see it
    Attached Files Attached Files

  18. #18
    sick_peng is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    idk man.... im sorry im really not the guy to talk to haha... if it isn't due today, i would talk to your teacher about it cause he can give you direct help. otherwise w8 4 someone else to help im just trying as best as i can.... im a first year student as well lol

  19. #19
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    Thank you for trying :)

    i'm waiting for any one can help me in this work !! ?

  20. #20
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. function
    By nanna in forum New To Java
    Replies: 1
    Last Post: 11-17-2008, 09:20 PM
  2. Need help with get function
    By calicocal in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 07:59 PM
  3. Need a little help with a function!
    By Nuluvius in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 11:33 PM
  4. I want to add function
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:25 AM
  5. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •