Hi, I need to simulate a drag race between 4 cars ( a total of 3 races) to determine a winner based on the ratio between weigh/horsepower and I'm having trouble creating a determineWinner method. Here's my code so far:

Track class:

import java.util.*;
import java.text.*;

/*
Author: Joey Turner
Date: April 23rd, 2010
Purpose: For this lab we will simulate a race using 3 seperate classes.  A total of
            3 races will take place between cars of different make/model.

*/

public class Track
{

    //Attributes

    //Constructors

    public Track()
    {

    }//end default constructor

    //Methods

    public static void main(String[] args)
    {

        Car myCar = new Car();

        Race myRace = new Race();

        //settings for 1st car
        myCar.setMake("Pontiac");
        myCar.setModel("Firebird");
        myCar.setWeight(3700);
        myCar.setHorsePower(345);

        //settings for 2nd car
        Car newCar = new Car("Ford", "Mustang",3600, 335);

        //settings for 3rd car
        Car newCar3 = new Car("Dodge", "Charger", 3670, 335);

        //settings for final car
        Car finalCar = new Car("Chevy", "Camaro", 3300, 325);

        //method call --> determineWinner
        Race.determineWinner();



    }//end main

}//end Track Class



Car class:

public class Car
{

    //Attributes

    private String make;
    private String model;
    private int weight;
    private int horsepower;

    //Constructors

    public Car()
    {

    }//end default constructor

    public Car(String ma, String mo, int w, int horse)
    {
        make = ma;
        model = mo;
        weight = w;
        horsepower = horse;

    }//end 4-arg constructor

    public Car(Car ca)
    {
        make = ca.make;
        model = ca.model;
        weight = ca.weight;
        horsepower = ca.horsepower;

    }//end copy constructor

    //Methods

    //Getters

    public String getMake()
    {
        return make;
    }//end getMake

    public String getModel()
    {
        return model;
    }//end getModel

    public int getWeight()
    {
        return weight;
    }//end getWeight

    public int getHorsePower()
    {
        return horsepower;
    }//end getHorsePower

    //Setters

    public void setMake(String ma)
    {
        make = ma;
    }//end setMake

    public void setModel(String mo)
    {
        model = mo;
    }//end setModel

    public void setWeight(int w)
    {
        weight = w;
    }//end setWeight

    public void setHorsePower(int horse)
    {
        horsepower = horse;
    }//end setHorsePower

}//end car Class

Race class:

public class Race
{

    //attributes

    raceCarOne = x;
    raceCarTwo = y;

    //constructors

    //methods
    public String determineWinner(int x, int y, double w)
    {
        x

    }//end determineWinner


}//end class

I only need 2 attributes and a determineWinner method in the race class. Any suggestions on what i should do?

determineWinner is not static, So call it like the other methods:

Race myRace = new Race();
myRace.determineWinner(..);

You can have it take as parameter 4 Cars and inside based on their attributes decide which one would be the winner. It is also best to have it take as parameter a Car array:

...
Car [] cars = {myCar, newCar, newCar3, finalCar};
myRace.determineWinner(cars );
public Car determineWinner(Car [] cars) {

// returns the winner car.
}

You can have one extra method that compares only 2 Cars and determines which one will win and call that method in the determineWinner, in a loop that iterates the array.

private boolean isCar1FasterCar2(Car cr1, Car cr2) {
   return ....;
}

public Car determineWinner(Car [] cars) {
    for-loop{
       // use known algorithms with the use of the isCar1FasterCar2(cars[i], cars[j])
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.