employee[i]=new YearWorker(); }
Company company=new Company(employee);
System.out.println(\公司年工资总额:\ } }
实验3 接口回调
1.答案:
【代码1】:
public double computeWeight() { return 45.5; } 【代码2】:
public double computeWeight() { return 65.5; } 【代码3】:
public double computeWeight() {
return 145;
} 【代码4】:
for(int k=0;k totalWeights=totalWeights+goods[k].computeWeight(); } 2.模板代码 Road.java interface ComputerWeight { public double computeWeight(); } class Television implements ComputerWeight { 【代码1】 //实现computeWeight()方法。 } class Computer implements ComputerWeight { 【代码2】 //实现computeWeight()方法。 } class WashMachine implements ComputerWeight 21 { 【代码3】 //实现computeWeight()方法。 } class Car { ComputerWeight[] goods; double totalWeights=0; Car(ComputerWeight[] goods) { this.goods=goods; } public double getTotalWeights() { totalWeights=0; 【代码4】 //计算totalWeights return totalWeights; } } public class Road { public static void main(String args[]) { ComputerWeight[] goodsOne=new ComputerWeight[50], goodsTwo=new ComputerWeight[22] ; for(int i=0;i goodsOne[i]=new Television(); else if(i%3==1) goodsOne[i]=new Computer(); else if(i%3==2) goodsOne[i]=new WashMachine(); } for(int i=0;i goodsTwo[i]=new Television(); else if(i%3==1) goodsTwo[i]=new Computer(); else if(i%3==2) goodsTwo[i]=new WashMachine(); } Car 大货车=new Car(goodsOne); System.out.println(\大货车装载的货物重量:\大货车.getTotalWeights()); Car 小货车=new Car(goodsTwo); System.out.println(\小货车装载的货物重量:\小货车.getTotalWeights()); } } 22 上机实践5 字符串、时间与数字 实验1 String类的常用方法 模板代码 StringExample.java class StringExample { public static void main(String args[]) { String s1=new String(\ s2=new String(\ if(【代码1】) // 使用equals方法判断s1与s2是否相同 { System.out.println(\与s2相同\ } else { System.out.println(\与s2不相同\ } String s3=new String(\ if(【代码2】) //判断s3的前缀是否是“220302”。 { System.out.println(\吉林省的身份证\ } String s4=new String(\你\ s5=new String(\我\ if(【代码3】)//按着字典序s4大于s5的表达式。 { System.out.println(\按字典序s4大于s5\ } else { System.out.println(\按字典序s4小于s5\ } int position=0; String path=\ position=【代码5】 //获取path中最后出现目录分隔符号的位置 System.out.println(\中最后出现\\\\的位置:\ String fileName=【代码6】//获取path中“A.java”子字符串。 System.out.println(\中含有的文件名:\ String s6=new String(\ s7=new String(\ 23 int n1=【代码7】 //将s6转化成int型数据。 double n2=【代码8】 //将s7转化成double型数据。 double m=n1+n2; System.out.println(m); String s8=【代码9】 //String调用valuOf(int n)方法将m转化为字符串对象 position=s8.indexOf(\ String temp=s8.substring(position+1); System.out.println(\数字\有\位小数\ String s9=new String(\ char a[]=【代码10】 //将s8存放到数组a中。 for(int i=a.length-1;i>=0;i--) { System.out.print(\ } } } 实验2 比较日期的大小 模板代码 DateExample import java.util.*; import javax.swing.JOptionPane; public class DateExample { public static void main(String args[ ]) { String str=JOptionPane.showInputDialog(\输入第一个日期的年份:\ int yearOne=Integer.parseInt(str); str=JOptionPane.showInputDialog(\输入该年的月份:\ int monthOne=Integer.parseInt(str); str=JOptionPane.showInputDialog(\输入该月份的日期:\ int dayOne=Integer.parseInt(str); str=JOptionPane.showInputDialog(\输入第二个日期的年份:\ int yearTwo=Integer.parseInt(str); str=JOptionPane.showInputDialog(\输入该年的月份:\ int monthTwo=Integer.parseInt(str); str=JOptionPane.showInputDialog(\输入该月份的日期:\ int dayTwo=Integer.parseInt(str); Calendar calendar=【代码1】 //初始化日历对象 【代码2】 //将calendar的时间设置为yearOne年monthOne月dayOne日 long timeOne=【代码3】 //calendar表示的时间转换成毫秒 【代码4】 //将calendar的时间设置为yearTwo年monthTwo月dayTwo日 24 long timeTwo=【代码5】 //calendar表示的时间转换成毫秒。 Date date1=【代码6】 // 用timeOne做参数构造date1 Date date2=【代码7】 // 用timeTwo做参数构造date2 if(date2.equals(date1)) { System.out.println(\两个日期的年、月、日完全相同\ } else if(date2.after(date1)) { System.out.println(\您输入的第二个日期大于第一个日期\ } else if(date2.before(date1)) { System.out.println(\您输入的第二个日期小于第一个日期\ } long days=【代码8】//计算两个日期相隔天数 System.out.println(yearOne+\年\月\日和\ +yearTwo+\年\月\相隔\天\ } } 实验3 处理大整数 模板代码 BigintegerExample import java.math.*; class BigIntegerExample { public static void main(String args[]) { BigInteger n1=new BigInteger(\ n2=new BigInteger(\ result=null; result=【代码1】//n1和n2做加法运算 System.out.println(\和:\ result=【代码2】//n1和n2做减法运算 System.out.println(\差:\ result=【代码3】//n1和n2做乘法运算 System.out.println(\积:\ result=【代码4】//n1和n2做除法运算 System.out.println(\商:\ BigInteger m=new BigInteger(\ COUNT=new BigInteger(\ ONE=new BigInteger(\ 25