day06作业:
必做题:
------------------------------------------------------------------
第一题:分析以下需求,并用代码实现
手机类Phone
属性:
品牌brand
价格price
行为:
打电话call()
发短信sendMessage()
玩游戏playGame()
要求:
1.按照以上要求定义类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建该类的对象并使用set方式给属性赋值(价格:998,品牌:小米)
3.调用三个成员方法,打印格式如下:
正在使用价格为998元的手机打电话....
正在使用小米品牌的手机发短信....
正在使用价格为998元的小米品牌的手机玩游戏....
private String brand;
private int price;
public void stu (){
System.out.println("品牌为:"+brand+"价格为:"+price);
}
public void stu (String brand,int price){
}
public String getBrand() {
return brand;
}
public void call() {
System.out.println("正在使用价格为"+price+"元的手机打电话");
}
public void sendMessge() {
System.out.println("正在使用"+brand+"品牌的手机发短信");
}
public void playGame() {
System.out.println("正在使用价格为"+price+"的"+brand+"品牌的手机玩游戏");
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public static void main(String[] args) {
stu s1=new stu();
s1.setBrand("小米");
s1.setPrice(998);
s1.stu();
s1.call();
s1.sendMessge();
s1.playGame();
}
------------------------------------------------------------------
第一题:分析以下需求,并用代码实现
手机类Phone
属性:
品牌brand
价格price
行为:
打电话call()
发短信sendMessage()
玩游戏playGame()
要求:
1.按照以上要求定义类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建该类的对象并使用set方式给属性赋值(价格:998,品牌:小米)
3.调用三个成员方法,打印格式如下:
正在使用价格为998元的手机打电话....
正在使用小米品牌的手机发短信....
正在使用价格为998元的小米品牌的手机玩游戏....
private String brand;
private int price;
public void stu (){
System.out.println("品牌为:"+brand+"价格为:"+price);
}
public void stu (String brand,int price){
}
public String getBrand() {
return brand;
}
public void call() {
System.out.println("正在使用价格为"+price+"元的手机打电话");
}
public void sendMessge() {
System.out.println("正在使用"+brand+"品牌的手机发短信");
}
public void playGame() {
System.out.println("正在使用价格为"+price+"的"+brand+"品牌的手机玩游戏");
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public static void main(String[] args) {
stu s1=new stu();
s1.setBrand("小米");
s1.setPrice(998);
s1.stu();
s1.call();
s1.sendMessge();
s1.playGame();
}
第二题:分析以下需求,并用代码实现
1.猫类Cat
属性:
毛的颜色color
品种breed
行为:
吃饭eat()
抓老鼠catchMouse()
2.狗类Dog
属性:
毛的颜色color
品种breed
行为:
吃饭()
看家lookHome()
要求:
1.按照以上要求定义Cat类和Dog类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建每个类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
3.调用每个对象的成员方法,打印格式如下:
花色的波斯猫正在吃鱼.....
花色的波斯猫正在逮老鼠....
黑色的藏獒正在啃骨头.....
黑色的藏獒正在看家.....
private String color;
private String breed;
private String breed;
public An() {
super();
// TODO Auto-generated constructor stub
}
public An(String color, String breed) {
super();
this.color = color;
this.breed = 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;
}
public void eat(){
System.out.println(color+"的"+breed+"正在吃鱼.....");
}
public void catchMouse() {
System.out.println(color+"的"+breed+"正在逮老鼠....");
}
super();
// TODO Auto-generated constructor stub
}
public An(String color, String breed) {
super();
this.color = color;
this.breed = 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;
}
public void eat(){
System.out.println(color+"的"+breed+"正在吃鱼.....");
}
public void catchMouse() {
System.out.println(color+"的"+breed+"正在逮老鼠....");
}
}
private String color;
private String breed;
public Dog() {
super();
// TODO Auto-generated constructor stub
}
public Dog(String color, String breed) {
super();
this.color = color;
this.breed = 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;
}
public void eat() {
System.out.println(color+"的"+breed+"正在啃骨头.....");
}
public void lookHome() {
System.out.println(color+"的"+breed+"正在看家.....");
}
}
public static void main(String[] args) {
An a1=new An();
a1.setColor("花色");
a1.setBreed("波斯猫");
System.out.println(a1.getColor());
System.out.println(a1.getBreed());
a1.eat();
a1.catchMouse();
Dog d1=new Dog();
d1.setBreed("藏獒");
d1.setColor("黑色");
System.out.println(d1.getBreed());;
System.out.println(d1.getColor());
d1.eat();
d1.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的韩光同学正在专心致志的听着面向对象的知识....("面向对象"代表学生学习的内容)
private String name;
private int age;
private String 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 void eat() {
System.out.println("年龄为"+age+"的"+name+"老师正在吃饭....");
}
public void jiangke() {
System.out.println("年龄为"+age+"的"+name+"老师正在亢奋的讲着"+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;
}
private String name;
private int age;
private String 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 void eat() {
System.out.println("年龄为"+age+"的"+name+"同学正在吃饭....");
}
public void study() {
System.out.println("年龄为"+age+"的"+name+"同学正在专心致志的听着"+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;
}
public static void main(String[] args) {
Teacher t1=new Teacher();
t1.setName("周志鹏");
t1.setAge(30);
t1.setContent("Java基础中面向对象");
System.out.println(t1.getName());
System.out.println(t1.getAge());
System.out.println(t1.getContent());
t1.eat();
t1.jiangke();
Student u1=new Student();
u1.setName("韩光");
u1.setAge(18);
u1.setContent("面向对象");
System.out.println(u1.getName());
System.out.println(u1.getAge());
System.out.println(u1.getContent());
u1.eat();
u1.study();
}
第四题:分析以下需求,并用代码实现
定义人类Person,包含以下成员:
成员属性:
姓名 name( String类型)
年龄 age(double类型)
1.按照以上要求定义Person,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类:根据如下需求创建多个对象(使用满参构造创建,即有参构造).
老王-35 小芳-23
3.通过两个对象,比较谁的年龄大,并打印出来.
例: 老王年龄比较大
private String name;
private int age ;
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 Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
26 }
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 }
private String color;
private String breed;
public Dog() {
super();
// TODO Auto-generated constructor stub
}
public Dog(String color, String breed) {
super();
this.color = color;
this.breed = 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;
}
public void eat() {
System.out.println(color+"的"+breed+"正在啃骨头.....");
}
public void lookHome() {
System.out.println(color+"的"+breed+"正在看家.....");
}
}
public static void main(String[] args) {
An a1=new An();
a1.setColor("花色");
a1.setBreed("波斯猫");
System.out.println(a1.getColor());
System.out.println(a1.getBreed());
a1.eat();
a1.catchMouse();
Dog d1=new Dog();
d1.setBreed("藏獒");
d1.setColor("黑色");
System.out.println(d1.getBreed());;
System.out.println(d1.getColor());
d1.eat();
d1.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的韩光同学正在专心致志的听着面向对象的知识....("面向对象"代表学生学习的内容)
private String name;
private int age;
private String 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 void eat() {
System.out.println("年龄为"+age+"的"+name+"老师正在吃饭....");
}
public void jiangke() {
System.out.println("年龄为"+age+"的"+name+"老师正在亢奋的讲着"+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;
}
private String name;
private int age;
private String 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 void eat() {
System.out.println("年龄为"+age+"的"+name+"同学正在吃饭....");
}
public void study() {
System.out.println("年龄为"+age+"的"+name+"同学正在专心致志的听着"+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;
}
public static void main(String[] args) {
Teacher t1=new Teacher();
t1.setName("周志鹏");
t1.setAge(30);
t1.setContent("Java基础中面向对象");
System.out.println(t1.getName());
System.out.println(t1.getAge());
System.out.println(t1.getContent());
t1.eat();
t1.jiangke();
Student u1=new Student();
u1.setName("韩光");
u1.setAge(18);
u1.setContent("面向对象");
System.out.println(u1.getName());
System.out.println(u1.getAge());
System.out.println(u1.getContent());
u1.eat();
u1.study();
}
第四题:分析以下需求,并用代码实现
定义人类Person,包含以下成员:
成员属性:
姓名 name( String类型)
年龄 age(double类型)
1.按照以上要求定义Person,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类:根据如下需求创建多个对象(使用满参构造创建,即有参构造).
老王-35 小芳-23
3.通过两个对象,比较谁的年龄大,并打印出来.
例: 老王年龄比较大
private String name;
private int age ;
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 Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
26 }
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 }
练习题:
------------------------------------------------------------------
1.定义“电脑类”Computer,包含以下成员:
成员属性:
品牌brand( String类型)
价格 price(double类型)
成员方法:
编码coding(), 调用方法打印 ***电脑正在使用Java语言编程
玩游戏,playGame(),调用方法打印 ***电脑正在玩王者荣耀s
1.按照以上要求定义Computer,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,a.创建一个电脑对象,设置品牌为ThinkPad,价格为7399,调用方法coding
b.创建一个电脑对象,设置品牌为Acer,价格为5399,调用方法playGame
private String brand;
private double price;
public Computer() {
super();
// TODO Auto-generated constructor stub
}
public Computer(String brand, double price) {
super();
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void coding (){
System.out.println("品牌:"+brand+"价格:"+price+"电脑正在使用Java语言编程");
}
public void palyGame(){
System.out.println("品牌:"+brand+"价格:"+price+"电脑正在玩王者荣耀s");
}
public static void main(String[] args) {
Computer computer1=new Computer("ThinkPad",7399 );
Computer computer2=new Computer("Acer",5399 );
computer1.coding();
computer2.palyGame();
}
}
成员属性:
品牌brand( String类型)
价格 price(double类型)
成员方法:
编码coding(), 调用方法打印 ***电脑正在使用Java语言编程
玩游戏,playGame(),调用方法打印 ***电脑正在玩王者荣耀s
1.按照以上要求定义Computer,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,a.创建一个电脑对象,设置品牌为ThinkPad,价格为7399,调用方法coding
b.创建一个电脑对象,设置品牌为Acer,价格为5399,调用方法playGame
private String brand;
private double price;
public Computer() {
super();
// TODO Auto-generated constructor stub
}
public Computer(String brand, double price) {
super();
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void coding (){
System.out.println("品牌:"+brand+"价格:"+price+"电脑正在使用Java语言编程");
}
public void palyGame(){
System.out.println("品牌:"+brand+"价格:"+price+"电脑正在玩王者荣耀s");
}
public static void main(String[] args) {
Computer computer1=new Computer("ThinkPad",7399 );
Computer computer2=new Computer("Acer",5399 );
computer1.coding();
computer2.palyGame();
}
}
2.定义汽车类Car,包含以下成员:
成员属性:
品牌 brand( String类型)
电量 power(double类型)
成员方法:
报警 warning() 调用方法,可以检验当前电量是否低于10,如果低于10,就打印"电量不足". 如果不低于10,就打印"电量充足"
1.按照以上要求定义Car,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类:根据如下需求创建多个对象,调用warning()方法.
特斯拉-50 比亚迪-9
private String brand;
5 private double power;
6
7 public Car() {
8 super();
9 // TODO Auto-generated constructor stub
10 }
public Car(String brand, double price) {
super();
this.brand = brand;
this.power = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return power;
}
public void setPrice(double price) {
this.power = price;
}
public void warning(){
if (power<10) {
System.out.println("电量不足");
}else {
System.out.println("电量充足");
}
public static void main(String[] args) {
Car car1=new Car("特斯拉",50 );
Car car2=new Car("比亚迪",9 );
car1.warning();
car2.warning();
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的程序员杨白劳正在努力的写着代码......
private String name;
private int id;
private int salary;
private int 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 Manager() {
super();
// TODO Auto-generated constructor stub
}
public Manager(String name, int id, int salary, int bonus) {
super();
this.name = name;
this.id = id;
this.salary = salary;
this.bonus = bonus;
}
public void work(){
System.out.println("工号为"+id+"基本工资为"+salary+"奖金为"+bonus+"的项目经理"+name+"正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....");
}
private String name;
private int id;
private int salary;
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 Coder(String name, int id, int salary) {
super();
this.name = name;
this.id = id;
this.salary = salary;
}
public Coder() {
super();
// TODO Auto-generated constructor stub
}
public void work(){
System.out.println("工号为"+id+"基本工资为"+salary+"的程序员"+name+"正在努力的写着代码......");
}
public static void main(String[] args) {
Coder coder=new Coder("杨白劳",135 ,10000 );
coder.setId(135);
coder.setName("杨白劳");
coder.setSalary(10000);
Manager manager =new Manager("周扒皮", 123, 15000, 6000);
manager.setId(123);
manager.setName("周扒皮");
manager.setSalary(15000);
manager.setBonus(6000);
coder.work();
manager.work();
}
Car car1=new Car("特斯拉",50 );
Car car2=new Car("比亚迪",9 );
car1.warning();
car2.warning();
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的程序员杨白劳正在努力的写着代码......
private String name;
private int id;
private int salary;
private int 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 Manager() {
super();
// TODO Auto-generated constructor stub
}
public Manager(String name, int id, int salary, int bonus) {
super();
this.name = name;
this.id = id;
this.salary = salary;
this.bonus = bonus;
}
public void work(){
System.out.println("工号为"+id+"基本工资为"+salary+"奖金为"+bonus+"的项目经理"+name+"正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....");
}
private String name;
private int id;
private int salary;
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 Coder(String name, int id, int salary) {
super();
this.name = name;
this.id = id;
this.salary = salary;
}
public Coder() {
super();
// TODO Auto-generated constructor stub
}
public void work(){
System.out.println("工号为"+id+"基本工资为"+salary+"的程序员"+name+"正在努力的写着代码......");
}
public static void main(String[] args) {
Coder coder=new Coder("杨白劳",135 ,10000 );
coder.setId(135);
coder.setName("杨白劳");
coder.setSalary(10000);
Manager manager =new Manager("周扒皮", 123, 15000, 6000);
manager.setId(123);
manager.setName("周扒皮");
manager.setSalary(15000);
manager.setBonus(6000);
coder.work();
manager.work();
}