I want to design a racetrack model in Java. To do this, create a few simple objects: Track , Tires , Motor , Battery . Which in turn are components of the Car object.
The problem was hidden in the calculations, namely: you need to write the buildCar(Track track, Tires tires[], Motor motors[], Battery batteries[]) method buildCar(Track track, Tires tires[], Motor motors[], Battery batteries[]) in the Car class, which will accept arrays of parts objects and calculate the optimal configuration of the car. Those. choose car parts in such a way that it would be possible to drive more with less battery consumption.
For calculations there are formulas:
calculateConsumption() result = this.motor.getPower() * (this.tires.getWeight() + this.motor.getWeight() + this.battery.getWeight()) / 100; calculateSpeed() result = this.tires.getDiameter() * this.motor.getPower() * 1000 / ((this.tires.getWeight() + this.motor.getWeight() + this.battery.getWeight())); And here are the Classes themselves:
public class Tires { /* * Attributes * */ private double weight; private double diameter; /* * Constructor's * */ // create new example of an object Tires(set's value for diameter and weight on 0 automatically) public Tires(){ this(0d, 0d); } // create new example of an object Tires public Tires(double weight, double diameter){ this.diameter = diameter; this.weight = weight; } .
public class Motor { /* * Attributes * */ private double weight; private double power; /* * Constructor's * */ // create new example of an object Motor(set's value for power and weight on 0 automatically) public Motor(){ this(0d, 0d); } // create new example of an object Motor public Motor(double weight, double power){ this.power = power; this.weight = weight; } .
public class Battery { /* * Attributes * */ private double capacity; private double weight; /* * Constructor's * */ // create new example of an object Battery(set's value for capacity and weight on 0 automatically) public Battery(){ this(0d, 0d); } // create new example of an object Battery public Battery(double capacity, double weight){ this.capacity = capacity; this.weight = weight; } public boolean reduceCapacity(double n){ if(this.capacity - n < 0){ this.capacity -= n; return true; } return false; } And here is the class itself, in which the buildCar method is needed.
public class Car { /* * Attributes * */ private String name; private Tires tires; private Motor motor; private Battery battery; /* * Constructor's * */ // create new example of an object Car(set only name of a car) public Car(String name){ this.name = name; } // create new example of an object Car public Car(String name, Tires tires, Motor motor, Battery battery){ this.name = name; this.tires = tires; this.motor = motor; this.battery = battery; } // return true if a car has motor, tires and battery. Battery must not be empty public boolean isFunctional(){ if (this.motor == null || this.tires == null || this.battery == null){ return false; } if (this.battery.getCapacity() <= 0){ return false; } return true; } public double calculateSpeed(){ double result; result = this.tires.getDiameter() * this.motor.getPower() * 1000 / ((this.tires.getWeight() + this.motor.getWeight() + this.battery.getWeight())); return result; } public double calculateConsumption(){ double result; result = this.motor.getPower() * (this.tires.getWeight() + this.motor.getWeight() + this.battery.getWeight()) / 100; return result; } public double raceStep(){ double result; if(this.battery.reduceCapacity(this.calculateConsumption())){ this.calculateSpeed(); result = this.calculateSpeed(); return result; } else return 0; } public void buildCar(Track track, Tires tires[], Motor motors[], Battery batteries[] ){ ???????????????????????????????????????????????????????? } } Thank you very much for any advice and tips !!!