Hello! Please tell me how to correctly describe a comment to the race code with an example!
Thanks in advance!
package dragracing; /* ΠΠ΅ΡΠΎΠ΄Ρ ΠΈΠ½ΡΠ΅ΡΡΠ΅ΠΉΡΠ° CarInterface */ public interface CarInterface { public int getId(); public void setId(int id); public String getDriver(); public void setDriver(String driver); public String getManufacturer(); public void setManufacturer(String manufacturer); public int getMaxSpeed(); public void setMaxSpeed(int maxSpeed); public String getModel(); public void setModel(String model); public int getVelocity(); public void setVelocity(int velocity); public long getCurrentSpeed(); public void setCurrentSpeed(long currentSpeed); public long getRunDistance(); public void setRunDistance(long runDistance); public int getRunTime(); public void setRunTime(int runTime); } /* ΠΠΎΠ»Ρ ΠΊΠ»Π°ΡΡΠ° Car ΠΈΠ½ΡΠ΅ΡΡΠ΅ΠΉΡΠ° CarInterface */ class Car implements CarInterface { private int id; private String manufacturer; private String model; private int velocity; private int maxSpeed; private long currentSpeed = 0; private long runDistance = 0; private int runTime = 0; private String driver; /* ΠΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΠΊΠ»Π°ΡΡΠ° Car Ρ Π²Ρ
ΠΎΠ΄Π½ΡΠΌΠΈ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΠΊΠ»Π°ΡΡΠ° Car */ public Car(Car car) { this.id = car.id; this.manufacturer = car.manufacturer; this.model = car.model; this.velocity = car.velocity; this.maxSpeed = car.maxSpeed; this.driver = car.driver; this.currentSpeed = 0; this.runDistance = 0; this.runTime = 0; } /* ΠΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΠΊΠ»Π°ΡΡΠ° Car Ρ Π²Ρ
ΠΎΠ΄Π½ΡΠΌΠΈ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ */ public Car(int id, String manufacturer, String model, int velocity, int maxSpeed, String driver) { this.id = id; this.manufacturer = manufacturer; this.model = model; this.velocity = velocity; this.maxSpeed = maxSpeed; this.driver = driver; } /* ΠΠ΅ΡΠΎΠ΄ getId Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ id */ public int getId() { return this.id; } /* ΠΠ΅ΡΠΎΠ΄ setId Π½Π΅Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ id */ public void setId(int id) { this.id = id; } /* ΠΠ΅ΡΠΎΠ΄ getDriver Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ driver */ public String getDriver() { return this.driver; } /* ΠΠ΅ΡΠΎΠ΄ setDriver Π½Π΅Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ driver */ public void setDriver(String driver) { this.driver = driver; } /* ΠΠ΅ΡΠΎΠ΄ getManufacturer Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ manufacturer */ public String getManufacturer() { return this.manufacturer; } /* ΠΠ΅ΡΠΎΠ΄ setManufacturer Π½Π΅Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ manufacturer */ public void setManufacturer(String manufacturer) { this.manufacturer = manufacturer; } /* ΠΠ΅ΡΠΎΠ΄ getMaxSpeed Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ maxSpeed */ public int getMaxSpeed() { return this.maxSpeed; } /* ΠΠ΅ΡΠΎΠ΄ setMaxSpeed Π½Π΅Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ maxSpeed */ public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } /* ΠΠ΅ΡΠΎΠ΄ getModel Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ model */ public String getModel() { return this.model; } /* ΠΠ΅ΡΠΎΠ΄ setModel Π½Π΅Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ model */ public void setModel(String model) { this.model = model; } /* ΠΠ΅ΡΠΎΠ΄ getVelocity Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ velocity */ public int getVelocity() { return this.velocity; } /* ΠΠ΅ΡΠΎΠ΄ setVelocity Π½Π΅Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ velocity */ public void setVelocity(int velocity) { this.velocity = velocity; } /* ΠΠ΅ΡΠΎΠ΄ getCurrentSpeed Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ currentSpeed */ public long getCurrentSpeed() { return this.currentSpeed; } /* ΠΠ΅ΡΠΎΠ΄ setCurrentSpeed Π½Π΅Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ currentSpeed */ public void setCurrentSpeed(long currentSpeed) { this.currentSpeed = currentSpeed; } /* ΠΠ΅ΡΠΎΠ΄ getRunDistance Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ runDistance */ public long getRunDistance() { return this.runDistance; } /* ΠΠ΅ΡΠΎΠ΄ setRunDistance Π½Π΅Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ runDistance */ public void setRunDistance(long runDistance) { this.runDistance = runDistance; } /* ΠΠ΅ΡΠΎΠ΄ getRunTime Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ runTime */ public int getRunTime() { return this.runTime; } /* ΠΠ΅ΡΠΎΠ΄ setRunTime Π½Π΅Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π·Π½Π°ΡΠ΅Π½ΠΈΡ runTime */ public void setRunTime(int runTime) { this.runTime = runTime; } }
Second class
package dragracing; /* ΠΈΠΌΠΏΠΎΡΡ Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊ */ import java.util.HashMap; import java.util.Map; /* ΠΠΎΠ»Ρ ΠΊΠ»Π°ΡΡΠ° Dragracing Ρ ΡΠΈΠΏΠΈΠ·ΠΈΡΠΎΠ²Π°Π½Π½ΡΠΌΠΈ ΡΠΏΠΈΡΠ°ΠΊΠΌΠΈ */ public class Dragracing { private static Map<Integer, Car> cars = new HashMap<Integer, Car>(); /* Π‘ΡΠ°ΡΠΈΡΠ΅ΡΠΊΠ°Ρ ΠΊΠΎΠ½ΡΡΠ°Π½ΡΠ° trackLength */ private static final int trackLength = 402; /* Π‘ΡΠ°ΡΠΈΡΠ΅ΡΠΊΠ°Ρ ΠΌΠ΅ΡΠΎΠ΄ setCars Π½Π΅Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π½Π΅ ΠΊΠ°ΠΊΠΎΠ³ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Ρ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ΠΌ Π·Π½Π°ΡΠ΅Π½ΠΈΠΉ ΠΈ Π½Π°Π·Π²Π°Π½ΠΈΠΉ */ private static void setCars() { cars.put(1, new Car(1, "Toyota", "Celica", 6, 250, "Alex")); cars.put(2, new Car(2, "Dodge", "Viper", 7, 310, "Steve")); cars.put(3, new Car(3, "Porsche", "911", 9, 320, "Martin")); cars.put(4, new Car(4, "Dodge", "Challenger", 9, 250, "Andrew")); cars.put(5, new Car(5, "Toyota", "MR2", 5, 250, "Jim")); cars.put(6, new Car(6, "Honda", "NSX", 8, 280, "Hiroki")); cars.put(7, new Car(7, "Chevrolet", "Camaro", 8, 270, "Ricky")); cars.put(8, new Car(8, "Nissan", "GTR", 8, 310, "Kito")); cars.put(9, new Car(9, "Lamborghini", "Countach", 8, 300, "Linda")); cars.put(10, new Car(10, "VAZ", "2107", 8, 200, "Vasiliy")); } /* Π‘ΡΠ°ΡΠΈΡΠ΅ΡΠΊΠ°Ρ ΠΌΠ΅ΡΠΎΠ΄ refresh Π½Π΅Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π½Π΅ ΠΊΠ°ΠΊΠΎΠ³ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Ρ Π²Ρ
ΠΎΠ΄Π½ΡΠΌΠΈ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ Ρ ΡΡΠ»ΠΎΠ²Π½ΡΠΌ ΠΎΠΏΠ΅ΡΠ°ΡΠΎΡΠΎΠΌ if */ private static void refresh(Car car, int time) { long maxSpeed = Math.round(car.getMaxSpeed()/3.6); long curSpeed = Math.round(car.getVelocity() * time); if(curSpeed > maxSpeed) curSpeed = maxSpeed; car.setRunDistance(car.getRunDistance() + curSpeed > trackLength ? trackLength : car.getRunDistance() + curSpeed); car.setCurrentSpeed(Math.round(curSpeed*3.6)); } /* Π‘ΡΠ°ΡΠΈΡΠ΅ΡΠΊΠ°Ρ ΠΌΠ΅ΡΠΎΠ΄ main Π½Π΅Π²ΠΎΠ·ΡΠ°ΡΠ°ΡΡΠΈΠΉ Π½Π΅ ΠΊΠ°ΠΊΠΎΠ³ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Ρ Π²Ρ
ΠΎΠ΄Π½ΡΠΌΠΈ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ Ρ ΠΈΡΠΊΠ»ΡΡΠΈΡΠ΅Π»ΡΠ½ΠΎΠΉ ΡΠΈΡΡΠ°ΡΠΈΠ΅ΠΉ throws InterruptedException ΡΠ°ΠΊ ΠΆΠ΅ Ρ ΡΠΈΠΏΠΈΠ·ΠΈΡΠΎΠ²Π°Π½Π½ΡΠΌ ΡΠΏΠΈΡΠΊΠΎΠΌ Ρ ΡΠΈΠΊΠ»ΠΎΠΌ for ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ boolean Ρ ΡΠΈΠΊΠ»ΠΎΠΌ while c ΡΡΠ»ΠΎΠ²Π½ΡΠΌ ΠΎΠΏΠ΅ΡΠ°ΡΠΎΡΠΎΠΌ if */ public static void main(String[] args) throws InterruptedException { HashMap<Integer, Car> results = new HashMap<Integer, Car>(); setCars(); int runNum = 1; for(int i = 1; i < cars.size(); i++) { Car left = new Car(cars.get(i++)); Car right = new Car(cars.get(i)); boolean run = true; System.out.println("Run " + runNum + ": " + left.getDriver() + " on " + left.getManufacturer() + " " + left.getModel() + " vs. " + right.getDriver() + " on " + right.getManufacturer() + " " + right.getModel()); Thread.sleep(3000); System.out.println("Ready!"); Thread.sleep(1000); System.out.println("Steady!"); Thread.sleep(1000); System.out.println("GO!"); int seconds = 0; while(run) { System.out.println("Run " + runNum + ": " + left.getDriver() + " on " + left.getManufacturer() + " " + left.getModel() + " vs. " + right.getDriver() + " on " + right.getManufacturer() + " " + right.getModel()); System.out.println(left.getDriver() + ": " + left.getRunDistance() + " meters, " + left.getCurrentSpeed() + " km/h"); System.out.println(right.getDriver() + ": " + right.getRunDistance() + " meters, " + right.getCurrentSpeed() + " km/h"); Thread.sleep(1000); seconds++; if(left.getRunTime() == 0) refresh(left, seconds); if(right.getRunTime() == 0) refresh(right, seconds); if(left.getRunTime() > 0 && right.getRunTime() > 0) run = false; if(left.getRunDistance() >= trackLength && left.getRunTime() == 0) { left.setRunTime(seconds); results.put(cars.size() - results.size(), left); } if(right.getRunDistance() >= trackLength && right.getRunTime() == 0) { right.setRunTime(seconds); results.put(cars.size() - results.size(), right); } } runNum++; } for(Map.Entry<Integer, Car> set : results.entrySet()) System.out.println(set.getValue().getRunTime() + " seconds: " + set.getValue().getDriver() + " on " + set.getValue().getManufacturer() + " " + set.getValue().getModel()); } }