面向对象应用实例之作业题

面向对象应用实例之作业题

**

第一题

分析以下需求并实现
手机类Phone
属性:
品牌brand
价格price
行为:
打电话call()
发短信sendMessage()
玩游戏playGame()
要求:
1.按照以上要求定义类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建该类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
3.调用三个成员方法,打印格式如下:
正在使用价格为998元的小米品牌的手机打电话…
正在使用价格为998元的小米品牌的手机发短信…
正在使用价格为998元的小米品牌的手机玩游戏…**

package Day07.itheima_02;

public class Phone {
    private String brand;
    private int price;
    public Phone (){}
    public Phone (String brand,int price){
        this.brand = brand;
        this.price = price;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    /*public void call(){
        System.out.println("正在使用价格为"+price+"元的"+brand+"的手机打电话");
    }
    public void sengMessage(){
        System.out.println("正在使用价格为"+price+"元的"+brand+"的手机发短信");
    }
    public void playGame(){
        System.out.println("正在使用价格为"+price+"元的"+brand+"的手机玩游戏");
    }*/
    public void show(String call,String message,String play ){
        System.out.println("正在使用价格为"+price+"元的"+brand+"的手机"+call);
        System.out.println("正在使用价格为"+price+"元的"+brand+"的手机"+message);
        System.out.println("正在使用价格为"+price+"元的"+brand+"的手机"+play);
    }
}
--------------------------------------------------
public class Test {
    public static void main(String[] args) {
        Phone p = new Phone();
        p.setBrand("小米品牌");
        p.setPrice(998);
        /*p.call();
        p.sengMessage();
        p.playGame();*/
        p.show("打电话","发短信","玩游戏");

        Phone p1 = new Phone("小米品牌",998);
        /*p.call();
        p.sengMessage();
        p.playGame();*/
        p.show("打电话","发短信","玩游戏");
	}
}        	

第二题

分析以下需求并实现
1.项目经理类Manager
属性:
姓名name
工号id
工资salary
奖金bonus
行为:
工作work()
要求:
1.按照以上要求定义Manager类和Coder类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建该类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
3.调用成员方法,打印格式如下:
工号为123基本工资为15000奖金为6000的项目经理正在努力的做着管理工作,分配任务,检查员工提交上来的代码…

 public class Manage {
    private String name;
    private int id;
    private int salary;
    private int bonus;

    public Manage() {
    }
    public Manage(String name, int id, int salary, int bonus) {
        this.name = name;
        this.id = id;
        this.salary = salary;
        this.bonus = bonus;
    }

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public int getBonus() {
        return bonus;
    }

    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
    public void  work(){
        System.out.println("工号为"+id+"基本工资为"+salary+"奖金为"+bonus+"的"+name+"正在努力的做着管理工作,分配任务,检查员工提交上来的代码.");
    }
}
--------------------------------------------------------
public class Test {
    public static void main(String[] args) {
 Manage m = new Manage();
        m.setName("项目经理");
        m.setId(123);
        m.setSalary(15000);
        m.setBonus(6000);
        m.work();

        Manage m1 = new Manage("项目经理",123,15000,6000);
        m.work();
	}
}

第三题

狗类Dog
属性:
毛的颜色color
品种breed
行为:
吃饭eat()
看家lookHome()
要求:
1.按照以上要求定义Cat类和Dog类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建该类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
3.调用成员方法,打印格式如下:
黑色的藏獒正在啃骨头…
黑色的藏獒正在看家…

public class Dog {
    String color;
    String breed;
    public Dog(){ }
    public Dog(String color,String breed){
        this.color = color;
        this.breed = breed;
    }
    public void setColor(String color){
        this.color = color;
    }
    public String getColor(){
        return color;
    }
    public void setBreed(String breed){
        this.breed = breed;
    }
    public String getBreed(){
        return getBreed();
    }
    public void eat(){
        System.out.println(color+breed+"正在啃骨头");
    }
    public void lookHome(){
        System.out.println(color+breed+"正在看家");
    }
}
-------------------------------------------------------
public class Test {
    public static void main(String[] args) {
    Dog d = new Dog();
        d.setColor("黑色的");;
        d.setBreed("藏獒");
        d.eat();
        d.lookHome();

        Dog d1 = new Dog("黑色的","藏獒");
        d1.eat();
        d.lookHome();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值