Question

Create a race between 3 cars in which they have different speed limits such as 20...

Create a race between 3 cars in which they have different speed limits such as 20 mph, 60mph or 30. Respectively. the acceleration should be 15f/s or 10.23mph the race should be 3 tracks each of 1 mile long create instances for the 3 cars on the course and print out their speed, location and the time every 30 seconds

In java

Homework Answers

Answer #1

Hi, please find the solution. I am incrementing the speed every second, according to the acceleration.

Feel free to make changes as required. Solution is working great.

public class Car {
    private float speedLimit;
    private float distanceTravelled=0;
    private float speed;

    public float getSpeedLimit() {
        return speedLimit;
    }

    public void addSpeed(float speed){
        if(this.speed+speed>speedLimit) return;//cannot go beyond speed limit
        this.speed += speed;
    }

    public float getSpeed() {
        return speed;
    }

    /**
     * Makes the car travel with that speed
     */
    public void travel(){
        distanceTravelled+= speed;
    }

    public float getDistanceTravelled(){
        return distanceTravelled;
    }
    public Car(float speedLimit) {
        this.speedLimit = speedLimit;
    }
}
import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) throws InterruptedException{
        float acceleration = 10.23f/(60f*60f);//miles per sec, assuming acceleration is same for all


        /*the above speed gets added every second*/
        Car a = new Car(20f/(60f*60f));
        Car b = new Car(60/(60f*60f));
        Car c = new Car(30/(60f*60f));
        System.out.println("a's speed limit is "+a.getSpeedLimit());
        System.out.println("B's speed limit is "+b.getSpeedLimit());
        System.out.println("C's speed limit is "+c.getSpeedLimit());
        LocalDateTime localDateTime = LocalDateTime.now();
        int i = 0;
        while(true){
        


            /*we need to add speed every second by acceleration amount*/
            if(a.getDistanceTravelled()<1)
            a.addSpeed(acceleration);
            if(b.getDistanceTravelled()<1)
            b.addSpeed(acceleration);
            if(c.getDistanceTravelled()<1)
            c.addSpeed(acceleration);
            a.travel();b.travel();c.travel();
            System.out.println("A Travelled "+a.getDistanceTravelled()+ " Distance & speed is "+a.getSpeed());
            System.out.println("B Travelled "+b.getDistanceTravelled()+ " Distance & speed is "+b.getSpeed());
            System.out.println("C Travelled "+c.getDistanceTravelled()+ " Distance & speed is "+c.getSpeed());
            System.out.println();
            System.out.println();
            if(a.getDistanceTravelled()>1 && b.getDistanceTravelled()>1 && c.getDistanceTravelled()>1) break;
            Thread.sleep(1000);//wait for a sec before runnning again.
        }


    }
}

A Travelled 0.10514166 Distance & speed is 0.0028416666 B Travelled 0.4972919 Distance & speed is 0.014208333 C Travelled 0.20744166 Distance & speed is 0.005683333 A Travelled 0.10798333 Distance & speed is 0.0028416666 B Travelled 0.51150024 Distance & speed is 0.014208333 C Travelled 0.21312499 Distance & speed is 0.005683333 A Travelled 0.110824995 Distance & speed is 0.0028416666 B Travelled 0.52570856 Distance & speed is 0.014208333 C Travelled 0.21880832 Distance & speed is 0.005683333

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
A car is driving on a track that is sectioned off. Each section has a different...
A car is driving on a track that is sectioned off. Each section has a different speed limit. The first zone is 1 mile long and has a speed limit of 20 mph. Zone 2 is 1 mile long and has a speed limit of 60 mph. Zone 3 is 1 mile long and has a speed limit of 30 mph. Zone 4 is 0.5 miles long and has a speed limit of 20 mph. The driver must have already...
Merari feels lucky and tries to drag race a Lamborghini in a Camaro? Merari goes from...
Merari feels lucky and tries to drag race a Lamborghini in a Camaro? Merari goes from 65 mph (29 m/s) to 90 mph (40.2 m/s) in 3 seconds before realizing that there is no way he's going to catch it. What was the acceleration of Merari's Camaro during this time? For Henry, the worse part of being on lock down is having to deal with his little sister. While studying for his Physics exam she hits Henry in the head...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
The Business Case for Agility “The battle is not always to the strongest, nor the race...
The Business Case for Agility “The battle is not always to the strongest, nor the race to the swiftest, but that’s the way to bet ’em!”  —C. Morgan Cofer In This Chapter This chapter discusses the business case for Agility, presenting six benefits for teams and the enterprise. It also describes a financial model that shows why incremental development works. Takeaways Agility is not just about the team. There are product-management, project-management, and technical issues beyond the team’s control. Lean-Agile provides...
3 SECURING THE WORKFORCE Diversity management in X-tech, a Japanese organisation This case is intended to...
3 SECURING THE WORKFORCE Diversity management in X-tech, a Japanese organisation This case is intended to be used as a basis for class discussion rather than as an illustration of the effective or ineffective handling of an administrative situation. The name of the company is disguised. INTRODUCTION In light of demographic concerns, in 2012, the Japanese government initiated an effort to change the work environment in order to secure the workforce of the future. Japan is world renowned for its...
provide 3-4 paragraphs post (team 2) 1-What are 4 key things you learned about the topic...
provide 3-4 paragraphs post (team 2) 1-What are 4 key things you learned about the topic from reading their paper? 2-How does the topic relate to you and your current or past job? 3-Critique the paper in terms of the organization and quality.1- Employee Stress and how it has an Adverse Effect on a Company This paper explores employee stress and how it has an adverse effect on a company, its employees and the organization. Job stress can have a...
1. What are the requirements for successful price discrimination? Does this type of dry cleaning qualify?...
1. What are the requirements for successful price discrimination? Does this type of dry cleaning qualify? 2. What are other examples of price discrimination? 3. The effectiveness of price discrimination is a different question than whether businesses should be able to discriminate by price. Is this an issue that deserves regulation, or is it best left to the market? Why Women Pay More From dry cleaning to haircuts, women often pay more than men due to gender pricing. Find out...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
2. SECURING THE WORKFORCE Diversity management in X-tech, a Japanese organisation This case is intended to...
2. SECURING THE WORKFORCE Diversity management in X-tech, a Japanese organisation This case is intended to be used as a basis for class discussion rather than as an illustration of the effective or ineffective handling of an administrative situation. The name of the company is disguised. INTRODUCTION In light of demographic concerns, in 2012, the Japanese government initiated an effort to change the work environment in order to secure the workforce of the future. Japan is world renowned for its...
What role could the governance of ethics have played if it had been in existence in...
What role could the governance of ethics have played if it had been in existence in the organization? Assess the leadership of Enron from an ethical perspective. THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among the top Fortune 500 companies, collapsed in 2001 under a mountain of debt...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT