Laborator 3
Consultatii...
Exercitii suplimentare
1)
Scrieti codul pentru urmatoarea diagrama UML
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
interface Employee{
public double computeSalary();
public void addTimeCard(TimeCard tc);
}
class TimeCard{
private double workedHours;
public TimeCard(double workedHours){
this.workedHours=workedHours;
}
public double generatedIncome(double perHour){
return this.workedHours*perHour;
}
}
class RealEmployee implements Employee{
private int id;
private double perHour;
private ArrayList<TimeCard>itsCards=new ArrayList<>();
public RealEmployee(int id, double perHour){
this.id=id;
this.perHour=perHour;
}
public double computeSalary() {
double salary=0;
for(TimeCard tc:itsCards){
salary=salary+tc.generatedIncome(this.perHour);
}
return salary;
}
public void addTimeCard(TimeCard tc) {
if(tc!=null)
itsCards.add(tc);
else{
throw new NullPointerException("Nu se poate adauga timecard-ul");
}
}
}
class ProxyEmployee implements Employee {
private RealEmployee employee;
private static String password;
private static String readPasswd;
public ProxyEmployee(int id, double perHour) {
employee = new RealEmployee(id, perHour);
}
public static void setPassword(String passwd) {
if(true==checkPassword())
ProxyEmployee.password = passwd;
}
private static void readPassword(){
BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
try {
buff.readLine();
readPasswd=buff.toString();
}catch (IOException e){
System.out.println("Something wrong happend!");
}
}
private static boolean checkPassword(){
if(readPasswd.equals(password))
return true;
return false;
}
public void addTimeCard(TimeCard tc) {
employee.addTimeCard(tc);
}
public double computeSalary(){
return employee.computeSalary();
}
}
2)
Starting from the line marked with a *, draw the sequence diagram that includes all the interactions between the objects.
interface P {
public void r();
}
class S implements P {
private P f;
public S(P t) {
this.f = t;
}
public void m(P a) {
this.r();
f.r();
}
public void r() {
f.r();
}
}
class Z implements P {
public void q() {}
public void r() {
q();
}
}
class Main {
public static void main(String argv[]) {
Z t = new Z();
S s2 = new S(t);
S s1 = new S(s2);
/**/s1.m(s2);
}
}
3)
Starting from the line marked with a *, draw the sequence diagram that includes all the interactions between the objects.
class Message {
private String text;
private Person exp,dest;
public Message(Person exp, Person dest, String text) {
this.exp = exp;
this.text = text;
this.dest = dest;
}
public Person getDest() {
return dest;
}
public String toString() {
return exp + " said to " + dest + ":" + text;
}
}
class Person {
private String name;
private Transmitter t;
public void setTransmitter(Transmitter t) {
this.t = t;
}
public Person(String name) {
this.name = name;
}
public void send(String text, Person dest) {
Message m = new Message(this,dest,text);
t.store(m);
}
public void notifyMail(Transmitter t) {
System.out.println(t.retreive(this));
}
public boolean equals(Object o) {
if(o instanceof Person) {
return name.equals(((Person)o).name);
}
return false;
}
public String toString() {
return name;
}
}
interface Transmitter {
public void store(Message m);
public Message retreive(Person p);
}
class EMailTransmitter implements Transmitter {
private Message m;
private void recordMessage(Message m) {
this.m = m;
}
public void store(Message a) {
recordMessage(a);
m.getDest().notifyMail(this);
}
public Message retreive(Person p) {
return m;
}
}
class Test {
public static void main(String argvp[]) {
Person p1 = new Person("Mircea");
p1.setTransmitter(new EMailTransmitter());
Person p2 = new Person("Radu");
/**/p1.send("Ce faci?",p2);
}
}
4)
1)
import java.util.ArrayList;
import java.util.Iterator;
abstract class A{
private int o;
public int getO() {
return o;
}
public void setO(int o) {
this.o = o;
}
}
class C extends A{
private A obj;
public void set(A x){
this.obj=x;
}
}
class B{
private ArrayList<A> objA=new ArrayList<>();
public void add(A x){
objA.add(x);
}
public int sum(){
int sum=0;
Iterator it=objA.iterator();
while(it.hasNext()){
sum=sum+((A)it.next()).getO();
}
return sum;
}
}
5)
1)
import java.util.ArrayList;
interface X{
public int m();
}
class S implements X{
private T objT;
public S(T x){
this.objT=x;
}
public int m(){
if(objT!=null)
return 1+objT.m();
else
return 1;
}
}
class T implements X{
private ArrayList<X> obj=new ArrayList<>();
public void add(X a){
if(a!=null){
obj.add(a);
}
else{
throw new NullPointerException("Nu se poate adauga");
}
}
public int m(){
int sum=0;
for(X elem:obj){
sum=sum+elem.m();
}
return sum;
}
}
2)
Starting from the line marked with a *, draw the sequence diagram that includes all the interactions between the objects.
interface Y {
public void r(Y w);
public void s();
}
class S implements Y {
public void q(Y t1, Y t2, Y t3) {
this.r(t1);
t1.r(t2);
t1.r(t3);
}
public void r(Y g) {
g.s();
}
public void s() {}
}
class P implements Y {
public void s() {}
public void r(Y g) {}
}
class Main {
public static void main(String argv[]) {
P p1 = new P();
S s1 = new S();
S s2 = new S();
S s3 = new S();
/**/s3.q(s2, s1, p1);
}
}