代碼如下:
蘭陵ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
import?java.util.Arrays;
class?Circle?{
private?int?radius;
public?Circle(int?radius)?{
this.radius?=?radius;
}
public?int?getRadius()?{
return?radius;
}
public?void?setRadius(int?radius)?{
this.radius?=?radius;
}
@Override
public?String?toString()?{
return?"Circle?[radius="?+?radius?+?"]";
}
}
public?class?App?{
public?static?void?main(String[]?args)?throws?CloneNotSupportedException?{
//?創(chuàng)建一個包含5個元素的數(shù)組
Circle[]?circles?=?{?new?Circle(2),?new?Circle(10),?new?Circle(8),?new?Circle(4),?new?Circle(12)?};?
System.out.println(Arrays.toString(circles));
//?排序
Arrays.sort(circles,?(x,?y)?-?Integer.compare(x.getRadius(),?y.getRadius()));
System.out.println(Arrays.toString(circles));
//?查找半徑為?9?的圓
int?index?=?Arrays.binarySearch(circles,?9,?(x,?y)?-?((Circle)x).getRadius()?-?(int)y);
System.out.println(index?=0???circles[index]?:?"沒有找到半徑為?9?的圓。");
//?查找半徑為?10?的圓
index?=?Arrays.binarySearch(circles,?10,?(x,?y)?-?((Circle)x).getRadius()?-?(int)y);
System.out.println(index?=0???circles[index]?:?"沒有找到半徑為?10?的圓。");
//?拷貝數(shù)組
Circle[]?circles2?=?Arrays.copyOf(circles,?circles.length);
System.out.println(Arrays.toString(circles2));
}
}
//Color類
public class Color {
private String colorName;
final public void setColor(String color){
this.colorName = color;
}
public String getColor(){
return this.colorName;
}
}
//White類
public class White extends Color{
public White(){
this.setColor("white");
}
public String getColor() {
return super.getColor();
}
}
//Red類
public class Red extends White{
public Red(){
this.setColor("Red");
}
}
//Prism類
public class Prism {
static public void activePrism(Color c){
if(c.getColor().equals("white")){
Red r = new Red();
Blue b = new Blue();
System.out.println(r.getColor());
System.out.println(b.getColor());
}
else{
return;
}
}
}
//測試類
public class ColorTest {
public static void main(String[] args) {
White w = new White();
Prism.activePrism(w);
}
}
其他顏色自己寫吧。
import java.util.*;
public class Test24 {
public static void main(String[] args) {
int year1, month1, day1;
int num;
Scanner scan = new Scanner(System.in);
MyDate d1, d2;
System.out.println("請輸入一個日期");
System.out.print("年:");
year1 = scan.nextInt();
System.out.print("月:");
month1 = scan.nextInt();
System.out.print("日:");
day1 = scan.nextInt();
try{
d1 = new MyDate(year1, month1, day1);
System.out.println(d1.toString() + " 是一個合法日期");
}
catch(Exception e){
System.out.println(" 非法日期");
}
System.out.println("請輸入一個日期");
System.out.print("年:");
year1 = scan.nextInt();
System.out.print("月:");
month1 = scan.nextInt();
System.out.print("日:");
day1 = scan.nextInt();
System.out.println("時間間隔:");
num = scan.nextInt();
try{
d1 = new MyDate(year1, month1, day1);
d2 = d1.dateAdd(num);
System.out.println(d1.toString() + " 在" + num + "天后就是 " + d2.toString());
}
catch(Exception e){
System.out.println(" 非法日期");
}
try{
d1 = new MyDate(2009, 2, 27);
d2 = new MyDate(2011, 3, 1);
num = d1.dateDiff(d1, d2);
System.out.println(d1.toString() + " ~ " + d2.toString() + " 相隔 " + num + " 天");
}
catch(Exception e){
System.out.println(" 非法日期");
}
}
}
class MyDate{
private int year;
private int month;
private int day;
private int[] days = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public MyDate(){
}
public MyDate(int year, int month, int day) throws Exception{
if(year 0 || month1 || month12 || day1 || daydays[month-1]){
throw new Exception("非法日期");
}
if(month == 2){
if(isLeapYear(year)==true day29){
throw new Exception("非法日期");
}
if(isLeapYear(year)==false day28){
throw new Exception("非法日期");
}
}
this.year = year;
this.month = month;
this.day = day;
}
//給定一個日期值,計算若干天后的日期值
public MyDate dateAdd(int num) throws Exception{
MyDate d = new MyDate(this.year, this.month, this.day);
days[1] = isLeapYear(d.year) ? 29 : 28;
d.day += num;
if(num 0){
while(d.day days[d.month-1]){
d.day -= days[d.month-1];
d.month++;
if(d.month 12){
d.month = 1;
d.year++;
days[1] = isLeapYear(d.year) ? 29 : 28;
}
}
}
else{
while(d.day = 0){
d.day += days[(d.month+10)%12];
d.month--;
if(d.month 1){
d.month = 12;
d.year--;
days[1] = isLeapYear(d.year) ? 29 : 28;
}
}
}
return d;
}
//如果有兩個日期值,計算他們相距的天數(shù)。
public int dateDiff(MyDate d1, MyDate d2) throws Exception {
MyDate date1 = new MyDate(d1.year, d1.month, d1.day);
MyDate date2 = new MyDate(d2.year, d2.month, d2.day);
int i;
date1.days[1] = isLeapYear(date1.year) ? 29 : 28;
for(i=1; idate1.month; i++){
date1.day += date1.days[i-1];
}
date2.days[1] = isLeapYear(date2.year) ? 29 : 28;
for(i=1; idate2.month; i++){
date2.day += date2.days[i-1];
}
if(date1.year date2.year){
for(i=date1.year; idate2.year; i++){
date2.day += isLeapYear(i) ? 366 : 365;
}
}
else if(date1.year date2.year){
for(i=date2.year; idate1.year; i++){
date1.day += isLeapYear(i) ? 366 : 365;
}
}
return date2.day - date1.day;
}
public boolean isLeapYear(int year){
return ((year%400==0) || (year%4==0 year%100!=0)) ? true : false;
}
public String toString(){
StringBuffer buff = new StringBuffer();
if(year 10){
buff.append("000" + year);
}
else if(year 100){
buff.append("00" + year);
}
else if(year 1000){
buff.append("0" + year);
}
else{
buff.append(year);
}
if(month 10){
buff.append("-0" + month);
}
else{
buff.append("-" + month);
}
if(day 10){
buff.append("-0" + day);
}
else{
buff.append("-" + day);
}
return buff.toString();
}
}
運行測試:
請輸入一個日期
年:2011
月:2
日:29
非法日期
請輸入一個日期
年:2011
月:2
日:27
時間間隔:
2
2011-02-27 在2天后就是 2011-03-01
2009-02-27 ~ 2011-03-01 相隔 732 天
三大排序
import java.util.Arrays;
public class ArraySort {
public static void main(String[] args) {
int[] arr={43,21,40,35,87,15};
//選擇排序
//choiceSort(arr);
//冒泡排序
//buddleSort(arr);
//插入排序
insertSort(arr);
}
public static void insertSort(int[] arr) {
//21,35,40, 43,87,15
for(int j=1;jarr.length;j++){
int tmp=arr[j];//35
int i=j-1; //i:2 1
/*for(;i=0;i--){
//i:2: 4335
//i:1: 4035
//i:0: 2135
if(arr[i]tmp){
arr[i+1]=arr[i];
}else{
break;
}
}*/
for(;i=0arr[i]tmp;i--){
arr[i+1]=arr[i];
}
arr[i+1]=tmp;
System.out.println(
"最終結(jié)果為:"+Arrays.toString(arr));
}
}
public static void buddleSort(int[] arr) {
System.out.println("排序前的結(jié)果為:"
+Arrays.toString(arr));
for(int j=0;j=arr.length-2;j++){
for(int i=0;i=arr.length-2-j;i++){
System.out.println("第"+i+"次交換前:"
+Arrays.toString(arr));
if(arr[i]arr[i+1]){
int tmp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=tmp;
}
System.out.println("第"+i+"次交換后:"
+Arrays.toString(arr));
}
System.out.println("排序的結(jié)果為:"
+Arrays.toString(arr));
}
}
public static void choiceSort(
int[] arr) {
for(int i=0;i=arr.length-2;i++){
for(int j=i+1; j=arr.length-1;
j++){
System.out.println("第"+
j+"次交換前:"+Arrays.toString(arr));
if(arr[i]arr[j]){
int tmp=arr[j];
arr[j]=arr[i];
arr[i]=tmp;
}
System.out.println("第"+
j+"次交換后:"+Arrays.toString(arr));
}
}
System.out.println("排序的結(jié)果為:"
+Arrays.toString(arr));
}
}