This is an old revision of the document!


Laboratorul 13.

Problema 1 - Singleton

Implementați o clasa Catalog care conține o lista cu obiecte de tip Course. Va trebui sa va asigurați ca pentru aceasta clasa va putea exista o singura instanța care sa poata fi accesata din orice clasa a proiectului. Implementati metoda toString pentru clasa Catalog.

public class Catalog {
       // TODO -- Adaugati aici implementarea exercitiului
}
public class Course {
}

Problema 2 - Factory

Pornind de la clasa abstracta User, definiți clasele Student, Parent, Assistant și Teacher care vor moșteni clasa User:

public abstract class User {

private String firstName, lastName;
public User(String firstName, String lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}
public String toString() {
  return firstName + " " + lastName;
}

}

Pentru a putea realiza o instanțiere ușoara a obiectelor pentru aceste tipuri de clase, veți implementa o clasa UserFactory care va avea o metoda statica createUser ce va returna un obiect de tip User (se va folosi șablonul de proiectare Factory).

Pentru testare folositi clasa Test de mai jos, in care veti comenta cerintele neimplementate inca:

class Test {

  public static void main(String[] args) {
      //testare exercitiu 2
      User studentA = UserFactory.createUser("Student", "A", "Popescu");
      User studentB = UserFactory.createUser("Student", "B", "Ionescu");
      User studentC = UserFactory.createUser("Student", "C", "Ionescu");
      User mother = UserFactory.createUser("Parent", "MAC", "Ionescu");
      User father = UserFactory.createUser("Parent", "MAC", "Ionescu");
     
      User teacher = UserFactory.createUser("Teacher", "Teacher", "Georgescu");
      User assistant = UserFactory.createUser("Assistant", "Assistant", "Popescu");
      
      //testare exercitiu 3+5
      Course course = new Course.CourseBuilder("POO").teacher((Teacher) teacher)
              .assistant((Assistant) assistant).grade(new Grade("POO", (Student) studentA, 4d,5d))
              .grade(new Grade("POO", (Student) studentB,3d,3d)).strategy(new BestExamScore()).build();
      
      System.out.println("Curs: "+ course);
      System.out.println("Best Student:" + course.getBestStudent());
      
      //testare exercitiu 1+3+5
      Catalog catalog = Catalog.getInstance();
      catalog.addCourse(course);
       
      //testare exercitiu 4
      ((Student) studentB).setMother((Parent) mother);
      ((Student) studentB).setFather((Parent) father);
      ((Student) studentC).setMother((Parent) mother);
      catalog.addObserver((Parent) mother);
      catalog.addObserver((Parent) father);
      catalog.notifyObservers(new Grade("POO", (Student)studentB,2.5d,3d));
      
      //testare exercitiu 6
      HashMap<Teacher, ArrayList<Pair<Student, String, Double>>> examScores=new HashMap<>();
      ArrayList<Pair<Student, String, Double>> ar1=new ArrayList<>();
      ar1.add(new Pair(studentA,"POO",3.6d));
      examScores.put((Teacher)teacher,ar1);
      
      HashMap<Assistant, ArrayList<Pair<Student, String, Double>>> partialScores= new HashMap<>();
      ArrayList<Pair<Student, String, Double>> ar2=new ArrayList<>();
      ar2.add(new Pair(studentC,"POO",4.6d));
      partialScores.put((Assistant)assistant,ar2);
  
     Visitor v=new ScoreVisitor(examScores,partialScores);
     
      v.visit((Teacher)teacher);
      System.out.println("Actualizare teacher:" + catalog);
              
      v.visit((Assistant)assistant);
      System.out.println("Actualizare assistant:" + catalog);
     
  }

}

Problema 3

Problema 4

Problema 5

FileReader in = new FileReader(new File("test02.in"));
StreamTokenizer str = new StreamTokenizer(in);

Arhiva laborator

poo/laboratoare/13.1610903108.txt.gz · Last modified: 2021/01/17 19:05 by carmen.odubasteanu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0