java 作业

#千锋逆战班#
在千锋“逆战”学习第 19 天,
接口是java 学习的一个重点,今天继续巩固学习了接口的相关内容。
中国加油!武汉加油!千锋加油!
学习的脚步不停止!

天道酬勤,继续加油!

Day 19 作业
  • 接口回调USB
package com.day19.TestWork;

public class TestUsb {

	public static void main(String[] args) {
		Computer computer = new Computer();
		Fan fan = new Fan();
		Lamp lamp = new Lamp();
		UDisk UDisk = new UDisk();
		computer.on(UDisk);
		computer.excuteUSB();
	}
}

//接口
interface USB{
	public abstract void service();
}

//接口使用者
class Computer{
	USB usb;
	public void on (USB usb) {
		this.usb = usb;
	}
	public void excuteUSB() {
		usb.service();
	}
}

//接口实现者
class Fan implements USB{
	@Override
	public void service() {
		System.out.println("通电  旋转");
	}
}
class Lamp implements USB{
	@Override
	public void service() {
		System.out.println("通电  照明");
	}
}
class UDisk implements USB{
	@Override
	public void service() {
		System.out.println("通电  读写数据");
	}
}
  • 哥德巴赫
package com.day18.TestWork;

import java.util.Scanner;

public class Goldbach {

	public static void main(String[] args) {		
        Scanner scanner=new Scanner(System.in);
        System.out.println("输入一个大于6的偶数:");
        int n=scanner.nextInt();
        goldBach(n, new EngineerAsIsPrime());
	}
    static void goldBach(int n,MathTool tool){
    	//判断传入的数n是否是大于6的偶数
        if (n%2 == 0 && n>6) {
        	//将大于6的偶数进行拆分
            for (int i = 2; i <= n/2; i++) {
                if (tool.Prime(i) && tool.Prime(n-i)) {
                	System.out.println("该数为: " + i + "与" + (n-i) + "的和" );
                }
            }
        }else {
        System.out.println("输入错误!");
        }
    }
}

//接口
interface MathTool{
	public abstract boolean Prime(int n);
}

//实现,判断数是否是质数,只能整除1和本身
class EngineerAsIsPrime implements MathTool{
    public boolean Prime(int n){
        for (int i = 2; i < n; i++) {
            if (n%i == 0) {
            	return false;
            }
        }
        return true;
    }
}
  • Student排序
package com.day19.TestWork;

public class TestStudent {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student[] stus = new Student[] {new Student("tom",20),new Student("andy",21),new Student("mike",19)};
		java.util.Arrays.sort(stus);
		for (int i = 0; i < stus.length; i++) {
			System.out.println(stus[i].name +"\t"+ stus[i].age);
		}
	}
}
class Student implements Comparable<Student>{
	String name;
	int age;
	
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	@Override
	public int compareTo(Student o) {
		if(this.age > o.age) {
			return 1;
		}else if(this.age < o.age) {
			return -1;
		}
		return 0;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值