Jump to content

Help getting started with a "track"

- - - - -

  • Please log in to reply
11 replies to this topic

#1
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Hello people on codecall.

I have some issues getting started on an assignment we got from the university.
We have to make a racetrack with 4 participants.

We need to make 3 class'.
RaceTrack which hold advance() and winner() + the start and finish line (start = 0, finish = whatever).
Cars which holds the 4 participants with carName (BMW, Opel, Ford) and carCountry (which country the participant is from).
Its always the same participants actually running the track.

and last, the Race which actually initiate the Race.

In the end, the winner needs to be announced using a Cowsay, which I already made.

Since this is our first week assignment, we're currently just working in the 'main' area. So to start the race it would probably look like this:
public static void main(String[] args) {

Race race1 = new Race();

race1.initialize();

}

but I'm not actually sure how to make all this happen.

I'm not looking for a direct solution (not yet anyway), just a hint to get started :)


What i've currently done is the following:

// Car.java

public class Car

{

    private String country;

    private String name;

    

    public Car()

    {

        

    }

    

    public Car(String name, String country)

    {

        this.country = country;

        this.name = name;

    }


    public String getCountry()

    {

        return country;

    }

    

    public String getName()

    {

        return name;

    }

}


// RaceTrack.java

public class RaceTrack {

    private int trackStart;

    private int trackEnd;

    

    public void advance()

    {

        

    }

    

    public void winner()

    {

        

    }

}


//Race.java

public class Race {

    

    public void Race()

    {

        

    }

    

    public static void main(String[] args)

    {

        Car car1 = new Car("Car De Moo", "Car of awesome");

        Car car2 = new Car("Car De Old", "Car of NOTawesome");

        Car car3 = new Car("Car Ze German", "Car of Nazi");

        Car car4 = new Car("SuperCar", "Car of TOTALLY ORGASMIC!");

        

        Cowsay winnerCar = new Cowsay();

        winnerCar.display("Cartype, "+ car1.getName() +", from " + car1.getCountry() + ", won the race!");

        

    }

    

}

Please don't mind the random names and countries :)

---------- Post added at 04:45 PM ---------- Previous post was at 04:38 PM ----------

Oh yea.. Each car got to move independently using the random function, just in case you were wondering how "advance()" should work.

---------- Post added at 04:49 PM ---------- Previous post was at 04:45 PM ----------

Also, the cars advance one at a time, not all 4 at the same time. So the random function would probably also first figure out which car gets to move, and then how long it gets to move - All using ints.

#2
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Constructors don't have a return type:
public void Race()

Ill give you two hints:

  • You need a loop (or not since its just a few) to give each car a chance to advance.
  • A way to know which car finished first. Yes I see your void Winner Method.
I seem to have a block. Cant figure out how to tell you what do to without telling you what to do:cursing:
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#3
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Hello fread :-)

Can you try giving me a hint to what you would start with? That way, you don't tell me exactly what to do, just where to "start". Some code is OK, I can always analyze what it does and how. :-)

I can see you responded to my private message.. Lets try to see if you can find a way of not telling me what to do while explaining what to do..

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Have you learned arrays ( Car[] cars = new Car[4] ) or collections yet? ( List<Car> cars = new ArrayList<Car>() )

#5
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Ehm, not yet no. We've learned how to make stuff like:

public static void main(String[] args) {

    Car car1 = new Car("testname", "testcountry");

    System.out.println(car1.getName()); // Just an example

  }


Using

public class Car

{

    private String country;

    private String name;

    

    public Cow()

    {

        

    }

    

    public Car(String name, String country)

    {

        this.country = country;

        this.name = name;

    }


    public String getCountry()

    {

        return country;

    }

    

    public String getName()

    {

        return name;

    }

}



---------- Post added at 06:08 PM ---------- Previous post was at 06:06 PM ----------

Considering how little we have actually learned so far (we haven't even learned
for(int i=0;i<something;i++){}
) which we had to use when we made a cowsay.

---------- Post added at 07:01 PM ---------- Previous post was at 06:08 PM ----------

Some code would be appreciated, Im good at analyzing :)

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Okay, as far as Random number generating goes, check this method of the Math class (import java.lang.Math; )
Math (Java 2 Platform SE v1.4.2)
It generates numbers between 0 and 1 (Actually, 0 and 0.999999999999999),
You then need to multiply it to generate higher numbers. It doesn't matter if there are decimals. If you cast it to an int, all decimals are dropped (not rounded)

Quote

So the random function would probably also first figure out which car gets to move
Okay without arrays or collections this may look somewhat ugly.
int randomNumber = ....

if(randomNumber == 1){
  car1.something();
} else if(randomNumber == 2){
  car2.something();
} else.....

}

Where do you plan to store each car's individual progress?

#7
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts

wim DC said:

Okay, as far as Random number generating goes, check this method of the Math class (import java.lang.Math; )
Math (Java 2 Platform SE v1.4.2)
It generates numbers between 0 and 1 (Actually, 0 and 0.999999999999999),
You then need to multiply it to generate higher numbers. It doesn't matter if there are decimals. If you cast it to an int, all decimals are dropped (not rounded)


Okay without arrays or collections this may look somewhat ugly.
int randomNumber = ....


if(randomNumber == 1){

  car1.something();

} else if(randomNumber == 2){

  car2.something();

} else.....


}

Where do you plan to store each car's individual progress?

Im not sure about the storing of the progress..

The cars is made in the main function of race using Car car0 = new Car("name", "country");

As for the random(), couldn't I use java.util.Random ? And then do a random(4) so it randoms 0,1,2,3?

---------- Post added at 08:11 PM ---------- Previous post was at 08:06 PM ----------

Couldn't I make it so it got a standard value of 0?


// Car0

positionCar0 = 0

positionCar0 = positionCar0 + randomInt


// Car1

positionCar1 = 0

positionCar1 = positionCar1 + randomInt


I know this is not Java, but just if it can be done "like" this.

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Random doesn't work like that, it'll be more like:
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(4);

As for the position. Since RaceTrack needs to know the winner, and also knows the race, I would assume RaceTrack "owns" the 4 Cars so it can keep track of them.

(What I forgot to mention, you have 2 main methods in 2 classes - that's pretty useless in this case)

I'm not sure how "stuck" you are with those starting classes of yours. Are they given from the teacher as starting point?


I would have
// RaceTrack.java
public class RaceTrack {
    private int trackStart;
    private int trackEnd;
    private Car car1, car2, ...;

    public RaceTrack(Car car1, Car car2, ...){
        this.car1 = car1;
        this.car2 = car2;
        ....
    }
    
    public void advance()
    {
        //pick random car here, and move it a random amount further.
    }
    
    public void winner()
    {
        
    }
}
And I would store the progress in each car

#9
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
how would you store the name and country of each car using the above code? It looks very interesting, and usable, and something I can work with :)

#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

public class Car{* * private String country;* * private String name;

    private int position;


    ...


    public void advance(int amount){

       position += amount;

    }
And use that in RaceTrack

#11
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Hmm.. I'm not sure if we are allowed to do this, if I understand you correctly, you would have the country and name directly in the RaceTrack.

However, our assignment gives us the following info:
Create a class Car for representing the cars in a race. Every car has a name and a country, both of type String.

And then
Create a class RaceTrack for representing the race track.
Holds advance() and winner()

Last
Create a class Race for representing the actual race.
Holds run()
And the only one which got the 'main' in it.


Not sure if it changes anything, might just have misunderstood something.

#12
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
If you put the three classes on paper and look at them, in particular the method parameters and the return types, you will see its awkward to construct the relationship especially with the constraints added in your latest post. It can be done but i would have to change things that return void to return something and methods that take no arguments will take arguments. Some amount of detail is lacking, well at least that is what i think.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users