zoukankan      html  css  js  c++  java
  • work05

    第一题:分析以下需求,并用代码实现
    手机类Phone
    属性:
    品牌brand
    价格price
    行为:
    打电话call()
    发短信sendMessage()
    玩游戏playGame()

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

     1 private String brand;
     2     private int price;
     3     
     4     public Phone(String brand, int price) {
     5         super();
     6         this.brand = brand;
     7         this.price = price;
     8     }
     9     
    10 
    11     public Phone() {
    12         super();
    13         // TODO Auto-generated constructor stub
    14     }
    15 
    16 
    17     public String getBrand() {
    18         return brand;
    19     }
    20 
    21     public void setBrand(String brand) {
    22         this.brand = brand;
    23     }
    24 
    25     public int getPrice() {
    26         return price;
    27     }
    28 
    29     public void setPrice(int price) {
    30         this.price = price;
    31     }
    32 
    33     public void call(){
    34         System.out.println("正在使用价格为"+price+"元的手机打电话....");
    35     }
    36     public void sendMessage(){
    37         System.out.println("正在使用"+brand+"品牌的手机发短信....");
    38     }
    39     public void playGame(){
    40         System.out.println("正在使用价格为"+price+"元的"+brand+"品牌的手机玩游戏....");
    41     }
     1 package com.hp.dome;
     2 
     3 public class Dome01 {
     4     public static void main(String[] args) {
     5     Phone phone=new Phone();
     6     phone.setPrice(998);
     7     phone.setBrand("小米");
     8     phone.call();
     9     phone.sendMessage();
    10     phone.playGame();
    11         }
    12 }


    第二题:分析以下需求,并用代码实现
    1.猫类Cat
    属性:
    毛的颜色color
    品种breed
    行为:
    吃饭eat()
    抓老鼠catchMouse()
    2.狗类Dog
    属性:
    毛的颜色color
    品种breed
    行为:
    吃饭()
    看家lookHome()
    要求:
    1.按照以上要求定义Cat类和Dog类,属性要私有,生成空参、有参构造,setter和getter方法
    2.定义测试类,在main方法中创建每个类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
    3.调用每个对象的成员方法,打印格式如下:
    花色的波斯猫正在吃鱼.....
    花色的波斯猫正在逮老鼠....
    黑色的藏獒正在啃骨头.....
    黑色的藏獒正在看家.....

     1 package com.hp.cat;
     2 
     3 public class Cat {
     4     private String color ;
     5     private String breed ;
     6     
     7     public String getColor() {
     8         return color;
     9     }
    10     public void setColor(String color) {
    11         this.color = color;
    12     }
    13     public String getBreed() {
    14         return breed;
    15     }
    16     public void setBreed(String breed) {
    17         this.breed = breed;
    18     }
    19     
    20     public Cat(String color, String breed) {
    21         super();
    22         this.color = color;
    23         this.breed = breed;
    24     }
    25     public Cat() {
    26         super();
    27         // TODO Auto-generated constructor stub
    28     }
    29     public void eat(){
    30         System.out.println(color+"的"+breed+"正在吃鱼.....");
    31         
    32     }
    33     public void catchMouse(){
    34         System.out.println(color+"的"+breed+"正在逮老鼠.....");
    35         
    36     }
    37 
    38 }
     1 package com.hp.cat;
     2 
     3 public class Dog {
     4     private String color ;
     5     private String breed ;
     6     
     7     public Dog(String color, String breed) {
     8         super();
     9         this.color = color;
    10         this.breed = breed;
    11     }
    12     public Dog() {
    13         super();
    14         // TODO Auto-generated constructor stub
    15     }
    16     public String getColor() {
    17         return color;
    18     }
    19     public void setColor(String color) {
    20         this.color = color;
    21     }
    22     public String getBreed() {
    23         return breed;
    24     }
    25     public void setBreed(String breed) {
    26         this.breed = breed;
    27     }
    28     public void eat(){
    29         System.out.println(color+"的"+breed+"正在啃骨头.....");
    30         
    31     }
    32     public void lookHome(){
    33         System.out.println(color+"的"+breed+"正在看家.....");
    34         
    35     }
    36 }
     1 package com.hp.cat;
     2 
     3 public class Dome02 {
     4 public static void main(String[] args) {
     5     Cat cat=new Cat();
     6     Dog dog=new Dog();
     7     Cat cat1=new Cat("波斯猫2","花色");
     8     Dog dog1=new Dog("黑色2","藏獒");
     9     cat.setBreed("波斯猫");
    10     cat.setColor("花色");
    11     dog.setBreed("黑色");
    12     dog.setColor("藏獒");
    13     cat.eat();
    14     cat.catchMouse();
    15     dog.eat();
    16     dog.lookHome();
    17     System.out.println("==============================================");
    18     cat1.eat();
    19     cat1.catchMouse();
    20     dog1.eat();
    21     dog1.lookHome();
    22 }
    23 }



    第三题:分析以下需求,并用代码实现
    1.老师类Teacher
    属性:
    姓名name
    年龄age
    讲课内容content
    行为:
    吃饭
    讲课
    2.学生类Student
    属性:
    姓名name
    年龄age
    学习内容content
    行为:
    吃饭eat()
    学习study()
    要求:
    1.按照以上要求定义Teacher类和Student类,属性要私有,生成空参、有参构造,setter和getter方法
    2.定义测试类,在main方法中创建每个类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
    3.调用每个对象的成员方法,打印格式如下:
    年龄为30的周志鹏老师正在吃饭....
    年龄为30的周志鹏老师正在亢奋的讲着Java基础中面向对象的知识........("Java基础中面向对象"代表老师讲课的内容)
    年龄为18的韩光同学正在吃饭....
    年龄为18的韩光同学正在专心致志的听着面向对象的知识....("面向对象"代表学生学习的内容)

     1 package com.hp.teacher;
     2 
     3 public class Teacher {
     4     public String name;
     5     public int age;
     6     public String content;
     7     public String getName() {
     8         return name;
     9     }
    10     public void setName(String name) {
    11         this.name = name;
    12     }
    13     public int getAge() {
    14         return age;
    15     }
    16     public void setAge(int age) {
    17         this.age = age;
    18     }
    19     public String getContent() {
    20         return content;
    21     }
    22     public void setContent(String content) {
    23         this.content = content;
    24     }
    25     public Teacher(String name, int age, String content) {
    26         super();
    27         this.name = name;
    28         this.age = age;
    29         this.content = content;
    30     }
    31     public Teacher() {
    32         super();
    33         // TODO Auto-generated constructor stub
    34     }
    35     public void eat(){
    36         System.out.println("年龄为"+age+"的"+name+"老师正在吃饭....");
    37     }
    38     public void slot(){
    39         System.out.println("年龄为"+age+"的"+name+"老师正在亢奋的讲着Java基础中面向对象的知识........"+content);
    40     }
    41 
    42 }
     1 package com.hp.teacher;
     2 
     3 public class Student {
     4     public String name;
     5     public int age;
     6     public String content;
     7     public String getName() {
     8         return name;
     9     }
    10     public void setName(String name) {
    11         this.name = name;
    12     }
    13     public int getAge() {
    14         return age;
    15     }
    16     public void setAge(int age) {
    17         this.age = age;
    18     }
    19     public String getContent() {
    20         return content;
    21     }
    22     public void setContent(String content) {
    23         this.content = content;
    24     }
    25     public Student() {
    26         super();
    27         // TODO Auto-generated constructor stub
    28     }
    29     public Student(String name, int age, String content) {
    30         super();
    31         this.name = name;
    32         this.age = age;
    33         this.content = content;
    34     }
    35     public void eat(){
    36         System.out.println("年龄为"+age+"的"+name+"老师正在吃饭....");
    37     }
    38     public void listing(){
    39         System.out.println("年龄为"+age+"的"+name+"同学正在专心致志的听着面向对象的知识...."+content);
    40     }
    41 }
     1 package com.hp.teacher;
     2 
     3 public class Dome03 {
     4     public static void main(String[] args) {
     5         Teacher teacher=new Teacher();
     6         Student student=new Student();
     7         Teacher teacher1=new Teacher("周志鹏2",30,"Java基础中面向对象");
     8         Student student1=new Student("韩光2",18,"面向对象");
     9         teacher.setAge(30);
    10         teacher.setName("周志鹏");
    11         teacher.setContent("Java基础中面向对象");
    12         student.setAge(18);
    13         student.setName("韩光");
    14         student.setContent("面向对象");
    15         teacher.eat();
    16         teacher.slot();
    17         student.eat();
    18         student.listing();
    19         System.out.println("=======================================================================");
    20         teacher1.eat();
    21         teacher1.slot();
    22         student1.eat();
    23         student1.listing();
    24     }
    25 }




    第四题:分析以下需求,并用代码实现
    定义人类Person,包含以下成员:
    成员属性:
    姓名 name( String类型)
    年龄 age(double类型)

    1.按照以上要求定义Person,属性要私有,生成空参、有参构造,setter和getter方法
    2.定义测试类:根据如下需求创建多个对象(使用满参构造创建,即有参构造).
    老王-35 小芳-23
    3.通过两个对象,比较谁的年龄大,并打印出来.
    例: 老王年龄比较大

     1 package com.hp.person;
     2 
     3 public class Person {
     4     private String name;
     5     private int  age ;
     6     public String getName() {
     7         return name;
     8     }
     9     public void setName(String name) {
    10         this.name = name;
    11     }
    12     public int getAge() {
    13         return age;
    14     }
    15     public void setAge(int age) {
    16         this.age = age;
    17     }
    18     public Person() {
    19         super();
    20         // TODO Auto-generated constructor stub
    21     }
    22     public Person(String name, int age) {
    23         super();
    24         this.name = name;
    25         this.age = age;
    26     }    
    27     
    28 
    29 }
     1 package com.hp.person;
     2 
     3 public class Dome04 {
     4     public static void main(String[] args) {
     5         Person person=new Person("老王",35);
     6         Person person2=new Person("小芳",23);
     7         if (person.getAge()>person2.getAge()) {
     8             System.out.println("老王年龄比较大");
     9         }else {
    10             System.out.println("小芳年龄比较大");
    11         }
    12     }
    13 
    14 }


    练习题:
    ------------------------------------------------------------------

    1.定义“电脑类”Computer,包含以下成员:
    成员属性:
    品牌brand( String类型)
    价格 price(double类型)
    成员方法:
    编码coding(), 调用方法打印 ***电脑正在使用Java语言编程
    玩游戏,playGame(),调用方法打印 ***电脑正在玩王者荣耀s

    1.按照以上要求定义Computer,属性要私有,生成空参、有参构造,setter和getter方法
    2.定义测试类,a.创建一个电脑对象,设置品牌为ThinkPad,价格为7399,调用方法coding
    b.创建一个电脑对象,设置品牌为Acer,价格为5399,调用方法playGame

     1 package com.hp.computer;
     2 
     3 public class Computer {
     4     private String brand;
     5     private double  price;
     6     
     7     public Computer() {
     8         super();
     9         // TODO Auto-generated constructor stub
    10     }
    11     public Computer(String brand, double price) {
    12         super();
    13         this.brand = brand;
    14         this.price = price;
    15     }
    16     public String getBrand() {
    17         return brand;
    18     }
    19     public void setBrand(String brand) {
    20         this.brand = brand;
    21     }
    22     public double getPrice() {
    23         return price;
    24     }
    25     public void setPrice(double price) {
    26         this.price = price;
    27     }
    28     public void coding (){
    29         System.out.println("品牌:"+brand+"价格:"+price+"电脑正在使用Java语言编程");
    30     }
    31     public  void palyGame(){
    32         System.out.println("品牌:"+brand+"价格:"+price+"电脑正在玩王者荣耀s");
    33     }
    34     
    35 }
     1 package com.hp.computer;
     2 
     3 public class Dome01 {
     4 public static void main(String[] args) {
     5     Computer computer1=new Computer("ThinkPad",7399 );
     6     Computer computer2=new Computer("Acer",5399 );
     7     computer1.coding();
     8     computer2.palyGame();
     9 }
    10 }



    2.定义汽车类Car,包含以下成员:
    成员属性:
    品牌 brand( String类型)
    电量 power(double类型)
    成员方法:
    报警 warning() 调用方法,可以检验当前电量是否低于10,如果低于10,就打印"电量不足". 如果不低于10,就打印"电量充足"

    1.按照以上要求定义Car,属性要私有,生成空参、有参构造,setter和getter方法
    2.定义测试类:根据如下需求创建多个对象,调用warning()方法.
    特斯拉-50 比亚迪-9

     1 package com.hp.computer;
     2 
     3 public class Car {
     4     private String brand;
     5     private double  power;
     6     
     7     public Car() {
     8         super();
     9         // TODO Auto-generated constructor stub
    10     }
    11     public Car(String brand, double price) {
    12         super();
    13         this.brand = brand;
    14         this.power = price;
    15     }
    16     public String getBrand() {
    17         return brand;
    18     }
    19     public void setBrand(String brand) {
    20         this.brand = brand;
    21     }
    22     public double getPrice() {
    23         return power;
    24     }
    25     public void setPrice(double price) {
    26         this.power = price;
    27     }
    28     public void warning(){
    29         if (power<10) {
    30             System.out.println("电量不足");
    31         }else {
    32             System.out.println("电量充足");
    33         }
    34     }
    35 }
     1 package com.hp.computer;
     2 
     3 public class Dome02 {
     4 public static void main(String[] args) {
     5     Car car1=new Car("特斯拉",50 );
     6     Car car2=new Car("比亚迪",9 );
     7     car1.warning();
     8     car2.warning();
     9 }
    10 }



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

     1 package com.hp.computer;
     2 
     3 public class Manager {
     4     private String name;
     5     private int id;
     6     private    int salary;
     7     private int bonus;
     8     public String getName() {
     9         return name;
    10     }
    11     public void setName(String name) {
    12         this.name = name;
    13     }
    14     public int getId() {
    15         return id;
    16     }
    17     public void setId(int id) {
    18         this.id = id;
    19     }
    20     public int getSalary() {
    21         return salary;
    22     }
    23     public void setSalary(int salary) {
    24         this.salary = salary;
    25     }
    26     public int getBonus() {
    27         return bonus;
    28     }
    29     public void setBonus(int bonus) {
    30         this.bonus = bonus;
    31     }
    32     public Manager() {
    33         super();
    34         // TODO Auto-generated constructor stub
    35     }
    36     public Manager(String name, int id, int salary, int bonus) {
    37         super();
    38         this.name = name;
    39         this.id = id;
    40         this.salary = salary;
    41         this.bonus = bonus;
    42     }
    43     public void work(){
    44         System.out.println("工号为"+id+"基本工资为"+salary+"奖金为"+bonus+"的项目经理"+name+"正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....");
    45     }
    46     
    47 }
     1 package com.hp.computer;
     2 
     3 public class Coder {
     4     private String name;
     5     private int id;
     6     private int salary;
     7     public String getName() {
     8         return name;
     9     }
    10     public void setName(String name) {
    11         this.name = name;
    12     }
    13     public int getId() {
    14         return id;
    15     }
    16     public void setId(int id) {
    17         this.id = id;
    18     }
    19     public int getSalary() {
    20         return salary;
    21     }
    22     public void setSalary(int salary) {
    23         this.salary = salary;
    24     }
    25     public Coder(String name, int id, int salary) {
    26         super();
    27         this.name = name;
    28         this.id = id;
    29         this.salary = salary;
    30     }
    31     public Coder() {
    32         super();
    33         // TODO Auto-generated constructor stub
    34     }
    35     public void work(){
    36         System.out.println("工号为"+id+"基本工资为"+salary+"的程序员"+name+"正在努力的写着代码......");
    37     }
    38     
    39 }
     1 package com.hp.computer;
     2 
     3 public class Dome03 {
     4     public static void main(String[] args) {
     5         Coder coder=new Coder("杨白劳",135 ,10000 );
     6         coder.setId(135);
     7         coder.setName("杨白劳");
     8         coder.setSalary(10000);
     9         Manager manager =new Manager("周扒皮", 123, 15000, 6000);
    10         manager.setId(123);
    11         manager.setName("周扒皮");
    12         manager.setSalary(15000);
    13         manager.setBonus(6000);
    14         coder.work();
    15         manager.work();
    16     }
    17 }
  • 相关阅读:
    读《大道至简》有感
    软件工程概论第一次课堂作业
    课后小程序
    继承与多态课后作业
    课后作业2-字符串
    课后作业1:字串加密
    3-类与对象-动手动脑
    使用类的静态字段和构造函数,跟踪某个类所创建对象的个数。
    课程作业02-1-课后作业1-(3、4)汉诺塔、回文数
    课程作业02-2-动手动脑
  • 原文地址:https://www.cnblogs.com/lemperor/p/13823105.html
Copyright © 2011-2022 走看看