/import/teaching/BSc/1st/ItP/grommets
you
found two .class
files: Grommet.class
and Machine.class
.
A Machine
object had five public methods:
int getSpeed() int getTemp() void addFuel(double) void addCoolant(double) Grommet pushButton()A
Grommet
object had two public methods:
int getQuality() double getWidth()A
Machine
object could be constructed with a zero argument
constructor.
This represented a situation in which machines produce grommets. Any machine
at any particular time had a temperature and a speed which may be changed
by adding fuel to it or adding coolant to it. You could 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
returned the speed and temperature of a machine,
while addFuel
and addCoolant
represented adding
amounts of fuel and coolant (assume the amounts are in grammes) to a
machine.
The machine method pushButton
represented pushing a button
that made the machine produce a grommet. The grommet was represented
by a Grommet
object. A machine's temperature and speed was
slowed down when it produces a grommet. In this simulation, there were
only two aspects of a grommet we were interested in: its quality which is
given as an integer value, and its width which is a floating point value.
These value could be obtained in the simulation by calling the
getQuality
and getWidth
methods. A Grommet
object was immutable, so the result of these methods when applied to a
particular Grommet
object never varied.
Machines were independent of each other, so calling any method on one
Machine
object wouldn'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).
The example below was given, of 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 was 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 have written programs that represented the scenarios given. The answers which should be produced if the
.class
files from /import/teaching/BSc/1st/ItP/grommets
were used
were also given. You were 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
Code:
class UseMachine1 { public static void main(String[] args) { Machine m1 = new Machine(); m1.addFuel(15); m1.addCoolant(12); System.out.println("Temperature is: "+m1.getTemp()); } }
Expected answer: Speed is: 828
Code:
class UseMachine2 { public static void main(String[] args) { Machine m1 = new Machine(); while(m1.getTemp()<=60) m1.addFuel(5); System.out.println("Speed is: "+m1.getSpeed()); } }
Expected answer: Speed is: 804
Code:
class UseMachine3 { public static void main(String[] args) { Machine m1 = new Machine(); while(m1.getSpeed()<=1000) m1.addFuel(5); while(m1.getTemp()>=55) m1.addCoolant(3); System.out.println("Speed is: "+m1.getSpeed()); } }
Expected answer: The quality of the widest grommet is: 15
Code:
class UseMachine4 { public static void main(String[] args) { Machine m1 = new Machine(); Grommet g1,g2; int quality; g1=m1.pushButton(); g2=m1.pushButton(); quality=g1.getWidth()>g2.getWidth()?g1.getQuality():g2.getQuality(); System.out.println("The quality of the widest grommet is: "+quality); } }
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).
Code:
class UseMachine5 { public static void main(String[] args) { Machine m1 = new Machine(); int i=0; double sum=0; while(i<100) { sum+=m1.pushButton().getWidth(); i++; } System.out.println("Average width is: "+sum/100); } }
Expected answer: Average width is: 1.5109166666666676
Code:
class UseMachine6 { public static void main(String[] args) { Machine m1 = new Machine(); int i=0,count=0; double sum=0; while(i<100) { Grommet g1 = m1.pushButton(); if(g1.getQuality()>17) { sum+=g1.getWidth(); count++; } i++; } System.out.println("Average width is: "+sum/count); } }
Expected answer: The machine which starts with 50g of fuel and 16g of
coolant produces the better grommets
Code:
class UseMachine7 { public static void main(String[] args) { Machine m1 = new Machine(), m2 = new Machine(); int i=0,sum1=0,sum2=0; m1.addFuel(50); m1.addCoolant(16); m2.addFuel(40); m2.addCoolant(13); while(i<100) { sum1+=m1.pushButton().getQuality(); sum2+=m2.pushButton().getQuality(); i++; } System.out.print("The machine which starts with "); if(sum1>sum2) System.out.print("50g of fuel and 16g of coolant "); else System.out.print("40g of fuel and 13g of cooleant "); System.out.println("\nproduces the better grommets"); } }
Expected answer: Total fuel added: 189g
Code:
class UseMachine8 { public static void main(String[] args) { Machine m1 = new Machine(); int count=0,totalFuel=0; while(count<200) { Grommet g1 = m1.pushButton(); if(m1.getSpeed()<300) while(m1.getSpeed()<=700) { m1.addFuel(3); m1.addCoolant(1); totalFuel+=3; } count++; } System.out.println("Total fuel added: "+totalFuel); } }
Expected answer: Average width is 1.50715
Code:
class UseMachine9 { public static void main(String[] args) { Machine m1 = new Machine(), m2 = new Machine(), m3; int i=0,sum1=0,sum2=0; double sum3=0; m1.addFuel(60); m1.addCoolant(35); m2.addFuel(50); m2.addCoolant(25); while(i<100) { sum1+=m1.pushButton().getQuality(); sum2+=m2.pushButton().getQuality(); i++; } m3=sum1>sum2?m1:m2; m3.addFuel(30); m3.addCoolant(10); i=0; while(i<100) { sum3+=m3.pushButton().getWidth(); i++; } System.out.println("Average width is "+sum3/100); } }
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.Code:
class UseMachine10 { public static void main(String[] args) { Machine m1 = new Machine(), m2 = new Machine(); int count1=0,count2=0; double sum3=0; m1.addFuel(70); m1.addCoolant(50); m2.addFuel(65); m2.addCoolant(30); while(count1+count2<100) if(m1.getSpeed()>m2.getSpeed()) { m1.pushButton(); count1++; } else { m2.pushButton(); count2++; } System.out.print("The machine that started with 70g of fuel "); System.out.println("and 50g of coolant "); System.out.println("produced "+count1+" grommets"); System.out.print("The machine that started with 65g of fuel "); System.out.println("and 30g of coolant "); System.out.println("produced "+count2+" grommets"); } }
Matthew Huntbach 14th December 2000