注册 登录
查看: 3505|回复: 0
打印 上一主题 下一主题

java面向对象编程 题目

[复制链接]

该用户从未签到

3524

主题

3538

帖子

67万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
673176
QQ
跳转到指定楼层
楼主
发表于 2016-11-10 12:02:19 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
实验2 面向对象编程1.实验目的
(1)掌握Java中类和对象的基本知识。
(2)理解Java中的继承(类,子类,接口)和多态机制。
(3)掌握Java中this、super及访问限定符和特征限定符的用法。
(4)应用Java的包的文件组织方式管理代码。

2.实验要求
在Eclipse下创建Practice1项目,对未有包名要求的题目统一按照实验题名建包,然后将本题源代码放在同一包下。对有包名要求的题目按照要求建包。作业提交时将Practice1项目下src文件包命名为Practice1.src压缩后提交。

3.实验题目
Exer1: 创建Person类,并设置name,sexage三个private属性,设置无参构造方法和带参数的构造方法;设置setName(),setSex(),setAge()方法给三个属性赋值,并用getName(),getSex(),getAge()方法获取三个属性的值;重写父类(Object)的toString()方法,实现Person类对象的属性输出,注意toString()方法返回值类型为String
基本要求:
(1) 设计CreatPerson类,使用无参构造方法创建Person类对象,用set***get***方法完成属性初始化和输出。
(2) 使用有参构造方法创建Person类对象,并调用toString()方法完成属性输出。


Exer2: 设计一个用来描述汽车的类Car,使用类的非静态成员变量来表示汽车的车主姓名name、当前的速率speed:
基本要求:
(1) 创建构造函数为成员变量赋初值。
(2) 使用类的非静态成员方法来表示改车主姓名changeName操作, 并通过该方法显示修改后的车主名称。
(3) 使用类的非静态成员方法来表示改变汽车的速率changSpeed操作,并通过该方法显示修改后的车速。
(4) 使用类的非静态成员方法来表示停车stop操作,并通过该方法显示停车后的车速。
(5) 创建一个Car类的对象,初始状态时,车主名称是自己的名字,speed=0
(6) 分别调用这三个方法,更改车名为朋友的名字,车速为20,停车车速为0,并且得到如下的结果:


Exer3: 定义人类Human及其子类Child。其中Human具有姓名name、年龄age属性成员,具有无参和有参构造方法HumanString name, int age),其他方法有void walk(){System.out.printlnname+}void eat() {System.out.println(name+“爱吃西瓜”)}Child子类还具有schoolName属性成员及其有参构造方法,具有void study() {System.out.println(name+“学习数学”)},且重写父类方法walk()。HumanChildwalk方法走路的方式分别是“稳健的走”和“蹦跳的走”。

基本要求:
(1) 在子类的构造方法中调用父类的有参构造方法
(2) 建立测试类,在测试类中完成以下操作
(3) 创建Human类对象h,调用它的方法walk(),eat()
(4) 创建Child类对象c,调用方法study(),walk()
(5) 将对象c的引用赋给h,观察h.walk(),h.eat()h可以调用study()方法吗?为什么?

无法调用study()方法  h类中方法没有study()方法  将c赋值给h 只是将对象的引用地址赋给次对象 编译器在 编译时 才能识别study()方法
(6) 思考Human中如果没有无参构造函数可以吗?

                 可以的

Exer 4: 对象的组合。现有一个Rectangle类,请设计一个柱形类Cuboid
基本要求:
(1) 此类具有两个成员变量,(1Rectangle类型的成员变量rect长方体以长方形做底)和(2height柱形的高
(2) 此类具有构造方法。
(3) 此类应具有求面积的方法getVolme()
(4) 此类应具有设置和获得底的长度和宽度的方法:getBottomWidth()
setBottomWidth(),
getBottomLength(),
setBottomLength()。
Rectangle的代码如下:
public class Rectangle {
    double width,length;      
    public void setWidth(double w) {
       if(w > 0)
         this.width=w;
    }
    public double getWidth(){
       return width;
    }
    public void setLength (double h) {
       if(h > 0)
         this.length =h;
    }
    public double getLength () {
       return length;
    }
}

Exer 5:设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声。
基本要求:
(1) 编写抽象类Animal
Animal抽象类有2个抽象方法cry()getAnimaName(),即要求各种具体的动物给出自己叫声和动物种类名称
(2) 编写模拟器类Simulator
该类有一个playSound(Animal animal)方法,该方法的参数是Animal类型。即参数animal可以调用Animal的子类重写的cry()方法播放具体动物的声音、调用子类重写的getAnimalName()方法显示动物种类的名称。
(3) 编写Animal类的子类:DogCat
(4) 编写主类Application(用户程序)
在主类Applicationmain方法中至少包含如下代码:
Simulator simulator = new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());

下图是Simulator、Animal、Dog、Cat的UML图。


Exer 6: 有两个类:MobileManagementMobile,分别描述如图所示两部手机名称及价格,类MobileManagement在包cn.edu.nwsuaf.jp.p3中,而Mobile在包cn.edu.nwsuaf.jp.p3.data中。它们代码如下。运行MobileManagement.java,你应该看到如图所示结果。
[基本要求] 在空白处填写相关代码并修改下面程序,使程序能够显示两部手机的价格和数量,运行结果如图6.2所示。

程序Mobile.java源代码:
public class Mobile {
        /** Holds the name of the mobile. */
        private String name;

        /** Holds the price of the mobile. */
        private float price;

        /** Creates a new mobile object. */
        public Mobile(String name, float price) {
                this.name = name;
                this.price = price;
        }

        /** Gets the name of the mobile. */
        public String getName() {
                return name;
        }

        /** Gets the price of the mobile. */
        Public float getPrice() {
                return price;
        }
}
程序MobileManagement.java源代码:
import javax.swing.JOptionPane;

public class MobileManagement {
        /** Defines the entry point of the application. */
        public static void main(String[] args) {
                // Creates two mobile phone objects.
                Mobile mobile1 =    new Mobile("E365", 1780)                           ;
                Mobile mobile2 =           new Mobile("M330", 1450)                    ;

                // Displays the two mobile phones in a dialog box.
JOptionPane.showMessageDialog(null,mobile1.getName()+":"+mobile1.getPrice()+"\n"+mobile2.getName()+":"+mobile2.getPrice());
  }
}


Exer 7有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,MobileMp3PlayerProduct在包cn.edu.nwsuaf.jp.p4.data中,MobileMp3PlayerProduct的子类,ProductStore代码如下:
Store.java源代码:
package cn.edu.nwsuaf.jp.p4;
import java.util.Arrays;
import javax.swing.JOptionPane;
import cn.edu.nwsuaf.jp.p4.data.Mobile;
import cn.edu.nwsuaf.jp.p4.data.Mp3Player;
import cn.edu.nwsuaf.jp.p4.data.Product;

public class Store {
//** Defines the entry point of the application. */
public static void main(String[] args) {
        // Creates two mobile phone objects.
        Mobile mobile1 = new Mobile("China Mobile", "E365",1780);
        Mobile mobile2 = new Mobile("China Mobile", "M330",1450);
        Mp3Player player1 = new Mp3Player("Meizo X3", 256, 399);
        Mp3Player player2 = new Mp3Player("Meizo E5", 512, 580);
        Mp3Player player3 = new Mp3Player("Xlive XM MP3 Play",256, 930);
        Product[] products={mobile1,mobile2,player1,player2, player3};
        Arrays.sort(products);
        String text = "";
        for(int index = 0; index <products.length; ++index)
                text += products[index].toString()+"\n";
        // Displays the two mobile phones in a dialog box.
        JOptionPane.showMessageDialog(null,"The products
are:\n\n"+text+"\nThere are "+Product.getCount()
                                                +" products.");
}
}

Product.java源代码:
package cn.edu.nwsuaf.jp.p4.data;

public abstract class Product implements Comparable<Product> {
        /** Holds the name of the product. */
        protected String name;

        /** Holds the price of the product. */
        protected float price;
        /** Holds the number of products. */
        protected static int count;

        /** Create a new product object. */
        protected Product(String name, float price) {
                this.name = name;
                this.price = price;               
                ++count;               
        }
        /** Gets the name of the product. */
        public String getName() {
                return name;
        }

        /** Gets the price of the product. */
        public float getPrice() {
                return price;
        }

        /** Gets the number of products. */
        public static int getCount() {
                return count;
        }

/** Compares this product with the given product. */
        public int compareTo(Product product) {
                return new Float(product.getPrice()).compareTo(price);
        }
}
[基本要求] 设计类Mobile和类MP3Player,使它们和类ProductStore组成一个完整的程序,且运行结果如下图所示。

Exer 8 实现一个名为Person的类和它的子类Employee ManagerEmployee的子类,设计一个接口Add用于涨工资,接口中有一个抽象方法addVage()。普通员工一次能涨10%,经理能涨20%。
具体要求如下:
(1) Person类中的属性有:姓名nameString类型),地址addressString类型)并写出该类的构造方法;
(2) Employee类中的属性有:工号IDString型),工资wagedouble类型),工龄(int型),写出该类的构造方法;
(3) Manager类中的属性有:级别levelString类型),写出该类的构造方法;
(4) 编写一个测试类,产生一个员工和一个经理并输出其具有的信息, 之后给这两个人增加工资,然后再次输出信息。




链接:http://share.weiyun.com/20b92be434836419b9b0284deee076f0 (密码:tXivdX)

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
java无忧网(http://www.javawyw.com),专业提供Java大作业,小作业,课程设计,毕业设计源码,答辩辅导,作业排错,bug修复,专业解答,远程部署运行项目等服务
本网站所有源码,保证能运行!
QQ:1399491757
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回列表 返回顶部