(Solved) : Test Practice Questions Java Question 3 Consider Following Code Homework Assignments Abst Q42681002 . . .

Test practice questions .. in Java

Question 3
Consider the following code from the homework assignments:
abstract public class AbstractUndoableCommand implementsUndoableCommand {
private CommandHistory _history;
protected AbstractUndoableCommand(CommandHistory history) {_history = history; }
abstract protected boolean setup();
abstract protected void dodo();
abstract public void undo();
final public void execute() {
if (setup()) {
dodo();
_history.addCommand(this);
}
}
final public void redo() {
dodo();
}
}
public class CompositeCommand extends AbstractUndoableCommand{
private final List _list;
public CompositeCommand(CommandHistory history) {
super(history);
_list = new ArrayList();
}
public void addCommand(AbstractUndoableCommand cmd) {_list.add(cmd); }
public boolean setup() { return true; }
public void dodo() {
Iterator i = _list.iterator();
while (i.hasNext()) {
AbstractUndoableCommand cmd = (AbstractUndoableCommand)i.next();
cmd.dodo();
}
}
public void undo() {
ListIterator i = _list.listIterator(_list.size());
while (i.hasPrevious()) {
AbstractUndoableCommand cmdObject = (AbstractUndoableCommand)i.previous();
cmdObject.undo();
}
}
}
public class AddCommand extends AbstractUndoableCommand {
private Database _db; private int _year;
private Video _newVideo; private String _category;
private String _title; private int _numCopies;
public AddCommand(Database db, String title, int year, Stringcategory,
Name: 4
int numCopies) {
super(db); _year = year;
_db = db; _category = category;
_title = title; _numCopies = numCopies;
}
protected boolean setup() {
return (_db.findVideo(_title) == null);
}
public void dodo() {
_newVideo = new VideoImpl(_title, _year, _category,_numCopies);
_db.addVideo(_newVideo);
}
public void undo() {
_db.removeVideo(_title);
}
}
Given the following main program fragment
AbstractUndoableCommand c11 = new AddCommand(db, “Vanishing Point”,1973, “Drama”, 1);
AbstractUndoableCommand c12 = new AddCommand(db, “AmericanGraffiti”, 1975, “Comedy”, 3);
AbstractUndoableCommand c13 = new AddCommand(db, “El Mariachi”,1996, “Drama”, 2);
AbstractUndoableCommand c14 = new AddCommand(db, “Play it again,Sam”, 1978, “Comedy”, 4);
CompositeCommand c1 = new CompositeCommand(db);
c1.addCommand(c11);
c1.addCommand(c12);
c1.addCommand(c13);
c1.addCommand(c14);
c1.execute();
draw an object interaction diagram tracing the method executionsfor the call c1.execute(). Your
diagram should have columns corresponding to objects c1, c11, c12,c13, c14, with the leftmost column
being c1. Do not include any other objects.

Question 4

In this problem, we will look at the following “Stream”iterator.
abstract class Stream implements Iterator {
public final boolean hasNext() { return true; }
}
class IntegerStream extends Stream {
private int _i = -1;
public Object next() { _i++; return new Integer(_i);}
}
See the comments for the output of the following code:
IntegerStream I = new IntegerStream();
System.out.println(I.next()); // prints 0 on the screen
System.out.println(I.next()); // prints 1 on the screen
System.out.println(I.next()); // prints 2 on the screen
System.out.println(I.next()); // prints 3 on the screen
System.out.println(I.next()); // prints 4 on the screen
(a) Finish the code of class FilteredStream implementing Iteratorso that the following code
fragments work as indicated:
IntegerStream I = new IntegerStream();
FilteredStream F = new FilteredStream(I, new IsEven());
System.out.println(F.next()); // prints 0 on the screen
System.out.println(F.next()); // prints 2 on the screen
System.out.println(F.next()); // prints 4 on the screen
System.out.println(F.next()); // prints 6 on the screen
System.out.println(F.next()); // prints 8 on the screen
IntegerStream J = new IntegerStream();
J.next(); // move forward one item in J
FilteredStream G = new FilteredStream(J, new IsEven());
System.out.println(G.next()); // prints 2 on the screen
System.out.println(G.next()); // prints 4 on the screen
System.out.println(G.next()); // prints 6 on the screen
System.out.println(G.next()); // prints 8 on the screen
IntegerStream K = new IntegerStream();
class Div3 implements Predicate {
public boolean evaluate(int n) { return (n%3) == 0; }
}
FilteredStream H = new FilteredStream(K, new Div3());
System.out.println(H.next()); // prints 0 on the screen
System.out.println(H.next()); // prints 3 on the screen
System.out.println(H.next()); // prints 6 on the screen
System.out.println(H.next()); // prints 9 on the screen
Your job is to write the method next() in the following code.
Name: 13
class FilteredStream extends Stream {
private Stream _it;
private Predicate _p;
public FilteredStream(Stream it, Predicate p) {
_it = it;
_p =p;
}
public Object next() {
}
}
(b) Consider the following classes:
class NotDivn implements Predicate {
final private int _n;
NotDivn(int n) {
_n = n;
}
public boolean evaluate(int m) {
return (m%_n) != 0;
}
}
class WhatAPain extends Stream {
private Stream _it;
public WhatAPain(Stream it) {
_it = it;
}
public Object next() {
final int n = ((Integer) _it.next()).intValue();
final Predicate d = new NotDivn(n);
Stream newit = new FilteredStream(_it, d);
_it = newit;
return (new Integer(n));
}
}
What does the following code print?
IntegerStream I = new IntegerStream();
System.out.println(I.next()); // prints 0 on the screen
System.out.println(I.next()); // prints 1 on the screen
WhatAPain w = new WhatAPain(I);
System.out.println(w.next());
System.out.println(w.next());
System.out.println(w.next());
System.out.println(w.next());
System.out.println(w.next());
System.out.println(w.next());
System.out.println(w.next());
System.out.println(w.next());

Expert Answer


Answer to Test practice questions .. in Java Question 3 Consider the following code from the homework assignments: abstract public…

Leave a Comment

About

We are the best freelance writing portal. Looking for online writing, editing or proofreading jobs? We have plenty of writing assignments to handle.

Quick Links

Browse Solutions

Place Order

About Us