重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
代码如下:
兰陵ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信: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 {
// 创建一个包含5个元素的数组
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 的圆。");
// 拷贝数组
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;
}
//如果有两个日期值,计算他们相距的天数。
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(
"最终结果为:"+Arrays.toString(arr));
}
}
public static void buddleSort(int[] arr) {
System.out.println("排序前的结果为:"
+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("排序的结果为:"
+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("排序的结果为:"
+Arrays.toString(arr));
}
}