On every assignment, write your name and section number. Write your name as
it appears on your university ID card! Use the section number as it appears
in your current registration! We will not do detective work to figure
out who did what. Homework is due on paper at the start of your discussion
section. Exceptions will be made only by advance arrangement with your TA
(excepting "acts of God"). Never push homework under someone's door!
-
Background:
In the code distributed with the assignment for MP5, the columns of
simulation output will only be properly titled if the gate names are all
exactly 5 characters long.
The code that does the relevant printing is:
System.out.print( " " + g.name );
A problem:
Write code to replace the above that pads shorter names to 5 characters and
truncates longer names. This is a small exercise in Java string manipulation.
Computational efficiency is not really important, as this code is only used
once to compute the title line of the output. Hint: It can be done in one
simple expression involving concatenation and substring operations.
(1 point)
-
Background:
The output required for MP5 relies on a method in class Gate called
printValue(). This returns a string giving the appropriate
graphical representation of that gate's logic transitions since the last time
it was called.
a)
What new instance variable must be added to class Gate
to make it possible for printValue() to do its job.
(0.5 points)
b)
Explain how a 2-dimensional array of strings can be used to do
the bulk of the work done by printValue().
(0.5 points)
-
Background:
The output required for
printValue() in MP5 only requires that it show the value at the
instant of each tick of the "print interval clock." If an engineer hooks
an oscilloscope to a real logic gate's output, the scope will show very
brief changes. We can fake this up as follows:
- "| "
-- the output was false, is false and has not changed between.
- "|- "
-- the output was false, is false and was briefly unknown between.
- "|--- "
-- the output was false, is false and was briefly true between.
- " | "
-- the output was unknown, is unknown and has not changed between.
- " -| "
-- the output was unknown, is unknown and was briefly false.
- " |- "
-- the output was unknown, is unknown and was briefly true.
- " |"
-- the output was true, is true and has not changed between..
- " -|"
-- the output was true, is true and was briefly unknown.
- " ---|"
-- the output was true, is true and was briefly false.
We could even generalize this to allow such things as:
- " -|_ "
-- was unknown, is true and was briefly false.
- "|=-- "
-- was false, is false and was briefly unknown and briefly true.
- " -|- "
-- was unknown, is unknown and was briefly true and briefly false.
- " -|= "
-- was unknown, is unknown and was briefly true twice and briefly false once.
- "|-__ "
-- was false, is true and was briefly unknown.
a)
What additional instance variable or variables would you add to class
Gate to allow something approximating this more complex output format?
(0.5 points)
a)
Briefly describe how you would use the data from part a) to create the output
desired. Do not give code!
(0.5 points)