一、實驗目的
1. 理解包的基本概念。
2. 掌握創建、聲明、導入和引用包。
3. 理解接口的基本概念。
4. 掌握接口的定義。
5. 掌握實現接口類的聲明。
二、 實驗內容
1.在包中創建類
說明:創建並使用自定義包
(1)自定義包的聲明方式
<package> <自定義包名>
聲明包語句必須添加在源程序的第一行,表示該程序文件聲明的全部類都屬於這個包。
(2)創建自定義包Mypackage
在存放源程序的文件夾中建立一個子文件夾Mypackage。例如,在“E:\java\程序”文件夾之中創建一個與包同名的子文件夾 Mypackage(E:\java\程序\Mypackage),並將編譯過的 class文件放入該文件夾中。注意:包名與文件夾名大小寫要一致。再添加環境變量 classpath的路徑,例如: E:\j2sdk1.4.2_01\lib; E:\java\程序
(1)MyDate.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)編寫使用包 Mypackage中 MyDate類的程序
1)sy6_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; //定義最終變量YEAR爲2013
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