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 !!!

Closed due to the fact that the question is too general for participants Yuriy SPb , cheops , Kromster , user194374, aleksandr barakin 9 Jun '16 at 14:31 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    And what is the question? If you want to get a review code, then there is a separate project for this. What is the problem in the calculations and why do you think that there is a problem at all? - Viacheslav Vedenin
  • one
    @ViacheslavVedenin, what project? - Nofate
  • five
    @Nikolay, is your question about class design or about the task of maximizing speed and minimizing consumption at acceptable values ​​of the parameters? - Nofate
  • one
    @ViacheslavVedenin My mistake. Incorrectly formed a question. In the comments above, I tried to more accurately state the essence of the issue. And what a project? - Nikolay
  • one
    @ViacheslavVedenin, there is no need for this, code review is allowed here, see meta.ru.stackoverflow.com/questions/1761/… - Nofate

2 answers 2

As far as I understand, the calculateSpeed() method does not count speed, but the distance traveled per unit of time. If so, then drove on =)

For convenience, I will write logic reducing any unnecessary. Instead of this.motor.getPower() I will write motor.power .

First, let's calculate which battery we should take:

Speed ​​calculations:

 tires.diameter * motor.power * 1000 / (tires.weight + motor.weight + battery.weight) 

Consumption Calculations:

 motor.power * (tires.weight + motor.weight + battery.weight) / 100 

We need to get the highest speed with the least consumption. Since we only count the battery, we can safely discard the remaining parameters and get:

Speed ​​(distance per unit of time): speed = 1000 / battery.weight

Consumption: consumption = battery.weight / 100

Now let's calculate how many steps a car will be able to go with a specific battery volume:

 steps = battery.capacity / consumption 

Next, round steps down and multiply by speed . Thus, we get the distance that the car can drive with a certain battery without taking into account other factors. In the cycle through the array of batteries we go over and do the calculations, memorize the index with the largest distance and use the battery that lies in the array under this index.

I tried not to solve the problem completely for you, but to show how this is done, so the rest will have to be derived myself =)

PS There is no need to complicate the code with unnecessary meaningless lines. The function calculateSpeed() , which now looks like this:

 public double calculateSpeed(){ double result; result = this.tires.getDiameter() * this.motor.getPower() * 1000 / ((this.tires.getWeight() + this.motor.getWeight() + this.battery.getWeight())); return result; } 

in this case, you can and should write this:

 public double calculateSpeed() { return this.tires.getDiameter() * this.motor.getPower() * 1000 / ((this.tires.getWeight() + this.motor.getWeight() + this.battery.getWeight())); } 

since your version is less readable, each time you look at the function, you pay attention to these lines and try to understand whether they are doing something important or not.

Good luck! I hope my answer will help you =)

  • Thank you very much! Everything worked out. - Nikolay
  • Not at all, it was very interesting =) - Ivan Dembicki

I would advise applying the Builder pattern. That is, to create a car not in the Car class itself, but to create a separate CarBuilder class that will create the car based on the parameters passed.