java基础随笔05

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

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

package com.hp.www;

public class Phone {
    private String brand;
    private int price;

    public void call() {
        System.out.println("正在使用价格为" + price + "元的手机打电话....");

    }

    public void sendMessage() {
        System.out.println("正在使用" + brand + "品牌的手机发短信....");
    }

    public void playGame() {
        System.out.println("正在使用价格为" + price + "元的" + brand + "品牌的手机玩游戏....");
    }

    Phone() {
    }

    Phone(String brand, int 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;
    }

}
package com.hp.www;

public class PhoneTest {
public static void main(String[] args) {
    Phone phone = new Phone();
    phone.setBrand("小米");
    phone.setPrice(998);
    phone.call();
    phone.sendMessage();
    phone.playGame();
}
}


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

package com.hp.www;

    public class Cat{
        private String color;
        private String breed;
    
    void eat(){
        System.out.println(color+breed+"正在吃鱼.....");
    }
    void catchMouse(){
        System.out.println(color+breed+"正在逮老鼠.....");
    }
    Cat(){
        
    }
    Cat(String color,String breed){
        
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getBreed() {
        return breed;
    }
    public void setBreed(String breed) {
        this.breed = breed;
    }
    
    
    }
package com.hp.www;

public class Dog {
    private String color;
    private String bread;
    void eat(){
        System.out.println(color+bread+"正在啃骨头.....");
    }
    void lookHome(){
        System.out.println(color+bread+"正在看家.....");
    }
    Dog(){
        
    }
    Dog(String color, String breed){
        
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getBread() {
        return bread;
    }
    public void setBread(String bread) {
        this.bread = bread;
    }

}
package com.hp.www;

public class CatDogtest {
    public static void main(String[] args) {
        Cat cat=new Cat();
        cat.setBreed("波斯猫");
        cat.setColor("花色的");
        cat.eat();
        cat.catchMouse();
        Dog dog=new Dog();
        dog.setColor("黑色的");
        dog.setBread("藏獒");
        dog.eat();
        dog.lookHome();
    }
    

}



第三题:分析以下需求,并用代码实现
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的韩光同学正在专心致志的听着面向对象的知识....("面向对象"代表学生学习的内容)

package com.hp.www;

public class Teacher {
    private String name;
    private int age;
    private String content;
    void eat(){
        System.out.println("年龄为"+age+""+name+"老师正在吃饭....");
    }
    void teach(){
        System.out.println("年龄为"+age+""+name+"老师正在亢奋的讲着"+content+"........");
    }
    
    public Teacher() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Teacher(String name, int age, String content) {
        super();
        this.name = name;
        this.age = age;
        this.content = content;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
package com.hp.www;

public class Student {
    private String name;
    private int age;
    private String content;
    void eat(){
        System.out.println("年龄为"+age+""+name+"同学正在吃饭....");
    }
    void study(){
        System.out.println("年龄为"+age+""+name+"同学正在专心致志的听着"+content+"的知识....");
    }
    
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public Student(String name, int age, String content) {
        super();
        this.name = name;
        this.age = age;
        this.content = content;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    

}
    }

}

第四题:分析以下需求,并用代码实现
定义人类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 }
原文地址:https://www.cnblogs.com/xyt123/p/13823339.html