HardWork.java
abstract class Employee {
public abstract double earnings(); }
class YearWorker extends Employee {
【代码1】 //重写earnings()方法 }
class MonthWorker extends Employee {
【代码2】 //重写earnings()方法。 }
class WeekWorker extends Employee {
【代码3】 //重写earnings()方法。 }
class Company {
Employee[] employee; double salaries=0;
Company(Employee[] employee) {
this.employee=employee; }
public double salariesPay() {
salaries=0;
【代码4】 //计算salaries。 return salaries; } }
public class HardWork {
public static void main(String args[]) {
Employee[] employee=new Employee[20]; for(int i=0;i if(i%3==0) employee[i]=new WeekWorker(); else if(i%3==1) employee[i]=new MonthWorker(); else if(i%3==2) 21 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 22 { 【代码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()); } } 23 上机实践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(\ 24 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日 25