第八题:
主类
public static void main(String[] args){
GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
ShowDate showDate = new ShowDate();
showDate.show(calendar);
calendar.setTimeInMillis(123456789765L);
showDate.show(calendar);
}
ShowDate类
public class ShowDate {
public void show(GregorianCalendar calendar){
String year = String.valueOf(calendar.get(GregorianCalendar.YEAR));
String month = String.valueOf(calendar.get(GregorianCalendar.MONTH)+1);
String day = String.valueOf(calendar.get(GregorianCalendar.DAY_OF_MONTH));
System.out.println(year+"年"+month+"月"+day+"日");
}
}
第九题:
主类
public class Test {
public static void main(String[] args){
ComputerBig computerBig = new ComputerBig("12345", "987654321987654321987654321");
computerBig.add();
computerBig.substract();
computerBig.multiply();
computerBig.divide();
computerBig.Function();
}
//当数过大时无法求出他的因子个数
}
ComputerBig类
public class ComputerBig {
BigInteger num1;
BigInteger num2;
public ComputerBig(String a,String b){
this.num1 = new BigInteger(a);
this.num2 = new BigInteger(b);
}
public void add(){
BigInteger a3 = num1.add(num2);
System.out.println("两数之和为"+a3);
}
public void substract(){
BigInteger a3 = num1.subtract(num2);
System.out.println("两数之差为"+a3);
}
public void multiply(){
BigInteger a3 = num1.multiply(num2);
System.out.println("两数之积为"+a3);
}
public void divide(){
BigDecimal a1 = new BigDecimal(num1);
BigDecimal a2 = new BigDecimal(num2);
BigDecimal a3 = a1.divide(a2,30,BigDecimal.ROUND_UP);
System.out.println("两数之商为"+a3);
}
public void Function(){
int count = 0;
BigInteger i = BigInteger.valueOf(2);
for (; i.compareTo(num1) < 0; i = i.add(BigInteger.ONE)){
if (num1.remainder(i).equals(BigInteger.ZERO))
count++;
}
System.out.println(num1+"的因子个数为"+count);
}
}
第十题:
主类
public class Test {
public static void main(String[] args){
Calendar a=Calendar.getInstance();
a.set(2006, 6,6 );
Calendar b=Calendar.getInstance();
b.set(2008, 8,3 );
JavaComputerDate j = new JavaComputerDate(a, b);
j.compare();
}
}
JavaComputerDate类
public class JavaComputerDate {
Calendar a,b;
long a1,b1;
public JavaComputerDate(Calendar a,Calendar b){
this.a=a;
this.b=b;
a1=a.getTimeInMillis();
b1=b.getTimeInMillis();
}
public void compare(){
if(a1>b1){
System.out.println("您输入的第一个日期大于第二个日期");
long a3 = (a1 - b1)/(1000*3600*24)-1;
System.out.print(a.get(Calendar.YEAR)+"年"+a.get(Calendar.MONTH)+"月"+a.get(Calendar.DAY_OF_MONTH)+"日");
System.out.print("和"+b.get(Calendar.YEAR)+"年"+b.get(Calendar.MONTH)+"月"+b.get(Calendar.DAY_OF_MONTH)+"日");
System.out.println("相隔"+a3+"天");
}
else if(a1<b1){
System.out.println("您输入的第二个日期大于第一个日期");
long a3 = (b1 - a1)/(1000*3600*24)-1;
System.out.print(a.get(Calendar.YEAR)+"年"+a.get(Calendar.MONTH)+"月"+a.get(Calendar.DAY_OF_MONTH)+"日");
System.out.print("和"+b.get(Calendar.YEAR)+"年"+b.get(Calendar.MONTH)+"月"+b.get(Calendar.DAY_OF_MONTH)+"日");
System.out.println("相隔"+a3+"天");
}
else
System.out.println("您输入的两个日期相等");
}
}