Introduction to Programming: Exercises for week 6

In the directory /import/teaching/BSc/1st/ItP/grommets you will find two .class files: Grommet.class and Machine.class.

A Machine object has five public methods:

int getSpeed()
int getTemp()
void addFuel(double)
void addCoolant(double)
Grommet pushButton()
A Grommet object has two public methods:
int getQuality()
double getWidth()
A Machine object can be constructed with a zero argument constructor.

This represents a situation in which machines produce grommets. Any machine at any particular time has a temperature and a speed which may be changed by adding fuel to it or adding coolant to it. You may assume adding fuel will always cause the speed to increase and adding coolant will always cause the temperature to decrease. The methods getSpeed and getTemp return the speed and temperature of a machine, while addFuel and addCoolant represent adding amounts of fuel and coolant (assume the amounts are in grammes) to a machine.

The machine method pushButton represents pushing a button that will make the machine produce a grommet. The grommet is represented by a Grommet object. A machine's temperature and speed will be slowed down when it produces a grommet. In this simulation, there are only two aspects of a grommet we are interested in: its quality which is given as an integer value, and its width which is a floating point value. These value can be obtained in the simulation by calling the getQuality and getWidth methods. A Grommet object is immutable, so the result of these methods when applied to a particular Grommet object will never vary.

Machines are independent of each other, so calling any method on one Machine object won't affect any other machine object (note, the ability to have class variables which in Java are defined as static variables means this can't be assumed).

As an example, here is a program that represents starting a new machine, adding 10g of fuel then 5g of coolant, and making it produce a grommet. The quality of the grommet is then printed.

class UseMachine0
{
 public static void main(String[] args)
 {
  Machine m1 = new Machine();
  m1.addFuel(10);
  m1.addCoolant(5);
  Grommet g1 = m1.pushButton();
  System.out.println("Grommet quality is: "+g1.getQuality());
 }
}
In this exercise you should write programs that represent the scenarios given. The answers which should be produced if the .class files from /import/teaching/BSc/1st/ItP/grommets are used are also given. You are not expected to write your own Machine.java and Grommet.java files.

1) Write a program that starts a machine, adds 15g of fuel then 12g of coolant, and then prints the resulting temperature of the machine.

Expected answer: Temperature is: 47

2) Write a program that starts a machine, keeps adding fuel to it in 5g amounts until its temperature is over 60, then prints the resulting speed of the machine.

Expected answer: Speed is: 828

3) Write a program that starts a machine, keeps adding fuel to it in 5g amounts until its speed is over 1000, and then keeps adding coolant to it in 3g amounts until its temperature is under 55. Then it prints the speed of the machine.

Expected answer: Speed is: 804

4) Write a program that starts a machine, makes it produce two grommets and prints the quality of whichever of the grommets has the greatest width.

Expected answer: The quality of the widest grommet is: 15

5) Write a program that starts a machine, makes it produce 100 grommets, and prints the average width of the grommets.

Expected answer:Average width is: 1.5074199999999993
(there are ways to get floating point numbers formatted in Java so they don't print out a silly number of decimal places, but you needn't use them here).

6) Write a program that starts a machine, makes it produce 100 grommets, and prints the average width of all the grommets whose quality is over 17.

Expected answer: Average width is: 1.5109166666666676

7) Write a program that starts two machines. To one it adds 50g of fuel followed by 16g of coolant, to the other it adds 50g of fuel followed by 13g of coolant. It produced 100 grommets from each, and says which produces the higher quality grommets on average.

Expected answer: The machine which starts with 50g of fuel and 16g of coolant produces the better grommets

8) Write a program that starts a machine and produces 200 grommets. If at any time after producing a grommet the speed of the machine is below 300, it keeps adding 3g of fuel followed by 1g of coolant until the speed is above 700 before producing the next grommet. At the end, it prints how much fuel in total has been aded.

Expected answer: Total fuel added: 189g

9) Write a program that starts two machines. To one it adds 60g of fuel followed by 35g of coolant, to the other it adds 50g of fuel followed by 25g of coolant. It then produces 100 grommets from each. After this, it adds 30g of fuel followed by 10g of coolant to whichever machine produced the higher quality grommets on average. Then it produces 100 more grommets from that machine and prints the average width of this further 100.

Expected answer: Average width is 1.50715

10) Write a program that starts two machines. To one it adds 70g of fuel followed by 50g of coolant, to the other it adds 65g of fuel followed by 30g of coolant. It then produes 100 grommets in total. Each grommet is produced from whichever machine has the highest speed at the time the grommet is to be produced, with a new test of speeds for the next grommet. It then prints how many grommets each machine has produced.

Expected answer:

The machine that started with 70g of fuel and 50g of coolant produced 42 grommets.
The machine that started with 65g of fuel and 30g of coolant produced 58 grommets.

Matthew Huntbach
31st October 2000