Java实验十

1

 1 public class Exercise21_06 {
 2     public static void main(String[] args){
 3         int key;
 4         // 创建一个散列映射
 5         HashMap<Integer, Integer> hashMap = new HashMap<>();
 6         // 创建一个Scanner对象
 7         Scanner input = new Scanner(System.in);
 8         System.out.println("请输入整数");
 9         
10         do{
11             key = input.nextInt(); 
12             if(hashMap.containsKey(key)){
13                 hashMap.put(key, hashMap.get(key) + 1);
14             }else{
15                 hashMap.put(key, 1);
16             }
17         }while(key != 0);
18         
19         TreeSet<Integer> treeSet = new TreeSet<>(hashMap.values());
20         for(int k: hashMap.keySet()){
21             if(hashMap.get(k)==treeSet.last()){
22                 System.out.println(k + "出现的次数最高,共计" + treeSet.last() + "次");
23             }
24         }
25     }
26 }

2

public class WordOccurrence implements Comparable{
    String word;
    int count;
        
    public WordOccurrence(String word, int count){
        this.word = word;
        this.count = count;
    }

    @Override
    public int compareTo(Object o) {
        WordOccurrence w = (WordOccurrence)o;
        return this.count - w.count;   
    }  
}
public class CountOccurrenceOfWords {
    public static void main(String[] args){
        // 创建一个String文本
        String text = "Good Morning. Have a good class." + "Have a good visit. Have fun!";
       // 创建一个树形映射
        TreeMap<String, Integer> treeMap = new TreeMap<>();
        // 提取文本中的单词
        String[] words = text.split("[\\s+\\p{P}]");
        
        for(String s: words){
            if(s.length() > 0){
                if(!treeMap.containsKey(s)){
                    treeMap.put(s.toLowerCase(), 1);
                }else{
                    treeMap.put(s.toLowerCase(), treeMap.get(s) + 1);
                }
            }
        }
        
        ArrayList<WordOccurrence> aList = new ArrayList<>();
        for(String k: treeMap.keySet()){
            WordOccurrence wo = new WordOccurrence(k, treeMap.get(k)); 
            aList.add(wo);
        }
        
        Collections.sort(aList, new Comparator(){
            public int compare(Object o1, Object o2){
                WordOccurrence wo1 = (WordOccurrence)o1;
                WordOccurrence wo2 = (WordOccurrence)o2;
                return wo1.compareTo(wo2); 
            }
        });
        
        for(WordOccurrence w: aList){
            System.out.println(w.word + "\t" + w.count);
        }
    }
}

如果将WordOccurrence实例放入树形集,最终只会输出数量不重复的单词!

3

public class Exercise30_01 extends Application{
    static TextArea tf = new TextArea();
    public static void main(String[] args){
        Application.launch(args); 
    }

    @Override
    public void start(Stage stage){
        tf.setMinWidth(700);
        tf.setWrapText(true);

        Scene scene = new Scene(tf, 700, 300);
        stage.setScene(scene);
        stage.setTitle("Exercise30_01"); 
        stage.show();
        
        Runnable printA = new printChar(tf, "a", 100);
        Runnable printB = new printChar(tf, "b", 100);
        Runnable print100 = new printNum(tf, 100);
        
        Thread thread1 = new Thread(printA);
        Thread thread2 = new Thread(printB);
        Thread thread3 = new Thread(print100);
        
        thread1.start();
        thread2.start();
        thread3.start();
        
    }   
}

class printChar implements Runnable{
    TextArea tf;
    String charToPrint;
    int times;
    public printChar(TextArea tf, String c, int t){
        this.tf = tf;
        charToPrint = c;
        times = t;
    }
    
    @Override
    public void run() {
            Platform.runLater(new Runnable() {
                public void run() {
                    for(int i = 0; i < times; i++){
                    tf.appendText(" " + charToPrint);
                    }
                }
            });
    }
}

class printNum implements Runnable{
    TextArea tf;
    int lastNum;
    
    public printNum(TextArea tf,int last){
        lastNum = last;
        this.tf = tf;
    }

    @Override
    public void run() {
        Platform.runLater(new Runnable() {
            public void run() {
                for(int i = 1; i <= lastNum; i++){
                    final int j = i;
                    tf.appendText(Integer.toString(j) + " "); 
                }
            }
         });
    }
}

4

public class FlagRisingAnimation extends Application{
    static ImageView imageView = new ImageView("file:D:\\China.jpg");
    @Override
    public void start(Stage stage) throws Exception {
        Pane pane = new Pane();
        pane.getChildren().add(imageView);
        Scene scene = new Scene(pane,1000,600);
        stage.setTitle("Exercise30.3");
        stage.setScene(scene);
        stage.show();
        Runnable rising1 = new Rising();
        Thread thread1 = new Thread(rising1);
        thread1.start();
    }
    
    static class Rising implements Runnable{

        @Override
        public void run() {
            PathTransition pt = new PathTransition(Duration.millis(10000),new Line(400,200,400,0),imageView);
            pt.setCycleCount(5); 
            pt.play();
        }    
    }
    
    public static void  main(String[] args){
        Application.launch(args);
    }
}
用线程模拟更快!

 

posted @ 2020-05-19 18:42  AngoLi  阅读(308)  评论(0编辑  收藏  举报