program and track a car on a road
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: program and track a car on a road

  1. #1
    Join Date
    May 2017
    Posts
    182

    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.
    Attached Images Attached Images  
    Last edited by david16; November 30th, 2017 at 04:35 PM.

  2. #2
    Join Date
    May 2017
    Posts
    182

    Re: program and track a car on a road

    I'm new in java but I saw this topic and was interested in solving it . Any advice on how to start this task ?
    Last edited by david16; December 2nd, 2017 at 06:33 AM. Reason: additional information mentioned

  3. #3
    Join Date
    Feb 2017
    Posts
    186

    Re: program and track a car on a road

    Quote Originally Posted by david16 View Post
    Any advice on how to start this task ?
    It's always a good idea to start simple. Why not begin with just one car racing along a straight line with one stop in the middle.

  4. #4
    Join Date
    May 2017
    Posts
    182

    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

  5. #5
    Join Date
    Feb 2017
    Posts
    186

    Re: program and track a car on a road

    Quote Originally Posted by david16 View Post
    Any way on how to start the first task I mentioned ?
    It depends on what kind of program you're writing. If it proceeds with time there's usually a main loop that does something like this (in principle):

    Repeat forever: {
    1. Wait for some time (a tick).
    2. Inform concerned parties (say cars) that a tick has occurred.
    3. Update the consequences of 2 (react to the car's new positions and display the consequences).
    4. Check for user action (commands to start things, stop things, modify things or exit the program).
    }

    I suggest you use a GUI framework such as Swing or the new JavaFX. It very much works like the loop I described.

    I also usually recommend a game engine such as JMonkeyEngine but that may be overkill for a learning project like yours.

    http://jmonkeyengine.org/
    Last edited by wolle; December 4th, 2017 at 09:43 AM.

  6. #6
    Join Date
    May 2017
    Posts
    182

    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 :

    Code:
    public class Forever {
        public static void main(String[] args) throws InterruptedException {
            while (true) { Thread.sleep(1000); }
        }
    }
    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.
    Last edited by david16; December 4th, 2017 at 10:51 AM.

  7. #7
    Join Date
    Feb 2017
    Posts
    186

    Re: program and track a car on a road

    Quote Originally Posted by david16 View Post
    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.
    Last edited by wolle; December 4th, 2017 at 12:57 PM.

  8. #8
    Join Date
    May 2017
    Posts
    182

    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 ?
    Last edited by david16; December 4th, 2017 at 02:01 PM.

  9. #9
    Join Date
    Feb 2017
    Posts
    186

    Re: program and track a car on a road

    Quote Originally Posted by david16 View Post
    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.

  10. #10
    Join Date
    May 2017
    Posts
    182

    Re: program and track a car on a road

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

  11. #11
    Join Date
    Feb 2017
    Posts
    186

    Re: program and track a car on a road

    Quote Originally Posted by david16 View Post
    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.

  12. #12
    Join Date
    May 2017
    Posts
    182

    Re: program and track a car on a road

    I see. No problem take your time . I'll be here when you'll be back . thanks a lot
    Last edited by david16; December 10th, 2017 at 05:08 AM.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)