JavaSE习题 第七章 常用实用类

问答题

1.怎样实例化一个Calendar对象?

Calendar ca=Calendar.getInstance();

2.Calendar对象调用set(1949,9,1)设置的年月日分别是多少?

就是1949,9,1

3.怎样得到一个1—100的随机数?

int a=(int) (Math.random()*100+1);
//或者
Random ra=new Random();
int b=ra.nextInt(100)+1;

4.有集合{1,2,3,4}和集合{1,3,7,9,11}编写一个应用程序输出交集并集差集

Set<Integer> s1=new HashSet<>();
        s1.add(1);
        s1.add(2);
        s1.add(3);
        s1.add(4);
        Set<Integer> s2=new HashSet<>();
        s2.add(1);
        s2.add(3);
        s2.add(7);
        s2.add(9);
        s2.add(11);
        
        s1.retainAll(s2);//交集
        System.out.println(s1);
        
        s1.addAll(s2);//并集
        System.out.println(s1);
        
        s1.removeAll(s2);//差集
        System.out.println(s1);

5.硬盘中有2个重要的属性:价格和容量,编写一个应用程序,使用TreeMap<K,V>类,分别按照价格和容量排序输出10个硬盘的信息

package cn.littlepage.game;

import java.util.TreeMap;

public class test {
    public static void main(String[] args) {
        
        Disk d1=new Disk(300, 500);
        Disk d2=new Disk(200, 200);
        Disk d3=new Disk(800, 300);
        Disk d4=new Disk(600, 800);
        
        Key kp1=new Key(d1.price);
        Key kp2=new Key(d2.price);
        Key kp3=new Key(d3.price);
        Key kp4=new Key(d4.price);
        
        Key kc1=new Key(d1.capacity);
        Key kc2=new Key(d2.capacity);
        Key kc3=new Key(d3.capacity);
        Key kc4=new Key(d4.capacity);

        TreeMap<Key, Disk> diskMap=new TreeMap<>();
        diskMap.put(kp1, d1);
        diskMap.put(kp2, d2);
        diskMap.put(kp3, d3);
        diskMap.put(kp4, d4);
        
        TreeMap<Key, Disk> diskMap2=new TreeMap<>();
        diskMap2.put(kc1, d1);
        diskMap2.put(kc2, d2);
        diskMap2.put(kc3, d3);
        diskMap2.put(kc4, d4);
        
        System.out.println(diskMap);
        
        System.out.println(diskMap2);
    }
}
class Disk{
    public int price;
    public int capacity;
    public Disk(int price, int capacity) {
        super();
        this.price = price;
        this.capacity = capacity;
    }
    @Override
    public String toString() {
        return "Disk [price=" + price + "元, capacity=" + capacity + "GB]\n";
    }
    
}
class Key implements Comparable{
    public int num=0;

    public Key(int num) {
        super();
        this.num = num;
    }

    @Override
    public int compareTo(Object o) {
        Key k=(Key)o;
        return num-k.num;
    }

    
}
控制台:{cn.littlepage.game.Key@7852e922=Disk [price=200元, capacity=200GB]
, cn.littlepage.game.Key@4e25154f=Disk [price=300元, capacity=500GB]
, cn.littlepage.game.Key@70dea4e=Disk [price=600元, capacity=800GB]
, cn.littlepage.game.Key@5c647e05=Disk [price=800元, capacity=300GB]
}
{cn.littlepage.game.Key@33909752=Disk [price=200元, capacity=200GB]
, cn.littlepage.game.Key@55f96302=Disk [price=800元, capacity=300GB]
, cn.littlepage.game.Key@3d4eac69=Disk [price=300元, capacity=500GB]
, cn.littlepage.game.Key@42a57993=Disk [price=600元, capacity=800GB]
}

猜你喜欢

转载自www.cnblogs.com/littlepage/p/9902882.html