Question

Write a program that simulates four cars racing. You can set the speed for each car,...

Write a program that simulates four cars racing. You can set the speed for each car, with maximum 100.

Java language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
import javafx.scene.control.TextField;
import javafx.scene.Node;
import javafx.collections.ObservableList;
import javafx.scene.input.KeyCode;

public class Exercise_16_25 extends Application {
protected HBox paneForSpeed = new HBox(5);
protected VBox paneForCars = new VBox(5);
protected ObservableList<Node> textFields = paneForSpeed.getChildren();
protected ObservableList<Node> cars = paneForCars.getChildren();

@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
final int NUMBER_OF_CARS = 4;
final int PREF_COLUMN_COUNT = 2;

paneForSpeed.setAlignment(Pos.CENTER);

// keep the labels and text field in a pane
for (int i = 0; i < NUMBER_OF_CARS; i++) {
paneForSpeed.getChildren().addAll(
new Label("Car " + (i + 1) + ":"), new TextField());
}

// Set specified properties for each text field in a list
for (int i = 1; i < textFields.size(); i+= 2) {
((TextField)textFields.get(i)).setPrefColumnCount(PREF_COLUMN_COUNT);
}

// Place nodes in a pane for cars
for (int i = 0; i < NUMBER_OF_CARS; i++) {
paneForCars.getChildren().add(new CarPane());
}

// Set specified properties for each element in a list
for (Node car: cars) {
((CarPane)car).setStyle("-fx-border-color: black");
((CarPane)car).setY(40);
}

// Create and register handlers
for (int i = 1; i < textFields.size(); i += 2) {
((TextField)textFields.get(i)).setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ENTER) {
setSpeed();
}
});

}

// Create a border pane
BorderPane pane = new BorderPane();
pane.setTop(paneForSpeed);
pane.setCenter(paneForCars);

// Create a scene and place it in the stage
Scene scene = new Scene(pane, 400, 200);
primaryStage.setTitle("Exercise_16_25"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}

/** Set specified rate for a CarPane list */
private void setSpeed() {
for (int i = 1, j = 0; i < textFields.size(); i += 2, j++) {
if (((TextField)textFields.get(i)).getText().length() > 0) {
((CarPane)cars.get(j)).setSpeed(
Double.parseDouble(((TextField)textFields.get(i)).getText()));
((CarPane)cars.get(j)).play();
}
else {
((CarPane)cars.get(j)).pause();
}
}
}
}

Add a comment
Know the answer?
Add Answer to:
Write a program that simulates four cars racing. You can set the speed for each car,...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a java program that simulates the rolling of four dice. The program should use random...

    Write a java program that simulates the rolling of four dice. The program should use random number generator to roll dice. The sum of the four values should then be calculated. Note that each die can show an integer value from 1 to 6, so the sum of the four values will vary from 4 to 24, with 14 being the most frequent sum and 4 and 24 being the least frequent sums. Your program should roll the dice 12,960...

  • WRITE JAVA PROGRAM Problem Statement You are required to write a stock price simulator, which simulates...

    WRITE JAVA PROGRAM Problem Statement You are required to write a stock price simulator, which simulates a price change of a stock. When the program runs, it should do the following:  Create a Stock class which must include fields: name, symbol, currentPrice, nextPrice, priceChange, and priceChangePercentage.  The Stock class must have two constructor: a no-argument constructor and a constructor with four parameters that correspond to the four fields. o For the no-argument constructor, set the default value for...

  • Write a C++ program that simulates coin tossing. For each toss of the coin the program...

    Write a C++ program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. The program should toss a coin 100 times. Count the number of times each side of the coin appears and print the results at the end of the 100 tosses.   The program should have the following functions as a minimum: void toss() - called from main() and will randomly toss the coin and set a variable equal to the...

  • Java Programming Rash is known about his love for racing sports. He is an avid Formula...

    Java Programming Rash is known about his love for racing sports. He is an avid Formula 1 fan. He went to watch this year's Indian Grand Prix at New Delhi. He noticed that one segment of the circuit was a long straight road. It was impossible for a car to overtake other cars on this segment. Therefore, a car had to lower down its speed if there was a slower car in front of it. While watching the race, Rash...

  • The assignment is to write a program in unix using C++ environment Car Instrument Simulator For this assignment you w...

    The assignment is to write a program in unix using C++ environment Car Instrument Simulator For this assignment you will design a set of classes that work together to simulate a car’s fuel gauge and odometer. The classes you will design are: • The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities are – To know the car’s current amount of fuel, in gallons. – To report the car’s current amount of fuel, in gallons. – To...

  • Write a program to define ​a ​virtual car. Car has these features: number of wheels, max speed, current speed, isMoving, remainingFuel, distanceTravelled. The car can move when the programmer decides...

    Write a program to define ​a ​virtual car. Car has these features: number of wheels, max speed, current speed, isMoving, remainingFuel, distanceTravelled. The car can move when the programmer decides to accelerate. With each acceleration current speed increases by 10. Fuel decreases depending on the speed of the car and follows this formula: fuel= fuel-(speed/2). Your program should display “Out of fuel” when there is no remaining fuel. Fuel starts form 100. ​Use a constructor to initialize the car object....

  • available until you signif Four toy racecars are racing along a circular race track. The cars...

    available until you signif Four toy racecars are racing along a circular race track. The cars start at the 3-o'clock position and travel CCW along the tracks t = 4.46 Car B is constantly 3 feet from the center of the race track and travels at a constant speed. The angle Car B sweeps out increases at a constant rate of 2 radian per second. a. How many radians does car B sweep out in t seconds? Preview b. Define...

  • Overcoming a Head Start Review Part A Cars A and B are racing each other along...

    Overcoming a Head Start Review Part A Cars A and B are racing each other along the same straight road in the following manner Car A has a head start and is a distance DA beyond the starting line at t-0. The starting line is at z-0. Car A travels at a constant speed vA. Car B starts at the starting line but has a better engine than Car A, and thus Car B travels at a constant speed vp,...

  • In C++ Write a program to calculate a car dealer's sale of the same car. Enter...

    In C++ Write a program to calculate a car dealer's sale of the same car. Enter the car price and the number of cars sold. Calculate the tax (9% of each car price). Calculate the shipping charge ($3,000 per car). The price of the car sale is the sum of the total car price for all cars, the tax for all cars, and the shipping charge for all cars. You must display the total price of the car sale at...

  • Write a program that simulates a traffic light. When a radio button is selected, the corresponding light is turned on, a...

    Write a program that simulates a traffic light. When a radio button is selected, the corresponding light is turned on, and only one light can be on at a time. No light is on when the program starts. Use Group instead of Pane to combine shapes, and then put the group to the center of a BorderPane will make all shapes staying in center when resizing the window. using NetBeans Java

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT