2019-7-12 [JavaSE] 泛型 枚举 排序 各一个练习题

1.泛型 盒子Box

定义一个泛型类盒子Box,属性width和height,
调用验证支持Integer,Double,String类型。
知识点:泛型

class Box<T>{
	private T width;
	private T height;
	public T getWidth() {
		return width;
	}
	public void setWidth(T width) {
		this.width = width;
	}
	public T getHeight() {
		return height;
	}
	public void setHeight(T height) {
		this.height = height;
	}
	
}
public class TestBox {

	public static void main(String[] args) {
		// 
		Box<Integer> box = new Box<Integer>();
		box.setHeight(22);
		box.setWidth(33);
		System.out.println(box.getHeight()+","+box.getWidth());
		Box<Double> box2 = new Box<Double>();
		box2.setHeight(22.2);
		box2.setHeight(33.3);
		System.out.println(box2.getHeight()+","+box2.getWidth());
		Box<String> box3 = new Box<String>();
		box3.setHeight("22");
		box3.setWidth("33");
		System.out.println(box3.getHeight()+","+box3.getWidth());		
	}
}

2.枚举 季节

定义一个季节枚举类,枚举成员只有春、夏、秋、冬。
实现控制台输入季节,
符合春天,显示“春暖花开”;
符合夏天,显示“夏日炎炎”;
符合秋天,显示“秋高气爽”;
符合冬天,显示“冬日雪飘”。

import java.util.Scanner;

enum Season{
	SPRING,SUMMER,AUTUMN,WINTER;
}

public class TestSeason {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("输入季节:(SPRING,SUMMER,AUTUMN,WINTER)");
		String season = input.next();
		Season s = Season.valueOf(season);
		switch(s) {
		case SPRING:
			System.out.println("春暖花开");
			break;
		case SUMMER:
			System.out.println("夏日炎炎");
			break;
		case AUTUMN:
			System.out.println("秋高气爽");
			break;
		case WINTER:
			System.out.println("冬日雪飘");
			break;
		}
	}
}

3.排序 会员类

定义一个会员类,会员有编号,名字,积分。
1.编写外部比较器,实现按积分降序排序。
2.编写一个内部比较器,按照编号进行升序排序。

import java.util.Arrays;

class User implements Comparable<User>{
	private int no;
	private String name;
	private int score;
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	@Override
	public int compareTo(User o) {

		return this.no - o.no;
	}
	public User(int no, String name, int score) {
		super();
		this.no = no;
		this.name = name;
		this.score = score;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return no+","+name+","+score;
	}	
}
public class TestUser {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		User user1 = new User(1,"aa",11);
		User user2 = new User(3, "cc", 33);
		User user3 = new User(2, "bb", 22);
		User [] us = {user1,user2,user3};
		Arrays.sort(us, (u1,u2)->u2.getScore() - u1.getScore());
		Arrays.stream(us).forEach(System.out::println);	
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值