Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://people.na.infn.it/~mfaella/Didattica/LpII/2014/esame2.pdf
- package com.company;
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- import java.util.concurrent.*;
- class A {
- public String f(Object x, double n) { return "A1"; }
- public String f(A x, int n) { return "A2"; }
- private String f(B x, int n) { return "A3"; }
- }
- class B extends A {
- private String f(A x, double n) { return "B1"; }
- public String f(B x, double n) { return "B2:" + f((A) x, 2); }
- public String f(A x, long n) { return "B3"; }
- }
- class C extends B {
- public String f(A x, int n) { return "C1"; }
- }
- class Song {
- private String name;
- private int sec;
- public Song(String name, int sec){
- this.name = name;
- this.sec = sec;
- }
- public int getSec(){
- return sec;
- }
- }
- class Playlist implements Comparable<Playlist>{
- private LinkedList<Song> list = new LinkedList<Song>();
- private int sec;
- public boolean add(Song s){
- sec += s.getSec();
- return list.add(s);
- }
- public boolean remove(Song s){
- boolean removed = false;
- while(list.remove(s)){
- sec -= s.getSec();
- removed = true;
- }
- return removed;
- }
- public int compareTo(Playlist p){
- return Integer.compare(this.sec , p.sec);
- }
- }
- class PriorityExecutor extends Thread{
- private class MyTask implements Comparable<MyTask>{
- private Runnable runnable;
- private int priority;
- public MyTask(int priority, Runnable run){
- this.runnable = run;
- this.priority = priority;
- }
- public int compareTo(MyTask t){
- return Integer.compare(this.priority, t.priority);
- }
- public void run(){
- runnable.run();
- }
- }
- private PriorityBlockingQueue<MyTask> queue = new PriorityBlockingQueue<MyTask>();
- public boolean addTask(Runnable task, int priorty){
- return queue.add(new MyTask(priorty, task));
- }
- @Override
- public void run() {
- try {
- while (!isInterrupted()){
- queue.take().run();
- System.out.println("Task Completed");
- }
- }
- catch(Exception e){
- return;
- }
- }
- }
- public class Main {
- public static <K,V> Map<K,V> inverseMap(Map<? extends V, ? extends K> m){
- Map<K,V> map = new HashMap<K,V>();
- for (V key : m.keySet()){
- if (map.containsKey(m.get(key))) throw new RuntimeException();
- map.put(m.get(key), key);
- }
- return map;
- }
- public static void main(String[] args) {
- //eser1
- C gamma = new C();
- B beta = new B();
- A alfa = gamma;
- System.out.println(alfa.f(gamma, 2L));
- System.out.println(beta.f(beta, 5.0) );
- System.out.println(gamma.f(beta, 5.0));
- System.out.println(11 & 3);
- //eser2
- Song one = new Song("One", 275), two = new Song("Two", 362);
- Playlist a = new Playlist(), b = new Playlist();
- a.add(one); a.add(two); a.add(one);
- b.add(one); b.add(two);
- System.out.println(a.compareTo(b));
- a.remove(one);
- System.out.println(a.compareTo(b));
- //eser3
- Runnable r1 = new Runnable() {
- @Override
- public void run() {
- for (int i=0; i<999999; i++);
- }
- };
- Runnable r2 = new Runnable() {
- @Override
- public void run() {
- for (int i=999999; i>0; i--);
- }
- };
- PriorityExecutor e = new PriorityExecutor();
- e.addTask(r2, 10);
- e.addTask(r1, 100);
- e.start();
- e.addTask(r2, 15);
- e.addTask(r1, 50);
- //eser4
- Map<Integer,String> map1 = new HashMap<Integer, String>();
- map1.put(1, "uno"); map1.put(2, "due"); map1.put(3, "tre");
- for (Object value: map1.values()) System.out.print(value.toString() + "\n");
- Map<String ,Integer> map2 = inverseMap(map1);
- for (Object value: map2.values()) System.out.print(value.toString() + "\n");
- map1.put(4, "tre");
- //map2 = inverseMap(map1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement