Problem:
Machine
|
-
weight : int
-
cost : int
|
//constructor
and set/get
|
Assembler
|
-
machines : Queue<Machine>
|
+ Assembler()
+ enqueueMachine(machine :
Machine) : void
+ finishMachine() : void
+
getCurrentMachine() : Machine
+
toString() : String
|
Implement
the previous two classes.
Class
machine has two private variables with a constructor and setters and getters.
The Assembler class has a Queue of machines with the following functions:
- enqueueMachine(machine :
Machine): adds a machine to the queue
- finishMachine(): removes the
machine at the front of the queue
- getCurrentMachine(): returns the machine at the front of the queue
Solution:
/**---------------------------Assembler.java--------------------------*/ import java.util.LinkedList; import java.util.Queue; public class Assembler { private Queuemachines = new LinkedList (); public Assembler() { } public void enqueueMachine(Machine machine) { machines.offer(machine); } public void finishMachine() { machines.poll(); } public Machine getCurrentMachine() { return machines.element(); } public String toString() { return "Machines are: " + machines + "\n"; } } /**---------------------------Machine.java--------------------------*/ public class Machine { private int weight; private int cost; public Machine(int weight, int cost) { this.weight = weight; this.cost = cost; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public int getCost() { return cost; } public void setCost(int cost) { this.cost = cost; } public String toString() { return "Cost: " + cost + " Weight: " + weight; } } /**---------------------------Tester.java--------------------------*/ public class Tester { public static void main(String[] args) { Assembler assembler = new Assembler(); assembler.enqueueMachine(new Machine(1,10)); assembler.enqueueMachine(new Machine(2,20)); assembler.enqueueMachine(new Machine(3,30)); assembler.enqueueMachine(new Machine(4,40)); System.out.println(assembler); assembler.finishMachine(); System.out.println(assembler); System.out.println(assembler.getCurrentMachine()); } }
No comments :
Post a Comment