1.完成MathLib类,可以做加,减,乘,除等功能,其中加法不但可以做数字的加法,还可以做字符串的相加。
public class MathLib {
public static Object add(Object a, Object b) {
if (a instanceof Number && b instanceof Number) {
return ((Number) a).doubleValue() + ((Number) b).doubleValue();
} else if (a instanceof String && b instanceof String) {
return (String) a + (String) b;
} else {
return null;
}
}
public static double subtract(double a, double b) {
return a - b;
}
public static double multiply(double a, double b) {
return a * b;
}
public static double divide(double a, double b) {
if (b == 0) {
throw new IllegalArgumentException("除数不能为零");
}
return a / b;}
}
2.任意给定的一串字母,统计字符串里面的大写字母和小写字母的个数。
public class Count {
public static void main(String[] args) {
String str = "sbuUIBVDnasdIbs";
StringBuilder sb = new StringBuilder(str);
int big = 0;
int small = 0;
for (int i = 0; i < sb.length(); i++) {
char ch = sb.charAt(i);
if (ch >= 'a' && ch <= 'z') {
small++;
}
if (ch >= 'A' && ch <= 'Z') {
big++;
}
}
System.out.println("大写字母的数量为"+big);
System.out.println("小写字母的数量为"+small);
}
}
3.根据传入得路径,获取文件名。例如:D:/myfile/hello.java取出hello.java
public class FileName {
public static String Name(String path) {
String[] parts = path.split("/");
return parts[parts.length - 1];
}public static void main(String[] args) {
String path = "D:/myfile/hello.java";
String fileName = Name(path);
System.out.println("文件名是:" + fileName);
}
}
4.求两个日期之间相隔的天数
例如:写一个方法(fun3("2010-09-20","2010-09-21") ) 结果为1天
public class Time {
public static void main(String[] args) {
System.out.println(fun3("2010-09-20","2010-09-21"));
}
public static long fun3(String str1,String str2){
LocalDate date1 = LocalDate.parse(str1);
LocalDate date2 = LocalDate.parse(str2);
long day = 0;
while (!date1.isEqual(date2)){
date1 = date1.plusDays(1);
day++;
} return day;
}
}
5.请使用日期时间相关的API,计算出一个人已经出生了多少天。
import java.time.LocalDate;
import java.time.Period;public class Brithday {
public static void main(String[] args) {
LocalDate birth = LocalDate.of(2002, 12, 19);
long days = getday(birth);
System.out.println(days);
}public static long getday(LocalDate birth) {
LocalDate now = LocalDate.now();
Period period = Period.between(birth, now);
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();
long total = years * 365 + months * 30 + days;
return total;
}
}
6.验证for循环打印数字1-9999所需要使用的时间(毫秒)
import java.time.Duration;
import java.time.LocalTime;public class Seconds {
public static void main(String[] args) {
LocalTime before = LocalTime.now();
long seconds = time(before);
System.out.println(seconds);
}
public static long time(LocalTime before){
for (int i =1;i<=9999;i++){
System.out.println(i);
}
LocalTime after = LocalTime.now();
Duration tm = Duration.between(before,after);
return tm.toMillis();
}
}
7.求出今天距离2023年1月1日还有多少天
import java.time.LocalDate;
import java.time.Period;public class Demo {
public static void main(String[] args) {
LocalDate day1 = LocalDate.of(2023,1,1);
long all = days(day1);
System.out.println(all);
}
public static long days(LocalDate day1){
LocalDate day2 = LocalDate.now();
Period period = Period.between(day1,day2);
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();
long total = years*365 + months *30 + days;
return total;
}
}