包与接口

 包与接口

 

一、实验目的

1.   理解包的基本概念。

2.   掌握创建、声明、导入和引用包。

3.   理解接口的基本概念。

4.   掌握接口的定义。

5.  掌握实现接口类的声明。

二、 实验内容

1.在包中创建类

说明:创建并使用自定义包

1)自定义包的声明方式

<package> <自定义包名>

声明包语句必须添加在源程序的第一行,表示该程序文件声明的全部类都属于这个包。

2)创建自定义包Mypackage

在存放源程序的文件夹中建立一个子文件夹Mypackage。例如,在“E:\java\程序”文件夹之中创建一个与包同名的子文件夹 MypackageE:\java\程序\Mypackage),并将编译过的 class文件放入该文件夹中。注意:包名与文件夹名大小写要一致。再添加环境变量 classpath的路径,例如: E:\j2sdk1.4.2_01\lib; E:\java\程序

1MyDate.java程序功能:在源程序中,首先声明使用的包名Mypackage,然后创建 MyDate类,该类具有计算今年的年份,可以输出一个带有年月日的字符串的功能。源代码如下。

package Mypackage; //      创建自定义包 Mypackage   

import java.util.*; //引用 java.util

public class MyDate {

private int year,month,day;

public static void main(String[] arg3){}

public MyDate(int y,int m,int d) {

year = y;

month = (((m>=1) & (m<=12)) ? m : 1);

day = (((d>=1) & (d<=31)) ? d : 1);

}

public MyDate() {

this(0,0,0);

}

public static int thisyear() {

  return Calendar.getInstance().get(Calendar.YEAR);//返回当年的年份

}

public int year() {

return year;//返回年份

}

public String toString(){

  return year+"-"+month+"-"+day;//返回转化为字符串的年--

}

}

(2)编写使用包 MypackageMyDate类的程序

1sy6_1.java程序功能:给定某人姓名与出生日期,计算该人年龄,并输出该人姓名,年龄,出生日期。程序使用了age的方法来计算年龄。源代码如下。

import Mypackage.MyDate; //     引用Mypackage包的MyData                

public class sy6_1 {

    private String name;

    private MyDate birth;

 

    public sy6_1(String n1,MyDate d1){

       name = n1;

       birth = d1;

    }

    public sy6_1(String n1,int y,int m,int d) {

      this(n1,new MyDate(y,m,d));

   }

 

    public int age(){

        return MyDate.thisyear() -birth.year();

    }

 

    public void output() {

       System.out.println("姓名 : "+name);

       System.out.println("出生日期: "+birth.toString());

       System.out.println("今年年龄 : "+age());

    }

    public static void main(String args[]) {

        sy6_1 a = new sy6_1("张驰",1990,1,11);

        a.output();

    }

 

}

运行结果:

姓名 : 张驰

出生日期: 1990-1-11

今年年龄 : 23

2.阅读以下程序,注意包的创建及类的创建,并分析程序实现的功能。

package Mypackage;

public class SquareEquation

{

    double a,b,c;

    double root1,root2;

    boolean boo;

    public  SquareEquation(double a,double b,double c)

    {

       this.a=a;

       this.b=b;

       this.c=c;

       if(a!=0)

       {

           boo=true;

       }

       else

       {

          boo=false;

       }

    }

    public void  getRoots()

    {

       if(boo)

        {

           System.out.println("是一元2次方程");

           double disk=b*b-4*a*c;

           if(disk>=0)

             {

               root1=(-b+Math.sqrt(disk))/(2*a);

               root2=(-b-Math.sqrt(disk))/(2*a);

               System.out.printf("方程的根:%f,%f\n",root1,root2);

             }

           else

             {

                System.out.printf("方程没有实根\n");

             }

 

        }

        else

        {

           System.out.println("不是一元2次方程");

        }

    }

   public void setCoefficient(double a,double b,double c)

    {

       this.a=a;

       this.b=b;

       this.c=c;

       if(a!=0)

        {

            boo=true;

        }

       else

       {

            boo=false;

       }

    }

}

 

import Mypackage.SquareEquation;

public class SunRise

{

    public static void main(String args[ ])

    {

        SquareEquation equation=new SquareEquation(4,5,1);

        equation.getRoots();

        equation.setCoefficient(1,2,5);

        equation.getRoots();

    }

}

运行结果:

是一元2次方程

方程的根:-0.250000,-1.000000

是一元2次方程

方程没有实根

3.编写程序(文件名为sy6_2.java,要求如下:

1)定义Biology(生物)、Animal(动物)和Mankind(人)3个接口;

2)接口Biology声明breath()抽象方法;

3)接口Animal继承Biolog并声明move()eat()抽象方法;

4)接口Mankind继承Animal并声明study()think()抽象方法;

5)定义类NormalMan实现接口Mankind,仅显示相应的功能信息来实现它们声明的抽象方法;

6)在类NormalMan中定义私有变量name,实现无参构造方法和有参构造方法初始化该变量。

程序运行结果如下:

部分代码如下:

interface Biology

{

    void breath();

}

interface Animals extends Biology{

    void breathe();

    void eat();

    void move();

}

interface Mankind extends Animals{

    void breathe();

    void eat();

    void move();

    void think();

    void study();

}

class NormalMan implements Mankind

{

    private String name;

    public NormalMan(String name)

    {

        this.name=name;

    }

    public void output()

    {

        System.out.println(this.name+"can breathe");   

        System.out.println(this.name+"can eat");   

        System.out.println(this.name+"can move");

        System.out.println(this.name+"can think");

        System.out.println(this.name+"can study");

    }

   

}

public class sy6_2

{

    public static void main(String args[])

    {

    NormalMan p1=new NormalMan("张三");

    p1.output();

    p1=new NormalMan("李四");

    p1.output();

     }

}

请编写剩余语句。

4.以下程序实现的功能是计算某人的年龄,请将程序补充完整

interface N1{ //定义接口

 final int YEAR=2013;   //定义最终变量YEAR2013

int age();

void output();

}

public class NL   implements N1  { //实现接口

String xm; //类自己的成员变量(姓名)

int csrq; //类自己的成员变量(出生日期)

public NL(String n1,int y){

xm = n1;

csrq = y;

}

 

public int age(){//实现接口的方法

 return    YEAR-csrq ; 

}

 

public void output() { //实现接口的方法

System.out.println(  this.xm    +"今年的年龄是"+ this.age()  +"");

}

 

public static void main (String args[]) {//类自己的成员方法

NL a = new NL("张驰",1990);

a.output();

}

}

5. 可计算面积接口与实现该接口的矩形类、椭圆类,请将程序补充完整。

       

//Rectangle.java

public interface Area                       //面积接口

{   public abstract double area();     //计算面积,抽象方法

 }

public class Rectangle implements Area           //矩形类,实现可计算面积接口

{ protected double a;

    protected double b;

    public Rectangle(double a,double b)

    {

        this.a=a;

        this.b=b;

    }

    public Rectangle(double a)

    {

        this(a,a);

    }

    public Rectangle()

    {

        this(0,0);

    }

    public double area()

    {

        return this.a*this.b;

    }

    public String toString()

    {

        return "矩形面积为"+this.area();

    }  

}

// Ellipse.java

public class Ellipse implements Area           //椭圆类,实现可计算面积接口

{

   

    protected double m;

    protected double n;

    public Ellipse(double m,double n)

    {

        this.m=m;

        this.n=n;

    }

    public  Ellipse(double m)

    {

        this(m,m);

    }

    public Ellipse()

    {

        this(0,0);

    }

    public double area()

    {

        return Math.PI*this.m*this.n;

    }

    public String toString()

    {

        return "椭圆面积为"+this.area();

    }  

}

//Area_ex.java

public  class  Area_ex

{    public static void main(String args[])

    {

       Area g = new Rectangle(10,20);    //接口变量g引用实现接口的类的对象

       System.out.println(g.toString());  //覆盖,运行时多态性

       g = new Ellipse(10,20);                //椭圆对象,也是Area

       System.out.println(g.toString());

    }

}

结果为:

矩形面积为200.0

椭圆面积为628.3185307179587

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章