If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

program and track a car on a road

I trying to work on a challenging topic which I would certainly need help and advice . I will upload an image followed by the explanation in order to make things much easier to understand .

So I have uploaded the image. What I'm trying to figure out is to program cars to begin from the starting point in the right corner of the image and take either road 1 in black or road 2 in violet and go all the way round and come back to the starting point where everything began. lets say I have 5 cars in first place and the cars should surf and stop in specific location which I represented in stars before coming back to the starting point I mentioned in the beginning . I should be able at any time to track and locate all cars surfing on either road 1 or road 2 . what I think I should use is coordinates (x,y) with toString to tell me the location of cars on the roads. And I think also use of inheritance is important to pass object between cars and make it more flexible . What is a bit tricky here is how to use coordinates and be able to make the cars stop by itself at specific places which are represented with stars. hope you can give me your tips , advice . thank you for your time and support.

Re: program and track a car on a road

Actually your right if its working for one we can simply add the number of cars then and all roads . what I'm trying to do ( to be more specific then my first description ) first the most important thing is that a car should start where I mentioned (starting point) it should have the ability to stop at any star on its way ( I should tell the car on which star to stop for example ) and then drive back to the starting point when I tell it to. Now lets say a car is on a straight line like you said to make it more simple what I did is I made a class named Car which has private members : road , position ( which I should keep tracking at all time) , and max_speed ( I want for example to be able to fix max speed of all cars by 50 km/h for all time ) did the default and non-default constructors, setters getters and toString and then in the driver I created a new Car car1 ? Any way on how to start the first task I mentioned ?

Last edited by david16; December 3rd, 2017 at 01:02 PM.
Reason: spelling mistake

Re: program and track a car on a road

Yes I guess the program should keep on running till I decided to stop . By the way , I'm using TEXTPAD or notepad++ to implement this program . The main problem I'm facing is how to translate what I said in post #4 into a code . What I'm going to do is add maybe something such as do while in driver to keep on going infinitely what do you think or is there something better ?
I mean something like that :

Lets say I have a car with an engine controlled by my program how to send information to the car to start and go to a certain position or a stars ? is there a way to translate this logic to start and stop into a code that's the problem I'm facing at the moment.

Re: program and track a car on a road

Originally Posted by david16

Lets say I have a car with an engine controlled by my program how to send information to the car to start and go to a certain position or a stars ? is there a way to translate this logic to start and stop into a code that's the problem I'm facing at the moment.

It's not hard in principle but it depends on what kind of program you are writing. What for example do you mean by "go to a certain position"?

It could mean you call a set position method in a car object and the move would be instant in one gigant leap. But it could also mean the car is supposed to move in small smooth increments like it's driving from one position to another, implying you're planning to show it visually.

Even though you're using a simple text editor you can still make use of Swing or JavaFX (or even AWT) which I think you should. It will help you deal with user events and graphic output. Otherwise you will be forced to do lots of complicated low-level coding to accomplish the same. As I mentioned, these GUI frameworks essentially implement the basic loop I suggested in #5 and you fill in the application specifics.

So start by determining which kind of application you want to write, then decide on an appropriate application structure including choice of GUI, and only then start thinking about the rest. I have a feeling you're doing it backwards.

Re: program and track a car on a road

Yeah the program should actually meet the real life condition .

It could mean you call a set position method in a car object and the move would be instant in one gigant leap. But it could also mean the car is supposed to move in small smooth increments like it's driving from one position to another, implying you're planning to show it visually.

Actually I supposed that the drawing I did in post #1 is a real surface where I ask car to drive slowly at a fixed speed from a starting point to a position on the image which I represented with stars stop there then go back to the starting point . Each car has 2 things to do with 1 parameter :
1-drive to the position I asked for and stop .
2- Then when I ask it to start again it will drive to the starting point again non stop
3- speed is fixed all the time for all cars ( lets say to 50 km/h )

that is the first task I should do . So like you said it should move in a small smooth increments . now translating it to coding that's where I'm facing difficulties see what I mean ?

Re: program and track a car on a road

So like you said it should move in a small smooth increments . now translating it to coding that's where I'm facing difficulties see what I mean ?

I've planned to get reacquainted with Java after a fairly long pause so I've downloaded the latest Java and Eclipse now.

I'll write a small "game" first thing tomorrow and post here. Not exactly your application but I'm sure you will be able to use it as a starting point. And then you'll have some real code to ask more specific questions about.

I will latch on to the Swing/AWT event loop to accomplish the loop I mentioned in #5. It's not the only way but it's the easiest way to get something up and running.

Re: program and track a car on a road

Re: program and track a car on a road

Originally Posted by david16

Oh I'm glad to hear from you again . Thanks I'll be waiting for it then

My program is coming along quite nicely.

I quit Java at about when Java 7 was introduced so it's been some time (since around 2011). Apart from being somewhat rusty in Java I can only program in the evenings so it takes a little bit longer than expected.

I'm doing it in Swing which was the thing to use around the time I left Java. I probably should use JavaFX so I guess my next project will be to port this program to that GUI to really get up to date with Java.

Then there's the functional thing. My program is OO, period. I know Java has become more functional and I'll see what I can do about that but that's for a next project.

Re: program and track a car on a road

I've written a Java program now. The purpose was to get reacquainted with Java again after a long period of C++ programming. It means my Java style probably is a somewhat dated. The program is a non-trivial game of predator-prey type called Circle Chase. It's keyboard controlled and the commands are described at the beginning of the source file. I've used Eclipse version Mars as IDE with the latest Java version 8.

The whole program is on one file for simplicity. The most specific code comes first and then the code becomes more and more general towards the end where the Java entry point main() can be found in class BoardGame. I'm using Swing as GUI so BoardGame is a JFrame. BoardGame sets up class PlayGround which is a JPanel where the game is drawn. The logical part of the game is implemented in class ChaseGame. The rest of the program defines the participating game agents and a collision handling system. For the graphics and the physics I strongly recommend a game engine such as jMonkeyEngine rather than rolling it oneself as I did this time.

The program is designed according to object orientation (OO) which can be somewhat convoluted if you're not familiar with it. The core principle is that objects are made to act as autonomously as possible based on supplied information rather than being controlled in detail from the outside. This leads to a high degree of decoupling which makes the code easy to modify and extend and that's the ultimate goal of OO.

This project has been a nostalgic trip for me but I don't regret for a second I switched to C++. Java still is weak on the desktop, especially for scientific computing, and has been so from the beginning. There were some early attempts to improve the situation, R.I.P Java Grande, R.I.P Java3D, but they all fizzled out. I had high hopes when Oracle took over from Sun but I soon realized this wasn't a priority for them either so I finally gave up. Still Java is an excellent language for many purposes and well worth learning. It's also the most popular language according to the TIOBE index.

The Circle Chase source code is too long for one post so I post it in two parts.