Homework
5 Details for MPCS 51410
Each
homework assignment will consist of a small problem in some OO
code, which is somehow problematic, given your developing
understanding of good OO best practices and patterns, you will
improve, thus eliminating any negative issues in the code you
are given. Each homework assignment is intended to give every
student hands-on experience with the core concepts covered
during the course. You may code your solution to the
problem in any legitimate OO language of your choice. You
need to submit labs to the TAs for grading--see submission
instructions below. Generally, unless otherwise specified,
you will have one week to complete each assigned homework
problem.
See
the
syllabus for information on grading. Turning in lab
assignments on time is required, without exception, and all late deliveries will be
penalized, regardless
of cause. Submit your assignments to the
subversion repository according to the directions on the
syllabus page.
You
may
write these solutions in any programming language of
your choice. Our suggestion is now is not the time to
learn a new programming language along with the concepts
themselves. So our suggestion is to use whatever
programming language you know best.
Homework
5 Due: 5:00 pm, Monday,
March 8, 2021
Coding
Problem:
BACKGROUND:
Like
all programming-related problems, learning a new programming
language or style is not an exercise in reading but rather an
exercise in thinking and typing. This homework is designed
to give you hands-on experience in some fundamental skills
involved in object-oriented design and coding. You will
generally find the References section below helpful in addition
to the required and recommended reading.
WHAT YOU NEED TO DO:
Download the code here.
You are to read and understand the code, what it is intending to
accomplish, and what the responsibilities are of the class or
classes. Nonetheless, there are one or more problems in the
code which require your insight in their amelioration. Your
mission, should you choose to accept it, is to "fix" the
problem(s) in the code, without introducing any new problems, by
coding a replacement set of code in any legitimate object-oriented
programming language of your choice. Be sure to include a
README with instructions on how to compile (if necessary) and run
your coded solution.
Very important: Inside comments at the top of your
coded solution, you are to fully describe the problem(s) in the
original code as you have determined it, and describe why your
solution eliminates the original problem(s).
Step 1:
Change into the Step.1 subdirectory. Take a look at the
C++ files there. Don't worry, if you don't know C++, we've
provided a Makefile that you can run to build the program called
"mfp_printer." All you have to do to build the program is
type "make" and hit return. [NOTE: If you encounter
errors, it means you have not set up g++ to run. If you're
running a linux laptop, you should never see this error.
If you are running a Mac laptop, it means you will need to
install Apple's XCode software (or just run it on the
cluster...see below). You should go here
and follow the instructions for installing the XCode command
line tools. If you are running Windoze, you can avoid all
this effort if you just scp the downloaded gz file to the
cluster. It will all run just fine there. In fact,
anyone, even Mac people, can run this on the cluster to simplify
their lives.]
Examine the code in the Step.1 subdirectory. Make sure you
understand the call path starting with the main() function (in
Main.cpp). It's a very simple program and should not take
you long to figure out what's going on. You may type make
and it will compile the code and you can execute it by running
the generated output, mfp_printer.
Step 2:
After you have an idea of what's
happening with the Step.1 code (pay particular attention to the
instantiation of a new Cartridge in Printer.cpp), change to the
Step.2 directory, and type make. You will see some
errors. To wit (at least on the Mac):
~/UofC/51410/src/D/Step.2:$ make
g++ -I. -g -Wall -ansi -c Printer.cpp
Printer.cpp:8:25: error: no matching constructor for
initialization of 'Cartridge'
Cartridge * c = new Cartridge( name
);
^ ~~~~
./Cartridge.h:9:7: note: candidate constructor (the
implicit copy constructor) not viable: no known conversion
from
'std::__1::string' (aka
'basic_string<char, char_traits<char>,
allocator<char> >') to 'const Cartridge' for 1st
argument
class Cartridge
^
./Cartridge.h:12:3: note: candidate constructor not
viable: requires 2 arguments, but 1 was provided
Cartridge(string model_no, int num_pages);
^
1 error generated.
make: *** [Printer.o] Error 1
[mark@MusicServer]~/UofC/51410/src/D/Step.2
$
Running the same make command on linux gives a similar set of
errors:
mark@poster:/tmp/Step.2$ make
g++ -I. -g -Wall -ansi -c Main.cpp
g++ -I. -g -Wall -ansi -c Printer.cpp
Printer.cpp: In member function ‘void
Printer::prepare()’:
Printer.cpp:8:41: error: no matching function for call
to ‘Cartridge::Cartridge(std::__cxx11::string&)’
Cartridge * c = new Cartridge(
name );
^
In file included from Printer.cpp:2:0:
Cartridge.h:12:3: note: candidate:
Cartridge::Cartridge(std::__cxx11::string, int)
Cartridge(string model_no, int num_pages);
^
Cartridge.h:12:3: note: candidate expects 2
arguments, 1 provided
Cartridge.h:9:7: note: candidate:
Cartridge::Cartridge(const Cartridge&)
class Cartridge
^
Cartridge.h:9:7: note: no known conversion
for argument 1 from ‘std::__cxx11::string {aka
std::__cxx11::basic_string<char>}’ to ‘const
Cartridge&’
Makefile:21: recipe for target 'Printer.o' failed
make: *** [Printer.o] Error 1
You should notice that the program will not build, and it
will not build because in the Step.2 code, the constructor for a
Cartridge has changed, and that change has affected the code in
Printer.cpp, specifically on line 7 or 8:
Cartridge * c = new Cartridge( name );
The key takeaway here is that the Printer.cpp file is dependent
on the concrete class Cartridge, and specifically, it's
constructor. In light of our SOLID principles, as well as
our creational patterns, please take the Step.2 code, rewrite it
in any OO language you'd like (including C++), and fix the
problem(s). For hints, see the References section below.
References:
You
may
find the following references helpful:
https://web.archive.org/web/20110714224327/http://www.objectmentor.com/resources/articles/dip.pdf
https://en.wikipedia.org/wiki/Dependency_inversion_principle
https://www.martinfowler.com/articles/dipInTheWild.html
Submitting:
Use the
folder named "hw5" in your Subversion repository. See the
syllabus for more info about submission using Subversion. Upload
your HW5 solution file(s) and any supporting materials to the
repo.