Assignment 4, due Sept 18
Part of
the homework for CS:2820, Fall 2015
|
On every assignment, write your name legibly as it appears on your University ID card! Homework is due on paper at the start of class on the day indicated (usually Friday). Exceptions will be made only by advance arrangement (excepting "acts of God"). Late work must be turned in to the TA's mailbox (ask the CS receptionist in 14 MLH for help). Never push homework under someone's door!
s = sc.nextLine();
Java allows you to call a method for its side effect and discard the return value. For example, you can write this code to skip the rest of a line and discard the text:
sc.nextLine();
A Scavenger hunt question: How does this language feature interfere with a possible alternative overloading rule that might otherwise be a useful extension to Java? (0.5 points)
class A { int i; A( int i ) { // missing code } }
The goal is that the initializer call A(5) should initialize field i of the newly allocated instance of class A to the value 5. The problem is, the field name is the same as the formal parameter name used in the initializer. One way to solve this is to use a different formal parameter name. You could write this:
class A { int i; A( int j ) { i = j; } }
A Scavenger hunt question: There is another solution to this problem that does not involve changing any field or parameter names. What is it? (0.5 points)
In the logic simulation problem, the class gate contains, up to this point, a bug notice saying that there are many kinds of gates, each with different behavior. Specifically, every gate has a method called inputChange(t,i,v) that is used to tell the gate that, at time t, input i, of that gate changed from its previous value to value v. This method determines whether the will schedule an output change event at some later time, depending on all the current values of its inputs and output and depending on the type of gate.
A Problem: Given that there are gates that perform each of the classical functions of Boolean logic, how would you use the subclass mechanism to describe different kinds of gates? Specifically, which attributes of a gate would be defined in the generic gate class and which attributes would be defined in the specific subclasses for the different kinds of gates? (1 point)