加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

【实验课件】实验4  接口与异常处理

(2011-10-12 15:24:00)
标签:

杂谈

分类: 第四章类、对象和接口

JAVA上机实验4

实验名称:  接口与异常处理

实验目的: 掌握接口的定义、实现与使用; 理解接口和继承的混合使用;掌握异常的概念和处理方法。

实验内容:

1、定义接口Shape,其中包括一个方法size(),设计“矩形”、“圆”、“圆柱体”等类实现Shape接口,其size()方法分别表示计算矩形面积、圆面积、圆柱体的体积。分别创建代表“矩形”、“圆”、“圆柱体”的三个对象存入一个Shape类型的数组中,通过调用size()方法将数组中各类图形的大小输出。

2、  接口回调技术的应用:

卡车要装载一批货物,货物有三种商品:电视、计算机和洗衣机。需要计算出大货车和小货车各自所装载的3种货物的总重量。

要求有一个ComputeWeight接口,该接口中有一个方法:

public double computeWeig()

有3个实现该接口的类:Television、Computer和WashMachine。这3个类通过实现接口ComputeTotalSales给出自重。

有一个Car类,该类用ComputeWeight接口类型的数组作为成员,那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。程序能输出Car对象所装载的货物的总重量。

程序模版

请按照模版要求,将【代码】替换为程序代码。

Road.java

interface ComputerWeight

{

   public double computeWeight();

}

class Television implements ComputerWeight

  【代码1】 //实现computeWeight()方法。

}

class Computer implements ComputerWeight

{   【代码2】 //实现computeWeight()方法。

class WashMachine implements ComputerWeight

【代码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.length;i++)

        if(i%3==0)

             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.length;i++)

        if(i%3==0)

             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());

   }

}

 

3、写出下列程序的运行结果

程序1:public class ex2{

 public static void main(String args[]){

      String str = null;

      try {

          if (str.length() == 0)

              System.out.print("The");

         System.out.print(" Cow");

      } catch (Exception e) {

           System.out.print(" and");

           System.exit(0);

      } finally {

           System.out.print(" Chicken");

      }

      System.out.println(" show");

   }

}

 

程序2:

public class A {

    static int some() {

        try {

            System.out.println("try");

            return 1;        }

       finally {

            System.out.println("finally");

        }

    }

    public static void main(String arg[]) {

         System.out.println(some());

    }

}

4、从键盘输入一个十六进制数,将其转化为十进制输出。如果输入的不是一个有效的十六进制数数字则抛出异常。    

5、编写一个方法将格式为“yyyy/mm/dd”形式的日期转化为日期类型,如果日期数据非法,则抛出异常。对正常和异常的输入串分别进行验证,输出转换后的日期对象。

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有