Java實驗--繼承與多態


---恢復內容開始---

題目如下:

[實驗任務一]:面積計算(設計型)。
1、 實驗要求:
實驗報告中要求包括程序設計思想、程序流程圖、源代碼、運行結果截圖、編譯錯誤分
析等內容。
2、實驗內容:
(1)設計一個形狀類 Shape,包含一個 getArea()方法,該方法不包含實際語句。
(2)在 Shape 類基礎上設計圓形、矩形、三角形和梯形四個子類,要求根據實際形狀重
寫 getArea()方法。
(3)設計一個 TestShape 類,包含變量 area(存儲總面積)、靜態方法 countArea(Shape s),
該方法負責把參數中的形狀面積加入到 area 中。在 main 函數中新建(2)中四種類型的對象
s1、s2、s3、s4,通過調用 countArea 方法把四個對象面積累加到 area 中,最后輸出 area。
[實驗任務二]:抽象類和接口的設計(設計型)。
1、 實驗要求:
(1)能實現抽象類的繼承關系;
(2)程序應包括各個被調用方法的執行結果的顯示。
(3)寫出實驗報告。要求記錄編譯和執行 Java 程序當中的系統錯誤信息提示,
並給出解決辦法。(附運行界面、源代碼)。
2、 實驗內容:
(1) 定義抽象類 Shape,抽象方法為 showArea(),求出面積並顯示,定義矩形


類 Rectangle,正方形類 Square,圓類 Circle,根據各自的屬性,用 showArea
方法求出各自的面積,在 main 方法中構造 3 個對象,調用 showArea 方法。

(2) 定義接口 DiagArea,其中包含方法 double getDiagonal()求對角線長, double
getArea()求面積,定義一個矩形類,實現此接口,並自行擴充成員變量和
方法,定義一個正方形類繼承矩形類(如矩形有長 w 和寬 h,正方形有邊
x,並有相應的構造函數,有一個方法中一次直接顯示邊長、面積和對角線
長),在另一類中的主方法里使用測試該類。

[實驗任務三]:班級信息管理類設計(設計綜合型實驗)
1、 實驗要求:
(1)能實現類的繼承關系;
(2)用多種方法創建各個類的對象;
(3)程序應包括各個被調用方法的執行結果的顯示。
(4)寫出實驗報告。要求記錄編譯和執行 Java 程序當中的系統錯誤信息提示,
並給出解決辦法。(附運行界面、源代碼)。
2、 實驗內容:
(1)定義一個描述人的基本類,該類包括人的性別和出生日期兩個數據成員,
以及設置和獲取這些屬性值的方法成員;
(2)再定義一個大學生類,使大學生類具有人的所有屬性外,還具有姓名、學
號,大學入學成績,籍貫屬性以及設置和獲取這些屬性值的方法成員;編寫完整的程
序,完成一個具有班級學生信息管理功能的程序。

[實驗任務四]:按照題目要求設計並編寫一個 JAVA 程序(綜合設計型)
1、 實驗要求:
(1) 能實現類的繼承關系;
(2) 用多種方法創建各個類的對象;
(3)程序應包括各個被調用方法的執行結果的顯示。
(4)寫出實驗報告。要求記錄編譯和執行 Java 程序當中的系統錯誤信息提示,並
給出解決辦法。(附運行界面、源代碼)。
2、實驗內容:學校中有老師和學生兩類人,而在職研究生既是老師又是學生,對學


生的管理和對教師的管理在他們身上都有體現。
(1)設計兩個信息管理接口 StudentManageInterface 和 TeacherManageInterface。其中,
StudentInterface 接口包括 setFee()方法和 getFee()方法,分別用於設置和獲取學生的學費;
TeacherInterface 接口包括 setPay()方法和 getPay()方法,分別用於設置和獲取教師的工資
(2)定義一個研究生類 Graduate,實現 StudentInterface 接口和 TeacherInterface 接口,它定
義的成員變量有 name(姓名)、sex(性別)、age(年齡)、fee(每學期學費)、pay(月工資)。
創建一個姓名為“zhangsan”的研究生,統計他的年收入和學費,如果收入減去學費不足
2000 元,則輸出“provide a loan”(需要貸款)信息。
(3)提示:
①定義兩個接口,分別在其中申明兩個方法。
②定義主類 Graduate,實現這兩個接口。
③ 定義主類的成員變量,和構造方法。
④ 給出四個接口方法的實現。
⑤ 給出一個計算是否需要貸款的方法,在里面統計年收入和學費,並輸出
是否需要貸款的信息。
⑥ 寫 main 方法。在其中創建一個姓名為“zhangsan”的研究生,調用計算是
否需要貸款的方法。

下面是實驗結果:

設計思想:

    通過java之間對類的繼承的了解,及其對接口和抽象類的定義,使用,來完成本節課的實驗練習,並且通過練習掌握其中的技巧,熟練運用這些定義,來完成本節課的實驗

源程序:

實驗一:面積計算

//MainTest.java

package shape;
//洪鼎淇 信1705-3 20173627
public class MainTest {

    public static void main(String[] args) {
        // TODO 自動生成的方法存根
        Circle s1=new Circle(3);
        Triangle s2=new Triangle(3,3);
        Juxing s3=new Juxing(3,3);
        Tixing s4=new Tixing(2,3,4);
        System.out.println(TestShape.countArea(s1));
        System.out.println(TestShape.countArea(s2));
        System.out.println(TestShape.countArea(s3));
        System.out.println(TestShape.countArea(s4));
        
    }

}

//Shape.java

package shape;

public abstract class Shape {
    public abstract double getArea();
}
class Circle extends Shape
{
    final double PI=3.1415926;
    private double x;
    public Circle(double bian)
    {
        x=bian;
    }
    public double getArea()
    {
        return x*x*PI;
    }
    
}
class Triangle extends Shape
{
    private double width,height;
    public Triangle(double bian,double gao)
    {
        width=bian;
        height=gao;
    }
    public double getArea()
    {
        return width*height/2;
    }
}
class Juxing extends Shape
{
    private double width,height;
    public Juxing(double bian,double gao)
    {
        width=bian;
        height=gao;
    }
    public double getArea()
    {
        return width*height;
    }
}
class Tixing extends Shape
{
    private double width1,width2,height;
    public Tixing(double bian1,double bian2,double gao)
    {
        width1=bian1;
        width2=bian2;
        height=gao;
    }
    public double getArea()
    {
        return (width1+width2)*height/2;
    }
}
class TestShape
{
    private static double area;
    TestShape()
    {
        area=0;
    }
    public static double countArea(Shape s)
    {
        area+=s.getArea();
        return area;
    }

}

實驗二:抽象類和接口的設計

第一小題:

//DiagArea.java

package diagArea;
//洪鼎淇 信1705-3 20173627
public interface DiagArea {
    double getDiagonal();
    double getArea();
}
class Rectangle implements DiagArea
{
    double w,h;
    Rectangle(double bian,double gao)
    {
        w=bian;
        h=gao;
    }
    public double getDiagonal()
    {
        return Math.sqrt(Math.pow(w, 2)+Math.pow(h, 2));
    }
    public double getArea()
    {
        return h*w;
    }
}
class Square extends Rectangle
{
    double x;
    Square(double a)
    {
        super(a,a);
    }
}

//TestDiag.java

package diagArea;

public class TestDiag {
    public static void main(String args[])
    {
        Rectangle s1=new Rectangle(3,4);
        Square s2=new Square(3);
        System.out.println("矩形的對角線長度為:"+s1.getDiagonal());
        System.out.println("矩形的面積為:"+s1.getArea());
        System.out.println("正方形的對角線長度為:"+s2.getDiagonal());
        System.out.println("正方形的面積為:"+s2.getArea());
    }
}

第二小題:

//Shape.java

package shape1;
//信1705-3 洪鼎淇 20173627
public abstract class Shape {
    public abstract void showArea();
}
class Rectangle extends Shape
{
    double width,height;
    Rectangle(double bian,double gao)
    {
        width=bian;
        height=gao;
    }
    public void showArea()
    {
        System.out.println("該矩形的面積為:"+(width*height));
    }
}
class Square extends Shape
{
    double width;
    Square(double bian)
    {
        width=bian;
    }
    public void showArea()
    {
        System.out.println("該正方形的面積為:"+(width*width));
    }
}
class Circle extends Shape
{
    private final double PI=3.1415926;
    private double width;
    Circle(double bian)
    {
        width=bian;
    }
    public void showArea()
    {
        System.out.println("該圓形的面積為:"+(width*width*PI));
    }
}

//TestShape.java

package shape1;

public class TestShape {

    public static void main(String[] args) {
        // TODO 自動生成的方法存根
        Rectangle s1=new Rectangle(3,4);
        s1.showArea();
        Square s2=new Square(3);
        s2.showArea();
        Circle s3=new Circle(3);
        s3.showArea();
    }

}

實驗三:班級信息管理類

//BanjiManager.java

package banjiinfo;
//洪鼎淇 信1705-3 20173627
import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;



public class BanjiManager {
    Vector<Student> map=new Vector<Student>();
    Iterator<Student> it;
    Scanner can;
    BanjiManager()
    {
        can=new Scanner(System.in);
    }
    public static void main(String args[])
    {
        BanjiManager a=new BanjiManager();
        a.run();
    }
    public void run()
    {
        int k=1;
        while(k<=5&&k>=1)
        {
            k=showmenu();
        }
        System.out.println("已退出系統");
        can.close();
    }
    public int showmenu()
    {
        System.out.println("大學生班級管理菜單");
        System.out.println("1.添加學生");
        System.out.println("2.刪除學生");
        System.out.println("3.修改學生");
        System.out.println("4.查看所有學生");
        System.out.println("5.查看學生信息");
        System.out.println("其他數字.退出循環");
        int num=can.nextInt();
        if(num==1)
        {
            tianjia();
        }
        else if(num==2)
        {
            shanchu();
        }
        else if(num==3)
        {
            xiugai();
        }
        else if(num==4)
        {
            show();
        }
        else if(num==5)
        {
            chaxun();
        }
        return num;
    }
    public void tianjia()
    {
        Student st=new Student();
        String x=null,n=null,s=null,s1=null;
        double s2=0;
        System.out.println("請輸入要添加的學號");
        if(can.hasNext())
        {
            x=can.next();
        }
        if(canAdd(x))
        {
            System.out.println("請輸入姓名,性別,籍貫和大學入學成績");
            if(can.hasNext())
            {
                n=can.next();
            }
            if(can.hasNext())
            {
                s=can.next();
            }
            if(can.hasNext())
            {
                s1=can.next();
            }
            if(can.hasNext())
            {
                s2=can.nextInt();
            }
            st.setId(x);
            st.setName(n);
            st.setSex(s);
            st.setAddress(s1);
            st.setScore(s2);
            map.add(st);
        }
        else System.out.println("學號重復,請重新輸入");
    }
    public void shanchu()
    {
        String x=null;
        System.out.println("請輸入要刪除的學號");
        if(can.hasNext())
        {
            x=can.next();
        }
        if(lookp(x)!=0)
        {
            map.removeElementAt(lookp(x)-1);
        }
        else System.out.println("查無此學號");
        
    }
    public void chaxun()
    {
        Student st=new Student();
        String x=null;
        System.out.println("請輸入你想要查詢的信息");
        if(can.hasNext())
        {
            x=can.next();
        }
        it=map.iterator();
        while(it.hasNext())
        {
            st=it.next();
            if(x.equals(st.getId()))
            {
                System.out.println("該學生信息如下所示(學號,姓名,性別,籍貫,大學入學成績)");
                System.out.println(st.getId()+" "+st.getName()+" "+st.getSex()+" "+st.getAddress()+" "+st.getScore());
                return;
            }
        }
        System.out.println("沒有找到要找到的信息");
        
    }
    public void show()
    {
        Student st=new Student();
        if(map.size()==0)
        {
            System.out.println("沒有任何學生信息");
            return;
        }
            
        it=map.iterator();
        System.out.println("所有學生信息如下(學號,姓名,性別,籍貫,大學入學成績):");
        while(it.hasNext())
        {
            st=it.next();
            System.out.println(st.getId()+" "+st.getName()+" "+st.getSex()+" "+st.getAddress()+" "+st.getScore());
        }
    }
    public void xiugai()
    {
        Student st=new Student();
        String x=null,n=null,s=null,x1=null,s1=null;
        double s2=0;
        System.out.println("請輸入要修改的學號");
        if(can.hasNext())
        {
            x=can.next();
        }
        if(!canAdd(x))
        {
            System.out.println("請輸入要修改的學號,姓名,性別,籍貫和大學入學成績");
            if(can.hasNext())
            {
                n=can.next();
            }
            if(can.hasNext())
            {
                s=can.next();
            }
            if(can.hasNext())
            {
                s1=can.next();
            }
            if(can.hasNext())
            {
                s2=can.nextInt();
            }
            
            if(canChange(x1,lookp(x)))
            {
                st.setId(x);
                st.setName(n);
                st.setSex(s);
                st.setAddress(s1);
                st.setScore(s2);
                map.setElementAt(st, lookp(x));
            }
            else System.out.println("學號重復,請重新輸入");
            
        }
        else System.out.println("查無此學號");
    }
    //----------------------------------------------
    public int lookp(String xuehao)
    {
        Student st=new Student();
        int i=0;
        it=map.iterator();
        while(it.hasNext())
        {
            i++;
            st=it.next();
            if(st.getId().equals(xuehao))
                break;
        }
        return i;
    }
    public boolean canChange(String xuehao,int n)
    {
        Student st=new Student();
        int i=0;
        it=map.iterator();
        boolean ok=true;
        while(it.hasNext())
        {
            i++;
            st=it.next();
            if(i==n)
                continue;
            if(st.getId().equals(xuehao))
            {
                ok=false;
                break;
            }
        }
        return ok;
    }
    public boolean canAdd(String xuehao)
    {
        Student st=new Student();
        it=map.iterator();
        boolean ok=true;
        while(it.hasNext())
        {
            st=it.next();
            if(st.getId().equals(xuehao))
            {
                ok=false;
                break;
            }
        }
        return ok;
    }
}

//People.java

package banjiinfo;

public class People {
    String name,sex;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    
    
}

//Student.java

package banjiinfo;

public class Student extends People {
    String name,id,address;
    Student()
    {
        super();
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    double score;
    
}

實驗四:按照題目要求設計一個java程序

//Graduate.java

package graduate;
//洪鼎淇 信1705-3 20173627
public class Graduate implements StudentManagerInterface, TeacherManagerInterface {
    public static void main(String args[])
    {
        Graduate zhanshan=new Graduate();
        zhanshan.setFee(2200);
        zhanshan.setPay(3400);
        if(zhanshan.getPay()-zhanshan.getFee()<2000)
        {
            System.out.println("provide a loan");
        }
    }

    @Override
    public void setPay(double a) {
        // TODO 自動生成的方法存根
        pay=a;
    }

    @Override
    public double getPay() {
        // TODO 自動生成的方法存根
        return 0;
    }

    @Override
    public void setFee(double a) {
        // TODO 自動生成的方法存根
        fee=a;
    }

    @Override
    public double getFee() {
        // TODO 自動生成的方法存根
        return 0;
    }
    double fee=0;
    double pay=0;
}

//StudentManagerInterface.java

package graduate;
public interface StudentManagerInterface
{
    
    void setFee(double a);
    double getFee();
}

//TeacherManagerInterface.java

package graduate;

public interface TeacherManagerInterface {
    
    void setPay(double a);
    double getPay();
}

實驗結果截圖:

實驗一:面積計算

下面是實驗的結果截圖

 

程序輸入的信息如下所示

 

 

實驗二:抽象類與接口類的設計

第一小題:

第一小題輸入的信息如下所示:

 

 

第二小題:

 

 

第二小題輸入的信息如下所示:

 

 

實驗三:班級信息管理類設計
實驗的截圖如下所示

 

 

實驗四:按照題目要求設計並編寫一個Java程序

按照題目於要求設計

 

 

其中輸入的數據如下所示:

 

 

 

實驗總結:

       通過本次課的練習,掌握了類之間繼承的基本用法,並且還能夠粗略領悟到在哪一方面能夠使用到,例如同一方法的在不同類的相同定義,這里大概就是面對對象的思想的體現,總的來說這是一節學習到很多知識的實驗課。並且,在抽象類和接口的設計上有了初步的認識,了解到了其中的語法,這在我之前的java學習中尚且沒有學習過,領悟頗深的一次實驗。

 

---恢復內容結束---


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM