Spaces:
Running
Running
File size: 3,460 Bytes
121cf62 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
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.
|