I created 4 objects, each has its own dichte value (density), and should have its masse value (mass). Mass is calculated as density * volume . How do I implement this? What would the program calculate this value and insert into the object?

 public Baukloetze Eichen = new Baukloetze(1.2, 800d, masse); public Baukloetze Fichten = new Baukloetze(1.2, 500d, masse); public Baukloetze Granit = new Baukloetze(1.2, 2800d, masse); public Baukloetze Stahl = new Baukloetze(1.2, 7900d, masse); public double volume, dichte, masse; public Baukloetze(double volume, double dichte, double masse) { this.dichte = dichte; this.volume = 1.2; this.masse = masse; 
  • Well, just multiply the necessary fields and set the result as the value of one more field ... - YuriiSPb
  • How to do it? maybe you showed the code itself? - Schweigert Vitalij
  • this.masse = dichte * volume; and remove the mass from the constructor - keekkenen

1 answer 1

 public Baukloetze(double volume, double dichte) { this.dichte = dichte; this.volume = volume; masse = volume * dichte; } 
  • And for each object, will its value be calculated and substituted? - Schweigert Vitalij
  • @SchweigertVitalij, yes. The code above is the constructor for the Baukloetze class. Creating a class new Baukloetze(1.2, 800d); You pass the necessary parameters to the constructor and they are stored in the corresponding variables and the mass is calculated from them, and so on for each object. - Pollux
  • If you have a little time, could you help me more? - Schweigert Vitalij
  • task: There are 4 types of cubes with different density, but with the same density. From the cubes you can make a column and calculate the force with which they act on the surface. You can add cubes and remove, respectively, only on top of this column. (density * volume = mass. Mass at 9.81 = force). How do I better implement this all through LinkedList? By assignment, this is the method I need to use! - Schweigert Vitalij
  • @SchweigertVitalij, if you are satisfied with the above answer to the question, check it with a tick on the left. The rules of the resource are such that 1 question = 1 answer. If you have any other questions, please create a new one. - Pollux