Spaces:
Running
Running
public static int howManyFit(Container largeContainer, Container smallContainer) {...}
121cf62
verified
| The program has defined an interface class 'Container' and its implementing classes 'Barrel' and 'Bottle'. | |
| Write a method: | |
| public static int howManyFit(Container largeContainer, Container smallContainer) | |
| which returns as an integer the information about how many small containerfuls completely fit into the large container. | |
| Example of calling the method: | |
| public static void main(String[] args) { | |
| Barrel barrel = new Barrel("oak", 5.0); | |
| Bottle bottle = new Bottle("orange", 1.5); | |
| System.out.println(howManyFit(barrel, bottle)); | |
| } | |
| The program prints: | |
| 3 | |
| ==================== | |
| import java.util.Random; | |
| public class Test { | |
| public static void main(String[] args) { | |
| final Random r = new Random(); | |
| String[] materials = "oak alder birch plastic steel copper iron rubber".split(" "); | |
| double[] volumes = {2.0, 3.0, 4.5, 5.0, 6.0, 7.0, 8.0, 10.0, 20.0, 25.0, 30.0}; | |
| double[] volumes2 = {0.1, 0.25, 0.5, 0.75, 1.0, 2.0}; | |
| for (int test = 1; test <= 3; test++) { | |
| System.out.println("Test " + test); | |
| String largeMat = materials[r.nextInt(materials.length)]; | |
| String smallMat = materials[r.nextInt(materials.length)]; | |
| Container large = new Barrel(largeMat, volumes[r.nextInt(volumes.length)] + volumes[r.nextInt(volumes.length)]); | |
| Container small = new Bottle(smallMat, volumes2[r.nextInt(volumes2.length)]); | |
| System.out.println("Large container: " + large); | |
| System.out.println("Small container: " + small); | |
| System.out.println("The large container can hold " + howManyFit(large, small) + " small containerfuls."); | |
| } | |
| } | |
| //ADD | |
| public static int howManyFit(Container largeContainer, Container smallContainer) { | |
| // https://www.geeksforgeeks.org/java/convert-double-to-integer-in-java/ | |
| // calculate fraction and return it as an int | |
| return (int) (largeContainer.getVolume() / smallContainer.getVolume()); | |
| } | |
| } | |
| interface Container { | |
| double getVolume(); | |
| } | |
| class Bottle implements Container { | |
| private String name; | |
| private double volume; | |
| public Bottle(String name, double volume) { | |
| this.name = name; | |
| this.volume = volume; | |
| } | |
| @Override | |
| public double getVolume() { | |
| // TODO Auto-generated method stub | |
| return volume; | |
| } | |
| @Override | |
| public String toString() { | |
| return name + " bottle, volume " + volume + " liters"; | |
| } | |
| } | |
| class Barrel implements Container { | |
| private String name; | |
| private double volume; | |
| public Barrel(String name, double volume) { | |
| this.name = name; | |
| this.volume = volume; | |
| } | |
| @Override | |
| public double getVolume() { | |
| // TODO Auto-generated method stub | |
| return volume; | |
| } | |
| @Override | |
| public String toString() { | |
| return name + " barrel, volume " + volume + " liters"; | |
| } | |
| } | |
| Test 1 | |
| Large container: copper barrel, volume 40.0 liters | |
| Small container: plastic bottle, volume 0.25 liters | |
| The large container can hold 160 small containerfuls. | |
| Test 2 | |
| Large container: plastic barrel, volume 22.0 liters | |
| Small container: rubber bottle, volume 0.1 liters | |
| The large container can hold 220 small containerfuls. | |
| Test 3 | |
| Large container: steel barrel, volume 33.0 liters | |
| Small container: alder bottle, volume 0.5 liters | |
| The large container can hold 66 small containerfuls. | |