There are 4 types of cubes with different density, but with the same volume. 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 implement this all through LinkedList? By assignment, this is the method I need to use!
1 answer
I wanted to solve the problem)
class Cube{ private final int volume = 3; private double density, power; public Cube(int density) { this.density = density; power = 9.81 * density * volume; } public double getPower(){ return power; } } public class Test { public static void main(String[] args) { double power = 0; LinkedList<Cube> column = new LinkedList<>(); //Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΠΊΡΠ±ΠΈΠΊΠΈ column.addFirst(new Cube(10)); column.addFirst(new Cube(20)); column.addFirst(new Cube(30)); column.addFirst(new Cube(40)); //ΠΏΠ΅ΡΠ΅ΡΠΈΡΠ»ΡΠ΅ΠΌ ΠΊΡΠ±ΠΈΠΊΠΈ Π² "ΡΡΠΎΠ»Π±ΠΈΠΊΠ΅", ΡΡΠΌΠΌΠΈΡΡΠ΅ΠΌ ΠΌΠ°ΡΡΡ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΈ Π²ΡΠ²ΠΎΠ΄ΠΈΠΌ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ for (Cube cube : column) { power += cube.getPower(); } System.out.println("ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΊΡΠ±ΠΈΠΊΠΎΠ²: " + column.size() + ", ΡΠΈΠ»Π° Π½Π° ΠΏΠΎΠ²Π΅ΡΡ
Π½ΠΎΡΡΡ: " + power); power = 0; //ΡΠ΄Π°Π»ΡΠ΅ΠΌ ΠΎΠ΄ΠΈΠ½ Π²Π΅ΡΡ
Π½ΠΈΠΉ ΠΊΡΠ±ΠΈΠΊ column.removeFirst(); //ΠΏΠ΅ΡΠ΅ΡΠΈΡΠ»ΡΠ΅ΠΌ ΠΈ Π²ΡΠ²ΠΎΠ΄ΠΈΠΌ for (Cube cube : column) { power += cube.getPower(); } System.out.println("ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΊΡΠ±ΠΈΠΊΠΎΠ²: " + column.size() + ", ΡΠΈΠ»Π° Π½Π° ΠΏΠΎΠ²Π΅ΡΡ
Π½ΠΎΡΡΡ: " + power); } } |