Comp 313/413: Test 2 Practice
Problem 1
a.TRUEor FALSE (circle one)? To add another shape (such as Ellipse) to the graphical shapes in project 3, we must add the corresponding Shape implementation No changes to existing classes or interfaces are required.
b.TRUEor FALSE (circle one)? To add another capability (a Visitor such as BoundingBox) to the graphical shapes in project 3, we must define the corresponding method in the Shape interface and implement it in all classes that implement this
Problem 2
In this test, we will look at a data structure for representing containers of items produced by a 3D printer. The overall interface for these containers is here:
interface Container {
int size(); // # of 3D-printed items in the Container Container duplicate(int factor);
// duplicates the number of Items by the factor, presumably by 3D-printing them void collect(Collection- warehouse);
// moves all 3D-printed Items in the Container into the given warehouse
}
a.First, complete the following class for representing individual3D-printed items in a abstract class Item implements Container {
@Override public int size() { // TODO}
@Override public Container duplicate(final int factor) { /* TODO in 2d) below, not here */ }
@Override public void collect(final Collection- warehouse) { // TODO}}
// Here are some concrete 3D-printed items to package up and put in the warehouse:
class Gear extends Item { /* intentionally left empty */ } class Flange extends Item { /* intentionally left empty */ }\
b.Next, complete the following class to collect up multiple Containers of 3D-printed class Carton implements Container {
public Carton(final Container… contents) { this.contents = Arrays.asList(contents); } private final List contents;
public List getcontents() { return contents; }
@Override public int size() { // TODO}
@Override public Container duplicate(final int factor) { /* TODO in 2d) below, not here */ }
@Override public void collect(final Collection- warehouse) { // TODO}
c.Now,complete the following class for representing multiples of the same sub-container in a compact Concretely, a Crate node makes its contents (sub-container) appear howmany times for the purpose of the operations size and collect. The size of each Crate node is the actual size of the node’s contents times the multiplier; e.g., Crate(4, Carton(Gear, Flange)) has size 8.
class Crate implements Container {
public Crate(final int howmany, final Container contents) { this.howmany = howmany; this.contents = contents;
}
private final int howmany; private final Container contents;
public int getMultiplier() { return howmany; }
public Container getcontents() { return contents; }
@Override public int size() { // TODO
}
@Override public Container duplicate(final int factor) { // TODO
// duplication occurs here!!! use a suitable new multiplier in the result
}
@Override public void collect(final Collection- warehouse) { // TODO
}
d.Implement the duplicate methods for Itemand Carton Keep your code as simple as possible. Hints: these are simple methods; introduce new Container nodes as appropriate.
/* Item: */ @Override public Container duplicate(final int factor) { // TODO
}
/* Carton: */ @Override public Container duplicate(final int factor) { // TODO
}
e.Usingthe classes Carton, Gear, and Flange, write code that builds the following hybrid Container:
ashipping container (main Carton) of the Container
asub-carton with three separate Gears (without any Crate)
another sub-cartoncontaining
? a Crate of one Gear
? a Crate of two Flanges
final Carton shippingContainer = new Carton( // TODO
);
f.Whatis the size of this Container, e., what should shippingContainer.size() return?
g.Whatis the result of collecting the Item in this Container, e., what will be in the warehouse after executing final List- warehouse = …; shippingContainer.collect(warehouse);?
h.Whatis the size of the Container after having duplicated it fourfold by evaluating shippingContainer.duplicate(4);, i.e., what should shippingContainer.size() return now?
i.Whatare the two software design pattern underlying the Container interface and its implementation classes called? Circle exactly Composite Strategy Decorator Abstract Factory
j.TRUEor FALSE? (circle one)? Containers with Crates can always be replaced by Containers with explicit multiples instead of Crates?
k.Whichis the relevant software design principle for the requirement in the preceding subproblem j? Circle exactly
Design by Contract Single Responsibility Principle Moore’s Law Liskov Substitution Principle
i.TRUEor FALSE (circle one)? To add another kind of Container (such as a new type of Item), we must add the corresponding Container implementation No changes to existing classes or interfaces are required.
m.TRUEor FALSE (circle one)? To add another capability (such as collect) to these Containers, we must define the corresponding method in the Container interface and implement it in all classes that implement this interface.