Advertisement
UmbertoKing

LP2esame2

Jan 18th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 KB | None | 0 0
  1. //http://people.na.infn.it/~mfaella/Didattica/LpII/2014/esame2.pdf
  2.  
  3. package com.company;
  4. import java.util.*;
  5. import java.lang.*;
  6. import java.io.*;
  7. import java.util.concurrent.*;
  8.  
  9. class A {
  10.     public String f(Object x, double n) { return "A1"; }
  11.     public String f(A x, int n) { return "A2"; }
  12.     private String f(B x, int n) { return "A3"; }
  13. }
  14. class B extends A {
  15.     private String f(A x, double n) { return "B1"; }
  16.     public String f(B x, double n) { return "B2:" + f((A) x, 2); }
  17.     public String f(A x, long n) { return "B3"; }
  18. }
  19. class C extends B {
  20.     public String f(A x, int n) { return "C1"; }
  21. }
  22.  
  23. class Song {
  24.     private String name;
  25.     private int sec;
  26.  
  27.     public Song(String name, int sec){
  28.         this.name = name;
  29.         this.sec = sec;
  30.     }
  31.  
  32.     public int getSec(){
  33.         return sec;
  34.     }
  35.  
  36.  
  37. }
  38.  
  39. class Playlist implements Comparable<Playlist>{
  40.     private LinkedList<Song> list = new LinkedList<Song>();
  41.     private int sec;
  42.  
  43.     public boolean add(Song s){
  44.         sec += s.getSec();
  45.         return list.add(s);
  46.     }
  47.  
  48.     public boolean remove(Song s){
  49.         boolean removed = false;
  50.         while(list.remove(s)){
  51.             sec -= s.getSec();
  52.             removed = true;
  53.         }
  54.         return removed;
  55.     }
  56.  
  57.     public int compareTo(Playlist p){
  58.         return Integer.compare(this.sec , p.sec);
  59.     }
  60.  
  61. }
  62.  
  63. class PriorityExecutor extends Thread{
  64.  
  65.     private class MyTask implements Comparable<MyTask>{
  66.         private Runnable runnable;
  67.         private int priority;
  68.  
  69.         public MyTask(int priority, Runnable run){
  70.             this.runnable = run;
  71.             this.priority = priority;
  72.         }
  73.  
  74.         public int compareTo(MyTask t){
  75.             return Integer.compare(this.priority, t.priority);
  76.         }
  77.  
  78.         public void run(){
  79.             runnable.run();
  80.         }
  81.  
  82.     }
  83.  
  84.     private PriorityBlockingQueue<MyTask> queue = new PriorityBlockingQueue<MyTask>();
  85.  
  86.     public boolean addTask(Runnable task, int priorty){
  87.         return queue.add(new MyTask(priorty, task));
  88.     }
  89.  
  90.     @Override
  91.     public void run() {
  92.         try {
  93.             while (!isInterrupted()){
  94.                 queue.take().run();
  95.                 System.out.println("Task Completed");
  96.             }
  97.         }
  98.         catch(Exception e){
  99.             return;
  100.         }
  101.     }
  102. }
  103.  
  104. public class Main {
  105.  
  106.     public static <K,V> Map<K,V> inverseMap(Map<? extends V, ? extends K> m){
  107.         Map<K,V> map = new HashMap<K,V>();
  108.         for (V key : m.keySet()){
  109.             if (map.containsKey(m.get(key))) throw new RuntimeException();
  110.             map.put(m.get(key), key);
  111.         }
  112.         return map;
  113.     }
  114.  
  115.     public static void main(String[] args) {
  116.  
  117.         //eser1
  118.         C gamma = new C();
  119.         B beta = new B();
  120.         A alfa = gamma;
  121.         System.out.println(alfa.f(gamma, 2L));
  122.         System.out.println(beta.f(beta, 5.0) );
  123.         System.out.println(gamma.f(beta, 5.0));
  124.         System.out.println(11 & 3);
  125.  
  126.         //eser2
  127.         Song one = new Song("One", 275), two = new Song("Two", 362);
  128.         Playlist a = new Playlist(), b = new Playlist();
  129.         a.add(one); a.add(two); a.add(one);
  130.         b.add(one); b.add(two);
  131.         System.out.println(a.compareTo(b));
  132.         a.remove(one);
  133.         System.out.println(a.compareTo(b));
  134.  
  135.         //eser3
  136.         Runnable r1 = new Runnable() {
  137.             @Override
  138.             public void run() {
  139.                 for (int i=0; i<999999; i++);
  140.             }
  141.         };
  142.         Runnable r2 = new Runnable() {
  143.             @Override
  144.             public void run() {
  145.                 for (int i=999999; i>0; i--);
  146.             }
  147.         };
  148.  
  149.         PriorityExecutor e = new PriorityExecutor();
  150.         e.addTask(r2, 10);
  151.         e.addTask(r1, 100);
  152.         e.start();
  153.         e.addTask(r2, 15);
  154.         e.addTask(r1, 50);
  155.  
  156.         //eser4
  157.         Map<Integer,String> map1 = new HashMap<Integer, String>();
  158.         map1.put(1, "uno"); map1.put(2, "due"); map1.put(3, "tre");
  159.         for (Object value: map1.values()) System.out.print(value.toString() + "\n");
  160.         Map<String ,Integer> map2 = inverseMap(map1);
  161.         for (Object value: map2.values()) System.out.print(value.toString() + "\n");
  162.         map1.put(4, "tre");
  163.         //map2 = inverseMap(map1);
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement