Exercise 6A (Midterm Simulation)
We want to realize a program to handle a document archive. For each document the following information is stored: name of the author, title of the document, and number of available copies.
Part 1.
Write a class Document
that implements the following methods:
Document(String a, String t)
: constructor that, given author
and title, constructs a document for which the number of available copies
is 0;
String toString()
: that returns a string with all the
information about the document;
void createCopies(int n)
: that increments by n
the number of available copies;
void sellCopies(int n)
: that decrements by n
the number of available copies; (if n
is greater than the
number of available copies, then such number should become 0);
String getAuthor()
: that returns the name of the author of
the document;
String getTitle()
: that returns the title of the document;
int getCopies()
: that returns the number of available
copies of the document.
An example program that uses class Document
is given:
TestDocument.java
Part 2.
Write a class UseDocument
, client of Document
, that
contains the following public static methods:
static String authorMaxCopies(Document d1, Document d2)
:
that, given two documents d1
and d2
, returns
the author of the document among d1
and d2
for
which more copies are available. If for both documents the same number
of copies are available, the method should return the concatenation of the
names of the two authors, separated by " and "
.
static boolean bothAvailable(Document d1, Document d2, int count)
:
that, given two documents d1
and d2
, returns
true
if at least count
copies of both documents are
available and false
otherwise.
An example program that uses class UseDocument
is given:
TestUseDocument.java
Solutions:
Document.java
,
UseDocument.java
Exercise 6B (Conditional Statements)
Design and implement a class ExchangeMachine
.
The class should model an exchange cash dispenser that accepts Dollar bills and
provides Euro bills applying pre-set exchange rates (dependent on the amount of
money to be changed).
We're interested in the following features:
ExchangeMachine
with an initial cash amount of 0 Euros;
The Euro/Dollar exchange rate is defined as the amount of Euros
corresponding to one Dollar:
0.5 if the exchanged amount is less than 10 dollars,
0.9 if the exchanged amount is between 10 dollars and 100 dollars and
1.0 if the exchanged amount is more than 100 dollars.
Solution: ExchangeMachine.java