Spaces:
Running
Running
public static int howManyFit(Container largeContainer, Container smallContainer) {...}
Browse files
Week 6: Methods of OO Programming/10B. How Many Can Fit in a Container?
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The program has defined an interface class 'Container' and its implementing classes 'Barrel' and 'Bottle'.
|
| 2 |
+
|
| 3 |
+
Write a method:
|
| 4 |
+
public static int howManyFit(Container largeContainer, Container smallContainer)
|
| 5 |
+
which returns as an integer the information about how many small containerfuls completely fit into the large container.
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
Example of calling the method:
|
| 10 |
+
public static void main(String[] args) {
|
| 11 |
+
Barrel barrel = new Barrel("oak", 5.0);
|
| 12 |
+
Bottle bottle = new Bottle("orange", 1.5);
|
| 13 |
+
|
| 14 |
+
System.out.println(howManyFit(barrel, bottle));
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
The program prints:
|
| 19 |
+
3
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
====================
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
import java.util.Random;
|
| 27 |
+
|
| 28 |
+
public class Test {
|
| 29 |
+
public static void main(String[] args) {
|
| 30 |
+
final Random r = new Random();
|
| 31 |
+
|
| 32 |
+
String[] materials = "oak alder birch plastic steel copper iron rubber".split(" ");
|
| 33 |
+
double[] volumes = {2.0, 3.0, 4.5, 5.0, 6.0, 7.0, 8.0, 10.0, 20.0, 25.0, 30.0};
|
| 34 |
+
double[] volumes2 = {0.1, 0.25, 0.5, 0.75, 1.0, 2.0};
|
| 35 |
+
|
| 36 |
+
for (int test = 1; test <= 3; test++) {
|
| 37 |
+
System.out.println("Test " + test);
|
| 38 |
+
|
| 39 |
+
String largeMat = materials[r.nextInt(materials.length)];
|
| 40 |
+
String smallMat = materials[r.nextInt(materials.length)];
|
| 41 |
+
|
| 42 |
+
Container large = new Barrel(largeMat, volumes[r.nextInt(volumes.length)] + volumes[r.nextInt(volumes.length)]);
|
| 43 |
+
Container small = new Bottle(smallMat, volumes2[r.nextInt(volumes2.length)]);
|
| 44 |
+
|
| 45 |
+
System.out.println("Large container: " + large);
|
| 46 |
+
System.out.println("Small container: " + small);
|
| 47 |
+
|
| 48 |
+
System.out.println("The large container can hold " + howManyFit(large, small) + " small containerfuls.");
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
//ADD
|
| 55 |
+
public static int howManyFit(Container largeContainer, Container smallContainer) {
|
| 56 |
+
// https://www.geeksforgeeks.org/java/convert-double-to-integer-in-java/
|
| 57 |
+
// calculate fraction and return it as an int
|
| 58 |
+
return (int) (largeContainer.getVolume() / smallContainer.getVolume());
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
interface Container {
|
| 81 |
+
double getVolume();
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
class Bottle implements Container {
|
| 88 |
+
private String name;
|
| 89 |
+
private double volume;
|
| 90 |
+
|
| 91 |
+
public Bottle(String name, double volume) {
|
| 92 |
+
this.name = name;
|
| 93 |
+
this.volume = volume;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
@Override
|
| 97 |
+
public double getVolume() {
|
| 98 |
+
// TODO Auto-generated method stub
|
| 99 |
+
return volume;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
@Override
|
| 103 |
+
public String toString() {
|
| 104 |
+
return name + " bottle, volume " + volume + " liters";
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
class Barrel implements Container {
|
| 113 |
+
private String name;
|
| 114 |
+
private double volume;
|
| 115 |
+
|
| 116 |
+
public Barrel(String name, double volume) {
|
| 117 |
+
this.name = name;
|
| 118 |
+
this.volume = volume;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
@Override
|
| 122 |
+
public double getVolume() {
|
| 123 |
+
// TODO Auto-generated method stub
|
| 124 |
+
return volume;
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
@Override
|
| 128 |
+
public String toString() {
|
| 129 |
+
return name + " barrel, volume " + volume + " liters";
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
Test 1
|
| 139 |
+
Large container: copper barrel, volume 40.0 liters
|
| 140 |
+
Small container: plastic bottle, volume 0.25 liters
|
| 141 |
+
The large container can hold 160 small containerfuls.
|
| 142 |
+
|
| 143 |
+
Test 2
|
| 144 |
+
Large container: plastic barrel, volume 22.0 liters
|
| 145 |
+
Small container: rubber bottle, volume 0.1 liters
|
| 146 |
+
The large container can hold 220 small containerfuls.
|
| 147 |
+
|
| 148 |
+
Test 3
|
| 149 |
+
Large container: steel barrel, volume 33.0 liters
|
| 150 |
+
Small container: alder bottle, volume 0.5 liters
|
| 151 |
+
The large container can hold 66 small containerfuls.
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|