Compre Sol
Compre Sol
Solutions
1. Class Diagram (15M)
Draw a class diagram with appropriate attributes (operations need not be shown) for the credit card system out-
lined below. Clearly show the multiplicity and qualifiers, if any of each association.
An institution may issue many credit card accounts, each identified by an account number. Each account has a
maximum credit limit, a current balance, and a mailing address. The account serves one or more customers who
reside at a mailing address. The institution periodically issues a statement for each account. The statement lists a
payment due date, finance charge, and minimum payment. The statement itemizes various transactions that have
occured throughout the billing interval: cash advances, interest charges, purchases, fees, and adjustments to the
account. The name of the merchant is printed for each purchase.
Ans:
4. Objects (3 × 2M = 6M)
Identify the following statements as TRUE/FALSE. Circle the appropriate answer for each question.
(a) Java downcasts automatically, but you must explicitly upcast. (FALSE)
(b) The rules for upcasting and downcasting depend upon whether classes are declared public, protected, or
private. (FALSE)
(c) As the toString() method is defined in the Object class, System.out.println can be used to print any object.
(TRUE)
5. Local Classes and Exceptions (10M)
Write code in the identified locations (a), (b), (c) and (d) to print the required output. Each method can’t have more
than one print statement.
abstract class Musician {
abstract public void createRaga();
protected void recite(){ System.out.println("Musician:Recite"); }
public void concert(){ createRaga(); recite(); }
}
//(a)--->
class AdaptableMusician {
private String genre;
public AdaptableMusician(String genre){ this.genre = genre; }
public void createMusic() ______(b)--->__________________{
//(c)--->
}
public static void main(String[] args) __________(d)--->__________________{
AdaptableMusician salim = new AdaptableMusician("Classical");
salim.createMusic();
AdaptableMusician coltrane = new AdaptableMusician("Jazz");
coltrane.createMusic();
//any other music genre results in "UnSupportedGenre" exception
AdaptableMusician elvis = new AdaptableMusician("Rock");
elvis.createMusic();
}
}
/*
output:
-------
Classical anonymous: Experimental, Create Raga, Recite
Jazz anonymous: Create Quartet, Recite
Exception in thread "main" UnSupportedGenre
at AdaptableMusician.createMusic ....
*/
Ans:
abstract class Musician {
abstract public void createRaga();
protected void recite(){ System.out.println("Musician:Recite"); }
public void concert(){
createRaga();
recite();
}
}
class AdaptableMusician {
private String genre;
public AdaptableMusician(String genre){ this.genre = genre; }
@Override
protected void recite(){ System.out.println(", Recite"); }
@Override
public void concert(){
createRaga();
recite();
}
};
}
else if (genre.equals("Jazz")) {
m = new Musician(){
public void createRaga(){
System.out.print("Jazz anonymous: Create Quartet");
}
@Override
protected void recite(){ System.out.println(", Recite"); }
@Override
public void concert(){
createRaga();
recite();
}
};
} else
throw new UnSupportedGenre();
m.concert();
}
ArrayList:
ListIterator:
7. Generics + Inner classes (20M)
Write Java code to satisfy the given class diagram; next() method of ReverseArrayIterator must throw No-
SuchElementException exception if the stack is empty; remove() method of ReverseArrayIterator must always
throw UnsupportedOperationException exception.
Ans:
package java.lang;
import java.util.Iterator;
package java.util;
import java.util.Iterator;
import java.util.NoSuchElementException;
8. Concurrency (5M-Bonus)
Write Java code to protect the operation of pop() method using Lock. Similarly, protect the operation of push()
method using synchronized.
Ans:
pop() method:
Lock mylock = new ReentrantLock();
...
public Item pop() {
mylock.lock();
try {
--N;
} finally {
mylock.unlock();
}
return a[N];
}
push() method:
public synchronized void push(Item item) { a[N++] = item; }
9. Concurrency (15M)
Consider the JumbleNames.java program given in the next sheet. The task is to print firstName, makes the
thread sleep for delay time period, and then prints secondName in an infinite loop. The task must handle any
interrupts received by the thread. Upon receiving an interrupt, the task must come out of infinite loop and finish
its work. Create three threads to drive three tasks, namely JumbleNames("Chandragupta ", "Maurya ", 200L),
JumbleNames("Krishna ", "Devaraya ", 300L), and JumbleNames("James ", "Carse ", 500L).
import java.io.IOException;
public class JumbleNames implements Runnable {
private String firstName,secondName;
private long delay;
public JumbleNames(String firstName, String secondName, long delay) {
this.firstName = firstName;
this.secondName = secondName;
this.delay = delay; }
public void run() {
//define the task
//(a)--->
}
public static void main(String[] args) {
//create three threads to drive three tasks
//(b)--->
} catch (IOException e) {
System.err.println(e);
}
System.out.println("Ending main()");
return;
}
}
Ans:
import java.io.IOException;
first.setDaemon(true);
second.setDaemon(true);
third.setDaemon(true);
first.start();
second.start();
third.start();
} catch (IOException e) {
System.err.println(e);
}
System.out.println("Ending main()");
return;
}
}
10. Concurrency (5M-Bonus)
Ignoring the daemon status requirement of part-(c) of question #9, rewrite parts-(b) and (d) using Executors.
Make sure you use the cached thread pool option and provide for graceful shutdown.
Ans:
ExecutorService cachedPool = Executors.newCachedThreadPool();
cachedPool.execute(new JumbleNames2("Chandragupta ", "Maurya ", 200L));
cachedPool.execute(new JumbleNames2("Krishna ", "Devaraya ", 300L));
cachedPool.execute(new JumbleNames2("James ", "Carse ", 500L));
cachedPool.shutdown();