倒腾(com.duoduo.util)

Accumulator

package com.duoduo.util;

import com.duoduo.std.StdOut;

public class Accumulator {
	private int N;
	private double total;
	private String name;

	public Accumulator() {

	}

	public Accumulator(String name) {
		this.name = name;
	}

	public void addDataValue(double val) {
		total += val;
		N++;
	}

	public double mean() {
		return total / N;
	}

	public String toString() {
		return String.format("[%s]:(%d个数的平均值为: %7.5f)", name, N, mean());
	}

	public static void main(String[] args) {
		Accumulator head = new Accumulator("head");

		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 21; j++) {
				head.addDataValue(Math.random());
			}
			StdOut.println(head);
		}

	}

}


Counter

package com.duoduo.util;

import com.duoduo.std.StdOut;

public class Counter {
	private int count;
	private String name;

	public Counter() {
		name = "计数器";
	}

	public Counter(String name) {
		this.name = name;
	}

	public int tally() {
		return count;
	}

	public void increment() {
		count++;
	}

	public String toString() {
		return String.format("[%s]:总共进行了%d次操作", name, count);
	}

	public static void main(String[] args) {
		Counter head = new Counter("head");

		for (int i = 0; i < 20; i++) {
			for (int j = 0; j < 3; j++) {
				head.increment();
			}
			StdOut.println(head);
		}

	}

}

IsLeapYear

package com.duoduo.util;

import com.duoduo.std.StdOut;

public class IsLeapYear {
	public static boolean isLeapYear(int year) {
		return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
	}

	public static void main(String args[]) {
		StdOut.println(isLeapYear(1904));
	}
}
/*
 * 4.输入一个年份,判断这个年份是否是闰年(知识点:条件、循环语句)
 */


Pair

package com.duoduo.util;

public class Pair<Item> {
	public Pair() {
		super();
	}

	public Pair(Item x, Item y) {
		super();
		this.x = x;
		this.y = y;
	}

	public Item x;
	public Item y;

	public String toString() {
		return "x: " + x + " y: " + y;
	}
}


Prime

package com.duoduo.util;

import java.util.NoSuchElementException;

import com.duoduo.std.StdOut;
import com.duoduo.std.StopWatch;

public class Prime {
	public static boolean isPrime(int num) {
		if (num < 2) {
			return false;
		}
		int N = (int) Math.floor((Math.sqrt(num))) + 1;
		for (int i = 2; i < N; i++) {
			if (num % i == 0) {
				return false;
			}
		}
		return true;
	}

	public static int minPrime(int num) throws Exception {
		return minPrime(num, 2);
	}

	public static int minPrime(int num, int begin) throws Exception {
		if (num < 2) {

			throw new IllegalArgumentException("待求数字最小为2");
		}
		if (begin < 2) {
			throw new IllegalArgumentException("区间范围的起始点最小为2");
		}
		for (int i = begin; i <= num; i++) {
			if (num % i == 0 && Prime.isPrime(i)) {
				return i;
			}
		}
		throw new NoSuchElementException(String.format("在此区间内(%d~%d),无最小质因子",
				begin, num));
	}

	public static void main(String[] args) throws InterruptedException {
		StopWatch watch = new StopWatch();
		for (int i = 200;; i++) {
			if (isPrime(i)) {
				StdOut.println(i);
				break;
			}
		}
		watch.elapsedTime();
		StdOut.println(watch.toString());

	}

}
/*
 * 15. 编写一个程序,找出大于200的最小的质数(知识点:循环语句)
 */



RedirectionIn

package com.duoduo.util;

import java.io.*;

public class RedirectionIn {
	private static InputStream in;

	private RedirectionIn() {
	}

	public static void fileToStdIn(String path) {
		try {
			in = new FileInputStream(path);
			System.setIn(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static void close() {
		try {
			if (in != null) {
				in.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}


RedirectionOut

package com.duoduo.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class RedirectionOut {
	private static OutputStream out;
	private static PrintStream old;

	private RedirectionOut() {
	}

	private static void resume() {
		FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
		old = new PrintStream(new BufferedOutputStream(fdOut, 128), true);
		System.setOut(old);
	}

	public static void StdOutToFile(String path) {
		try {

			out = new FileOutputStream(path);
			System.setOut(new PrintStream(out));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static void StdOutToFile(File f) {
		try {

			out = new FileOutputStream(f);
			System.setOut(new PrintStream(out));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static void close() {
		try {
			if (out != null) {
				out.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		resume();
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值