State the three lines of output produced by the following code.
class Tank:
def __init__(self, capacity):
self.capacity = capacity
self.level = 0
def fill(self, litres):
self.level = min(self.level + litres, self.capacity)
t1 = Tank(50)
t2 = Tank(20)
t1.fill(30)
t2.fill(30)
t1.fill(30)
print(t1.level)
print(t2.level)
print(t1.capacity - t1.level)
Show the markscheme
Markscheme (3 marks)
50200
A full-marks answer
Output: 50, 20 and 0. t1 goes from 0 to 30 and then to min(60, 50), which is 50 (it cannot exceed its capacity); t2 receives min(30, 20), which is 20, because its capacity is 20; finally . Each tank keeps its own level.
State the three lines of output produced by the following code.
class Tank {
private int capacity;
private int level;
public Tank(int capacity) {
this.capacity = capacity;
this.level = 0;
}
public void fill(int litres) {
level = Math.min(level + litres, capacity);
}
public int getCapacity() {
return capacity;
}
public int getLevel() {
return level;
}
}
public class Main {
public static void main(String[] args) {
Tank t1 = new Tank(50);
Tank t2 = new Tank(20);
t1.fill(30);
t2.fill(30);
t1.fill(30);
System.out.println(t1.getLevel());
System.out.println(t2.getLevel());
System.out.println(t1.getCapacity() - t1.getLevel());
}
}
Show the markscheme
Markscheme (3 marks)
50200
A full-marks answer
Output: 50, 20 and 0. t1 goes from 0 to 30 and then to Math.min(60, 50), which is 50 (it cannot exceed its capacity); t2 receives Math.min(30, 20), which is 20, because its capacity is 20; finally . Each Tank object keeps its own level.